michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function test() { michael@0: // initialization michael@0: waitForExplicitFinish(); michael@0: let windowsToClose = []; michael@0: let testURI = "https://www.mozilla.org/en-US/"; michael@0: let initialURL = michael@0: "http://example.com/tests/toolkit/components/places/tests/browser/begin.html"; michael@0: let finalURL = michael@0: "http://example.com/tests/toolkit/components/places/tests/browser/final.html"; michael@0: let observer = null; michael@0: let enumerator = null; michael@0: let currentObserver = null; michael@0: let uri = null; michael@0: michael@0: function doTest(aIsPrivateMode, aWindow, aTestURI, aCallback) { michael@0: aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { michael@0: if (aWindow.gBrowser.selectedBrowser.contentWindow.location != aTestURI) { michael@0: aWindow.gBrowser.selectedBrowser.contentWindow.location = aTestURI; michael@0: return; michael@0: } michael@0: aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: michael@0: if (aCallback) { michael@0: aCallback(); michael@0: } michael@0: }, true); michael@0: michael@0: observer = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: // The uri-visit-saved topic should only work when on normal mode. michael@0: if (aTopic == "uri-visit-saved") { michael@0: // Remove the observers set on per window private mode and normal michael@0: // mode. michael@0: enumerator = aWindow.Services.obs.enumerateObservers("uri-visit-saved"); michael@0: while (enumerator.hasMoreElements()) { michael@0: currentObserver = enumerator.getNext(); michael@0: aWindow.Services.obs.removeObserver(currentObserver, "uri-visit-saved"); michael@0: } michael@0: michael@0: // The expected visit should be the finalURL because private mode michael@0: // should not register a visit with the initialURL. michael@0: uri = aSubject.QueryInterface(Ci.nsIURI); michael@0: is(uri.spec, finalURL, "Check received expected visit"); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: aWindow.Services.obs.addObserver(observer, "uri-visit-saved", false); michael@0: aWindow.gBrowser.selectedBrowser.loadURI(aTestURI); michael@0: } michael@0: michael@0: function testOnWindow(aOptions, aCallback) { michael@0: whenNewWindowLoaded(aOptions, function(aWin) { michael@0: windowsToClose.push(aWin); michael@0: // execute should only be called when need, like when you are opening michael@0: // web pages on the test. If calling executeSoon() is not necesary, then michael@0: // call whenNewWindowLoaded() instead of testOnWindow() on your test. michael@0: executeSoon(function() aCallback(aWin)); michael@0: }); michael@0: }; michael@0: michael@0: // This function is called after calling finish() on the test. michael@0: registerCleanupFunction(function() { michael@0: windowsToClose.forEach(function(aWin) { michael@0: aWin.close(); michael@0: }); michael@0: }); michael@0: michael@0: // test first when on private mode michael@0: testOnWindow({private: true}, function(aWin) { michael@0: doTest(true, aWin, initialURL, function() { michael@0: // then test when not on private mode michael@0: testOnWindow({}, function(aWin) { michael@0: doTest(false, aWin, finalURL, function () { michael@0: promiseClearHistory().then(finish); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }