Backed out 7 changesets (bug 1845150) for causing dt failures on browser_screenshot_b...
[gecko.git] / browser / base / content / test / notificationbox / browser_notificationbar_telemetry.js
blob7810d4022d705914421f69e0e39fef92f4d85365
1 const TELEMETRY_BASE = "notificationbar.";
3 ChromeUtils.defineESModuleGetters(this, {
4   TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs",
5 });
7 add_task(async function showNotification() {
8   Services.telemetry.clearScalars();
10   let tab1 = await BrowserTestUtils.openNewForegroundTab(
11     gBrowser,
12     "https://example.com/"
13   );
15   ok(!gBrowser.readNotificationBox(), "no notificationbox created yet");
17   let box1 = gBrowser.getNotificationBox();
19   ok(gBrowser.readNotificationBox(), "notificationbox was created");
21   let tab2 = await BrowserTestUtils.openNewForegroundTab(
22     gBrowser,
23     "https://example.org/"
24   );
26   let tab3 = await BrowserTestUtils.openNewForegroundTab(
27     gBrowser,
28     "data:text/html,<body>Hello</body>"
29   );
30   let box3 = gBrowser.getNotificationBox();
32   verifyTelemetry("initial", 0, 0, 0, 0, 0, 0);
34   let notif3 = box3.appendNotification("infobar-testtwo-value", {
35     label: "Message for tab 3",
36     priority: box3.PRIORITY_INFO_HIGH,
37     telemetry: TELEMETRY_BASE + "testtwo",
38   });
40   verifyTelemetry("first notification", 0, 0, 0, 0, 0, 1);
42   let notif1 = box1.appendNotification(
43     "infobar-testone-value",
44     {
45       label: "Message for tab 1",
46       priority: box1.PRIORITY_INFO_HIGH,
47       telemetry: TELEMETRY_BASE + "testone",
48     },
49     [
50       {
51         label: "Button1",
52         telemetry: "button1-pressed",
53       },
54       {
55         label: "Button2",
56         telemetry: "button2-pressed",
57       },
58       {
59         label: "Button3",
60       },
61     ]
62   );
63   verifyTelemetry("second notification", 0, 0, 0, 0, 0, 1);
65   await BrowserTestUtils.switchTab(gBrowser, tab1);
66   verifyTelemetry("switch to first tab", 1, 0, 0, 0, 0, 1);
68   await BrowserTestUtils.switchTab(gBrowser, tab2);
69   verifyTelemetry("switch to second tab", 1, 0, 0, 0, 0, 1);
71   await BrowserTestUtils.switchTab(gBrowser, tab3);
72   verifyTelemetry("switch to third tab", 1, 0, 0, 0, 0, 1);
74   await BrowserTestUtils.switchTab(gBrowser, tab1);
75   verifyTelemetry("switch to first tab again", 1, 0, 0, 0, 0, 1);
77   notif1.buttonContainer.lastElementChild.click();
78   verifyTelemetry("press third button", 1, 1, 0, 0, 0, 1);
80   notif1.buttonContainer.lastElementChild.previousElementSibling.click();
81   verifyTelemetry("press second button", 1, 1, 0, 1, 0, 1);
83   notif1.buttonContainer.lastElementChild.previousElementSibling.previousElementSibling.click();
84   verifyTelemetry("press first button", 1, 1, 1, 1, 0, 1);
86   notif1.dismiss();
87   verifyTelemetry("dismiss notification for box 1", 1, 1, 1, 1, 1, 1);
89   notif3.dismiss();
90   verifyTelemetry("dismiss notification for box 3", 1, 1, 1, 1, 1, 1, 1);
92   let notif4 = box1.appendNotification(
93     "infobar-testtwo-value",
94     {
95       label: "Additional message for tab 1",
96       priority: box1.PRIORITY_INFO_HIGH,
97       telemetry: TELEMETRY_BASE + "testone",
98       telemetryFilter: ["shown"],
99     },
100     [
101       {
102         label: "Button1",
103       },
104     ]
105   );
106   verifyTelemetry("show first filtered notification", 2, 1, 1, 1, 1, 1, 1);
108   notif4.buttonContainer.lastElementChild.click();
109   notif4.dismiss();
110   verifyTelemetry("dismiss first filtered notification", 2, 1, 1, 1, 1, 1, 1);
112   let notif5 = box1.appendNotification(
113     "infobar-testtwo-value",
114     {
115       label: "Dimissed additional message for tab 1",
116       priority: box1.PRIORITY_INFO_HIGH,
117       telemetry: TELEMETRY_BASE + "testone",
118       telemetryFilter: ["dismissed"],
119     },
120     [
121       {
122         label: "Button1",
123       },
124     ]
125   );
126   verifyTelemetry("show second filtered notification", 2, 1, 1, 1, 1, 1, 1);
128   notif5.buttonContainer.lastElementChild.click();
129   notif5.dismiss();
130   verifyTelemetry("dismiss second filtered notification", 2, 1, 1, 1, 2, 1, 1);
132   let notif6 = box1.appendNotification(
133     "infobar-testtwo-value",
134     {
135       label: "Dimissed additional message for tab 1",
136       priority: box1.PRIORITY_INFO_HIGH,
137       telemetry: TELEMETRY_BASE + "testone",
138       telemetryFilter: ["button1-pressed", "dismissed"],
139     },
140     [
141       {
142         label: "Button1",
143         telemetry: "button1-pressed",
144       },
145     ]
146   );
147   verifyTelemetry("show third filtered notification", 2, 1, 1, 1, 2, 1, 1);
149   notif6.buttonContainer.lastElementChild.click();
150   verifyTelemetry(
151     "click button in third filtered notification",
152     2,
153     1,
154     2,
155     1,
156     2,
157     1,
158     1
159   );
160   notif6.dismiss();
161   verifyTelemetry("dismiss third filtered notification", 2, 1, 2, 1, 3, 1, 1);
163   BrowserTestUtils.removeTab(tab1);
164   BrowserTestUtils.removeTab(tab2);
165   BrowserTestUtils.removeTab(tab3);
168 function verify(scalars, scalar, key, expected, exists) {
169   scalar = TELEMETRY_BASE + scalar;
171   if (expected > 0) {
172     TelemetryTestUtils.assertKeyedScalar(scalars, scalar, key, expected);
173     return;
174   }
176   Assert.equal(
177     scalar in scalars,
178     exists,
179     `expected ${scalar} to be ${exists ? "present" : "unset"}`
180   );
182   if (exists) {
183     Assert.ok(
184       !(key in scalars[scalar]),
185       "expected key " + key + " to be unset"
186     );
187   }
190 function verifyTelemetry(
191   desc,
192   box1shown,
193   box1action,
194   box1button1,
195   box1button2,
196   box1dismissed,
197   box3shown,
198   box3dismissed = 0
199 ) {
200   let scalars = TelemetryTestUtils.getProcessScalars("parent", true, false);
202   info(desc);
203   let n1exists =
204     box1shown || box1action || box1button1 || box1button2 || box1dismissed;
206   verify(scalars, "testone", "shown", box1shown, n1exists);
207   verify(scalars, "testone", "action", box1action, n1exists);
208   verify(scalars, "testone", "button1-pressed", box1button1, n1exists);
209   verify(scalars, "testone", "button2-pressed", box1button2, n1exists);
210   verify(scalars, "testone", "dismissed", box1dismissed, n1exists);
211   verify(scalars, "testtwo", "shown", box3shown, box3shown || box3dismissed);
212   verify(
213     scalars,
214     "testtwo",
215     "dismissed",
216     box3dismissed,
217     box3shown || box3dismissed
218   );