browser/components/sessionstore/test/browser_636279.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_636279.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let stateBackup = ss.getBrowserState();
     1.8 +
     1.9 +let statePinned = {windows:[{tabs:[
    1.10 +  {entries:[{url:"http://example.com#1"}], pinned: true}
    1.11 +]}]};
    1.12 +
    1.13 +let state = {windows:[{tabs:[
    1.14 +  {entries:[{url:"http://example.com#1"}]},
    1.15 +  {entries:[{url:"http://example.com#2"}]},
    1.16 +  {entries:[{url:"http://example.com#3"}]},
    1.17 +  {entries:[{url:"http://example.com#4"}]},
    1.18 +]}]};
    1.19 +
    1.20 +function test() {
    1.21 +  waitForExplicitFinish();
    1.22 +
    1.23 +  registerCleanupFunction(function () {
    1.24 +    TabsProgressListener.uninit();
    1.25 +    ss.setBrowserState(stateBackup);
    1.26 +  });
    1.27 +
    1.28 +
    1.29 +  TabsProgressListener.init();
    1.30 +
    1.31 +  window.addEventListener("SSWindowStateReady", function onReady() {
    1.32 +    window.removeEventListener("SSWindowStateReady", onReady, false);
    1.33 +
    1.34 +    let firstProgress = true;
    1.35 +
    1.36 +    TabsProgressListener.setCallback(function (needsRestore, isRestoring) {
    1.37 +      if (firstProgress) {
    1.38 +        firstProgress = false;
    1.39 +        is(isRestoring, 3, "restoring 3 tabs concurrently");
    1.40 +      } else {
    1.41 +        ok(isRestoring <= 3, "restoring max. 2 tabs concurrently");
    1.42 +      }
    1.43 +
    1.44 +      if (0 == needsRestore) {
    1.45 +        TabsProgressListener.unsetCallback();
    1.46 +        waitForFocus(finish);
    1.47 +      }
    1.48 +    });
    1.49 +
    1.50 +    ss.setBrowserState(JSON.stringify(state));
    1.51 +  }, false);
    1.52 +
    1.53 +  ss.setBrowserState(JSON.stringify(statePinned));
    1.54 +}
    1.55 +
    1.56 +function countTabs() {
    1.57 +  let needsRestore = 0, isRestoring = 0;
    1.58 +  let windowsEnum = Services.wm.getEnumerator("navigator:browser");
    1.59 +
    1.60 +  while (windowsEnum.hasMoreElements()) {
    1.61 +    let window = windowsEnum.getNext();
    1.62 +    if (window.closed)
    1.63 +      continue;
    1.64 +
    1.65 +    for (let i = 0; i < window.gBrowser.tabs.length; i++) {
    1.66 +      let browser = window.gBrowser.tabs[i].linkedBrowser;
    1.67 +      if (browser.__SS_restoreState == TAB_STATE_RESTORING)
    1.68 +        isRestoring++;
    1.69 +      else if (browser.__SS_restoreState == TAB_STATE_NEEDS_RESTORE)
    1.70 +        needsRestore++;
    1.71 +    }
    1.72 +  }
    1.73 +
    1.74 +  return [needsRestore, isRestoring];
    1.75 +}
    1.76 +
    1.77 +let TabsProgressListener = {
    1.78 +  init: function () {
    1.79 +    Services.obs.addObserver(this, "sessionstore-debug-tab-restored", false);
    1.80 +  },
    1.81 +
    1.82 +  uninit: function () {
    1.83 +    Services.obs.removeObserver(this, "sessionstore-debug-tab-restored");
    1.84 +    this.unsetCallback();
    1.85 + },
    1.86 +
    1.87 +  setCallback: function (callback) {
    1.88 +    this.callback = callback;
    1.89 +  },
    1.90 +
    1.91 +  unsetCallback: function () {
    1.92 +    delete this.callback;
    1.93 +  },
    1.94 +
    1.95 +  observe: function (browser, topic, data) {
    1.96 +    TabsProgressListener.onRestored(browser);
    1.97 +  },
    1.98 +
    1.99 +  onRestored: function (browser) {
   1.100 +    if (this.callback && browser.__SS_restoreState == TAB_STATE_RESTORING) {
   1.101 +      this.callback.apply(null, countTabs());
   1.102 +    }
   1.103 +  }
   1.104 +}

mercurial