browser/components/sessionstore/test/browser_595601-restore_hidden.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_595601-restore_hidden.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let state = {windows:[{tabs:[
     1.8 +  {entries:[{url:"http://example.com#1"}]},
     1.9 +  {entries:[{url:"http://example.com#2"}]},
    1.10 +  {entries:[{url:"http://example.com#3"}]},
    1.11 +  {entries:[{url:"http://example.com#4"}]},
    1.12 +  {entries:[{url:"http://example.com#5"}], hidden: true},
    1.13 +  {entries:[{url:"http://example.com#6"}], hidden: true},
    1.14 +  {entries:[{url:"http://example.com#7"}], hidden: true},
    1.15 +  {entries:[{url:"http://example.com#8"}], hidden: true}
    1.16 +]}]};
    1.17 +
    1.18 +function test() {
    1.19 +  waitForExplicitFinish();
    1.20 +
    1.21 +  registerCleanupFunction(function () {
    1.22 +    Services.prefs.clearUserPref("browser.sessionstore.restore_hidden_tabs");
    1.23 +  });
    1.24 +
    1.25 +  // First stage: restoreHiddenTabs = true
    1.26 +  // Second stage: restoreHiddenTabs = false
    1.27 +  test_loadTabs(true, function () {
    1.28 +    test_loadTabs(false, finish);
    1.29 +  });
    1.30 +}
    1.31 +
    1.32 +function test_loadTabs(restoreHiddenTabs, callback) {
    1.33 +  Services.prefs.setBoolPref("browser.sessionstore.restore_hidden_tabs", restoreHiddenTabs);
    1.34 +
    1.35 +  let expectedTabs = restoreHiddenTabs ? 8 : 4;
    1.36 +  let firstProgress = true;
    1.37 +
    1.38 +  newWindowWithState(state, function (win, needsRestore, isRestoring) {
    1.39 +    if (firstProgress) {
    1.40 +      firstProgress = false;
    1.41 +      is(isRestoring, 3, "restoring 3 tabs concurrently");
    1.42 +    } else {
    1.43 +      ok(isRestoring < 4, "restoring max. 3 tabs concurrently");
    1.44 +    }
    1.45 +
    1.46 +    // We're explicity checking for (isRestoring == 1) here because the test
    1.47 +    // progress listener is called before the session store one. So when we're
    1.48 +    // called with one tab left to restore we know that the last tab has
    1.49 +    // finished restoring and will soon be handled by the SS listener.
    1.50 +    let tabsNeedingRestore = win.gBrowser.tabs.length - needsRestore;
    1.51 +    if (isRestoring == 1 && tabsNeedingRestore == expectedTabs) {
    1.52 +      is(win.gBrowser.visibleTabs.length, 4, "only 4 visible tabs");
    1.53 +
    1.54 +      TabsProgressListener.uninit();
    1.55 +      executeSoon(callback);
    1.56 +    }
    1.57 +  });
    1.58 +}
    1.59 +
    1.60 +let TabsProgressListener = {
    1.61 +  init: function (win) {
    1.62 +    this.window = win;
    1.63 +    Services.obs.addObserver(this, "sessionstore-debug-tab-restored", false);
    1.64 +  },
    1.65 +
    1.66 +  uninit: function () {
    1.67 +    Services.obs.removeObserver(this, "sessionstore-debug-tab-restored");
    1.68 +
    1.69 +    delete this.window;
    1.70 +    delete this.callback;
    1.71 +  },
    1.72 +
    1.73 +  setCallback: function (callback) {
    1.74 +    this.callback = callback;
    1.75 +  },
    1.76 +
    1.77 +  observe: function (browser) {
    1.78 +    TabsProgressListener.onRestored(browser);
    1.79 +  },
    1.80 +
    1.81 +  onRestored: function (browser) {
    1.82 +    if (this.callback && browser.__SS_restoreState == TAB_STATE_RESTORING)
    1.83 +      this.callback.apply(null, [this.window].concat(this.countTabs()));
    1.84 +  },
    1.85 +
    1.86 +  countTabs: function () {
    1.87 +    let needsRestore = 0, isRestoring = 0;
    1.88 +
    1.89 +    for (let i = 0; i < this.window.gBrowser.tabs.length; i++) {
    1.90 +      let browser = this.window.gBrowser.tabs[i].linkedBrowser;
    1.91 +      if (browser.__SS_restoreState == TAB_STATE_RESTORING)
    1.92 +        isRestoring++;
    1.93 +      else if (browser.__SS_restoreState == TAB_STATE_NEEDS_RESTORE)
    1.94 +        needsRestore++;
    1.95 +    }
    1.96 +
    1.97 +    return [needsRestore, isRestoring];
    1.98 +  }
    1.99 +}
   1.100 +
   1.101 +// ----------
   1.102 +function newWindowWithState(state, callback) {
   1.103 +  let opts = "chrome,all,dialog=no,height=800,width=800";
   1.104 +  let win = window.openDialog(getBrowserURL(), "_blank", opts);
   1.105 +
   1.106 +  registerCleanupFunction(function () win.close());
   1.107 +
   1.108 +  whenWindowLoaded(win, function onWindowLoaded(aWin) {
   1.109 +    TabsProgressListener.init(aWin);
   1.110 +    TabsProgressListener.setCallback(callback);
   1.111 +
   1.112 +    ss.setWindowState(aWin, JSON.stringify(state), true);
   1.113 +  });
   1.114 +}

mercurial