toolkit/mozapps/extensions/test/xpcshell/test_plugins.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.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 3 */
michael@0 4
michael@0 5 // This verifies that plugins exist and can be enabled and disabled.
michael@0 6 var gID = null;
michael@0 7
michael@0 8 function setTestPluginState(state) {
michael@0 9 let tags = AM_Cc["@mozilla.org/plugin/host;1"].getService(AM_Ci.nsIPluginHost)
michael@0 10 .getPluginTags();
michael@0 11 for (let tag of tags) {
michael@0 12 if (tag.name == "Test Plug-in") {
michael@0 13 tag.enabledState = state;
michael@0 14 return;
michael@0 15 }
michael@0 16 }
michael@0 17 throw Error("No plugin tag found for the test plugin");
michael@0 18 }
michael@0 19
michael@0 20 function run_test() {
michael@0 21 do_test_pending();
michael@0 22 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
michael@0 23 Services.prefs.setBoolPref("plugins.click_to_play", true);
michael@0 24
michael@0 25 setTestPluginState(AM_Ci.nsIPluginTag.STATE_CLICKTOPLAY);
michael@0 26
michael@0 27 startupManager();
michael@0 28 AddonManager.addAddonListener(AddonListener);
michael@0 29 AddonManager.addInstallListener(InstallListener);
michael@0 30
michael@0 31 run_test_1();
michael@0 32 }
michael@0 33
michael@0 34 // Finds the test plugin library
michael@0 35 function get_test_plugin() {
michael@0 36 var pluginEnum = Services.dirsvc.get("APluginsDL", AM_Ci.nsISimpleEnumerator);
michael@0 37 while (pluginEnum.hasMoreElements()) {
michael@0 38 let dir = pluginEnum.getNext().QueryInterface(AM_Ci.nsILocalFile);
michael@0 39 let plugin = dir.clone();
michael@0 40 // OSX plugin
michael@0 41 plugin.append("Test.plugin");
michael@0 42 if (plugin.exists()) {
michael@0 43 plugin.normalize();
michael@0 44 return plugin;
michael@0 45 }
michael@0 46 plugin = dir.clone();
michael@0 47 // *nix plugin
michael@0 48 plugin.append("libnptest.so");
michael@0 49 if (plugin.exists()) {
michael@0 50 plugin.normalize();
michael@0 51 return plugin;
michael@0 52 }
michael@0 53 // Windows plugin
michael@0 54 plugin = dir.clone();
michael@0 55 plugin.append("nptest.dll");
michael@0 56 if (plugin.exists()) {
michael@0 57 plugin.normalize();
michael@0 58 return plugin;
michael@0 59 }
michael@0 60 }
michael@0 61 return null;
michael@0 62 }
michael@0 63
michael@0 64 function getFileSize(aFile) {
michael@0 65 if (!aFile.isDirectory())
michael@0 66 return aFile.fileSize;
michael@0 67
michael@0 68 let size = 0;
michael@0 69 let entries = aFile.directoryEntries.QueryInterface(AM_Ci.nsIDirectoryEnumerator);
michael@0 70 let entry;
michael@0 71 while (entry = entries.nextFile)
michael@0 72 size += getFileSize(entry);
michael@0 73 entries.close();
michael@0 74 return size;
michael@0 75 }
michael@0 76
michael@0 77 function getPluginLastModifiedTime(aPluginFile) {
michael@0 78 // On OS X we use the bundle contents last modified time as using
michael@0 79 // the package directories modified date may be outdated.
michael@0 80 // See bug 313700.
michael@0 81 try {
michael@0 82 let localFileMac = aPluginFile.QueryInterface(AM_Ci.nsILocalFileMac);
michael@0 83 if (localFileMac) {
michael@0 84 return localFileMac.bundleContentsLastModifiedTime;
michael@0 85 }
michael@0 86 } catch (e) {
michael@0 87 }
michael@0 88
michael@0 89 return aPluginFile.lastModifiedTime;
michael@0 90 }
michael@0 91
michael@0 92 // Tests that the test plugin exists
michael@0 93 function run_test_1() {
michael@0 94 var testPlugin = get_test_plugin();
michael@0 95 do_check_neq(testPlugin, null);
michael@0 96
michael@0 97 AddonManager.getAddonsByTypes(["plugin"], function(addons) {
michael@0 98 do_check_true(addons.length > 0);
michael@0 99
michael@0 100 addons.forEach(function(p) {
michael@0 101 if (p.name == "Test Plug-in")
michael@0 102 gID = p.id;
michael@0 103 });
michael@0 104
michael@0 105 do_check_neq(gID, null);
michael@0 106
michael@0 107 AddonManager.getAddonByID(gID, function(p) {
michael@0 108 do_check_neq(p, null);
michael@0 109 do_check_eq(p.name, "Test Plug-in");
michael@0 110 do_check_eq(p.description,
michael@0 111 "Plug-in for testing purposes.\u2122 " +
michael@0 112 "(\u0939\u093f\u0928\u094d\u0926\u0940 " +
michael@0 113 "\u4e2d\u6587 " +
michael@0 114 "\u0627\u0644\u0639\u0631\u0628\u064a\u0629)");
michael@0 115 do_check_eq(p.creator, null);
michael@0 116 do_check_eq(p.version, "1.0.0.0");
michael@0 117 do_check_eq(p.type, "plugin");
michael@0 118 do_check_eq(p.userDisabled, "askToActivate");
michael@0 119 do_check_false(p.appDisabled);
michael@0 120 do_check_true(p.isActive);
michael@0 121 do_check_true(p.isCompatible);
michael@0 122 do_check_true(p.providesUpdatesSecurely);
michael@0 123 do_check_eq(p.blocklistState, 0);
michael@0 124 do_check_eq(p.permissions, AddonManager.PERM_CAN_DISABLE | AddonManager.PERM_CAN_ENABLE);
michael@0 125 do_check_eq(p.pendingOperations, 0);
michael@0 126 do_check_true(p.size > 0);
michael@0 127 do_check_eq(p.size, getFileSize(testPlugin));
michael@0 128 do_check_true(p.updateDate > 0);
michael@0 129 do_check_true("isCompatibleWith" in p);
michael@0 130 do_check_true("findUpdates" in p);
michael@0 131
michael@0 132 let lastModifiedTime = getPluginLastModifiedTime(testPlugin);
michael@0 133 do_check_eq(p.updateDate.getTime(), lastModifiedTime);
michael@0 134 do_check_eq(p.installDate.getTime(), lastModifiedTime);
michael@0 135
michael@0 136 run_test_2(p);
michael@0 137 });
michael@0 138 });
michael@0 139 }
michael@0 140
michael@0 141 // Tests that disabling a plugin works
michael@0 142 function run_test_2(p) {
michael@0 143 let test = {};
michael@0 144 test[gID] = [
michael@0 145 ["onDisabling", false],
michael@0 146 "onDisabled",
michael@0 147 ["onPropertyChanged", ["userDisabled"]]
michael@0 148 ];
michael@0 149 prepare_test(test);
michael@0 150
michael@0 151 p.userDisabled = true;
michael@0 152
michael@0 153 ensure_test_completed();
michael@0 154
michael@0 155 do_check_true(p.userDisabled);
michael@0 156 do_check_false(p.appDisabled);
michael@0 157 do_check_false(p.isActive);
michael@0 158
michael@0 159 AddonManager.getAddonByID(gID, function(p) {
michael@0 160 do_check_neq(p, null);
michael@0 161 do_check_true(p.userDisabled);
michael@0 162 do_check_false(p.appDisabled);
michael@0 163 do_check_false(p.isActive);
michael@0 164 do_check_eq(p.name, "Test Plug-in");
michael@0 165
michael@0 166 run_test_3(p);
michael@0 167 });
michael@0 168 }
michael@0 169
michael@0 170 // Tests that enabling a plugin works
michael@0 171 function run_test_3(p) {
michael@0 172 let test = {};
michael@0 173 test[gID] = [
michael@0 174 ["onEnabling", false],
michael@0 175 "onEnabled"
michael@0 176 ];
michael@0 177 prepare_test(test);
michael@0 178
michael@0 179 p.userDisabled = false;
michael@0 180
michael@0 181 ensure_test_completed();
michael@0 182
michael@0 183 do_check_false(p.userDisabled);
michael@0 184 do_check_false(p.appDisabled);
michael@0 185 do_check_true(p.isActive);
michael@0 186
michael@0 187 AddonManager.getAddonByID(gID, function(p) {
michael@0 188 do_check_neq(p, null);
michael@0 189 do_check_false(p.userDisabled);
michael@0 190 do_check_false(p.appDisabled);
michael@0 191 do_check_true(p.isActive);
michael@0 192 do_check_eq(p.name, "Test Plug-in");
michael@0 193
michael@0 194 do_execute_soon(run_test_4);
michael@0 195 });
michael@0 196 }
michael@0 197
michael@0 198 // Verify that after a restart the test plugin has the same ID
michael@0 199 function run_test_4() {
michael@0 200 restartManager();
michael@0 201
michael@0 202 AddonManager.getAddonByID(gID, function(p) {
michael@0 203 do_check_neq(p, null);
michael@0 204 do_check_eq(p.name, "Test Plug-in");
michael@0 205
michael@0 206 Services.prefs.clearUserPref("plugins.click_to_play");
michael@0 207
michael@0 208 do_execute_soon(do_test_finished);
michael@0 209 });
michael@0 210 }

mercurial