summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser/telemetry/head-spa.js
blob: 2718dbb9ff0d57aff975e4016fccd8288694e032 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Helpers to simulate the use of a single page application.
 */

/* import-globals-from head.js */

/**
 * Used to control the SPA SERP.
 */
class SinglePageAppUtils {
  static async clickAd(tab) {
    info("Clicking ad.");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#ad",
      {},
      tab.linkedBrowser
    );
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  }

  static async clickAllTab(tab) {
    info("Click All tab to return to a SERP.");
    let adsPromise = TestUtils.topicObserved(
      "reported-page-with-ad-impressions"
    );
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#all",
      {},
      tab.linkedBrowser
    );
    await adsPromise;
  }

  static async clickImagesTab(tab) {
    info("Click images tab.");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#images",
      {},
      tab.linkedBrowser
    );
    info("Wait a brief amount of time.");
    // There's no obvious way to know we shouldn't expect a SERP impression, so
    // we wait roughly the amount of time it would take for extracting ads to
    // take.
    await promiseWaitForAdLinkCheck();
  }

  static async clickOrganic(tab) {
    info("Clicking organic result.");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#organic",
      {},
      tab.linkedBrowser
    );
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  }

  static async clickRedirectAd(tab) {
    info("Clicking redirect ad.");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#ad-redirect",
      {},
      tab.linkedBrowser
    );
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  }

  static async clickRedirectAdInNewTab(tab) {
    info("Clicking redirect ad in new tab.");
    let tabPromise = BrowserTestUtils.waitForNewTab(tab.ownerGlobal.gBrowser);
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#ad-redirect",
      { button: 1 },
      tab.linkedBrowser
    );
    let redirectedTab = await tabPromise;
    return redirectedTab;
  }

  static async clickRedirectAdInNewWindow(tab) {
    let contextMenu = tab.linkedBrowser.ownerGlobal.document.getElementById(
      "contentAreaContextMenu"
    );
    let contextMenuPromise = BrowserTestUtils.waitForEvent(
      contextMenu,
      "popupshown"
    );
    info("Open context menu.");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#ad-redirect",
      { type: "contextmenu", button: 2 },
      tab.linkedBrowser
    );
    await contextMenuPromise;

    let newWindowPromise = BrowserTestUtils.waitForNewWindow({
      url: "https://example.com/hello_world",
    });
    let openLinkInNewWindow = contextMenu.querySelector("#context-openlink");
    info("Click on Open Link in New Window");
    contextMenu.activateItem(openLinkInNewWindow);
    return await newWindowPromise;
  }

  static async clickSearchbox(tab) {
    info("Clicking searchbox.");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#searchbox",
      {},
      tab.linkedBrowser
    );
    await waitForIdle();
  }

  static async clickSearchboxAndType(tab, str = "hello world") {
    await SinglePageAppUtils.clickSearchbox(tab);
    info(`Type ${str} in searchbox.`);
    for (let char of str) {
      await BrowserTestUtils.sendChar(char, tab.linkedBrowser);
    }
    await waitForIdle();
  }

  static async clickSuggestion(tab) {
    info("Clicking the first suggestion.");
    let adsPromise = TestUtils.topicObserved(
      "reported-page-with-ad-impressions"
    );
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#searchbox-suggestions div",
      {},
      tab.linkedBrowser
    );
    await adsPromise;
  }

  static async clickSuggestionOnImagesTab(tab) {
    info("Clicking the first suggestion on images tab.");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#searchbox-suggestions div",
      {},
      tab.linkedBrowser
    );
    await promiseWaitForAdLinkCheck();
  }

  static async createTabAndLoadURL(
    url = new URL(getSERPUrl("searchTelemetrySinglePageApp.html"))
  ) {
    info("Load SERP.");
    let adsPromise = TestUtils.topicObserved(
      "reported-page-with-ad-impressions"
    );
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url.href);
    await adsPromise;
    return tab;
  }

  static async createTabAndSearch(searchTerm = "test") {
    info("Load SERP.");
    let adsPromise = TestUtils.topicObserved(
      "reported-page-with-ad-impressions"
    );
    let url = new URL(getSERPUrl("searchTelemetrySinglePageApp.html"));
    url.searchParams.set("s", searchTerm);
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url.href);
    await adsPromise;
    return tab;
  }

  static async createTabsWithDifferentProviders() {
    let url1 = new URL(getSERPUrl("searchTelemetrySinglePageApp.html"));
    let tab1 = await SinglePageAppUtils.createTabAndLoadURL(url1);

    let url2 = new URL(
      getAlternateSERPUrl("searchTelemetrySinglePageApp.html")
    );
    let tab2 = await SinglePageAppUtils.createTabAndLoadURL(url2);

    return [tab1, tab2];
  }

  static async goBack(tab) {
    info("Go back to SERP ads.");
    let promise = TestUtils.topicObserved("reported-page-with-ad-impressions");
    tab.linkedBrowser.goBack();
    await promise;
  }

  static async goBackToPageWithoutAds(tab) {
    info("Go back to SERP without ads.");
    tab.linkedBrowser.goBack();
    await new Promise(resolve => setTimeout(resolve, 200));
  }

  static async goForward(tab) {
    info("Go forward to SERP ads.");
    let promise = TestUtils.topicObserved("reported-page-with-ad-impressions");
    tab.linkedBrowser.goForward();
    await promise;
  }

  static async goForwardToPageWithoutAds(tab) {
    info("Go forward to SERP without ads.");
    tab.linkedBrowser.goForward();
    await new Promise(resolve => setTimeout(resolve, 200));
  }

  static async pushUnrelatedState(tab, { key = "foobar", value = "baz" } = {}) {
    info(`Pushing ${key}=${value} to the list of query parameters in URL.`);
    await SpecialPowers.spawn(
      tab.linkedBrowser,
      [key, value],
      async function (contentKey, contentValue) {
        let url = new URL(content.window.location.href);
        url.searchParams.set(contentKey, contentValue);
        content.history.pushState({}, "", url);
      }
    );
  }

  static async visitRelatedSearch(tab) {
    let adsPromise = TestUtils.topicObserved(
      "reported-page-with-ad-impressions"
    );
    info("Clicking a related search with an ad.");
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#related-search",
      {},
      tab.linkedBrowser
    );
    await adsPromise;
  }

  static async visitRelatedSearchWithoutAds(tab) {
    info("Clicking a related search without ads.");
    let adsPromise = TestUtils.topicObserved(
      "reported-page-with-ad-impressions"
    );
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#related-search-without-ads",
      {},
      tab.linkedBrowser
    );
    await adsPromise;
  }
}

function getAlternateSERPUrl(page, organic = false) {
  let url =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://example.com"
    ) + page;
  return `${url}?s=test${organic ? "" : "&abc=ff"}`;
}