summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_595601-restore_hidden.js
blob: 4c2b2d24aced2f48c3099a3833cc6204e5fb8aa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

var state = {windows:[{tabs:[
  {entries:[{url:"http://example.com#1"}]},
  {entries:[{url:"http://example.com#2"}]},
  {entries:[{url:"http://example.com#3"}]},
  {entries:[{url:"http://example.com#4"}]},
  {entries:[{url:"http://example.com#5"}], hidden: true},
  {entries:[{url:"http://example.com#6"}], hidden: true},
  {entries:[{url:"http://example.com#7"}], hidden: true},
  {entries:[{url:"http://example.com#8"}], hidden: true}
]}]};

function test() {
  waitForExplicitFinish();
  requestLongerTimeout(2);

  registerCleanupFunction(function () {
    Services.prefs.clearUserPref("browser.sessionstore.restore_hidden_tabs");
  });

  // First stage: restoreHiddenTabs = true
  // Second stage: restoreHiddenTabs = false
  test_loadTabs(true, function () {
    test_loadTabs(false, finish);
  });
}

function test_loadTabs(restoreHiddenTabs, callback) {
  Services.prefs.setBoolPref("browser.sessionstore.restore_hidden_tabs", restoreHiddenTabs);

  let expectedTabs = restoreHiddenTabs ? 8 : 4;
  let firstProgress = true;

  newWindowWithState(state, function (win, needsRestore, isRestoring) {
    if (firstProgress) {
      firstProgress = false;
      is(isRestoring, 3, "restoring 3 tabs concurrently");
    } else {
      ok(isRestoring < 4, "restoring max. 3 tabs concurrently");
    }

    // We're explicity checking for (isRestoring == 1) here because the test
    // progress listener is called before the session store one. So when we're
    // called with one tab left to restore we know that the last tab has
    // finished restoring and will soon be handled by the SS listener.
    let tabsNeedingRestore = win.gBrowser.tabs.length - needsRestore;
    if (isRestoring == 1 && tabsNeedingRestore == expectedTabs) {
      is(win.gBrowser.visibleTabs.length, 4, "only 4 visible tabs");

      TabsProgressListener.uninit();
      executeSoon(callback);
    }
  });
}

var TabsProgressListener = {
  init: function (win) {
    this.window = win;
    Services.obs.addObserver(this, "sessionstore-debug-tab-restored", false);
  },

  uninit: function () {
    Services.obs.removeObserver(this, "sessionstore-debug-tab-restored");

    delete this.window;
    delete this.callback;
  },

  setCallback: function (callback) {
    this.callback = callback;
  },

  observe: function (browser) {
    TabsProgressListener.onRestored(browser);
  },

  onRestored: function (browser) {
    if (this.callback && browser.__SS_restoreState == TAB_STATE_RESTORING)
      this.callback.apply(null, [this.window].concat(this.countTabs()));
  },

  countTabs: function () {
    let needsRestore = 0, isRestoring = 0;

    for (let i = 0; i < this.window.gBrowser.tabs.length; i++) {
      let browser = this.window.gBrowser.tabs[i].linkedBrowser;
      if (browser.__SS_restoreState == TAB_STATE_RESTORING)
        isRestoring++;
      else if (browser.__SS_restoreState == TAB_STATE_NEEDS_RESTORE)
        needsRestore++;
    }

    return [needsRestore, isRestoring];
  }
}

// ----------
function newWindowWithState(state, callback) {
  let opts = "chrome,all,dialog=no,height=800,width=800";
  let win = window.openDialog(getBrowserURL(), "_blank", opts);

  registerCleanupFunction(() => BrowserTestUtils.closeWindow(win));

  whenWindowLoaded(win, function onWindowLoaded(aWin) {
    TabsProgressListener.init(aWin);
    TabsProgressListener.setCallback(callback);

    ss.setWindowState(aWin, JSON.stringify(state), true);
  });
}