|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 function test() { |
|
6 // initialization |
|
7 waitForExplicitFinish(); |
|
8 let windowsToClose = []; |
|
9 let testURI = "https://www.mozilla.org/en-US/"; |
|
10 let initialURL = |
|
11 "http://example.com/tests/toolkit/components/places/tests/browser/begin.html"; |
|
12 let finalURL = |
|
13 "http://example.com/tests/toolkit/components/places/tests/browser/final.html"; |
|
14 let observer = null; |
|
15 let enumerator = null; |
|
16 let currentObserver = null; |
|
17 let uri = null; |
|
18 |
|
19 function doTest(aIsPrivateMode, aWindow, aTestURI, aCallback) { |
|
20 aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
|
21 if (aWindow.gBrowser.selectedBrowser.contentWindow.location != aTestURI) { |
|
22 aWindow.gBrowser.selectedBrowser.contentWindow.location = aTestURI; |
|
23 return; |
|
24 } |
|
25 aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
|
26 |
|
27 if (aCallback) { |
|
28 aCallback(); |
|
29 } |
|
30 }, true); |
|
31 |
|
32 observer = { |
|
33 observe: function(aSubject, aTopic, aData) { |
|
34 // The uri-visit-saved topic should only work when on normal mode. |
|
35 if (aTopic == "uri-visit-saved") { |
|
36 // Remove the observers set on per window private mode and normal |
|
37 // mode. |
|
38 enumerator = aWindow.Services.obs.enumerateObservers("uri-visit-saved"); |
|
39 while (enumerator.hasMoreElements()) { |
|
40 currentObserver = enumerator.getNext(); |
|
41 aWindow.Services.obs.removeObserver(currentObserver, "uri-visit-saved"); |
|
42 } |
|
43 |
|
44 // The expected visit should be the finalURL because private mode |
|
45 // should not register a visit with the initialURL. |
|
46 uri = aSubject.QueryInterface(Ci.nsIURI); |
|
47 is(uri.spec, finalURL, "Check received expected visit"); |
|
48 } |
|
49 } |
|
50 }; |
|
51 |
|
52 aWindow.Services.obs.addObserver(observer, "uri-visit-saved", false); |
|
53 aWindow.gBrowser.selectedBrowser.loadURI(aTestURI); |
|
54 } |
|
55 |
|
56 function testOnWindow(aOptions, aCallback) { |
|
57 whenNewWindowLoaded(aOptions, function(aWin) { |
|
58 windowsToClose.push(aWin); |
|
59 // execute should only be called when need, like when you are opening |
|
60 // web pages on the test. If calling executeSoon() is not necesary, then |
|
61 // call whenNewWindowLoaded() instead of testOnWindow() on your test. |
|
62 executeSoon(function() aCallback(aWin)); |
|
63 }); |
|
64 }; |
|
65 |
|
66 // This function is called after calling finish() on the test. |
|
67 registerCleanupFunction(function() { |
|
68 windowsToClose.forEach(function(aWin) { |
|
69 aWin.close(); |
|
70 }); |
|
71 }); |
|
72 |
|
73 // test first when on private mode |
|
74 testOnWindow({private: true}, function(aWin) { |
|
75 doTest(true, aWin, initialURL, function() { |
|
76 // then test when not on private mode |
|
77 testOnWindow({}, function(aWin) { |
|
78 doTest(false, aWin, finalURL, function () { |
|
79 promiseClearHistory().then(finish); |
|
80 }); |
|
81 }); |
|
82 }); |
|
83 }); |
|
84 } |