summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_restore_reversed_z_order.js
blob: 33d96fd8da8952b268dda9a057cb1ff11b38e1a3 (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
"use strict";

const PRIMARY_WINDOW = window;

let gTestURLsMap = new Map([
  ["about:about", null],
  ["about:license", null],
  ["about:robots", null],
  ["about:mozilla", null],
]);
let gBrowserState;

add_setup(async function () {
  let windows = [];
  let count = 0;
  for (let url of gTestURLsMap.keys()) {
    let window = !count
      ? PRIMARY_WINDOW
      : await BrowserTestUtils.openNewBrowserWindow();
    let browserLoaded = BrowserTestUtils.browserLoaded(
      window.gBrowser.selectedBrowser
    );
    BrowserTestUtils.loadURIString(window.gBrowser.selectedBrowser, url);
    await browserLoaded;
    // Capture the title.
    gTestURLsMap.set(url, window.gBrowser.selectedTab.label);
    // Minimize the before-last window, to have a different window feature added
    // to the test.
    if (count == gTestURLsMap.size - 1) {
      let activated = BrowserTestUtils.waitForEvent(
        windows[count - 1],
        "activate"
      );
      window.minimize();
      await activated;
    }
    windows.push(window);
    ++count;
  }

  // Wait until we get the lastest history from all windows.
  await Promise.all(windows.map(window => TabStateFlusher.flushWindow(window)));

  gBrowserState = ss.getBrowserState();

  await promiseAllButPrimaryWindowClosed();
});

add_task(async function test_z_indices_are_saved_correctly() {
  let state = JSON.parse(gBrowserState);
  Assert.equal(
    state.windows.length,
    gTestURLsMap.size,
    "Correct number of windows saved"
  );

  // Check if we saved state in correct order of creation.
  let idx = 0;
  for (let url of gTestURLsMap.keys()) {
    Assert.equal(
      state.windows[idx].tabs[0].entries[0].url,
      url,
      `Window #${idx} is stored in correct creation order`
    );
    ++idx;
  }

  // Check if we saved a valid zIndex (no null, no undefined or no 0).
  for (let window of state.windows) {
    Assert.ok(window.zIndex, "A valid zIndex is stored");
  }

  Assert.equal(
    state.windows[0].zIndex,
    3,
    "Window #1 should have the correct z-index"
  );
  Assert.equal(
    state.windows[1].zIndex,
    2,
    "Window #2 should have correct z-index"
  );
  Assert.equal(
    state.windows[2].zIndex,
    1,
    "Window #3 should be the topmost window"
  );
  Assert.equal(
    state.windows[3].zIndex,
    4,
    "Minimized window should be the last window to restore"
  );
});

add_task(async function test_windows_are_restored_in_reversed_z_order() {
  await promiseBrowserState(gBrowserState);

  let indexedTabLabels = [...gTestURLsMap.values()];
  let tabsRestoredLabels = BrowserWindowTracker.orderedWindows.map(
    window => window.gBrowser.selectedTab.label
  );

  Assert.equal(
    tabsRestoredLabels[0],
    indexedTabLabels[2],
    "First restored tab should be last used tab"
  );
  Assert.equal(
    tabsRestoredLabels[1],
    indexedTabLabels[1],
    "Second restored tab is correct"
  );
  Assert.equal(
    tabsRestoredLabels[2],
    indexedTabLabels[0],
    "Third restored tab is correct"
  );
  Assert.equal(
    tabsRestoredLabels[3],
    indexedTabLabels[3],
    "Last restored tab should be a minimized window"
  );

  await promiseAllButPrimaryWindowClosed();
});