dom/plugins/test/unit/test_plugin_default_state_xpi.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/test/unit/test_plugin_default_state_xpi.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +Cu.import("resource://gre/modules/Services.jsm");
     1.9 +Cu.import("resource://gre/modules/Promise.jsm");
    1.10 +
    1.11 +const ADDON_ID = "test-plugin-from-xpi@tests.mozilla.org";
    1.12 +const XRE_EXTENSIONS_DIR_LIST = "XREExtDL";
    1.13 +const NS_APP_PLUGINS_DIR_LIST = "APluginsDL";
    1.14 +
    1.15 +const gPluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
    1.16 +const gXPCOMABI = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).XPCOMABI;
    1.17 +let gProfileDir = null;
    1.18 +
    1.19 +function getAddonRoot(profileDir, id) {
    1.20 +  let dir = profileDir.clone();
    1.21 +  dir.append("extensions");
    1.22 +  Assert.ok(dir.exists(), "Extensions dir should exist: " + dir.path);
    1.23 +  dir.append(id);
    1.24 +  return dir;
    1.25 +}
    1.26 +
    1.27 +function getTestaddonFilename() {
    1.28 +  let abiPart = "";
    1.29 +  if (gIsOSX) {
    1.30 +    abiPart = "_" + gXPCOMABI;
    1.31 +  }
    1.32 +  return "testaddon" + abiPart + ".xpi";
    1.33 +}
    1.34 +
    1.35 +function run_test() {
    1.36 +  loadAddonManager();
    1.37 +  gProfileDir = do_get_profile();
    1.38 +  do_register_cleanup(() => shutdownManager());
    1.39 +  run_next_test();
    1.40 +}
    1.41 +
    1.42 +add_task(function* test_state() {
    1.43 +  // Remove test so we will have only one "Test Plug-in" registered.
    1.44 +  // xpcshell tests have plugins in per-test profiles, so that's fine.
    1.45 +  let file = get_test_plugin();
    1.46 +  file.remove(true);
    1.47 +  file = get_test_plugin(true);
    1.48 +  file.remove(true);
    1.49 +
    1.50 +  Services.prefs.setIntPref("plugin.default.state", Ci.nsIPluginTag.STATE_CLICKTOPLAY);
    1.51 +  Services.prefs.setIntPref("plugin.defaultXpi.state", Ci.nsIPluginTag.STATE_ENABLED);
    1.52 +
    1.53 +  let success = yield installAddon(getTestaddonFilename());
    1.54 +  Assert.ok(success, "Should have installed addon.");
    1.55 +  let addonDir = getAddonRoot(gProfileDir, ADDON_ID);
    1.56 +
    1.57 +  let provider = {
    1.58 +    classID: Components.ID("{0af6b2d7-a06c-49b7-babc-636d292b0dbb}"),
    1.59 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider,
    1.60 +                                           Ci.nsIDirectoryServiceProvider2]),
    1.61 +
    1.62 +    getFile: function (prop, persistant) {
    1.63 +      throw Cr.NS_ERROR_FAILURE;
    1.64 +    },
    1.65 +
    1.66 +    getFiles: function (prop) {
    1.67 +      let result = [];
    1.68 +
    1.69 +      switch (prop) {
    1.70 +      case XRE_EXTENSIONS_DIR_LIST:
    1.71 +        result.push(addonDir);
    1.72 +        break;
    1.73 +      case NS_APP_PLUGINS_DIR_LIST:
    1.74 +        let pluginDir = addonDir.clone();
    1.75 +        pluginDir.append("plugins");
    1.76 +        result.push(pluginDir);
    1.77 +        break;
    1.78 +      default:
    1.79 +        throw Cr.NS_ERROR_FAILURE;
    1.80 +      }
    1.81 +
    1.82 +      return {
    1.83 +        QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]),
    1.84 +        hasMoreElements: () => result.length > 0,
    1.85 +        getNext: () => result.shift(),
    1.86 +      };
    1.87 +    },
    1.88 +  };
    1.89 +
    1.90 +  let dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
    1.91 +  dirSvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
    1.92 +
    1.93 +  // We installed a non-restartless addon, need to restart the manager.
    1.94 +  restartManager();
    1.95 +  gPluginHost.reloadPlugins();
    1.96 +
    1.97 +  Assert.ok(addonDir.exists(), "Addon path should exist: " + addonDir.path);
    1.98 +  Assert.ok(addonDir.isDirectory(), "Addon path should be a directory: " + addonDir.path);
    1.99 +  let pluginDir = addonDir.clone();
   1.100 +  pluginDir.append("plugins");
   1.101 +  Assert.ok(pluginDir.exists(), "Addon plugins path should exist: " + pluginDir.path);
   1.102 +  Assert.ok(pluginDir.isDirectory(), "Addon plugins path should be a directory: " + pluginDir.path);
   1.103 +
   1.104 +  let addon = yield getAddonByID(ADDON_ID);
   1.105 +  Assert.ok(!addon.appDisabled, "Addon should not be appDisabled");
   1.106 +  Assert.ok(addon.isActive, "Addon should be active");
   1.107 +  Assert.ok(addon.isCompatible, "Addon should be compatible");
   1.108 +  Assert.ok(!addon.userDisabled, "Addon should not be user disabled");
   1.109 +
   1.110 +  let testPlugin = get_test_plugintag();
   1.111 +  Assert.notEqual(testPlugin, null, "Test plugin should have been found");
   1.112 +  Assert.equal(testPlugin.enabledState, Ci.nsIPluginTag.STATE_ENABLED, "Test plugin from addon should have state enabled");
   1.113 +
   1.114 +  pluginDir.append(testPlugin.filename);
   1.115 +  Assert.ok(pluginDir.exists(), "Plugin file should exist in addon directory: " + pluginDir.path);
   1.116 +
   1.117 +  testPlugin = get_test_plugintag("Second Test Plug-in");
   1.118 +  Assert.notEqual(testPlugin, null, "Second test plugin should have been found");
   1.119 +  Assert.equal(testPlugin.enabledState, Ci.nsIPluginTag.STATE_ENABLED, "Second test plugin from addon should have state enabled");
   1.120 +});

mercurial