browser/components/sessionstore/test/browser_819510_perwindowpb.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_819510_perwindowpb.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,183 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +const originalState = ss.getBrowserState();
     1.8 +
     1.9 +/** Private Browsing Test for Bug 819510 **/
    1.10 +function test() {
    1.11 +  waitForExplicitFinish();
    1.12 +  runNextTest();
    1.13 +}
    1.14 +
    1.15 +let tests = [test_1, test_2, test_3 ];
    1.16 +
    1.17 +const testState = {
    1.18 +  windows: [{
    1.19 +    tabs: [
    1.20 +      { entries: [{ url: "about:blank" }] },
    1.21 +    ]
    1.22 +  }]
    1.23 +};
    1.24 +
    1.25 +function runNextTest() {
    1.26 +  // Set an empty state
    1.27 +  closeAllButPrimaryWindow();
    1.28 +
    1.29 +  // Run the next test, or finish
    1.30 +  if (tests.length) {
    1.31 +    let currentTest = tests.shift();
    1.32 +    waitForBrowserState(testState, currentTest);
    1.33 +  } else {
    1.34 +    Services.obs.addObserver(
    1.35 +      function observe(aSubject, aTopic, aData) {
    1.36 +        Services.obs.removeObserver(observe, aTopic);
    1.37 +        finish();
    1.38 +      },
    1.39 +      "sessionstore-browser-state-restored", false);
    1.40 +    ss.setBrowserState(originalState);
    1.41 +  }
    1.42 +}
    1.43 +
    1.44 +// Test opening default mochitest-normal-private-normal-private windows
    1.45 +// (saving the state with last window being private)
    1.46 +function test_1() {
    1.47 +  testOnWindow(false, function(aWindow) {
    1.48 +    aWindow.gBrowser.addTab("http://www.example.com/1");
    1.49 +    testOnWindow(true, function(aWindow) {
    1.50 +      aWindow.gBrowser.addTab("http://www.example.com/2");
    1.51 +      testOnWindow(false, function(aWindow) {
    1.52 +        aWindow.gBrowser.addTab("http://www.example.com/3");
    1.53 +        testOnWindow(true, function(aWindow) {
    1.54 +          aWindow.gBrowser.addTab("http://www.example.com/4");
    1.55 +
    1.56 +          let curState = JSON.parse(ss.getBrowserState());
    1.57 +          is (curState.windows.length, 5, "Browser has opened 5 windows");
    1.58 +          is (curState.windows[2].isPrivate, true, "Window is private");
    1.59 +          is (curState.windows[4].isPrivate, true, "Last window is private");
    1.60 +          is (curState.selectedWindow, 5, "Last window opened is the one selected");
    1.61 +
    1.62 +          forceWriteState(function(state) {
    1.63 +            is(state.windows.length, 3,
    1.64 +               "sessionstore state: 3 windows in data being written to disk");
    1.65 +            is (state.selectedWindow, 3,
    1.66 +               "Selected window is updated to match one of the saved windows");
    1.67 +            state.windows.forEach(function(win) {
    1.68 +              is(!win.isPrivate, true, "Saved window is not private");
    1.69 +            });
    1.70 +            is(state._closedWindows.length, 0,
    1.71 +               "sessionstore state: no closed windows in data being written to disk");
    1.72 +            runNextTest();
    1.73 +          });
    1.74 +        });
    1.75 +      });
    1.76 +    });
    1.77 +  });
    1.78 +}
    1.79 +
    1.80 +// Test opening default mochitest window + 2 private windows
    1.81 +function test_2() {
    1.82 +  testOnWindow(true, function(aWindow) {
    1.83 +    aWindow.gBrowser.addTab("http://www.example.com/1");
    1.84 +    testOnWindow(true, function(aWindow) {
    1.85 +      aWindow.gBrowser.addTab("http://www.example.com/2");
    1.86 +
    1.87 +      let curState = JSON.parse(ss.getBrowserState());
    1.88 +      is (curState.windows.length, 3, "Browser has opened 3 windows");
    1.89 +      is (curState.windows[1].isPrivate, true, "Window 1 is private");
    1.90 +      is (curState.windows[2].isPrivate, true, "Window 2 is private");
    1.91 +      is (curState.selectedWindow, 3, "Last window opened is the one selected");
    1.92 +
    1.93 +      forceWriteState(function(state) {
    1.94 +        is(state.windows.length, 1,
    1.95 +           "sessionstore state: 1 windows in data being written to disk");
    1.96 +        is (state.selectedWindow, 1,
    1.97 +           "Selected window is updated to match one of the saved windows");
    1.98 +        is(state._closedWindows.length, 0,
    1.99 +           "sessionstore state: no closed windows in data being written to disk");
   1.100 +        runNextTest();
   1.101 +      });
   1.102 +    });
   1.103 +  });
   1.104 +}
   1.105 +
   1.106 +// Test opening default-normal-private-normal windows and closing a normal window
   1.107 +function test_3() {
   1.108 +  testOnWindow(false, function(normalWindow) {
   1.109 +    waitForTabLoad(normalWindow, "http://www.example.com/", function() {
   1.110 +      testOnWindow(true, function(aWindow) {
   1.111 +        waitForTabLoad(aWindow, "http://www.example.com/", function() {
   1.112 +          testOnWindow(false, function(aWindow) {
   1.113 +            waitForTabLoad(aWindow, "http://www.example.com/", function() {
   1.114 +
   1.115 +              let curState = JSON.parse(ss.getBrowserState());
   1.116 +              is(curState.windows.length, 4, "Browser has opened 4 windows");
   1.117 +              is(curState.windows[2].isPrivate, true, "Window 2 is private");
   1.118 +              is(curState.selectedWindow, 4, "Last window opened is the one selected");
   1.119 +
   1.120 +              waitForWindowClose(normalWindow, function() {
   1.121 +                // Pin and unpin a tab before checking the written state so that
   1.122 +                // the list of restoring windows gets cleared. Otherwise the
   1.123 +                // window we just closed would be marked as not closed.
   1.124 +                let tab = aWindow.gBrowser.tabs[0];
   1.125 +                aWindow.gBrowser.pinTab(tab);
   1.126 +                aWindow.gBrowser.unpinTab(tab);
   1.127 +
   1.128 +                forceWriteState(function(state) {
   1.129 +                  is(state.windows.length, 2,
   1.130 +                     "sessionstore state: 2 windows in data being written to disk");
   1.131 +                  is(state.selectedWindow, 2,
   1.132 +                     "Selected window is updated to match one of the saved windows");
   1.133 +                  state.windows.forEach(function(win) {
   1.134 +                    is(!win.isPrivate, true, "Saved window is not private");
   1.135 +                  });
   1.136 +                  is(state._closedWindows.length, 1,
   1.137 +                     "sessionstore state: 1 closed window in data being written to disk");
   1.138 +                  state._closedWindows.forEach(function(win) {
   1.139 +                    is(!win.isPrivate, true, "Closed window is not private");
   1.140 +                  });
   1.141 +                  runNextTest();
   1.142 +                });
   1.143 +              });
   1.144 +            });
   1.145 +          });
   1.146 +        });
   1.147 +      });
   1.148 +    });
   1.149 +  });
   1.150 +}
   1.151 +
   1.152 +function waitForWindowClose(aWin, aCallback) {
   1.153 +  let winCount = JSON.parse(ss.getBrowserState()).windows.length;
   1.154 +  aWin.addEventListener("SSWindowClosing", function onWindowClosing() {
   1.155 +    aWin.removeEventListener("SSWindowClosing", onWindowClosing, false);
   1.156 +    function checkCount() {
   1.157 +      let state = JSON.parse(ss.getBrowserState());
   1.158 +      if (state.windows.length == (winCount - 1)) {
   1.159 +        aCallback();
   1.160 +      } else {
   1.161 +        executeSoon(checkCount);
   1.162 +      }
   1.163 +    }
   1.164 +    executeSoon(checkCount);
   1.165 +  }, false);
   1.166 +  aWin.close();
   1.167 +}
   1.168 +
   1.169 +function forceWriteState(aCallback) {
   1.170 +  return promiseSaveFileContents().then(function(data) {
   1.171 +    aCallback(JSON.parse(data));
   1.172 +  });
   1.173 +}
   1.174 +
   1.175 +function testOnWindow(aIsPrivate, aCallback) {
   1.176 +  whenNewWindowLoaded({private: aIsPrivate}, aCallback);
   1.177 +}
   1.178 +
   1.179 +function waitForTabLoad(aWin, aURL, aCallback) {
   1.180 +  let browser = aWin.gBrowser.selectedBrowser;
   1.181 +  whenBrowserLoaded(browser, function () {
   1.182 +    SyncHandlers.get(browser).flush();
   1.183 +    executeSoon(aCallback);
   1.184 +  });
   1.185 +  browser.loadURI(aURL);
   1.186 +}

mercurial