browser_bug627234_perwindowpb.js (2606B)
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 "use strict"; 5 6 function whenNewWindowLoaded(aPrivate, aCallback) { 7 BrowserTestUtils.openNewBrowserWindow({ 8 private: aPrivate, 9 }).then(aCallback); 10 } 11 12 // This is a template to help porting global private browsing tests 13 // to per-window private browsing tests 14 function test() { 15 // initialization 16 waitForExplicitFinish(); 17 let windowsToClose = []; 18 let testURI = "about:blank"; 19 let uri; 20 let gSSService = Cc["@mozilla.org/ssservice;1"].getService( 21 Ci.nsISiteSecurityService 22 ); 23 24 function originAttributes(aIsPrivateMode) { 25 return aIsPrivateMode ? { privateBrowsingId: 1 } : {}; 26 } 27 28 function doTest(aIsPrivateMode, aWindow, aCallback) { 29 BrowserTestUtils.browserLoaded(aWindow.gBrowser.selectedBrowser, { 30 wantLoad: testURI, 31 }).then(() => { 32 uri = aWindow.Services.io.newURI("https://localhost/img.png"); 33 gSSService.processHeader( 34 uri, 35 "max-age=1000", 36 originAttributes(aIsPrivateMode) 37 ); 38 ok( 39 gSSService.isSecureURI(uri, originAttributes(aIsPrivateMode)), 40 "checking sts host" 41 ); 42 43 aCallback(); 44 }); 45 46 BrowserTestUtils.startLoadingURIString( 47 aWindow.gBrowser.selectedBrowser, 48 testURI 49 ); 50 } 51 52 function testOnWindow(aPrivate, aCallback) { 53 whenNewWindowLoaded(aPrivate, function (aWin) { 54 windowsToClose.push(aWin); 55 // execute should only be called when need, like when you are opening 56 // web pages on the test. If calling executeSoon() is not necesary, then 57 // call whenNewWindowLoaded() instead of testOnWindow() on your test. 58 executeSoon(function () { 59 aCallback(aWin); 60 }); 61 }); 62 } 63 64 // this function is called after calling finish() on the test. 65 registerCleanupFunction(function () { 66 windowsToClose.forEach(function (aWin) { 67 aWin.close(); 68 }); 69 uri = Services.io.newURI("http://localhost"); 70 gSSService.resetState(uri); 71 }); 72 73 // test first when on private mode 74 testOnWindow(true, function (aWin) { 75 doTest(true, aWin, function () { 76 // test when not on private mode 77 testOnWindow(false, function (aWin) { 78 doTest(false, aWin, function () { 79 // test again when on private mode 80 testOnWindow(true, function (aWin) { 81 doTest(true, aWin, function () { 82 finish(); 83 }); 84 }); 85 }); 86 }); 87 }); 88 }); 89 }