summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/notificationbox/browser_notificationbar_telemetry.js
blob: 7810d4022d705914421f69e0e39fef92f4d85365 (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
const TELEMETRY_BASE = "notificationbar.";

ChromeUtils.defineESModuleGetters(this, {
  TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs",
});

add_task(async function showNotification() {
  Services.telemetry.clearScalars();

  let tab1 = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.com/"
  );

  ok(!gBrowser.readNotificationBox(), "no notificationbox created yet");

  let box1 = gBrowser.getNotificationBox();

  ok(gBrowser.readNotificationBox(), "notificationbox was created");

  let tab2 = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.org/"
  );

  let tab3 = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/html,<body>Hello</body>"
  );
  let box3 = gBrowser.getNotificationBox();

  verifyTelemetry("initial", 0, 0, 0, 0, 0, 0);

  let notif3 = box3.appendNotification("infobar-testtwo-value", {
    label: "Message for tab 3",
    priority: box3.PRIORITY_INFO_HIGH,
    telemetry: TELEMETRY_BASE + "testtwo",
  });

  verifyTelemetry("first notification", 0, 0, 0, 0, 0, 1);

  let notif1 = box1.appendNotification(
    "infobar-testone-value",
    {
      label: "Message for tab 1",
      priority: box1.PRIORITY_INFO_HIGH,
      telemetry: TELEMETRY_BASE + "testone",
    },
    [
      {
        label: "Button1",
        telemetry: "button1-pressed",
      },
      {
        label: "Button2",
        telemetry: "button2-pressed",
      },
      {
        label: "Button3",
      },
    ]
  );
  verifyTelemetry("second notification", 0, 0, 0, 0, 0, 1);

  await BrowserTestUtils.switchTab(gBrowser, tab1);
  verifyTelemetry("switch to first tab", 1, 0, 0, 0, 0, 1);

  await BrowserTestUtils.switchTab(gBrowser, tab2);
  verifyTelemetry("switch to second tab", 1, 0, 0, 0, 0, 1);

  await BrowserTestUtils.switchTab(gBrowser, tab3);
  verifyTelemetry("switch to third tab", 1, 0, 0, 0, 0, 1);

  await BrowserTestUtils.switchTab(gBrowser, tab1);
  verifyTelemetry("switch to first tab again", 1, 0, 0, 0, 0, 1);

  notif1.buttonContainer.lastElementChild.click();
  verifyTelemetry("press third button", 1, 1, 0, 0, 0, 1);

  notif1.buttonContainer.lastElementChild.previousElementSibling.click();
  verifyTelemetry("press second button", 1, 1, 0, 1, 0, 1);

  notif1.buttonContainer.lastElementChild.previousElementSibling.previousElementSibling.click();
  verifyTelemetry("press first button", 1, 1, 1, 1, 0, 1);

  notif1.dismiss();
  verifyTelemetry("dismiss notification for box 1", 1, 1, 1, 1, 1, 1);

  notif3.dismiss();
  verifyTelemetry("dismiss notification for box 3", 1, 1, 1, 1, 1, 1, 1);

  let notif4 = box1.appendNotification(
    "infobar-testtwo-value",
    {
      label: "Additional message for tab 1",
      priority: box1.PRIORITY_INFO_HIGH,
      telemetry: TELEMETRY_BASE + "testone",
      telemetryFilter: ["shown"],
    },
    [
      {
        label: "Button1",
      },
    ]
  );
  verifyTelemetry("show first filtered notification", 2, 1, 1, 1, 1, 1, 1);

  notif4.buttonContainer.lastElementChild.click();
  notif4.dismiss();
  verifyTelemetry("dismiss first filtered notification", 2, 1, 1, 1, 1, 1, 1);

  let notif5 = box1.appendNotification(
    "infobar-testtwo-value",
    {
      label: "Dimissed additional message for tab 1",
      priority: box1.PRIORITY_INFO_HIGH,
      telemetry: TELEMETRY_BASE + "testone",
      telemetryFilter: ["dismissed"],
    },
    [
      {
        label: "Button1",
      },
    ]
  );
  verifyTelemetry("show second filtered notification", 2, 1, 1, 1, 1, 1, 1);

  notif5.buttonContainer.lastElementChild.click();
  notif5.dismiss();
  verifyTelemetry("dismiss second filtered notification", 2, 1, 1, 1, 2, 1, 1);

  let notif6 = box1.appendNotification(
    "infobar-testtwo-value",
    {
      label: "Dimissed additional message for tab 1",
      priority: box1.PRIORITY_INFO_HIGH,
      telemetry: TELEMETRY_BASE + "testone",
      telemetryFilter: ["button1-pressed", "dismissed"],
    },
    [
      {
        label: "Button1",
        telemetry: "button1-pressed",
      },
    ]
  );
  verifyTelemetry("show third filtered notification", 2, 1, 1, 1, 2, 1, 1);

  notif6.buttonContainer.lastElementChild.click();
  verifyTelemetry(
    "click button in third filtered notification",
    2,
    1,
    2,
    1,
    2,
    1,
    1
  );
  notif6.dismiss();
  verifyTelemetry("dismiss third filtered notification", 2, 1, 2, 1, 3, 1, 1);

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
  BrowserTestUtils.removeTab(tab3);
});

function verify(scalars, scalar, key, expected, exists) {
  scalar = TELEMETRY_BASE + scalar;

  if (expected > 0) {
    TelemetryTestUtils.assertKeyedScalar(scalars, scalar, key, expected);
    return;
  }

  Assert.equal(
    scalar in scalars,
    exists,
    `expected ${scalar} to be ${exists ? "present" : "unset"}`
  );

  if (exists) {
    Assert.ok(
      !(key in scalars[scalar]),
      "expected key " + key + " to be unset"
    );
  }
}

function verifyTelemetry(
  desc,
  box1shown,
  box1action,
  box1button1,
  box1button2,
  box1dismissed,
  box3shown,
  box3dismissed = 0
) {
  let scalars = TelemetryTestUtils.getProcessScalars("parent", true, false);

  info(desc);
  let n1exists =
    box1shown || box1action || box1button1 || box1button2 || box1dismissed;

  verify(scalars, "testone", "shown", box1shown, n1exists);
  verify(scalars, "testone", "action", box1action, n1exists);
  verify(scalars, "testone", "button1-pressed", box1button1, n1exists);
  verify(scalars, "testone", "button2-pressed", box1button2, n1exists);
  verify(scalars, "testone", "dismissed", box1dismissed, n1exists);
  verify(scalars, "testtwo", "shown", box3shown, box3shown || box3dismissed);
  verify(
    scalars,
    "testtwo",
    "dismissed",
    box3dismissed,
    box3shown || box3dismissed
  );
}