dom/plugins/test/unit/test_persist_in_prefs.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     4  */
     6 Components.utils.import("resource://gre/modules/Services.jsm");
     8 // Plugin registry uses different field delimeters on different platforms
     9 let DELIM = ":";
    10 if ("@mozilla.org/windows-registry-key;1" in Components.classes)
    11   DELIM = "|";
    13 let gProfD = do_get_profile_startup();
    14 let gDirSvc = Cc["@mozilla.org/file/directory_service;1"].
    15              getService(Ci.nsIProperties);
    17 // Writes out some plugin registry to the profile
    18 function write_registry(version, info) {
    19   let runtime = Cc["@mozilla.org/xre/runtime;1"].getService(Ci.nsIXULRuntime);
    21   let header = "Generated File. Do not edit.\n\n";
    22   header += "[HEADER]\n";
    23   header += "Version" + DELIM + version + DELIM + "$\n";
    24   header += "Arch" + DELIM + runtime.XPCOMABI + DELIM + "$\n";
    25   header += "\n";
    26   header += "[PLUGINS]\n";
    28   let registry = gProfD.clone();
    29   registry.append("pluginreg.dat");
    30   let foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    31                            .createInstance(Components.interfaces.nsIFileOutputStream);
    32   // write, create, truncate
    33   foStream.init(registry, 0x02 | 0x08 | 0x20, 0666, 0);
    35   let charset = "UTF-8"; // Can be any character encoding name that Mozilla supports
    36   let os = Cc["@mozilla.org/intl/converter-output-stream;1"].
    37            createInstance(Ci.nsIConverterOutputStream);
    38   os.init(foStream, charset, 0, 0x0000);
    40   os.writeString(header);
    41   os.writeString(info);
    42   os.close();
    43 }
    45 function run_test() {
    46   do_check_true(gIsWindows || gIsOSX || gIsLinux);
    48   let file = get_test_plugin_no_symlink();
    49   if (!file)
    50     do_throw("Plugin library not found");
    52   const pluginDir = file.parent;
    53   const tempDir = do_get_tempdir();
    54   const suffix = get_platform_specific_plugin_suffix();
    55   const pluginName = file.leafName.substring(0, file.leafName.length - suffix.length).toLowerCase();
    56   const pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
    57   const statePref = "plugin.state." + pluginName;
    59   // write plugin registry data
    60   let registry = "";
    62   registry += file.leafName + DELIM + "$\n";
    63   registry += file.path + DELIM + "$\n";
    64   registry += "1.0.0.0" + DELIM + "$\n";
    65   registry += file.lastModifiedTime + DELIM + "0" + DELIM + "0" + DELIM + "$\n";
    66   registry += "Plug-in for testing purposes." + DELIM + "$\n";
    67   registry += "Test Plug-in" + DELIM + "$\n";
    68   registry += "1\n";
    69   registry += "0" + DELIM + "application/x-test" + DELIM + "Test mimetype" + DELIM + "tst" + DELIM + "$\n";
    71   registry += "\n";
    72   registry += "[INVALID]\n";
    73   registry += "\n";
    74   write_registry("0.15", registry);
    76   // Initialise profile folder
    77   do_get_profile();
    79   let plugin = get_test_plugintag();
    80   if (!plugin)
    81     do_throw("Plugin tag not found");
    83   // check that the expected plugin state was loaded correctly from the registry
    84   do_check_true(plugin.disabled);
    85   do_check_false(plugin.clicktoplay);
    86   // ... and imported into prefs, with 0 being the disabled state
    87   do_check_eq(0, Services.prefs.getIntPref(statePref));
    89   // prepare a copy of the plugin and backup the original
    90   file.copyTo(null, "nptestcopy" + suffix);
    91   let copy = pluginDir.clone();
    92   copy.append("nptestcopy" + suffix);
    93   file.moveTo(tempDir, null);
    95   // test that the settings persist through a few variations of test-plugin names
    96   let testNames = [
    97     pluginName + "2",
    98     pluginName.toUpperCase() + "_11_5_42_2323",
    99     pluginName + "-5.2.7"
   100   ];
   101   testNames.forEach(function(leafName) {
   102     dump("Checking " + leafName + ".\n");
   103     copy.moveTo(null, leafName + suffix);
   104     pluginHost.reloadPlugins(false);
   105     plugin = get_test_plugintag();
   106     do_check_false(plugin == null);
   107     do_check_true(plugin.disabled);
   108     do_check_false(plugin.clicktoplay);
   109   });
   111   // check that the state persists even if the plugin is not always present
   112   copy.moveTo(tempDir, null);
   113   pluginHost.reloadPlugins(false);
   114   copy.moveTo(pluginDir, null);
   115   pluginHost.reloadPlugins(false);
   117   plugin = get_test_plugintag();
   118   do_check_false(plugin == null);
   119   do_check_true(plugin.disabled);
   120   do_check_false(plugin.clicktoplay);
   122   // clean up
   123   Services.prefs.clearUserPref(statePref);
   124   Services.prefs.clearUserPref("plugin.importedState");
   125   copy.remove(true);
   126   file.moveTo(pluginDir, null);
   127 }

mercurial