security/manager/ssl/tests/mochitest/browser/browser_bug627234_perwindowpb.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/manager/ssl/tests/mochitest/browser/browser_bug627234_perwindowpb.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,70 @@
     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 +// This is a template to help porting global private browsing tests
     1.9 +// to per-window private browsing tests
    1.10 +function test() {
    1.11 +  // initialization
    1.12 +  waitForExplicitFinish();
    1.13 +  let windowsToClose = [];
    1.14 +  let testURI = "about:blank";
    1.15 +  let uri;
    1.16 +  let gSSService = Cc["@mozilla.org/ssservice;1"].
    1.17 +                   getService(Ci.nsISiteSecurityService);
    1.18 +
    1.19 +  function privacyFlags(aIsPrivateMode) {
    1.20 +    return aIsPrivateMode ? Ci.nsISocketProvider.NO_PERMANENT_STORAGE : 0;
    1.21 +  }
    1.22 +
    1.23 +  function doTest(aIsPrivateMode, aWindow, aCallback) {
    1.24 +    aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    1.25 +      aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    1.26 +
    1.27 +      uri = aWindow.Services.io.newURI("https://localhost/img.png", null, null);
    1.28 +      gSSService.processHeader(Ci.nsISiteSecurityService.HEADER_HSTS, uri,
    1.29 +                               "max-age=1000", privacyFlags(aIsPrivateMode));
    1.30 +      ok(gSSService.isSecureHost(Ci.nsISiteSecurityService.HEADER_HSTS, "localhost", privacyFlags(aIsPrivateMode)), "checking sts host");
    1.31 +
    1.32 +      aCallback();
    1.33 +    }, true);
    1.34 +
    1.35 +    aWindow.gBrowser.selectedBrowser.loadURI(testURI);
    1.36 +  }
    1.37 +
    1.38 +  function testOnWindow(aOptions, aCallback) {
    1.39 +    whenNewWindowLoaded(aOptions, function(aWin) {
    1.40 +      windowsToClose.push(aWin);
    1.41 +      // execute should only be called when need, like when you are opening
    1.42 +      // web pages on the test. If calling executeSoon() is not necesary, then
    1.43 +      // call whenNewWindowLoaded() instead of testOnWindow() on your test.
    1.44 +      executeSoon(function() aCallback(aWin));
    1.45 +    });
    1.46 +  };
    1.47 +
    1.48 +   // this function is called after calling finish() on the test.
    1.49 +  registerCleanupFunction(function() {
    1.50 +    windowsToClose.forEach(function(aWin) {
    1.51 +      aWin.close();
    1.52 +    });
    1.53 +    uri = Services.io.newURI("http://localhost", null, null);
    1.54 +    gSSService.removeState(Ci.nsISiteSecurityService.HEADER_HSTS, uri, 0);
    1.55 +  });
    1.56 +
    1.57 +  // test first when on private mode
    1.58 +  testOnWindow({private: true}, function(aWin) {
    1.59 +    doTest(true, aWin, function() {
    1.60 +      //test when not on private mode
    1.61 +      testOnWindow({}, function(aWin) {
    1.62 +        doTest(false, aWin, function() {
    1.63 +          //test again when on private mode
    1.64 +          testOnWindow({private: true}, function(aWin) {
    1.65 +            doTest(true, aWin, function () {
    1.66 +              finish();
    1.67 +            });
    1.68 +          });
    1.69 +        });
    1.70 +      });
    1.71 +    });
    1.72 +  });
    1.73 +}

mercurial