|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); |
|
5 Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
|
6 |
|
7 const ABOUT_PERMISSIONS_SPEC = "about:permissions"; |
|
8 |
|
9 const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/"); |
|
10 const TEST_URI_2 = NetUtil.newURI("http://mozilla.org/"); |
|
11 |
|
12 const TEST_PRINCIPAL_1 = Services.scriptSecurityManager.getNoAppCodebasePrincipal(TEST_URI_1); |
|
13 const TEST_PRINCIPAL_2 = Services.scriptSecurityManager.getNoAppCodebasePrincipal(TEST_URI_2); |
|
14 |
|
15 // values from DefaultPermissions object |
|
16 const PERM_UNKNOWN = 0; |
|
17 const PERM_ALLOW = 1; |
|
18 const PERM_DENY = 2; |
|
19 // cookie specific permissions |
|
20 const PERM_FIRST_PARTY_ONLY = 9; |
|
21 |
|
22 // used to set permissions on test sites |
|
23 const TEST_PERMS = { |
|
24 "password": PERM_ALLOW, |
|
25 "cookie": PERM_ALLOW, |
|
26 "geo": PERM_UNKNOWN, |
|
27 "indexedDB": PERM_UNKNOWN, |
|
28 "popup": PERM_DENY, |
|
29 "fullscreen" : PERM_UNKNOWN, |
|
30 "camera": PERM_UNKNOWN, |
|
31 "microphone": PERM_UNKNOWN |
|
32 }; |
|
33 |
|
34 const NO_GLOBAL_ALLOW = [ |
|
35 "geo", |
|
36 "indexedDB", |
|
37 "fullscreen" |
|
38 ]; |
|
39 |
|
40 // number of managed permissions in the interface |
|
41 const TEST_PERMS_COUNT = 8; |
|
42 |
|
43 function test() { |
|
44 waitForExplicitFinish(); |
|
45 registerCleanupFunction(cleanUp); |
|
46 |
|
47 // add test history visit |
|
48 addVisits(TEST_URI_1, function() { |
|
49 // set permissions ourselves to avoid problems with different defaults |
|
50 // from test harness configuration |
|
51 for (let type in TEST_PERMS) { |
|
52 if (type == "password") { |
|
53 Services.logins.setLoginSavingEnabled(TEST_URI_2.prePath, true); |
|
54 } else { |
|
55 // set permissions on a site without history visits to test enumerateServices |
|
56 Services.perms.addFromPrincipal(TEST_PRINCIPAL_2, type, TEST_PERMS[type]); |
|
57 } |
|
58 } |
|
59 |
|
60 // open about:permissions |
|
61 gBrowser.selectedTab = gBrowser.addTab("about:permissions"); |
|
62 }); |
|
63 |
|
64 function observer() { |
|
65 Services.obs.removeObserver(observer, "browser-permissions-initialized"); |
|
66 runNextTest(); |
|
67 } |
|
68 Services.obs.addObserver(observer, "browser-permissions-initialized", false); |
|
69 } |
|
70 |
|
71 function cleanUp() { |
|
72 for (let type in TEST_PERMS) { |
|
73 if (type != "password") { |
|
74 Services.perms.removeFromPrincipal(TEST_PRINCIPAL_1, type); |
|
75 Services.perms.removeFromPrincipal(TEST_PRINCIPAL_2, type); |
|
76 } |
|
77 } |
|
78 |
|
79 gBrowser.removeTab(gBrowser.selectedTab); |
|
80 } |
|
81 |
|
82 function runNextTest() { |
|
83 if (gTestIndex == tests.length) { |
|
84 waitForClearHistory(finish); |
|
85 return; |
|
86 } |
|
87 |
|
88 let nextTest = tests[gTestIndex++]; |
|
89 info("[" + nextTest.name + "] running test"); |
|
90 nextTest(); |
|
91 } |
|
92 |
|
93 var gSitesList; |
|
94 var gHeaderDeck; |
|
95 var gSiteLabel; |
|
96 |
|
97 var gTestIndex = 0; |
|
98 var tests = [ |
|
99 function test_page_load() { |
|
100 is(gBrowser.currentURI.spec, ABOUT_PERMISSIONS_SPEC, "about:permissions loaded"); |
|
101 |
|
102 gSitesList = gBrowser.contentDocument.getElementById("sites-list"); |
|
103 ok(gSitesList, "got sites list"); |
|
104 |
|
105 gHeaderDeck = gBrowser.contentDocument.getElementById("header-deck"); |
|
106 ok(gHeaderDeck, "got header deck"); |
|
107 |
|
108 gSiteLabel = gBrowser.contentDocument.getElementById("site-label"); |
|
109 ok(gSiteLabel, "got site label"); |
|
110 |
|
111 runNextTest(); |
|
112 }, |
|
113 |
|
114 function test_sites_list() { |
|
115 is(gSitesList.firstChild.id, "all-sites-item", |
|
116 "all sites is the first item in the sites list"); |
|
117 |
|
118 ok(getSiteItem(TEST_URI_1.host), "site item from places db exists"); |
|
119 ok(getSiteItem(TEST_URI_2.host), "site item from enumerating services exists"); |
|
120 |
|
121 runNextTest(); |
|
122 }, |
|
123 |
|
124 function test_filter_sites_list() { |
|
125 // set filter to test host |
|
126 let sitesFilter = gBrowser.contentDocument.getElementById("sites-filter"); |
|
127 sitesFilter.value = TEST_URI_1.host; |
|
128 sitesFilter.doCommand(); |
|
129 |
|
130 // make sure correct sites are collapsed/showing |
|
131 let testSite1 = getSiteItem(TEST_URI_1.host); |
|
132 ok(!testSite1.collapsed, "test site 1 is not collapsed"); |
|
133 let testSite2 = getSiteItem(TEST_URI_2.host); |
|
134 ok(testSite2.collapsed, "test site 2 is collapsed"); |
|
135 |
|
136 // clear filter |
|
137 sitesFilter.value = ""; |
|
138 sitesFilter.doCommand(); |
|
139 |
|
140 runNextTest(); |
|
141 }, |
|
142 |
|
143 function test_all_sites() { |
|
144 // "All Sites" item should be selected when the page is first loaded |
|
145 is(gSitesList.selectedItem, gBrowser.contentDocument.getElementById("all-sites-item"), |
|
146 "all sites item is selected"); |
|
147 |
|
148 let defaultsHeader = gBrowser.contentDocument.getElementById("defaults-header"); |
|
149 is(defaultsHeader, gHeaderDeck.selectedPanel, |
|
150 "correct header shown for all sites"); |
|
151 |
|
152 ok(gBrowser.contentDocument.getElementById("passwords-count").hidden, |
|
153 "passwords count is hidden"); |
|
154 ok(gBrowser.contentDocument.getElementById("cookies-count").hidden, |
|
155 "cookies count is hidden"); |
|
156 |
|
157 // Test to make sure "Allow" items hidden for certain permission types |
|
158 NO_GLOBAL_ALLOW.forEach(function(aType) { |
|
159 let menuitem = gBrowser.contentDocument.getElementById(aType + "-" + PERM_ALLOW); |
|
160 ok(menuitem.hidden, aType + " allow menuitem hidden for all sites"); |
|
161 }); |
|
162 |
|
163 runNextTest(); |
|
164 }, |
|
165 |
|
166 function test_all_sites_permission() { |
|
167 // apply the old default of allowing all cookies |
|
168 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); |
|
169 |
|
170 // there should be no user-set pref for cookie behavior |
|
171 is(Services.prefs.getIntPref("network.cookie.cookieBehavior"), PERM_UNKNOWN, |
|
172 "network.cookie.cookieBehavior is expected default"); |
|
173 |
|
174 // the default behavior is to allow cookies |
|
175 let cookieMenulist = getPermissionMenulist("cookie"); |
|
176 is(cookieMenulist.value, PERM_ALLOW, |
|
177 "menulist correctly shows that cookies are allowed"); |
|
178 |
|
179 // set the pref to block cookies |
|
180 Services.prefs.setIntPref("network.cookie.cookieBehavior", PERM_DENY); |
|
181 // check to make sure this change is reflected in the UI |
|
182 is(cookieMenulist.value, PERM_DENY, "menulist correctly shows that cookies are blocked"); |
|
183 |
|
184 // clear the pref |
|
185 Services.prefs.clearUserPref("network.cookie.cookieBehavior"); |
|
186 |
|
187 runNextTest(); |
|
188 }, |
|
189 |
|
190 function test_manage_all_passwords() { |
|
191 // make sure "Manage All Passwords..." button opens the correct dialog |
|
192 addWindowListener("chrome://passwordmgr/content/passwordManager.xul", runNextTest); |
|
193 gBrowser.contentDocument.getElementById("passwords-manage-all-button").doCommand(); |
|
194 |
|
195 }, |
|
196 |
|
197 function test_manage_all_cookies() { |
|
198 // make sure "Manage All Cookies..." button opens the correct dialog |
|
199 addWindowListener("chrome://browser/content/preferences/cookies.xul", runNextTest); |
|
200 gBrowser.contentDocument.getElementById("cookies-manage-all-button").doCommand(); |
|
201 }, |
|
202 |
|
203 function test_select_site() { |
|
204 // select the site that has the permissions we set at the beginning of the test |
|
205 let testSiteItem = getSiteItem(TEST_URI_2.host); |
|
206 gSitesList.selectedItem = testSiteItem; |
|
207 |
|
208 let siteHeader = gBrowser.contentDocument.getElementById("site-header"); |
|
209 is(siteHeader, gHeaderDeck.selectedPanel, |
|
210 "correct header shown for a specific site"); |
|
211 is(gSiteLabel.value, TEST_URI_2.host, "header updated for selected site"); |
|
212 |
|
213 ok(!gBrowser.contentDocument.getElementById("passwords-count").hidden, |
|
214 "passwords count is not hidden"); |
|
215 ok(!gBrowser.contentDocument.getElementById("cookies-count").hidden, |
|
216 "cookies count is not hidden"); |
|
217 |
|
218 // Test to make sure "Allow" items are *not* hidden for certain permission types |
|
219 NO_GLOBAL_ALLOW.forEach(function(aType) { |
|
220 let menuitem = gBrowser.contentDocument.getElementById(aType + "-" + PERM_ALLOW); |
|
221 ok(!menuitem.hidden, aType + " allow menuitem not hidden for single site"); |
|
222 }); |
|
223 |
|
224 runNextTest(); |
|
225 }, |
|
226 |
|
227 function test_permissions() { |
|
228 let menulists = gBrowser.contentDocument.getElementsByClassName("pref-menulist"); |
|
229 is(menulists.length, TEST_PERMS_COUNT, "got expected number of managed permissions"); |
|
230 |
|
231 for (let i = 0; i < menulists.length; i++) { |
|
232 let permissionMenulist = menulists.item(i); |
|
233 let permissionType = permissionMenulist.getAttribute("type"); |
|
234 |
|
235 // permissions should reflect what we set at the beginning of the test |
|
236 is(permissionMenulist.value, TEST_PERMS[permissionType], |
|
237 "got expected value for " + permissionType + " permission"); |
|
238 } |
|
239 |
|
240 runNextTest(); |
|
241 }, |
|
242 |
|
243 function test_permission_change() { |
|
244 let geoMenulist = getPermissionMenulist("geo"); |
|
245 is(geoMenulist.value, PERM_UNKNOWN, "menulist correctly shows that geolocation permission is unspecified"); |
|
246 |
|
247 // change a permission programatically |
|
248 Services.perms.addFromPrincipal(TEST_PRINCIPAL_2, "geo", PERM_DENY); |
|
249 // check to make sure this change is reflected in the UI |
|
250 is(geoMenulist.value, PERM_DENY, "menulist shows that geolocation is blocked"); |
|
251 |
|
252 // change a permisssion in the UI |
|
253 let geoAllowItem = gBrowser.contentDocument.getElementById("geo-" + PERM_ALLOW); |
|
254 geoMenulist.selectedItem = geoAllowItem; |
|
255 geoMenulist.doCommand(); |
|
256 // check to make sure this change is reflected in the permission manager |
|
257 is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, "geo"), PERM_ALLOW, |
|
258 "permission manager shows that geolocation is allowed"); |
|
259 |
|
260 |
|
261 // change a site-specific cookie permission, just for fun |
|
262 let cookieMenuList = getPermissionMenulist("cookie"); |
|
263 let cookieItem = gBrowser.contentDocument.getElementById("cookie-" + PERM_FIRST_PARTY_ONLY); |
|
264 cookieMenuList.selectedItem = cookieItem; |
|
265 cookieMenuList.doCommand(); |
|
266 is(cookieMenuList.value, PERM_FIRST_PARTY_ONLY, "menulist correctly shows that " + |
|
267 "first party only cookies are allowed"); |
|
268 is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, "cookie"), |
|
269 PERM_FIRST_PARTY_ONLY, "permission manager shows that first party cookies " + |
|
270 "are allowed"); |
|
271 |
|
272 runNextTest(); |
|
273 }, |
|
274 |
|
275 function test_forget_site() { |
|
276 // click "Forget About This Site" button |
|
277 gBrowser.contentDocument.getElementById("forget-site-button").doCommand(); |
|
278 waitForClearHistory(function() { |
|
279 is(gSiteLabel.value, "", "site label cleared"); |
|
280 |
|
281 let allSitesItem = gBrowser.contentDocument.getElementById("all-sites-item"); |
|
282 is(gSitesList.selectedItem, allSitesItem, |
|
283 "all sites item selected after forgetting selected site"); |
|
284 |
|
285 // check to make sure site is gone from sites list |
|
286 let testSiteItem = getSiteItem(TEST_URI_2.host); |
|
287 ok(!testSiteItem, "site removed from sites list"); |
|
288 |
|
289 // check to make sure we forgot all permissions corresponding to site |
|
290 for (let type in TEST_PERMS) { |
|
291 if (type == "password") { |
|
292 ok(Services.logins.getLoginSavingEnabled(TEST_URI_2.prePath), |
|
293 "password saving should be enabled by default"); |
|
294 } else { |
|
295 is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, type), PERM_UNKNOWN, |
|
296 type + " permission should not be set for test site 2"); |
|
297 } |
|
298 } |
|
299 |
|
300 runNextTest(); |
|
301 }); |
|
302 } |
|
303 ]; |
|
304 |
|
305 function getPermissionMenulist(aType) { |
|
306 return gBrowser.contentDocument.getElementById(aType + "-menulist"); |
|
307 } |
|
308 |
|
309 function getSiteItem(aHost) { |
|
310 return gBrowser.contentDocument. |
|
311 querySelector(".site[value='" + aHost + "']"); |
|
312 } |
|
313 |
|
314 function addWindowListener(aURL, aCallback) { |
|
315 Services.wm.addListener({ |
|
316 onOpenWindow: function(aXULWindow) { |
|
317 info("window opened, waiting for focus"); |
|
318 Services.wm.removeListener(this); |
|
319 |
|
320 var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
|
321 .getInterface(Ci.nsIDOMWindow); |
|
322 waitForFocus(function() { |
|
323 is(domwindow.document.location.href, aURL, "should have seen the right window open"); |
|
324 domwindow.close(); |
|
325 aCallback(); |
|
326 }, domwindow); |
|
327 }, |
|
328 onCloseWindow: function(aXULWindow) { }, |
|
329 onWindowTitleChange: function(aXULWindow, aNewTitle) { } |
|
330 }); |
|
331 } |
|
332 |
|
333 // copied from toolkit/components/places/tests/head_common.js |
|
334 function waitForClearHistory(aCallback) { |
|
335 let observer = { |
|
336 observe: function(aSubject, aTopic, aData) { |
|
337 Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED); |
|
338 aCallback(); |
|
339 } |
|
340 }; |
|
341 Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false); |
|
342 PlacesUtils.bhistory.removeAllPages(); |
|
343 } |