1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/browser/browser_visituri_privatebrowsing_perwindowpb.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,84 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function test() { 1.9 + // initialization 1.10 + waitForExplicitFinish(); 1.11 + let windowsToClose = []; 1.12 + let testURI = "https://www.mozilla.org/en-US/"; 1.13 + let initialURL = 1.14 + "http://example.com/tests/toolkit/components/places/tests/browser/begin.html"; 1.15 + let finalURL = 1.16 + "http://example.com/tests/toolkit/components/places/tests/browser/final.html"; 1.17 + let observer = null; 1.18 + let enumerator = null; 1.19 + let currentObserver = null; 1.20 + let uri = null; 1.21 + 1.22 + function doTest(aIsPrivateMode, aWindow, aTestURI, aCallback) { 1.23 + aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { 1.24 + if (aWindow.gBrowser.selectedBrowser.contentWindow.location != aTestURI) { 1.25 + aWindow.gBrowser.selectedBrowser.contentWindow.location = aTestURI; 1.26 + return; 1.27 + } 1.28 + aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); 1.29 + 1.30 + if (aCallback) { 1.31 + aCallback(); 1.32 + } 1.33 + }, true); 1.34 + 1.35 + observer = { 1.36 + observe: function(aSubject, aTopic, aData) { 1.37 + // The uri-visit-saved topic should only work when on normal mode. 1.38 + if (aTopic == "uri-visit-saved") { 1.39 + // Remove the observers set on per window private mode and normal 1.40 + // mode. 1.41 + enumerator = aWindow.Services.obs.enumerateObservers("uri-visit-saved"); 1.42 + while (enumerator.hasMoreElements()) { 1.43 + currentObserver = enumerator.getNext(); 1.44 + aWindow.Services.obs.removeObserver(currentObserver, "uri-visit-saved"); 1.45 + } 1.46 + 1.47 + // The expected visit should be the finalURL because private mode 1.48 + // should not register a visit with the initialURL. 1.49 + uri = aSubject.QueryInterface(Ci.nsIURI); 1.50 + is(uri.spec, finalURL, "Check received expected visit"); 1.51 + } 1.52 + } 1.53 + }; 1.54 + 1.55 + aWindow.Services.obs.addObserver(observer, "uri-visit-saved", false); 1.56 + aWindow.gBrowser.selectedBrowser.loadURI(aTestURI); 1.57 + } 1.58 + 1.59 + function testOnWindow(aOptions, aCallback) { 1.60 + whenNewWindowLoaded(aOptions, function(aWin) { 1.61 + windowsToClose.push(aWin); 1.62 + // execute should only be called when need, like when you are opening 1.63 + // web pages on the test. If calling executeSoon() is not necesary, then 1.64 + // call whenNewWindowLoaded() instead of testOnWindow() on your test. 1.65 + executeSoon(function() aCallback(aWin)); 1.66 + }); 1.67 + }; 1.68 + 1.69 + // This function is called after calling finish() on the test. 1.70 + registerCleanupFunction(function() { 1.71 + windowsToClose.forEach(function(aWin) { 1.72 + aWin.close(); 1.73 + }); 1.74 + }); 1.75 + 1.76 + // test first when on private mode 1.77 + testOnWindow({private: true}, function(aWin) { 1.78 + doTest(true, aWin, initialURL, function() { 1.79 + // then test when not on private mode 1.80 + testOnWindow({}, function(aWin) { 1.81 + doTest(false, aWin, finalURL, function () { 1.82 + promiseClearHistory().then(finish); 1.83 + }); 1.84 + }); 1.85 + }); 1.86 + }); 1.87 +}