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

/**
 * This file tests urlbar telemetry for quickactions.
 */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  UrlbarProviderQuickActions:
    "resource:///modules/UrlbarProviderQuickActions.sys.mjs",
});

let testActionCalled = 0;

add_setup(async function setup() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.suggest.quickactions", true],
      ["browser.urlbar.quickactions.enabled", true],
    ],
  });

  UrlbarProviderQuickActions.addAction("testaction", {
    commands: ["testaction"],
    label: "quickactions-downloads2",
    onPick: () => testActionCalled++,
  });

  registerCleanupFunction(() => {
    UrlbarProviderQuickActions.removeAction("testaction");
  });
});

add_task(async function test() {
  const histograms = snapshotHistograms();

  // Do a search to show the quickaction.
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "testaction",
    waitForFocus,
    fireInputEvent: true,
  });

  await UrlbarTestUtils.promisePopupClose(window, () => {
    EventUtils.synthesizeKey("KEY_ArrowDown");
    EventUtils.synthesizeKey("KEY_Enter");
  });

  Assert.equal(testActionCalled, 1, "Test action was called");

  TelemetryTestUtils.assertHistogram(
    histograms.resultMethodHist,
    UrlbarTestUtils.SELECTED_RESULT_METHODS.arrowEnterSelection,
    1
  );

  let scalars = TelemetryTestUtils.getProcessScalars("parent", true, true);
  TelemetryTestUtils.assertKeyedScalar(
    scalars,
    `urlbar.picked.quickaction`,
    1,
    1
  );

  TelemetryTestUtils.assertKeyedScalar(
    scalars,
    "quickaction.picked",
    "testaction-10",
    1
  );

  TelemetryTestUtils.assertKeyedScalar(
    scalars,
    "quickaction.impression",
    "testaction-10",
    1
  );

  // Clean up for subsequent tests.
  gURLBar.handleRevert();
});

add_task(async function test_impressions() {
  UrlbarProviderQuickActions.addAction("testaction2", {
    commands: ["testaction2"],
    label: "quickactions-downloads2",
    onPick: () => {},
  });

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "testaction",
    waitForFocus,
    fireInputEvent: true,
  });

  await UrlbarTestUtils.promisePopupClose(window, () => {
    EventUtils.synthesizeKey("KEY_ArrowDown");
    EventUtils.synthesizeKey("KEY_Enter");
  });

  let scalars = TelemetryTestUtils.getProcessScalars("parent", true, true);

  TelemetryTestUtils.assertKeyedScalar(
    scalars,
    "quickaction.impression",
    `testaction-10`,
    1
  );
  TelemetryTestUtils.assertKeyedScalar(
    scalars,
    "quickaction.impression",
    `testaction2-10`,
    1
  );

  UrlbarProviderQuickActions.removeAction("testaction2");
  gURLBar.handleRevert();
});

function snapshotHistograms() {
  Services.telemetry.clearScalars();
  Services.telemetry.clearEvents();
  return {
    resultMethodHist: TelemetryTestUtils.getAndClearHistogram(
      "FX_URLBAR_SELECTED_RESULT_METHOD"
    ),
  };
}