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

const { ASRouter } = ChromeUtils.import(
  "resource://activity-stream/lib/ASRouter.jsm"
);

const { TelemetryFeed } = ChromeUtils.import(
  "resource://activity-stream/lib/TelemetryFeed.jsm"
);

add_task(async function render_below_search_snippet() {
  ASRouter._validPreviewEndpoint = () => true;
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:newtab?endpoint=https://example.com/browser/browser/components/newtab/test/browser/snippet_below_search_test.json",
    },
    async browser => {
      await waitForPreloaded(browser);

      const complete = await SpecialPowers.spawn(browser, [], async () => {
        // Verify the simple_below_search_snippet renders in container below searchbox
        // and nothing is rendered in the footer.
        await ContentTaskUtils.waitForCondition(
          () =>
            content.document.querySelector(
              ".below-search-snippet .SimpleBelowSearchSnippet"
            ),
          "Should find the snippet inside the below search container"
        );

        is(
          0,
          content.document.querySelector("#footer-asrouter-container")
            .childNodes.length,
          "Should not find any snippets in the footer container"
        );

        return true;
      });

      Assert.ok(complete, "Test complete.");
    }
  );
});

add_task(async function render_snippets_icon_and_link() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:newtab?endpoint=https://example.com/browser/browser/components/newtab/test/browser/snippet_simple_test.json",
    },
    async browser => {
      await waitForPreloaded(browser);

      const complete = await SpecialPowers.spawn(browser, [], async () => {
        const syncLink = "https://www.mozilla.org/en-US/firefox/accounts";
        // Verify the simple_snippet renders in the footer and the container below
        // searchbox is not rendered.
        await ContentTaskUtils.waitForCondition(
          () =>
            content.document.querySelector(
              "#footer-asrouter-container .SimpleSnippet"
            ),
          "Should find the snippet inside the footer container"
        );
        await ContentTaskUtils.waitForCondition(
          () =>
            content.document.querySelector(
              "#footer-asrouter-container .SimpleSnippet .icon"
            ),
          "Should render an icon"
        );
        await ContentTaskUtils.waitForCondition(
          () =>
            content.document.querySelector(
              `#footer-asrouter-container .SimpleSnippet a[href='${syncLink}']`
            ),
          "Should render an anchor with the correct href"
        );

        ok(
          !content.document.querySelector(".below-search-snippet"),
          "Should not find any snippets below search"
        );

        return true;
      });

      Assert.ok(complete, "Test complete.");
    }
  );
});

add_task(async function render_preview_snippet() {
  ASRouter._validPreviewEndpoint = () => true;
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:newtab?endpoint=https://example.com/browser/browser/components/newtab/test/browser/snippet.json",
    },
    async browser => {
      let text = await SpecialPowers.spawn(browser, [], async () => {
        await ContentTaskUtils.waitForCondition(
          () => content.document.querySelector(".activity-stream"),
          `Should render Activity Stream`
        );
        await ContentTaskUtils.waitForCondition(
          () =>
            content.document.querySelector(
              "#footer-asrouter-container .SimpleSnippet"
            ),
          "Should find the snippet inside the footer container"
        );

        return content.document.querySelector(
          "#footer-asrouter-container .SimpleSnippet"
        ).innerText;
      });

      Assert.equal(
        text,
        "On January 30th Nightly will introduce dedicated profiles, making it simpler to run different installations of Firefox side by side. Learn what this means for you.",
        "Snippet content match"
      );
    }
  );
});

add_task(async function test_snippets_telemetry() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "browser.newtabpage.activity-stream.asrouter.providers.snippets",
        `{"id":"snippets","enabled":true,"type":"remote","url":"https://example.com/browser/browser/components/newtab/test/browser/snippet.json","updateCycleInMs":0}`,
      ],
      ["browser.newtabpage.activity-stream.feeds.snippets", true],
    ],
  });
  const sendPingStub = sinon.stub(
    TelemetryFeed.prototype,
    "sendStructuredIngestionEvent"
  );
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      // Work around any issues caching might introduce by navigating to
      // about blank first
      url: "about:blank",
    },
    async browser => {
      await BrowserTestUtils.loadURIString(browser, "about:home");
      await BrowserTestUtils.browserLoaded(browser);
      let text = await SpecialPowers.spawn(browser, [], async () => {
        await ContentTaskUtils.waitForCondition(
          () => content.document.querySelector(".activity-stream"),
          `Should render Activity Stream`
        );
        await ContentTaskUtils.waitForCondition(
          () =>
            content.document.querySelector(
              "#footer-asrouter-container .SimpleSnippet"
            ),
          "Should find the snippet inside the footer container"
        );

        return content.document.querySelector(
          "#footer-asrouter-container .SimpleSnippet"
        ).innerText;
      });

      Assert.equal(
        text,
        "On January 30th Nightly will introduce dedicated profiles, making it simpler to run different installations of Firefox side by side. Learn what this means for you.",
        "Snippet content match"
      );
    }
  );

  Assert.ok(sendPingStub.callCount >= 1, "We registered some pings");
  const snippetsPing = sendPingStub.args.find(args => args[2] === "snippets");
  Assert.ok(snippetsPing, "Found the snippets ping");
  Assert.equal(
    snippetsPing[0].event,
    "IMPRESSION",
    "It's the correct ping type"
  );

  sendPingStub.restore();
});