summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_urlbar_telemetry_sponsored_topsites.js
blob: 74d1fdb0dbc7364a205a8bc2f098fb9a244a9067 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  CONTEXTUAL_SERVICES_PING_TYPES:
    "resource:///modules/PartnerLinkAttribution.sys.mjs",
  NewTabUtils: "resource://gre/modules/NewTabUtils.sys.mjs",
  PartnerLinkAttribution: "resource:///modules/PartnerLinkAttribution.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(this, {
  HttpServer: "resource://testing-common/httpd.js",
});

const EN_US_TOPSITES =
  "https://www.youtube.com/,https://www.facebook.com/,https://www.amazon.com/,https://www.reddit.com/,https://www.wikipedia.org/,https://twitter.com/";

// This is used for "sendAttributionRequest"
var gHttpServer = null;
var gRequests = [];

function submitHandler(request, response) {
  gRequests.push(request);
  response.setStatusLine(request.httpVersion, 200, "Ok");
}

// Spy for telemetry sender
let spy;

add_setup(async function () {
  sandbox = sinon.createSandbox();
  spy = sandbox.spy(
    PartnerLinkAttribution._pingCentre,
    "sendStructuredIngestionPing"
  );

  let topsitesAttribution = Services.prefs.getStringPref(
    "browser.partnerlink.campaign.topsites"
  );
  gHttpServer = new HttpServer();
  gHttpServer.registerPathHandler(`/cid/${topsitesAttribution}`, submitHandler);
  gHttpServer.start(-1);

  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.sponsoredTopSites", true],
      ["browser.urlbar.suggest.topsites", true],
      ["browser.newtabpage.activity-stream.default.sites", EN_US_TOPSITES],
      [
        "browser.partnerlink.attributionURL",
        `http://localhost:${gHttpServer.identity.primaryPort}/cid/`,
      ],
    ],
  });

  await updateTopSites(
    sites => sites && sites.length == EN_US_TOPSITES.split(",").length
  );

  registerCleanupFunction(async () => {
    sandbox.restore();
    await gHttpServer.stop();
    gHttpServer = null;
  });
});

add_task(async function send_impression_and_click() {
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    let link = {
      label: "test_label",
      url: "http://example.com/",
      sponsored_position: 1,
      sendAttributionRequest: true,
      sponsored_tile_id: 42,
      sponsored_impression_url: "http://impression.test.com/",
      sponsored_click_url: "http://click.test.com/",
    };
    // Pin a sponsored TopSite to set up the test fixture
    NewTabUtils.pinnedLinks.pin(link, 0);

    await updateTopSites(sites => sites && sites[0] && sites[0].isPinned);

    await UrlbarTestUtils.promisePopupOpen(window, () => {
      EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {});
    });

    await UrlbarTestUtils.promiseSearchComplete(window);

    // Select the first result and confirm it.
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    EventUtils.synthesizeKey("KEY_ArrowDown");

    let loadPromise = BrowserTestUtils.waitForDocLoadAndStopIt(
      result.url,
      gBrowser.selectedBrowser
    );
    EventUtils.synthesizeKey("KEY_Enter");
    await loadPromise;

    Assert.ok(
      spy.calledTwice,
      "Should send an impression ping and a click ping"
    );

    // Validate the impression ping
    let [payload, endpoint] = spy.firstCall.args;
    Assert.ok(
      endpoint.includes(CONTEXTUAL_SERVICES_PING_TYPES.TOPSITES_IMPRESSION),
      "Should set the endpoint for TopSites impression"
    );
    Assert.ok(!!payload.context_id, "Should set the context_id");
    Assert.equal(payload.advertiser, "test_label", "Should set the advertiser");
    Assert.equal(
      payload.reporting_url,
      "http://impression.test.com/",
      "Should set the impression reporting URL"
    );
    Assert.equal(payload.tile_id, 42, "Should set the tile_id");
    Assert.equal(payload.position, 1, "Should set the position");

    // Validate the click ping
    [payload, endpoint] = spy.secondCall.args;
    Assert.ok(
      endpoint.includes(CONTEXTUAL_SERVICES_PING_TYPES.TOPSITES_SELECTION),
      "Should set the endpoint for TopSites click"
    );
    Assert.ok(!!payload.context_id, "Should set the context_id");
    Assert.equal(
      payload.reporting_url,
      "http://click.test.com/",
      "Should set the click reporting URL"
    );
    Assert.equal(payload.tile_id, 42, "Should set the tile_id");
    Assert.equal(payload.position, 1, "Should set the position");

    await UrlbarTestUtils.promisePopupClose(window, () => {
      gURLBar.blur();
    });

    NewTabUtils.pinnedLinks.unpin(link);
  });
});

add_task(async function zero_ping() {
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    spy.resetHistory();

    // Reload the TopSites
    await updateTopSites(
      sites => sites && sites.length == EN_US_TOPSITES.split(",").length
    );

    await UrlbarTestUtils.promisePopupOpen(window, () => {
      EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {});
    });

    await UrlbarTestUtils.promiseSearchComplete(window);

    // Select the first result and confirm it.
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    EventUtils.synthesizeKey("KEY_ArrowDown");

    let loadPromise = BrowserTestUtils.waitForDocLoadAndStopIt(
      result.url,
      gBrowser.selectedBrowser
    );
    EventUtils.synthesizeKey("KEY_Enter");
    await loadPromise;

    Assert.ok(
      spy.notCalled,
      "Should not send any ping if there is no sponsored Top Site"
    );

    await UrlbarTestUtils.promisePopupClose(window, () => {
      gURLBar.blur();
    });
  });
});