summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser/browser_BrowserWindowTracker.js
blob: ea6f75c0e36ffa0969e3fed3126b3fd62b15850e (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"use strict";

const TEST_WINDOW = window;

function windowActivated(win) {
  if (Services.ww.activeWindow == win) {
    return Promise.resolve();
  }
  return BrowserTestUtils.waitForEvent(win, "activate");
}

async function withOpenWindows(amount, cont) {
  let windows = [];
  for (let i = 0; i < amount; ++i) {
    let win = await BrowserTestUtils.openNewBrowserWindow();
    await windowActivated(win);
    windows.push(win);
  }
  await cont(windows);
  await Promise.all(
    windows.map(window => BrowserTestUtils.closeWindow(window))
  );
}

add_task(async function test_getTopWindow() {
  await withOpenWindows(5, async function (windows) {
    // Without options passed in.
    let window = BrowserWindowTracker.getTopWindow();
    let expectedMostRecentIndex = windows.length - 1;
    Assert.equal(
      window,
      windows[expectedMostRecentIndex],
      "Last opened window should be the most recent one."
    );

    // Mess with the focused window things a bit.
    for (let idx of [3, 1]) {
      let promise = BrowserTestUtils.waitForEvent(windows[idx], "activate");
      Services.focus.focusedWindow = windows[idx];
      await promise;
      window = BrowserWindowTracker.getTopWindow();
      Assert.equal(
        window,
        windows[idx],
        "Lastly focused window should be the most recent one."
      );
      // For this test it's useful to keep the array of created windows in order.
      windows.splice(idx, 1);
      windows.push(window);
    }
    // Update the pointer to the most recent opened window.
    expectedMostRecentIndex = windows.length - 1;

    // With 'private' option.
    window = BrowserWindowTracker.getTopWindow({ private: true });
    Assert.equal(window, null, "No private windows opened yet.");
    window = BrowserWindowTracker.getTopWindow({ private: 1 });
    Assert.equal(window, null, "No private windows opened yet.");
    windows.push(
      await BrowserTestUtils.openNewBrowserWindow({ private: true })
    );
    ++expectedMostRecentIndex;
    window = BrowserWindowTracker.getTopWindow({ private: true });
    Assert.equal(
      window,
      windows[expectedMostRecentIndex],
      "Private window available."
    );
    window = BrowserWindowTracker.getTopWindow({ private: 1 });
    Assert.equal(
      window,
      windows[expectedMostRecentIndex],
      "Private window available."
    );
    // Private window checks seems to mysteriously fail on Linux in this test.
    if (AppConstants.platform != "linux") {
      window = BrowserWindowTracker.getTopWindow({ private: false });
      Assert.equal(
        window,
        windows[expectedMostRecentIndex - 1],
        "Private window available, but should not be returned."
      );
    }

    // With 'allowPopups' option.
    window = BrowserWindowTracker.getTopWindow({ allowPopups: true });
    Assert.equal(
      window,
      windows[expectedMostRecentIndex],
      "Window focused before the private window should be the most recent one."
    );
    window = BrowserWindowTracker.getTopWindow({ allowPopups: false });
    Assert.equal(
      window,
      windows[expectedMostRecentIndex],
      "Window focused before the private window should be the most recent one."
    );
    let popupWindowPromise = BrowserTestUtils.waitForNewWindow();
    SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
      let features =
        "location=no, personalbar=no, toolbar=no, scrollbars=no, menubar=no, status=no";
      content.window.open("about:blank", "_blank", features);
    });
    let popupWindow = await popupWindowPromise;
    await windowActivated(popupWindow);
    window = BrowserWindowTracker.getTopWindow({ allowPopups: true });
    Assert.equal(
      window,
      popupWindow,
      "The popup window should be the most recent one, when requested."
    );
    window = BrowserWindowTracker.getTopWindow({ allowPopups: false });
    Assert.equal(
      window,
      windows[expectedMostRecentIndex],
      "Window focused before the popup window should be the most recent one."
    );
    popupWindow.close();
  });
});

add_task(async function test_orderedWindows() {
  await withOpenWindows(10, async function (windows) {
    Assert.equal(
      BrowserWindowTracker.windowCount,
      11,
      "Number of tracked windows, including the test window"
    );
    let ordered = BrowserWindowTracker.orderedWindows.filter(
      w => w != TEST_WINDOW
    );
    Assert.deepEqual(
      [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
      ordered.map(w => windows.indexOf(w)),
      "Order of opened windows should be as opened."
    );

    // Mess with the focused window things a bit.
    for (let idx of [4, 6, 1]) {
      let promise = BrowserTestUtils.waitForEvent(windows[idx], "activate");
      Services.focus.focusedWindow = windows[idx];
      await promise;
    }

    let ordered2 = BrowserWindowTracker.orderedWindows.filter(
      w => w != TEST_WINDOW
    );
    // After the shuffle, we expect window '1' to be the top-most window, because
    // it was the last one we called focus on. Then '6', the window we focused
    // before-last, followed by '4'. The order of the other windows remains
    // unchanged.
    let expected = [1, 6, 4, 9, 8, 7, 5, 3, 2, 0];
    Assert.deepEqual(
      expected,
      ordered2.map(w => windows.indexOf(w)),
      "After shuffle of focused windows, the order should've changed."
    );
  });
});

add_task(async function test_pendingWindows() {
  Assert.equal(
    BrowserWindowTracker.windowCount,
    1,
    "Number of tracked windows, including the test window"
  );

  let pending = BrowserWindowTracker.getPendingWindow();
  Assert.equal(pending, null, "Should be no pending window");

  let expectedWin = BrowserWindowTracker.openWindow();
  pending = BrowserWindowTracker.getPendingWindow();
  Assert.ok(pending, "Should be a pending window now.");
  Assert.ok(
    !BrowserWindowTracker.getPendingWindow({ private: true }),
    "Should not be a pending private window"
  );
  Assert.equal(
    pending,
    BrowserWindowTracker.getPendingWindow({ private: false }),
    "Should be the same non-private window pending"
  );

  let foundWin = await pending;
  Assert.equal(foundWin, expectedWin, "Should have found the right window");
  Assert.ok(
    !BrowserWindowTracker.getPendingWindow(),
    "Should be no pending window now."
  );

  await BrowserTestUtils.closeWindow(foundWin);

  expectedWin = BrowserWindowTracker.openWindow({ private: true });
  pending = BrowserWindowTracker.getPendingWindow();
  Assert.ok(pending, "Should be a pending window now.");
  Assert.ok(
    !BrowserWindowTracker.getPendingWindow({ private: false }),
    "Should not be a pending non-private window"
  );
  Assert.equal(
    pending,
    BrowserWindowTracker.getPendingWindow({ private: true }),
    "Should be the same private window pending"
  );

  foundWin = await pending;
  Assert.equal(foundWin, expectedWin, "Should have found the right window");
  Assert.ok(
    !BrowserWindowTracker.getPendingWindow(),
    "Should be no pending window now."
  );

  await BrowserTestUtils.closeWindow(foundWin);

  expectedWin = Services.ww.openWindow(
    null,
    AppConstants.BROWSER_CHROME_URL,
    "_blank",
    "chrome,dialog=no,all",
    null
  );
  BrowserWindowTracker.registerOpeningWindow(expectedWin, false);
  pending = BrowserWindowTracker.getPendingWindow();
  Assert.ok(pending, "Should be a pending window now.");

  foundWin = await pending;
  Assert.equal(foundWin, expectedWin, "Should have found the right window");
  Assert.ok(
    !BrowserWindowTracker.getPendingWindow(),
    "Should be no pending window now."
  );

  await BrowserTestUtils.closeWindow(foundWin);
});