toolkit/mozapps/extensions/test/xpcshell/test_plugins.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_plugins.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,210 @@
     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 +// This verifies that plugins exist and can be enabled and disabled.
     1.9 +var gID = null;
    1.10 +
    1.11 +function setTestPluginState(state) {
    1.12 +  let tags = AM_Cc["@mozilla.org/plugin/host;1"].getService(AM_Ci.nsIPluginHost)
    1.13 +    .getPluginTags();
    1.14 +  for (let tag of tags) {
    1.15 +    if (tag.name == "Test Plug-in") {
    1.16 +      tag.enabledState = state;
    1.17 +      return;
    1.18 +    }
    1.19 +  }
    1.20 +  throw Error("No plugin tag found for the test plugin");
    1.21 +}
    1.22 +
    1.23 +function run_test() {
    1.24 +  do_test_pending();
    1.25 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
    1.26 +  Services.prefs.setBoolPref("plugins.click_to_play", true);
    1.27 +
    1.28 +  setTestPluginState(AM_Ci.nsIPluginTag.STATE_CLICKTOPLAY);
    1.29 +
    1.30 +  startupManager();
    1.31 +  AddonManager.addAddonListener(AddonListener);
    1.32 +  AddonManager.addInstallListener(InstallListener);
    1.33 +
    1.34 +  run_test_1();
    1.35 +}
    1.36 +
    1.37 +// Finds the test plugin library
    1.38 +function get_test_plugin() {
    1.39 +  var pluginEnum = Services.dirsvc.get("APluginsDL", AM_Ci.nsISimpleEnumerator);
    1.40 +  while (pluginEnum.hasMoreElements()) {
    1.41 +    let dir = pluginEnum.getNext().QueryInterface(AM_Ci.nsILocalFile);
    1.42 +    let plugin = dir.clone();
    1.43 +    // OSX plugin
    1.44 +    plugin.append("Test.plugin");
    1.45 +    if (plugin.exists()) {
    1.46 +      plugin.normalize();
    1.47 +      return plugin;
    1.48 +    }
    1.49 +    plugin = dir.clone();
    1.50 +    // *nix plugin
    1.51 +    plugin.append("libnptest.so");
    1.52 +    if (plugin.exists()) {
    1.53 +      plugin.normalize();
    1.54 +      return plugin;
    1.55 +    }
    1.56 +    // Windows plugin
    1.57 +    plugin = dir.clone();
    1.58 +    plugin.append("nptest.dll");
    1.59 +    if (plugin.exists()) {
    1.60 +      plugin.normalize();
    1.61 +      return plugin;
    1.62 +    }
    1.63 +  }
    1.64 +  return null;
    1.65 +}
    1.66 +
    1.67 +function getFileSize(aFile) {
    1.68 +  if (!aFile.isDirectory())
    1.69 +    return aFile.fileSize;
    1.70 +
    1.71 +  let size = 0;
    1.72 +  let entries = aFile.directoryEntries.QueryInterface(AM_Ci.nsIDirectoryEnumerator);
    1.73 +  let entry;
    1.74 +  while (entry = entries.nextFile)
    1.75 +    size += getFileSize(entry);
    1.76 +  entries.close();
    1.77 +  return size;
    1.78 +}
    1.79 +
    1.80 +function getPluginLastModifiedTime(aPluginFile) {
    1.81 +  // On OS X we use the bundle contents last modified time as using
    1.82 +  // the package directories modified date may be outdated.
    1.83 +  // See bug 313700.
    1.84 +  try {
    1.85 +    let localFileMac = aPluginFile.QueryInterface(AM_Ci.nsILocalFileMac);
    1.86 +    if (localFileMac) {
    1.87 +      return localFileMac.bundleContentsLastModifiedTime;
    1.88 +    }
    1.89 +  } catch (e) {
    1.90 +  }
    1.91 +
    1.92 +  return aPluginFile.lastModifiedTime;
    1.93 +}
    1.94 +
    1.95 +// Tests that the test plugin exists
    1.96 +function run_test_1() {
    1.97 +  var testPlugin = get_test_plugin();
    1.98 +  do_check_neq(testPlugin, null);
    1.99 +
   1.100 +  AddonManager.getAddonsByTypes(["plugin"], function(addons) {
   1.101 +    do_check_true(addons.length > 0);
   1.102 +
   1.103 +    addons.forEach(function(p) {
   1.104 +      if (p.name == "Test Plug-in")
   1.105 +        gID = p.id;
   1.106 +    });
   1.107 +
   1.108 +    do_check_neq(gID, null);
   1.109 +
   1.110 +    AddonManager.getAddonByID(gID, function(p) {
   1.111 +      do_check_neq(p, null);
   1.112 +      do_check_eq(p.name, "Test Plug-in");
   1.113 +      do_check_eq(p.description,
   1.114 +                  "Plug-in for testing purposes.\u2122 " +
   1.115 +                    "(\u0939\u093f\u0928\u094d\u0926\u0940 " + 
   1.116 +                    "\u4e2d\u6587 " +
   1.117 +                    "\u0627\u0644\u0639\u0631\u0628\u064a\u0629)");
   1.118 +      do_check_eq(p.creator, null);
   1.119 +      do_check_eq(p.version, "1.0.0.0");
   1.120 +      do_check_eq(p.type, "plugin");
   1.121 +      do_check_eq(p.userDisabled, "askToActivate");
   1.122 +      do_check_false(p.appDisabled);
   1.123 +      do_check_true(p.isActive);
   1.124 +      do_check_true(p.isCompatible);
   1.125 +      do_check_true(p.providesUpdatesSecurely);
   1.126 +      do_check_eq(p.blocklistState, 0);
   1.127 +      do_check_eq(p.permissions, AddonManager.PERM_CAN_DISABLE | AddonManager.PERM_CAN_ENABLE);
   1.128 +      do_check_eq(p.pendingOperations, 0);
   1.129 +      do_check_true(p.size > 0);
   1.130 +      do_check_eq(p.size, getFileSize(testPlugin));
   1.131 +      do_check_true(p.updateDate > 0);
   1.132 +      do_check_true("isCompatibleWith" in p);
   1.133 +      do_check_true("findUpdates" in p);
   1.134 +
   1.135 +      let lastModifiedTime = getPluginLastModifiedTime(testPlugin);
   1.136 +      do_check_eq(p.updateDate.getTime(), lastModifiedTime);
   1.137 +      do_check_eq(p.installDate.getTime(), lastModifiedTime);
   1.138 +
   1.139 +      run_test_2(p);
   1.140 +    });
   1.141 +  });
   1.142 +}
   1.143 +
   1.144 +// Tests that disabling a plugin works
   1.145 +function run_test_2(p) {
   1.146 +  let test = {};
   1.147 +  test[gID] = [
   1.148 +    ["onDisabling", false],
   1.149 +    "onDisabled",
   1.150 +    ["onPropertyChanged", ["userDisabled"]]
   1.151 +  ];
   1.152 +  prepare_test(test);
   1.153 +
   1.154 +  p.userDisabled = true;
   1.155 +
   1.156 +  ensure_test_completed();
   1.157 +
   1.158 +  do_check_true(p.userDisabled);
   1.159 +  do_check_false(p.appDisabled);
   1.160 +  do_check_false(p.isActive);
   1.161 +
   1.162 +  AddonManager.getAddonByID(gID, function(p) {
   1.163 +    do_check_neq(p, null);
   1.164 +    do_check_true(p.userDisabled);
   1.165 +    do_check_false(p.appDisabled);
   1.166 +    do_check_false(p.isActive);
   1.167 +    do_check_eq(p.name, "Test Plug-in");
   1.168 +
   1.169 +    run_test_3(p);
   1.170 +  });
   1.171 +}
   1.172 +
   1.173 +// Tests that enabling a plugin works
   1.174 +function run_test_3(p) {
   1.175 +  let test = {};
   1.176 +  test[gID] = [
   1.177 +    ["onEnabling", false],
   1.178 +    "onEnabled"
   1.179 +  ];
   1.180 +  prepare_test(test);
   1.181 +
   1.182 +  p.userDisabled = false;
   1.183 +
   1.184 +  ensure_test_completed();
   1.185 +
   1.186 +  do_check_false(p.userDisabled);
   1.187 +  do_check_false(p.appDisabled);
   1.188 +  do_check_true(p.isActive);
   1.189 +
   1.190 +  AddonManager.getAddonByID(gID, function(p) {
   1.191 +    do_check_neq(p, null);
   1.192 +    do_check_false(p.userDisabled);
   1.193 +    do_check_false(p.appDisabled);
   1.194 +    do_check_true(p.isActive);
   1.195 +    do_check_eq(p.name, "Test Plug-in");
   1.196 +
   1.197 +    do_execute_soon(run_test_4);
   1.198 +  });
   1.199 +}
   1.200 +
   1.201 +// Verify that after a restart the test plugin has the same ID
   1.202 +function run_test_4() {
   1.203 +  restartManager();
   1.204 +
   1.205 +  AddonManager.getAddonByID(gID, function(p) {
   1.206 +    do_check_neq(p, null);
   1.207 +    do_check_eq(p.name, "Test Plug-in");
   1.208 +
   1.209 +    Services.prefs.clearUserPref("plugins.click_to_play");
   1.210 +
   1.211 +    do_execute_soon(do_test_finished);
   1.212 +  });
   1.213 +}

mercurial