summaryrefslogtreecommitdiffstats
path: root/browser/components/firefoxview/tests/browser/browser_firefoxview_search_telemetry.js
blob: c76a11d3adbc16c247a0543a1e43da111d876651 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
let gInitialTab;
let gInitialTabURL;

const NUMBER_OF_TABS = 6;

const syncedTabsData = [
  {
    id: 1,
    name: "This Device",
    isCurrentDevice: true,
    type: "desktop",
    tabs: Array(NUMBER_OF_TABS)
      .fill({
        type: "tab",
        title: "Internet for people, not profits - Mozilla",
        icon: "https://www.mozilla.org/media/img/favicons/mozilla/favicon.d25d81d39065.ico",
        client: 1,
      })
      .map((tab, i) => ({ ...tab, url: URLs[i] })),
  },
];

const searchEvent = page => {
  return [
    ["firefoxview_next", "search_initiated", "search", undefined, { page }],
  ];
};

const cleanUp = () => {
  while (gBrowser.tabs.length > 1) {
    BrowserTestUtils.removeTab(gBrowser.tabs.at(-1));
  }
};

add_setup(async () => {
  gInitialTab = gBrowser.selectedTab;
  gInitialTabURL = gBrowser.selectedBrowser.currentURI.spec;
  registerCleanupFunction(async () => {
    clearHistory();
  });
});

add_task(async function test_search_initiated_telemetry() {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    await clearAllParentTelemetryEvents();

    is(document.location.hash, "", "Searching within recent browsing.");
    const recentBrowsing = document.querySelector("view-recentbrowsing");
    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(
      recentBrowsing.searchTextbox,
      {},
      content
    );
    EventUtils.sendString("example.com", content);
    await telemetryEvent(searchEvent("recentbrowsing"));

    await navigateToViewAndWait(document, "opentabs");
    await clearAllParentTelemetryEvents();
    is(document.location.hash, "#opentabs", "Searching within open tabs.");
    const openTabs = document.querySelector("named-deck > view-opentabs");
    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(openTabs.searchTextbox, {}, content);
    EventUtils.sendString("example.com", content);
    await telemetryEvent(searchEvent("opentabs"));

    await navigateToViewAndWait(document, "recentlyclosed");
    await clearAllParentTelemetryEvents();
    is(
      document.location.hash,
      "#recentlyclosed",
      "Searching within recently closed."
    );
    const recentlyClosed = document.querySelector(
      "named-deck > view-recentlyclosed"
    );
    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(
      recentlyClosed.searchTextbox,
      {},
      content
    );
    EventUtils.sendString("example.com", content);
    await telemetryEvent(searchEvent("recentlyclosed"));

    await navigateToViewAndWait(document, "syncedtabs");
    await clearAllParentTelemetryEvents();
    is(document.location.hash, "#syncedtabs", "Searching within synced tabs.");
    const syncedTabs = document.querySelector("named-deck > view-syncedtabs");
    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(syncedTabs.searchTextbox, {}, content);
    EventUtils.sendString("example.com", content);
    await telemetryEvent(searchEvent("syncedtabs"));

    await navigateToViewAndWait(document, "history");
    await clearAllParentTelemetryEvents();
    is(document.location.hash, "#history", "Searching within history.");
    const history = document.querySelector("named-deck > view-history");
    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(history.searchTextbox, {}, content);
    EventUtils.sendString("example.com", content);
    await telemetryEvent(searchEvent("history"));

    await clearAllParentTelemetryEvents();
  });
});

add_task(async function test_show_all_recentlyclosed_telemetry() {
  for (let i = 0; i < NUMBER_OF_TABS; i++) {
    await open_then_close(URLs[1]);
  }
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const recentBrowsing = document.querySelector("view-recentbrowsing");

    info("Input a search query.");
    EventUtils.synthesizeMouseAtCenter(
      recentBrowsing.searchTextbox,
      {},
      content
    );
    EventUtils.sendString("example.com", content);
    const recentlyclosedSlot = recentBrowsing.querySelector(
      "[slot='recentlyclosed']"
    );
    await TestUtils.waitForCondition(
      () =>
        recentlyclosedSlot.tabList.rowEls.length === 5 &&
        recentlyclosedSlot.shadowRoot.querySelector(
          "[data-l10n-id='firefoxview-show-all']"
        ),
      "Expected search results are not shown yet."
    );
    await clearAllParentTelemetryEvents();

    info("Click the Show All link.");
    const showAllButton = recentlyclosedSlot.shadowRoot.querySelector(
      "[data-l10n-id='firefoxview-show-all']"
    );
    await TestUtils.waitForCondition(() => !showAllButton.hidden);
    ok(!showAllButton.hidden, "Show all button is visible");
    await TestUtils.waitForCondition(() => {
      EventUtils.synthesizeMouseAtCenter(showAllButton, {}, content);
      if (recentlyclosedSlot.tabList.rowEls.length === NUMBER_OF_TABS) {
        return true;
      }
      return false;
    }, "All search results are not shown.");

    await telemetryEvent([
      [
        "firefoxview_next",
        "search_show_all",
        "showallbutton",
        null,
        { section: "recentlyclosed" },
      ],
    ]);
  });
});

add_task(async function test_show_all_opentabs_telemetry() {
  for (let i = 0; i < NUMBER_OF_TABS; i++) {
    await BrowserTestUtils.openNewForegroundTab(gBrowser, URLs[1]);
  }
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const recentBrowsing = document.querySelector("view-recentbrowsing");

    info("Input a search query.");
    EventUtils.synthesizeMouseAtCenter(
      recentBrowsing.searchTextbox,
      {},
      content
    );
    EventUtils.sendString(URLs[1], content);
    const opentabsSlot = recentBrowsing.querySelector("[slot='opentabs']");
    await TestUtils.waitForCondition(
      () => opentabsSlot.viewCards[0].tabList.rowEls.length === 5,
      "Expected search results are not shown yet."
    );
    await clearAllParentTelemetryEvents();

    info("Click the Show All link.");
    const showAllButton = opentabsSlot.viewCards[0].shadowRoot.querySelector(
      "[data-l10n-id='firefoxview-show-all']"
    );
    await TestUtils.waitForCondition(() => !showAllButton.hidden);
    ok(!showAllButton.hidden, "Show all button is visible");
    await TestUtils.waitForCondition(() => {
      EventUtils.synthesizeMouseAtCenter(showAllButton, {}, content);
      if (opentabsSlot.viewCards[0].tabList.rowEls.length === NUMBER_OF_TABS) {
        return true;
      }
      return false;
    }, "All search results are not shown.");

    await telemetryEvent([
      [
        "firefoxview_next",
        "search_initiated",
        "search",
        null,
        { page: "recentbrowsing" },
      ],
      [
        "firefoxview_next",
        "search_show_all",
        "showallbutton",
        null,
        { section: "opentabs" },
      ],
    ]);
  });

  await SimpleTest.promiseFocus(window);
  await promiseAllButPrimaryWindowClosed();
  await BrowserTestUtils.switchTab(gBrowser, gInitialTab);
  await closeFirefoxViewTab(window);

  cleanUp();
});

add_task(async function test_show_all_syncedtabs_telemetry() {
  TabsSetupFlowManager.resetInternalState();

  const sandbox = setupRecentDeviceListMocks();
  const syncedTabsMock = sandbox.stub(SyncedTabs, "getRecentTabs");
  let mockTabs1 = getMockTabData(syncedTabsData);
  let getRecentTabsResult = mockTabs1;
  syncedTabsMock.callsFake(() => {
    info(
      `Stubbed SyncedTabs.getRecentTabs returning a promise that resolves to ${getRecentTabsResult.length} tabs\n`
    );
    return Promise.resolve(getRecentTabsResult);
  });
  sandbox.stub(SyncedTabs, "getTabClients").callsFake(() => {
    return Promise.resolve(syncedTabsData);
  });

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    Services.obs.notifyObservers(null, UIState.ON_UPDATE);
    const recentBrowsing = document.querySelector("view-recentbrowsing");

    info("Input a search query.");
    EventUtils.synthesizeMouseAtCenter(
      recentBrowsing.searchTextbox,
      {},
      content
    );
    EventUtils.sendString("mozilla", content);
    const syncedtabsSlot = recentBrowsing.querySelector("[slot='syncedtabs']");
    await TestUtils.waitForCondition(
      () =>
        syncedtabsSlot.fullyUpdated &&
        syncedtabsSlot.tabLists.length === 1 &&
        Promise.all(
          Array.from(syncedtabsSlot.tabLists).map(
            tabList => tabList.updateComplete
          )
        ),
      "Synced Tabs component is done updating."
    );
    syncedtabsSlot.tabLists[0].scrollIntoView();
    await TestUtils.waitForCondition(
      () => syncedtabsSlot.tabLists[0].rowEls.length === 5,
      "Expected search results are not shown yet."
    );
    await clearAllParentTelemetryEvents();

    const showAllButton = await TestUtils.waitForCondition(() =>
      syncedtabsSlot.shadowRoot.querySelector(
        "[data-l10n-id='firefoxview-show-all']"
      )
    );
    info("Scroll show all button into view.");
    showAllButton.scrollIntoView();
    await TestUtils.waitForCondition(() => !showAllButton.hidden);
    ok(!showAllButton.hidden, "Show all button is visible");
    info("Click the Show All link.");
    await TestUtils.waitForCondition(() => {
      EventUtils.synthesizeMouseAtCenter(showAllButton, {}, content);
      if (syncedtabsSlot.tabLists[0].rowEls.length === NUMBER_OF_TABS) {
        return true;
      }
      return false;
    }, "All search results are not shown.");

    await telemetryEvent([
      [
        "firefoxview_next",
        "search_initiated",
        "search",
        null,
        { page: "recentbrowsing" },
      ],
      [
        "firefoxview_next",
        "search_show_all",
        "showallbutton",
        null,
        { section: "syncedtabs" },
      ],
    ]);
  });

  await tearDown(sandbox);
});

add_task(async function test_sort_history_search_telemetry() {
  for (let i = 0; i < NUMBER_OF_TABS; i++) {
    await open_then_close(URLs[i]);
  }

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    await navigateToViewAndWait(document, "history");
    const historyComponent = document.querySelector("view-history");

    const searchTextbox = await TestUtils.waitForCondition(
      () => historyComponent.searchTextbox,
      "The search textbox is displayed."
    );

    info("Input a search query.");
    EventUtils.synthesizeMouseAtCenter(searchTextbox, {}, content);
    EventUtils.sendString("example.com", content);
    await TestUtils.waitForCondition(() => {
      const { rowEls } = historyComponent.lists[0];
      return rowEls.length === 1;
    }, "There is one matching search result.");
    await clearAllParentTelemetryEvents();
    // Select sort by site option
    await EventUtils.synthesizeMouseAtCenter(
      historyComponent.sortInputs[1],
      {},
      content
    );
    await TestUtils.waitForCondition(() => historyComponent.fullyUpdated);
    await telemetryEvent([
      [
        "firefoxview_next",
        "sort_history",
        "tabs",
        null,
        { sort_type: "site", search_start: "true" },
      ],
    ]);
    await clearAllParentTelemetryEvents();

    // Select sort by date option
    await EventUtils.synthesizeMouseAtCenter(
      historyComponent.sortInputs[0],
      {},
      content
    );
    await TestUtils.waitForCondition(() => historyComponent.fullyUpdated);
    await telemetryEvent([
      [
        "firefoxview_next",
        "sort_history",
        "tabs",
        null,
        { sort_type: "date", search_start: "true" },
      ],
    ]);
  });
});

add_task(async function test_cumulative_searches_recent_browsing_telemetry() {
  const cumulativeSearchesHistogram =
    TelemetryTestUtils.getAndClearKeyedHistogram(
      "FIREFOX_VIEW_CUMULATIVE_SEARCHES"
    );
  await PlacesUtils.history.clear();
  await open_then_close(URLs[0]);

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    is(document.location.hash, "", "Searching within recent browsing.");
    const recentBrowsing = document.querySelector("view-recentbrowsing");
    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(
      recentBrowsing.searchTextbox,
      {},
      content
    );
    EventUtils.sendString(URLs[0], content);
    const recentlyclosedSlot = recentBrowsing.querySelector(
      "[slot='recentlyclosed']"
    );
    await TestUtils.waitForCondition(
      () =>
        recentlyclosedSlot?.tabList?.rowEls?.length &&
        recentlyclosedSlot?.searchQuery,
      "Expected search results are not shown yet."
    );

    EventUtils.synthesizeMouseAtCenter(
      recentlyclosedSlot.tabList.rowEls[0].mainEl,
      {},
      content
    );
    await TestUtils.waitForCondition(
      () => "recentbrowsing" in cumulativeSearchesHistogram.snapshot(),
      `recentbrowsing key not found in cumulativeSearchesHistogram snapshot: ${JSON.stringify(
        cumulativeSearchesHistogram.snapshot()
      )}`
    );
    TelemetryTestUtils.assertKeyedHistogramValue(
      cumulativeSearchesHistogram,
      "recentbrowsing",
      1,
      1
    );
  });

  cleanUp();
});

add_task(async function test_cumulative_searches_recently_closed_telemetry() {
  const cumulativeSearchesHistogram =
    TelemetryTestUtils.getAndClearKeyedHistogram(
      "FIREFOX_VIEW_CUMULATIVE_SEARCHES"
    );
  await PlacesUtils.history.clear();
  await open_then_close(URLs[0]);

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    await navigateToViewAndWait(document, "recentlyclosed");
    is(
      document.location.hash,
      "#recentlyclosed",
      "Searching within recently closed."
    );
    const recentlyClosed = document.querySelector(
      "named-deck > view-recentlyclosed"
    );
    const searchTextbox = await TestUtils.waitForCondition(() => {
      return recentlyClosed.searchTextbox;
    });
    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(searchTextbox, {}, content);
    EventUtils.sendString(URLs[0], content);
    // eslint-disable-next-line no-unused-vars
    const [recentlyclosedSlot, tabList] = await waitForRecentlyClosedTabsList(
      document
    );
    await TestUtils.waitForCondition(() => recentlyclosedSlot?.searchQuery);

    await click_recently_closed_tab_item(tabList[0]);

    TelemetryTestUtils.assertKeyedHistogramValue(
      cumulativeSearchesHistogram,
      "recentlyclosed",
      1,
      1
    );
  });

  cleanUp();
});

add_task(async function test_cumulative_searches_open_tabs_telemetry() {
  const cumulativeSearchesHistogram =
    TelemetryTestUtils.getAndClearKeyedHistogram(
      "FIREFOX_VIEW_CUMULATIVE_SEARCHES"
    );
  await PlacesUtils.history.clear();
  await BrowserTestUtils.openNewForegroundTab(gBrowser, URLs[0]);

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    await navigateToViewAndWait(document, "opentabs");
    is(document.location.hash, "#opentabs", "Searching within open tabs.");
    const openTabs = document.querySelector("named-deck > view-opentabs");

    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(openTabs.searchTextbox, {}, content);
    EventUtils.sendString(URLs[0], content);
    let cards;
    await TestUtils.waitForCondition(() => {
      cards = getOpenTabsCards(openTabs);
      return cards.length == 1;
    });
    await TestUtils.waitForCondition(
      () => cards[0].tabList.rowEls.length === 1 && openTabs?.searchQuery,
      "Expected search results are not shown yet."
    );

    info("Click a search result tab");
    EventUtils.synthesizeMouseAtCenter(
      cards[0].tabList.rowEls[0].mainEl,
      {},
      content
    );
  });

  TelemetryTestUtils.assertKeyedHistogramValue(
    cumulativeSearchesHistogram,
    "opentabs",
    1,
    1
  );

  cleanUp();
});

add_task(async function test_cumulative_searches_history_telemetry() {
  const cumulativeSearchesHistogram =
    TelemetryTestUtils.getAndClearKeyedHistogram(
      "FIREFOX_VIEW_CUMULATIVE_SEARCHES"
    );
  await PlacesUtils.history.clear();
  await open_then_close(URLs[0]);

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    await navigateToViewAndWait(document, "history");
    is(document.location.hash, "#history", "Searching within history.");
    const history = document.querySelector("named-deck > view-history");
    const searchTextbox = await TestUtils.waitForCondition(() => {
      return history.searchTextbox;
    });

    info("Input a search query");
    EventUtils.synthesizeMouseAtCenter(searchTextbox, {}, content);
    EventUtils.sendString(URLs[0], content);
    await TestUtils.waitForCondition(
      () =>
        history.fullyUpdated &&
        history?.lists[0].rowEls?.length === 1 &&
        history?.searchQuery,
      "Expected search results are not shown yet."
    );

    info("Click a search result tab");
    EventUtils.synthesizeMouseAtCenter(
      history.lists[0].rowEls[0].mainEl,
      {},
      content
    );

    TelemetryTestUtils.assertKeyedHistogramValue(
      cumulativeSearchesHistogram,
      "history",
      1,
      1
    );
  });

  cleanUp();
});

add_task(async function test_cumulative_searches_syncedtabs_telemetry() {
  const cumulativeSearchesHistogram =
    TelemetryTestUtils.getAndClearKeyedHistogram(
      "FIREFOX_VIEW_CUMULATIVE_SEARCHES"
    );
  await PlacesUtils.history.clear();
  TabsSetupFlowManager.resetInternalState();

  const sandbox = setupRecentDeviceListMocks();
  const syncedTabsMock = sandbox.stub(SyncedTabs, "getRecentTabs");
  let mockTabs1 = getMockTabData(syncedTabsData);
  let getRecentTabsResult = mockTabs1;
  syncedTabsMock.callsFake(() => {
    info(
      `Stubbed SyncedTabs.getRecentTabs returning a promise that resolves to ${getRecentTabsResult.length} tabs\n`
    );
    return Promise.resolve(getRecentTabsResult);
  });
  sandbox.stub(SyncedTabs, "getTabClients").callsFake(() => {
    return Promise.resolve(syncedTabsData);
  });

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    Services.obs.notifyObservers(null, UIState.ON_UPDATE);

    await navigateToViewAndWait(document, "syncedtabs");
    is(document.location.hash, "#syncedtabs", "Searching within synced tabs.");
    let syncedTabs = document.querySelector(
      "view-syncedtabs:not([slot=syncedtabs])"
    );

    info("Input a search query.");
    EventUtils.synthesizeMouseAtCenter(syncedTabs.searchTextbox, {}, content);
    EventUtils.sendString(URLs[0], content);
    await TestUtils.waitForCondition(
      () =>
        syncedTabs.fullyUpdated &&
        syncedTabs.tabLists.length === 1 &&
        Promise.all(
          Array.from(syncedTabs.tabLists).map(tabList => tabList.updateComplete)
        ),
      "Synced Tabs component is done updating."
    );
    await TestUtils.waitForCondition(
      () =>
        syncedTabs.tabLists[0].rowEls.length === 1 && syncedTabs?.searchQuery,
      "Expected search results are not shown yet."
    );

    info("Click a search result tab");
    EventUtils.synthesizeMouseAtCenter(
      syncedTabs.tabLists[0].rowEls[0].mainEl,
      {},
      content
    );

    TelemetryTestUtils.assertKeyedHistogramValue(
      cumulativeSearchesHistogram,
      "syncedtabs",
      1,
      1
    );
  });

  cleanUp();
  await tearDown(sandbox);
});