1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/plugins/test/unit/test_persist_in_prefs.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,127 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + */ 1.8 + 1.9 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.10 + 1.11 +// Plugin registry uses different field delimeters on different platforms 1.12 +let DELIM = ":"; 1.13 +if ("@mozilla.org/windows-registry-key;1" in Components.classes) 1.14 + DELIM = "|"; 1.15 + 1.16 +let gProfD = do_get_profile_startup(); 1.17 +let gDirSvc = Cc["@mozilla.org/file/directory_service;1"]. 1.18 + getService(Ci.nsIProperties); 1.19 + 1.20 +// Writes out some plugin registry to the profile 1.21 +function write_registry(version, info) { 1.22 + let runtime = Cc["@mozilla.org/xre/runtime;1"].getService(Ci.nsIXULRuntime); 1.23 + 1.24 + let header = "Generated File. Do not edit.\n\n"; 1.25 + header += "[HEADER]\n"; 1.26 + header += "Version" + DELIM + version + DELIM + "$\n"; 1.27 + header += "Arch" + DELIM + runtime.XPCOMABI + DELIM + "$\n"; 1.28 + header += "\n"; 1.29 + header += "[PLUGINS]\n"; 1.30 + 1.31 + let registry = gProfD.clone(); 1.32 + registry.append("pluginreg.dat"); 1.33 + let foStream = Components.classes["@mozilla.org/network/file-output-stream;1"] 1.34 + .createInstance(Components.interfaces.nsIFileOutputStream); 1.35 + // write, create, truncate 1.36 + foStream.init(registry, 0x02 | 0x08 | 0x20, 0666, 0); 1.37 + 1.38 + let charset = "UTF-8"; // Can be any character encoding name that Mozilla supports 1.39 + let os = Cc["@mozilla.org/intl/converter-output-stream;1"]. 1.40 + createInstance(Ci.nsIConverterOutputStream); 1.41 + os.init(foStream, charset, 0, 0x0000); 1.42 + 1.43 + os.writeString(header); 1.44 + os.writeString(info); 1.45 + os.close(); 1.46 +} 1.47 + 1.48 +function run_test() { 1.49 + do_check_true(gIsWindows || gIsOSX || gIsLinux); 1.50 + 1.51 + let file = get_test_plugin_no_symlink(); 1.52 + if (!file) 1.53 + do_throw("Plugin library not found"); 1.54 + 1.55 + const pluginDir = file.parent; 1.56 + const tempDir = do_get_tempdir(); 1.57 + const suffix = get_platform_specific_plugin_suffix(); 1.58 + const pluginName = file.leafName.substring(0, file.leafName.length - suffix.length).toLowerCase(); 1.59 + const pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost); 1.60 + const statePref = "plugin.state." + pluginName; 1.61 + 1.62 + // write plugin registry data 1.63 + let registry = ""; 1.64 + 1.65 + registry += file.leafName + DELIM + "$\n"; 1.66 + registry += file.path + DELIM + "$\n"; 1.67 + registry += "1.0.0.0" + DELIM + "$\n"; 1.68 + registry += file.lastModifiedTime + DELIM + "0" + DELIM + "0" + DELIM + "$\n"; 1.69 + registry += "Plug-in for testing purposes." + DELIM + "$\n"; 1.70 + registry += "Test Plug-in" + DELIM + "$\n"; 1.71 + registry += "1\n"; 1.72 + registry += "0" + DELIM + "application/x-test" + DELIM + "Test mimetype" + DELIM + "tst" + DELIM + "$\n"; 1.73 + 1.74 + registry += "\n"; 1.75 + registry += "[INVALID]\n"; 1.76 + registry += "\n"; 1.77 + write_registry("0.15", registry); 1.78 + 1.79 + // Initialise profile folder 1.80 + do_get_profile(); 1.81 + 1.82 + let plugin = get_test_plugintag(); 1.83 + if (!plugin) 1.84 + do_throw("Plugin tag not found"); 1.85 + 1.86 + // check that the expected plugin state was loaded correctly from the registry 1.87 + do_check_true(plugin.disabled); 1.88 + do_check_false(plugin.clicktoplay); 1.89 + // ... and imported into prefs, with 0 being the disabled state 1.90 + do_check_eq(0, Services.prefs.getIntPref(statePref)); 1.91 + 1.92 + // prepare a copy of the plugin and backup the original 1.93 + file.copyTo(null, "nptestcopy" + suffix); 1.94 + let copy = pluginDir.clone(); 1.95 + copy.append("nptestcopy" + suffix); 1.96 + file.moveTo(tempDir, null); 1.97 + 1.98 + // test that the settings persist through a few variations of test-plugin names 1.99 + let testNames = [ 1.100 + pluginName + "2", 1.101 + pluginName.toUpperCase() + "_11_5_42_2323", 1.102 + pluginName + "-5.2.7" 1.103 + ]; 1.104 + testNames.forEach(function(leafName) { 1.105 + dump("Checking " + leafName + ".\n"); 1.106 + copy.moveTo(null, leafName + suffix); 1.107 + pluginHost.reloadPlugins(false); 1.108 + plugin = get_test_plugintag(); 1.109 + do_check_false(plugin == null); 1.110 + do_check_true(plugin.disabled); 1.111 + do_check_false(plugin.clicktoplay); 1.112 + }); 1.113 + 1.114 + // check that the state persists even if the plugin is not always present 1.115 + copy.moveTo(tempDir, null); 1.116 + pluginHost.reloadPlugins(false); 1.117 + copy.moveTo(pluginDir, null); 1.118 + pluginHost.reloadPlugins(false); 1.119 + 1.120 + plugin = get_test_plugintag(); 1.121 + do_check_false(plugin == null); 1.122 + do_check_true(plugin.disabled); 1.123 + do_check_false(plugin.clicktoplay); 1.124 + 1.125 + // clean up 1.126 + Services.prefs.clearUserPref(statePref); 1.127 + Services.prefs.clearUserPref("plugin.importedState"); 1.128 + copy.remove(true); 1.129 + file.moveTo(pluginDir, null); 1.130 +}