|
1 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 let scope = {}; |
|
7 Cu.import("resource://gre/modules/PermissionSettings.jsm", scope); |
|
8 |
|
9 const TEST_URL = |
|
10 "http://mochi.test:8888/browser/dom/tests/browser/test-webapps-permissions.html"; |
|
11 const TEST_MANIFEST_URL = |
|
12 "http://mochi.test:8888/browser/dom/tests/browser/test-webapp.webapp"; |
|
13 const TEST_ORIGIN_URL = "http://mochi.test:8888"; |
|
14 |
|
15 const installedPermsToTest = { |
|
16 "geolocation": "prompt", |
|
17 "alarms": "allow", |
|
18 "contacts": "deny", |
|
19 "device-storage:apps": "deny", |
|
20 }; |
|
21 |
|
22 const reinstalledPermsToTest = { |
|
23 "geolocation": "prompt", |
|
24 "alarms": "unknown", |
|
25 "contacts": "deny", |
|
26 "device-storage:apps": "deny", |
|
27 }; |
|
28 |
|
29 var gWindow, gNavigator; |
|
30 |
|
31 function test() { |
|
32 waitForExplicitFinish(); |
|
33 |
|
34 var tab = gBrowser.addTab(TEST_URL); |
|
35 gBrowser.selectedTab = tab; |
|
36 var browser = gBrowser.selectedBrowser; |
|
37 PopupNotifications.panel.addEventListener("popupshown", handlePopup, false); |
|
38 |
|
39 registerCleanupFunction(function () { |
|
40 gWindow = null; |
|
41 gBrowser.removeTab(tab); |
|
42 }); |
|
43 |
|
44 browser.addEventListener("DOMContentLoaded", function onLoad(event) { |
|
45 browser.removeEventListener("DOMContentLoaded", onLoad, false); |
|
46 gWindow = browser.contentWindow; |
|
47 SpecialPowers.setBoolPref("dom.mozPermissionSettings.enabled", true); |
|
48 SpecialPowers.addPermission("permissions", true, browser.contentWindow.document); |
|
49 SpecialPowers.addPermission("permissions", true, browser.contentDocument); |
|
50 |
|
51 let pendingInstall; |
|
52 |
|
53 function testInstall() { |
|
54 var nav = XPCNativeWrapper.unwrap(browser.contentWindow.navigator); |
|
55 ok(nav.mozApps, "we have a mozApps property"); |
|
56 var navMozPerms = nav.mozPermissionSettings; |
|
57 ok(navMozPerms, "mozPermissions is available"); |
|
58 |
|
59 // INSTALL app |
|
60 pendingInstall = nav.mozApps.install(TEST_MANIFEST_URL, null); |
|
61 pendingInstall.onsuccess = function onsuccess() |
|
62 { |
|
63 ok(this.result, "we have a result: " + this.result); |
|
64 function testPerm(aPerm, aAccess) |
|
65 { |
|
66 var res = |
|
67 navMozPerms.get(aPerm, TEST_MANIFEST_URL, TEST_ORIGIN_URL, false); |
|
68 is(res, aAccess, "install: " + aPerm + " is " + res); |
|
69 } |
|
70 |
|
71 for (let permName in installedPermsToTest) { |
|
72 testPerm(permName, installedPermsToTest[permName]); |
|
73 } |
|
74 |
|
75 writeUpdatesToWebappManifest(); |
|
76 }; |
|
77 |
|
78 pendingInstall.onerror = function onerror(e) |
|
79 { |
|
80 ok(false, "install()'s onerror was called: " + e); |
|
81 ok(false, "All permission checks failed, reinstall tests were not run"); |
|
82 }; |
|
83 } |
|
84 testInstall(); |
|
85 }, false); |
|
86 } |
|
87 |
|
88 function reinstallApp() |
|
89 { |
|
90 var browser = gBrowser.selectedBrowser; |
|
91 var nav = XPCNativeWrapper.unwrap(browser.contentWindow.navigator); |
|
92 var navMozPerms = nav.mozPermissionSettings; |
|
93 |
|
94 var pendingReinstall = nav.mozApps.install(TEST_MANIFEST_URL); |
|
95 pendingReinstall.onsuccess = function onsuccess() |
|
96 { |
|
97 ok(this.result, "we have a result: " + this.result); |
|
98 |
|
99 function testPerm(aPerm, aAccess) |
|
100 { |
|
101 var res = |
|
102 navMozPerms.get(aPerm, TEST_MANIFEST_URL, TEST_ORIGIN_URL, false); |
|
103 is(res, aAccess, "reinstall: " + aPerm + " is " + res); |
|
104 } |
|
105 |
|
106 for (let permName in reinstalledPermsToTest) { |
|
107 testPerm(permName, reinstalledPermsToTest[permName]); |
|
108 } |
|
109 writeUpdatesToWebappManifest(); |
|
110 finish(); |
|
111 }; |
|
112 }; |
|
113 |
|
114 var qtyPopups = 0; |
|
115 |
|
116 function handlePopup(aEvent) |
|
117 { |
|
118 qtyPopups++; |
|
119 if (qtyPopups == 2) { |
|
120 aEvent.target.removeEventListener("popupshown", handlePopup, false); |
|
121 } |
|
122 SpecialPowers.wrap(this).childNodes[0].button.doCommand(); |
|
123 } |
|
124 |
|
125 function writeUpdatesToWebappManifest(aRestore) |
|
126 { |
|
127 let newfile = Cc["@mozilla.org/file/directory_service;1"]. |
|
128 getService(Ci.nsIProperties). |
|
129 get("XCurProcD", Ci.nsIFile); |
|
130 |
|
131 let devPath = ["_tests", "testing", "mochitest", |
|
132 "browser", "dom" , "tests", "browser"]; |
|
133 // make package-tests moves tests to: |
|
134 // dist/test-package-stage/mochitest/browser/dom/tests/browser |
|
135 let slavePath = ["dist", "test-package-stage", "mochitest", |
|
136 "browser", "dom", "tests", "browser"]; |
|
137 |
|
138 newfile = newfile.parent; // up to dist/ |
|
139 newfile = newfile.parent;// up to obj-dir/ |
|
140 |
|
141 info(newfile.path); |
|
142 try { |
|
143 for (let idx in devPath) { |
|
144 newfile.append(devPath[idx]); |
|
145 if (!newfile.isDirectory()) { |
|
146 info("*** NOT RUNNING ON DEV MACHINE\n\n"); |
|
147 throw new Error("Test is not running on dev machine, try dev path"); |
|
148 } |
|
149 } |
|
150 } |
|
151 catch (ex) { |
|
152 info(ex + "\n\n"); |
|
153 for (let idx in slavePath) { |
|
154 newfile.append(slavePath[idx]); |
|
155 if (!newfile.isDirectory()) { |
|
156 info("*** NOT RUNNING ON BUILD SLAVE\n\n"); |
|
157 throw new Error("Test is not running on slave machine, abort test"); |
|
158 } |
|
159 } |
|
160 } |
|
161 |
|
162 info("Final path: " + newfile.path); |
|
163 |
|
164 if (aRestore) { |
|
165 newfile.append("test-webapp-original.webapp"); |
|
166 } else { |
|
167 newfile.append("test-webapp-reinstall.webapp"); |
|
168 } |
|
169 |
|
170 let oldfile = newfile.parent; |
|
171 oldfile.append("test-webapp.webapp"); |
|
172 |
|
173 newfile.copyTo(null, "test-webapp.webapp"); |
|
174 |
|
175 if (!aRestore) { |
|
176 executeSoon(function (){ reinstallApp(); }); |
|
177 } |
|
178 } |