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

"use strict";

// Test for the taking timing for the impression telemetry.

add_setup(async function () {
  await setup();
});

add_task(async function cancelImpressionTimerByEngagementEvent() {
  const additionalInterval = 1000;
  const originalInterval = UrlbarPrefs.get(
    "searchEngagementTelemetry.pauseImpressionIntervalMs"
  );
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "browser.urlbar.searchEngagementTelemetry.pauseImpressionIntervalMs",
        originalInterval + additionalInterval,
      ],
    ],
  });

  for (const trigger of [doEnter, doBlur]) {
    await doTest(async browser => {
      await openPopup("https://example.com");
      await trigger();

      // Check whether the impression timer was canceled.
      await new Promise(r =>
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        setTimeout(r, originalInterval + additionalInterval)
      );
      assertImpressionTelemetry([]);
    });
  }

  await SpecialPowers.popPrefEnv();
});

add_task(async function cancelInpressionTimerByType() {
  const originalInterval = UrlbarPrefs.get(
    "searchEngagementTelemetry.pauseImpressionIntervalMs"
  );

  await doTest(async browser => {
    await openPopup("x");
    await new Promise(r =>
      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
      setTimeout(r, originalInterval / 10)
    );
    assertImpressionTelemetry([]);

    EventUtils.synthesizeKey(" ");
    EventUtils.synthesizeKey("z");
    await UrlbarTestUtils.promiseSearchComplete(window);
    assertImpressionTelemetry([]);
    await waitForPauseImpression();

    assertImpressionTelemetry([{ n_chars: 3 }]);
  });
});

add_task(async function oneImpressionInOneSession() {
  await doTest(async browser => {
    await openPopup("x");
    await waitForPauseImpression();

    // Sanity check.
    assertImpressionTelemetry([{ n_chars: 1 }]);

    // Add a keyword to start new query.
    EventUtils.synthesizeKey(" ");
    EventUtils.synthesizeKey("z");
    await UrlbarTestUtils.promiseSearchComplete(window);
    await waitForPauseImpression();

    // No more taking impression telemetry.
    assertImpressionTelemetry([{ n_chars: 1 }]);

    // Finish the current session.
    await doEnter();

    // Should take pause impression since new session started.
    await openPopup("x z y");
    await waitForPauseImpression();
    assertImpressionTelemetry([{ n_chars: 1 }, { n_chars: 5 }]);
  });
});