summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_590268.js
blob: 2b0c2f32d42f011f84a77836982cb58aba40c856 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const NUM_TABS = 12;

var stateBackup = ss.getBrowserState();

function test() {
  /** Test for Bug 590268 - Provide access to sessionstore tab data sooner **/
  waitForExplicitFinish();
  requestLongerTimeout(2);

  let startedTest = false;

  // wasLoaded will be used to keep track of tabs that have already had SSTabRestoring
  // fired for them.
  let wasLoaded = { };
  let restoringTabsCount = 0;
  let restoredTabsCount = 0;
  let uniq2 = { };
  let uniq2Count = 0;
  let state = { windows: [{ tabs: [] }] };
  // We're going to put a bunch of tabs into this state
  for (let i = 0; i < NUM_TABS; i++) {
    let uniq = r();
    let tabData = {
      entries: [{ url: "http://example.com/#" + i }],
      extData: { "uniq": uniq, "baz": "qux" }
    };
    state.windows[0].tabs.push(tabData);
    wasLoaded[uniq] = false;
  }


  function onSSTabRestoring(aEvent) {
    restoringTabsCount++;
    let uniq = ss.getTabValue(aEvent.originalTarget, "uniq");
    wasLoaded[uniq] = true;

    is(ss.getTabValue(aEvent.originalTarget, "foo"), "",
       "There is no value for 'foo'");

    // On the first SSTabRestoring we're going to run the the real test.
    // We'll keep this listener around so we can keep marking tabs as restored.
    if (restoringTabsCount == 1)
      onFirstSSTabRestoring();
    else if (restoringTabsCount == NUM_TABS)
      onLastSSTabRestoring();
  }

  function onSSTabRestored(aEvent) {
    if (++restoredTabsCount < NUM_TABS)
      return;
    cleanup();
  }

  function onTabOpen(aEvent) {
    // To test bug 614708, we'll just set a value on the tab here. This value
    // would previously cause us to not recognize the values in extData until
    // much later. So testing "uniq" failed.
    ss.setTabValue(aEvent.originalTarget, "foo", "bar");
  }

  // This does the actual testing. SSTabRestoring should be firing on tabs from
  // left to right, so we're going to start with the rightmost tab.
  function onFirstSSTabRestoring() {
    info("onFirstSSTabRestoring...");
    for (let i = gBrowser.tabs.length - 1; i >= 0; i--) {
      let tab = gBrowser.tabs[i];
      let actualUniq = ss.getTabValue(tab, "uniq");
      let expectedUniq = state.windows[0].tabs[i].extData["uniq"];

      if (wasLoaded[actualUniq]) {
        info("tab " + i + ": already restored");
        continue;
      }
      is(actualUniq, expectedUniq, "tab " + i + ": extData was correct");

      // Now we're going to set a piece of data back on the tab so it can be read
      // to test setting a value "early".
      uniq2[actualUniq] = r();
      ss.setTabValue(tab, "uniq2", uniq2[actualUniq]);

      // Delete the value we have for "baz". This tests that deleteTabValue
      // will delete "early access" values (c.f. bug 617175). If this doesn't throw
      // then the test is successful.
      try {
        ss.deleteTabValue(tab, "baz");
      }
      catch (e) {
        ok(false, "no error calling deleteTabValue - " + e);
      }

      // This will be used in the final comparison to make sure we checked the
      // same number as we set.
      uniq2Count++;
    }
  }

  function onLastSSTabRestoring() {
    let checked = 0;
    for (let i = 0; i < gBrowser.tabs.length; i++) {
      let tab = gBrowser.tabs[i];
      let uniq = ss.getTabValue(tab, "uniq");

      // Look to see if we set a uniq2 value for this uniq value
      if (uniq in uniq2) {
        is(ss.getTabValue(tab, "uniq2"), uniq2[uniq], "tab " + i + " has correct uniq2 value");
        checked++;
      }
    }
    ok(uniq2Count > 0, "at least 1 tab properly checked 'early access'");
    is(checked, uniq2Count, "checked the same number of uniq2 as we set");
  }

  function cleanup() {
    // remove the event listener and clean up before finishing
    gBrowser.tabContainer.removeEventListener("SSTabRestoring", onSSTabRestoring, false);
    gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, true);
    gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, false);
    // Put this in an executeSoon because we still haven't called restoreNextTab
    // in sessionstore for the last tab (we'll call it after this). We end up
    // trying to restore the tab (since we then add a closed tab to the array).
    executeSoon(function() {
      ss.setBrowserState(stateBackup);
      executeSoon(finish);
    });
  }

  // Add the event listeners
  gBrowser.tabContainer.addEventListener("SSTabRestoring", onSSTabRestoring, false);
  gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, true);
  gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen, false);
  // Restore state
  ss.setBrowserState(JSON.stringify(state));
}