summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/about/browser_aboutNewTab_bookmarksToolbarNewWindow.js
blob: da2358a31fbebfe21d8e75c516514134eb1ad35a (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

requestLongerTimeout(2);

const testCases = [
  {
    name: "bookmarks_toolbar_shown_on_newtab_newTabEnabled",
    newTabEnabled: true,
  },
  {
    name: "bookmarks_toolbar_shown_on_newtab",
    newTabEnabled: false,
  },
];

async function test_bookmarks_toolbar_visibility({ newTabEnabled }) {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.newtabpage.enabled", newTabEnabled]],
  });

  // Ensure the toolbar doesnt become visible at any point before the tab finishes loading

  let url =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://example.com"
    ) + "slow_loading_page.sjs";

  let startTime = Date.now();
  let newWindowOpened = BrowserTestUtils.domWindowOpened();
  let beforeShown = TestUtils.topicObserved("browser-window-before-show");

  let triggeringPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
  openUILinkIn(url, "window", { triggeringPrincipal });

  let newWin = await newWindowOpened;
  let slowSiteLoaded = BrowserTestUtils.firstBrowserLoaded(newWin, false);

  function checkToolbarIsCollapsed(win, message) {
    let toolbar = win.document.getElementById("PersonalToolbar");
    ok(toolbar && toolbar.collapsed, message);
  }

  await beforeShown;
  checkToolbarIsCollapsed(
    newWin,
    "Toolbar is initially hidden on the new window"
  );

  function onToolbarMutation() {
    checkToolbarIsCollapsed(newWin, "Toolbar should remain collapsed");
  }
  let toolbarMutationObserver = new newWin.MutationObserver(onToolbarMutation);
  toolbarMutationObserver.observe(
    newWin.document.getElementById("PersonalToolbar"),
    {
      attributeFilter: ["collapsed"],
    }
  );

  info("Waiting for the slow site to load");
  await slowSiteLoaded;
  info(`Window opened and slow site loaded in: ${Date.now() - startTime}ms`);

  checkToolbarIsCollapsed(newWin, "Finally, the toolbar is still hidden");

  toolbarMutationObserver.disconnect();
  await BrowserTestUtils.closeWindow(newWin);
}

// Make separate tasks for each test case, so we get more useful stack traces on failure
for (let testData of testCases) {
  let tmp = {
    async [testData.name]() {
      info("testing with: " + JSON.stringify(testData));
      await test_bookmarks_toolbar_visibility(testData);
    },
  };
  add_task(tmp[testData.name]);
}