summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser/browser_PageActions_newWindow.js
blob: e351727bff80e12ae146673a6b0fba9d62d4bf09 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"use strict";

// This is a test for PageActions.jsm, specifically the generalized parts that
// add and remove page actions and toggle them in the urlbar.  This does not
// test the built-in page actions; browser_page_action_menu.js does that.

// Initialization.  Must run first.
add_setup(async function () {
  await initPageActionsTest();
});

// Makes sure that urlbar nodes appear in the correct order in a new window.
add_task(async function urlbarOrderNewWindow() {
  // Make some new actions.
  let actions = [0, 1, 2].map(i => {
    return PageActions.addAction(
      new PageActions.Action({
        id: `test-urlbarOrderNewWindow-${i}`,
        title: `Test urlbarOrderNewWindow ${i}`,
        pinnedToUrlbar: true,
      })
    );
  });

  // Make sure PageActions knows they're inserted before the bookmark action in
  // the urlbar.
  Assert.deepEqual(
    PageActions._persistedActions.idsInUrlbar.slice(
      PageActions._persistedActions.idsInUrlbar.length - (actions.length + 1)
    ),
    actions.map(a => a.id).concat([PageActions.ACTION_ID_BOOKMARK]),
    "PageActions._persistedActions.idsInUrlbar has new actions inserted"
  );
  Assert.deepEqual(
    PageActions.actionsInUrlbar(window)
      .slice(PageActions.actionsInUrlbar(window).length - (actions.length + 1))
      .map(a => a.id),
    actions.map(a => a.id).concat([PageActions.ACTION_ID_BOOKMARK]),
    "PageActions.actionsInUrlbar has new actions inserted"
  );

  // Reach into _persistedActions to move the new actions to the front of the
  // urlbar, same as if the user moved them.  That way we can test that insert-
  // before IDs are correctly non-null when the urlbar nodes are inserted in the
  // new window below.
  PageActions._persistedActions.idsInUrlbar.splice(
    PageActions._persistedActions.idsInUrlbar.length - (actions.length + 1),
    actions.length
  );
  for (let i = 0; i < actions.length; i++) {
    PageActions._persistedActions.idsInUrlbar.splice(i, 0, actions[i].id);
  }

  // Save the right-ordered IDs to use below, just in case they somehow get
  // changed when the new window opens, which shouldn't happen, but maybe
  // there's bugs.
  let ids = PageActions._persistedActions.idsInUrlbar.slice();

  // Make sure that worked.
  Assert.deepEqual(
    ids.slice(0, actions.length),
    actions.map(a => a.id),
    "PageActions._persistedActions.idsInUrlbar now has new actions at front"
  );

  // _persistedActions will contain the IDs of test actions added and removed
  // above (unless PageActions._purgeUnregisteredPersistedActions() was called
  // for all of them, which it wasn't).  Filter them out because they should
  // not appear in the new window (or any window at this point).
  ids = ids.filter(id => PageActions.actionForID(id));

  // Open the new window.
  let win = await BrowserTestUtils.openNewBrowserWindow();

  // Collect its urlbar nodes.
  let actualUrlbarNodeIDs = [];
  for (
    let node = win.BrowserPageActions.mainButtonNode.nextElementSibling;
    node;
    node = node.nextElementSibling
  ) {
    actualUrlbarNodeIDs.push(node.id);
  }

  // Now check that they're in the right order.
  Assert.deepEqual(
    actualUrlbarNodeIDs,
    ids.map(id => win.BrowserPageActions.urlbarButtonNodeIDForActionID(id)),
    "Expected actions in new window's urlbar"
  );

  // Done, clean up.
  await BrowserTestUtils.closeWindow(win);
  for (let action of actions) {
    action.remove();
  }
});

// Stores version-0 (unversioned actually) persisted actions and makes sure that
// migrating to version 1 works.
add_task(async function migrate1() {
  // Add a test action so we can test a non-built-in action below.
  let actionId = "test-migrate1";
  PageActions.addAction(
    new PageActions.Action({
      id: actionId,
      title: "Test migrate1",
      pinnedToUrlbar: true,
    })
  );

  // Add the bookmark action first to make sure it ends up last after migration.
  // Also include a non-default action to make sure we're not accidentally
  // testing default behavior.
  let ids = [PageActions.ACTION_ID_BOOKMARK, actionId];
  let persisted = ids.reduce(
    (memo, id) => {
      memo.ids[id] = true;
      memo.idsInUrlbar.push(id);
      return memo;
    },
    { ids: {}, idsInUrlbar: [] }
  );

  Services.prefs.setStringPref(
    PageActions.PREF_PERSISTED_ACTIONS,
    JSON.stringify(persisted)
  );

  // Migrate.
  PageActions._loadPersistedActions();

  Assert.equal(PageActions._persistedActions.version, 1, "Correct version");

  // expected order
  let orderedIDs = [actionId, PageActions.ACTION_ID_BOOKMARK];

  // Check the ordering.
  Assert.deepEqual(
    PageActions._persistedActions.idsInUrlbar,
    orderedIDs,
    "PageActions._persistedActions.idsInUrlbar has right order"
  );
  Assert.deepEqual(
    PageActions.actionsInUrlbar(window).map(a => a.id),
    orderedIDs,
    "PageActions.actionsInUrlbar has right order"
  );

  // Open a new window.
  let win = await BrowserTestUtils.openNewBrowserWindow();
  await BrowserTestUtils.openNewForegroundTab({
    gBrowser: win.gBrowser,
    url: "http://example.com/",
  });

  // Collect its urlbar nodes.
  let actualUrlbarNodeIDs = [];
  for (
    let node = win.BrowserPageActions.mainButtonNode.nextElementSibling;
    node;
    node = node.nextElementSibling
  ) {
    actualUrlbarNodeIDs.push(node.id);
  }

  // Now check that they're in the right order.
  Assert.deepEqual(
    actualUrlbarNodeIDs,
    orderedIDs.map(id =>
      win.BrowserPageActions.urlbarButtonNodeIDForActionID(id)
    ),
    "Expected actions in new window's urlbar"
  );

  // Done, clean up.
  await BrowserTestUtils.closeWindow(win);
  Services.prefs.clearUserPref(PageActions.PREF_PERSISTED_ACTIONS);
  PageActions.actionForID(actionId).remove();
});

// Opens a new browser window and makes sure per-window state works right.
add_task(async function perWindowState() {
  // Add a test action.
  let title = "Test perWindowState";
  let action = PageActions.addAction(
    new PageActions.Action({
      iconURL: "chrome://browser/skin/mail.svg",
      id: "test-perWindowState",
      pinnedToUrlbar: true,
      title,
    })
  );

  let actionsInUrlbar = PageActions.actionsInUrlbar(window);

  // Open a new browser window and load an actionable page so that the action
  // shows up in it.
  let newWindow = await BrowserTestUtils.openNewBrowserWindow();
  await BrowserTestUtils.openNewForegroundTab({
    gBrowser: newWindow.gBrowser,
    url: "http://example.com/",
  });

  // Set a new title globally.
  let newGlobalTitle = title + " new title";
  action.setTitle(newGlobalTitle);
  Assert.equal(action.getTitle(), newGlobalTitle, "Title: global");
  Assert.equal(action.getTitle(window), newGlobalTitle, "Title: old window");
  Assert.equal(action.getTitle(newWindow), newGlobalTitle, "Title: new window");

  // Initialize panel nodes in the windows
  document.getElementById("pageActionButton").click();
  await BrowserTestUtils.waitForEvent(document, "popupshowing", true);
  newWindow.document.getElementById("pageActionButton").click();
  await BrowserTestUtils.waitForEvent(newWindow.document, "popupshowing", true);

  // The action's panel button nodes should be updated in both windows.
  let panelButtonID = BrowserPageActions.panelButtonNodeIDForActionID(
    action.id
  );
  for (let win of [window, newWindow]) {
    win.BrowserPageActions.placeLazyActionsInPanel();
    let panelButtonNode = win.document.getElementById(panelButtonID);
    Assert.equal(
      panelButtonNode.getAttribute("label"),
      newGlobalTitle,
      "Panel button label should be global title"
    );
  }

  // Set a new title in the new window.
  let newPerWinTitle = title + " new title in new window";
  action.setTitle(newPerWinTitle, newWindow);
  Assert.equal(
    action.getTitle(),
    newGlobalTitle,
    "Title: global should remain same"
  );
  Assert.equal(
    action.getTitle(window),
    newGlobalTitle,
    "Title: old window should remain same"
  );
  Assert.equal(
    action.getTitle(newWindow),
    newPerWinTitle,
    "Title: new window should be new"
  );

  // The action's panel button node should be updated in the new window but the
  // same in the old window.
  let panelButtonNode1 = document.getElementById(panelButtonID);
  Assert.equal(
    panelButtonNode1.getAttribute("label"),
    newGlobalTitle,
    "Panel button label in old window"
  );
  let panelButtonNode2 = newWindow.document.getElementById(panelButtonID);
  Assert.equal(
    panelButtonNode2.getAttribute("label"),
    newPerWinTitle,
    "Panel button label in new window"
  );

  // Disable the action in the new window.
  action.setDisabled(true, newWindow);
  Assert.equal(
    action.getDisabled(),
    false,
    "Disabled: global should remain false"
  );
  Assert.equal(
    action.getDisabled(window),
    false,
    "Disabled: old window should remain false"
  );
  Assert.equal(
    action.getDisabled(newWindow),
    true,
    "Disabled: new window should be true"
  );

  // Check PageActions.actionsInUrlbar for each window.
  Assert.deepEqual(
    PageActions.actionsInUrlbar(window).map(a => a.id),
    actionsInUrlbar.map(a => a.id),
    "PageActions.actionsInUrlbar: old window should have all actions in urlbar"
  );
  Assert.deepEqual(
    PageActions.actionsInUrlbar(newWindow).map(a => a.id),
    actionsInUrlbar.map(a => a.id).filter(id => id != action.id),
    "PageActions.actionsInUrlbar: new window should have all actions in urlbar except the test action"
  );

  // Check the urlbar nodes for the old window.
  let actualUrlbarNodeIDs = [];
  for (
    let node = BrowserPageActions.mainButtonNode.nextElementSibling;
    node;
    node = node.nextElementSibling
  ) {
    actualUrlbarNodeIDs.push(node.id);
  }
  Assert.deepEqual(
    actualUrlbarNodeIDs,
    actionsInUrlbar.map(a =>
      BrowserPageActions.urlbarButtonNodeIDForActionID(a.id)
    ),
    "Old window should have all nodes in urlbar"
  );

  // Check the urlbar nodes for the new window.
  actualUrlbarNodeIDs = [];
  for (
    let node = newWindow.BrowserPageActions.mainButtonNode.nextElementSibling;
    node;
    node = node.nextElementSibling
  ) {
    actualUrlbarNodeIDs.push(node.id);
  }
  Assert.deepEqual(
    actualUrlbarNodeIDs,
    actionsInUrlbar
      .filter(a => a.id != action.id)
      .map(a => BrowserPageActions.urlbarButtonNodeIDForActionID(a.id)),
    "New window should have all nodes in urlbar except for the test action's"
  );

  // Done, clean up.
  await BrowserTestUtils.closeWindow(newWindow);
  action.remove();
});

add_task(async function action_disablePrivateBrowsing() {
  let id = "testWidget";
  let action = PageActions.addAction(
    new PageActions.Action({
      id,
      disablePrivateBrowsing: true,
      title: "title",
      disabled: false,
      pinnedToUrlbar: true,
    })
  );
  // Open an actionable page so that the main page action button appears.
  let url = "http://example.com/";
  let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await BrowserTestUtils.openNewForegroundTab(
    privateWindow.gBrowser,
    url,
    true,
    true
  );

  Assert.ok(action.canShowInWindow(window), "should show in default window");
  Assert.ok(
    !action.canShowInWindow(privateWindow),
    "should not show in private browser"
  );
  Assert.ok(action.shouldShowInUrlbar(window), "should show in default urlbar");
  Assert.ok(
    !action.shouldShowInUrlbar(privateWindow),
    "should not show in default urlbar"
  );
  Assert.ok(action.shouldShowInPanel(window), "should show in default urlbar");
  Assert.ok(
    !action.shouldShowInPanel(privateWindow),
    "should not show in default urlbar"
  );

  action.remove();

  privateWindow.close();
});