summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/browser/browser_newtab_overrides.js
blob: ce7d82881ff30f932bfbdd5979d94444336a40e6 (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
"use strict";

const { AboutNewTab } = ChromeUtils.import(
  "resource:///modules/AboutNewTab.jsm"
);

registerCleanupFunction(() => {
  AboutNewTab.resetNewTabURL();
});

function nextChangeNotificationPromise(aNewURL, testMessage) {
  return TestUtils.topicObserved(
    "newtab-url-changed",
    function observer(aSubject, aData) {
      Assert.equal(aData, aNewURL, testMessage);
      return true;
    }
  );
}

/*
 * Tests that the default newtab page is always returned when one types "about:newtab" in the URL bar,
 * even when overridden.
 */
add_task(async function redirector_ignores_override() {
  let overrides = ["chrome://browser/content/aboutRobots.xhtml", "about:home"];

  for (let overrideURL of overrides) {
    let notificationPromise = nextChangeNotificationPromise(
      overrideURL,
      `newtab page now points to ${overrideURL}`
    );
    AboutNewTab.newTabURL = overrideURL;

    await notificationPromise;
    Assert.ok(AboutNewTab.newTabURLOverridden, "url has been overridden");

    let tabOptions = {
      gBrowser,
      url: "about:newtab",
    };

    /*
     * Simulate typing "about:newtab" in the url bar.
     *
     * Bug 1240169 - We expect the redirector to lead the user to "about:newtab", the default URL,
     * due to invoking AboutRedirector. A user interacting with the chrome otherwise would lead
     * to the overriding URLs.
     */
    await BrowserTestUtils.withNewTab(tabOptions, async browser => {
      await SpecialPowers.spawn(browser, [], async () => {
        Assert.equal(content.location.href, "about:newtab", "Got right URL");
        Assert.equal(
          content.document.location.href,
          "about:newtab",
          "Got right URL"
        );
        Assert.notEqual(
          content.document.nodePrincipal,
          Services.scriptSecurityManager.getSystemPrincipal(),
          "activity stream principal should not match systemPrincipal"
        );
      });
    });
  }
});

/*
 * Tests loading an overridden newtab page by simulating opening a newtab page from chrome
 */
add_task(async function override_loads_in_browser() {
  let overrides = [
    "chrome://browser/content/aboutRobots.xhtml",
    "about:home",
    " about:home",
  ];

  for (let overrideURL of overrides) {
    let notificationPromise = nextChangeNotificationPromise(
      overrideURL.trim(),
      `newtab page now points to ${overrideURL}`
    );
    AboutNewTab.newTabURL = overrideURL;

    await notificationPromise;
    Assert.ok(AboutNewTab.newTabURLOverridden, "url has been overridden");

    // simulate a newtab open as a user would
    BrowserOpenTab();

    let browser = gBrowser.selectedBrowser;
    await BrowserTestUtils.browserLoaded(browser);

    await SpecialPowers.spawn(browser, [{ url: overrideURL }], async args => {
      Assert.equal(content.location.href, args.url.trim(), "Got right URL");
      Assert.equal(
        content.document.location.href,
        args.url.trim(),
        "Got right URL"
      );
    });
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  }
});

/*
 * Tests edge cases when someone overrides the newtabpage with whitespace
 */
add_task(async function override_blank_loads_in_browser() {
  let overrides = ["", " ", "\n\t", " about:blank"];

  for (let overrideURL of overrides) {
    let notificationPromise = nextChangeNotificationPromise(
      "about:blank",
      "newtab page now points to about:blank"
    );
    AboutNewTab.newTabURL = overrideURL;

    await notificationPromise;
    Assert.ok(AboutNewTab.newTabURLOverridden, "url has been overridden");

    // simulate a newtab open as a user would
    BrowserOpenTab();

    let browser = gBrowser.selectedBrowser;
    await BrowserTestUtils.browserLoaded(browser);

    await SpecialPowers.spawn(browser, [], async () => {
      Assert.equal(content.location.href, "about:blank", "Got right URL");
      Assert.equal(
        content.document.location.href,
        "about:blank",
        "Got right URL"
      );
    });
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  }
});