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