summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/popupNotifications/browser_popupNotification_security_delay.js
blob: 3c652b26a2ced2fd780a42a33e9b48da12e29b2e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_SECURITY_DELAY = 5000;

SimpleTest.requestCompleteLog();

/**
 * Shows a test PopupNotification.
 */
function showNotification() {
  PopupNotifications.show(
    gBrowser.selectedBrowser,
    "foo",
    "Hello, World!",
    "default-notification-icon",
    {
      label: "ok",
      accessKey: "o",
      callback: () => {},
    },
    [
      {
        label: "cancel",
        accessKey: "c",
        callback: () => {},
      },
    ],
    {
      // Make test notifications persistent to ensure they are only closed
      // explicitly by test actions and survive tab switches.
      persistent: true,
    }
  );
}

add_setup(async function () {
  // Set a longer security delay for PopupNotification actions so we can test
  // the delay even if the test runs slowly.
  await SpecialPowers.pushPrefEnv({
    set: [["security.notification_enable_delay", TEST_SECURITY_DELAY]],
  });
});

/**
 * The security delay calculation in PopupNotification.sys.mjs is dependent on
 * the monotonically increasing value of performance.now. This timestamp is
 * not relative to a fixed date, but to runtime.
 * We need to wait for the value performance.now() to be larger than the
 * security delay in order to observe the bug. Only then does the
 * timeSinceShown check in PopupNotifications.sys.mjs lead to a timeSinceShown
 * value that is unconditionally greater than lazy.buttonDelay for
 * notification.timeShown = null = 0.
 * See: https://searchfox.org/mozilla-central/rev/f32d5f3949a3f4f185122142b29f2e3ab776836e/toolkit/modules/PopupNotifications.sys.mjs#1870-1872
 *
 * When running in automation as part of a larger test suite performance.now()
 * should usually be already sufficiently high in which case this check should
 * directly resolve.
 */
async function ensureSecurityDelayReady() {
  await TestUtils.waitForCondition(
    () => performance.now() > TEST_SECURITY_DELAY,
    "Wait for performance.now() > SECURITY_DELAY",
    500,
    50
  );
}

/**
 * Test helper for security delay tests which performs the following steps:
 * 1. Shows a test notification.
 * 2. Waits for the notification panel to be shown and checks that the initial
 *    security delay blocks clicks.
 * 3. Waits for the security delay to expire. Clicks should now be allowed.
 * 4. Executes the provided onSecurityDelayExpired function. This function
 *    should renew the security delay.
 * 5. Tests that the security delay was renewed.
 * 6. Ensures that after the security delay the notification can be closed.
 *
 * @param {*} options
 * @param {function} options.onSecurityDelayExpired - Function to run after the
 *  security delay has expired. This function should trigger a renew of the
 *  security delay.
 * @param {function} options.cleanupFn - Optional cleanup function to run after
 * the test has completed.
 * @returns {Promise<void>} - Resolves when the test has completed.
 */
async function runPopupNotificationSecurityDelayTest({
  onSecurityDelayExpired,
  cleanupFn = () => {},
}) {
  await ensureSecurityDelayReady();

  info("Open a notification.");
  let popupShownPromise = waitForNotificationPanel();
  showNotification();
  await popupShownPromise;
  ok(
    PopupNotifications.isPanelOpen,
    "PopupNotification should be open after show call."
  );

  // Test that the initial security delay works.
  info(
    "Trigger main action via button click during the initial security delay."
  );
  triggerMainCommand(PopupNotifications.panel);

  await new Promise(resolve => setTimeout(resolve, 0));

  ok(PopupNotifications.isPanelOpen, "PopupNotification should still be open.");
  let notification = PopupNotifications.getNotification(
    "foo",
    gBrowser.selectedBrowser
  );
  ok(
    notification,
    "Notification should still be open because we clicked during the security delay."
  );
  // If the notification is no longer shown (test failure) skip the remaining
  // checks.
  if (!notification) {
    await cleanupFn();
    return;
  }

  info("Wait for security delay to expire.");
  await new Promise(resolve =>
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    setTimeout(resolve, TEST_SECURITY_DELAY + 500)
  );

  info("Run test specific actions which restarts the security delay.");
  await onSecurityDelayExpired();

  info("Trigger main action via button click during the new security delay.");
  triggerMainCommand(PopupNotifications.panel);

  await new Promise(resolve => setTimeout(resolve, 0));

  ok(PopupNotifications.isPanelOpen, "PopupNotification should still be open.");
  notification = PopupNotifications.getNotification(
    "foo",
    gBrowser.selectedBrowser
  );
  ok(
    notification,
    "Notification should still be open because we clicked during the security delay."
  );
  // If the notification is no longer shown (test failure) skip the remaining
  // checks.
  if (!notification) {
    await cleanupFn();
    return;
  }

  // Ensure that once the security delay has passed the notification can be
  // closed again.
  let fakeTimeShown = TEST_SECURITY_DELAY + 500;
  info(`Manually set timeShown to ${fakeTimeShown}ms in the past.`);
  notification.timeShown = performance.now() - fakeTimeShown;

  info("Trigger main action via button click outside security delay");
  let notificationHiddenPromise = waitForNotificationPanelHidden();
  triggerMainCommand(PopupNotifications.panel);

  info("Wait for panel to be hidden.");
  await notificationHiddenPromise;

  ok(
    !PopupNotifications.getNotification("foo", gBrowser.selectedBrowser),
    "Should no longer see the notification."
  );

  info("Cleanup.");
  await cleanupFn();
}

/**
 * Tests that when we show a second notification while the panel is open the
 * timeShown attribute is correctly set and the security delay is enforced
 * properly.
 */
add_task(async function test_timeShownMultipleNotifications() {
  await ensureSecurityDelayReady();

  ok(
    !PopupNotifications.isPanelOpen,
    "PopupNotification panel should not be open initially."
  );

  info("Open the first notification.");
  let popupShownPromise = waitForNotificationPanel();
  showNotification();
  await popupShownPromise;
  ok(
    PopupNotifications.isPanelOpen,
    "PopupNotification should be open after first show call."
  );

  is(
    PopupNotifications._currentNotifications.length,
    1,
    "There should only be one notification"
  );

  let notification = PopupNotifications.getNotification(
    "foo",
    gBrowser.selectedBrowser
  );
  is(notification?.id, "foo", "There should be a notification with id foo");
  ok(notification.timeShown, "The notification should have timeShown set");

  info(
    "Call show again with the same notification id while the PopupNotification panel is still open."
  );
  showNotification();
  ok(
    PopupNotifications.isPanelOpen,
    "PopupNotification should still open after second show call."
  );
  notification = PopupNotifications.getNotification(
    "foo",
    gBrowser.selectedBrowser
  );
  is(
    PopupNotifications._currentNotifications.length,
    1,
    "There should still only be one notification"
  );

  is(
    notification?.id,
    "foo",
    "There should still be a notification with id foo"
  );
  ok(notification.timeShown, "The notification should have timeShown set");

  let notificationHiddenPromise = waitForNotificationPanelHidden();

  info("Trigger main action via button click during security delay");

  // Wait for a tick of the event loop to ensure the button we're clicking
  // has been slotted into moz-button-group
  await new Promise(resolve => setTimeout(resolve, 0));

  triggerMainCommand(PopupNotifications.panel);

  await new Promise(resolve => setTimeout(resolve, 0));

  ok(PopupNotifications.isPanelOpen, "PopupNotification should still be open.");
  notification = PopupNotifications.getNotification(
    "foo",
    gBrowser.selectedBrowser
  );
  ok(
    notification,
    "Notification should still be open because we clicked during the security delay."
  );

  // If the notification is no longer shown (test failure) skip the remaining
  // checks.
  if (!notification) {
    return;
  }

  // Ensure that once the security delay has passed the notification can be
  // closed again.
  let fakeTimeShown = TEST_SECURITY_DELAY + 500;
  info(`Manually set timeShown to ${fakeTimeShown}ms in the past.`);
  notification.timeShown = performance.now() - fakeTimeShown;

  info("Trigger main action via button click outside security delay");
  triggerMainCommand(PopupNotifications.panel);

  info("Wait for panel to be hidden.");
  await notificationHiddenPromise;

  ok(
    !PopupNotifications.getNotification("foo", gBrowser.selectedBrowser),
    "Should no longer see the notification."
  );
});

/**
 * Tests that when we reshow a notification after a tab switch the timeShown
 * attribute is correctly reset and the security delay is enforced.
 */
add_task(async function test_notificationReshowTabSwitch() {
  await runPopupNotificationSecurityDelayTest({
    onSecurityDelayExpired: async () => {
      let panelHiddenPromise = waitForNotificationPanelHidden();
      let panelShownPromise;

      info("Open a new tab which hides the notification panel.");
      await BrowserTestUtils.withNewTab("https://example.com", async () => {
        info("Wait for panel to be hidden by tab switch.");
        await panelHiddenPromise;
        panelShownPromise = waitForNotificationPanel();
      });
      info(
        "Wait for the panel to show again after the tab close. We're showing the original tab again."
      );
      await panelShownPromise;

      ok(
        PopupNotifications.isPanelOpen,
        "PopupNotification should be shown after tab close."
      );
      let notification = PopupNotifications.getNotification(
        "foo",
        gBrowser.selectedBrowser
      );
      is(
        notification?.id,
        "foo",
        "There should still be a notification with id foo"
      );

      info(
        "Because we re-show the panel after tab close / switch the security delay should have reset."
      );
    },
  });
});

/**
 * Tests that the security delay gets reset when a window is repositioned and
 * the PopupNotifications panel position is updated.
 */
add_task(async function test_notificationWindowMove() {
  let screenX, screenY;

  await runPopupNotificationSecurityDelayTest({
    onSecurityDelayExpired: async () => {
      info("Reposition the window");
      // Remember original window position.
      screenX = window.screenX;
      screenY = window.screenY;

      let promisePopupPositioned = BrowserTestUtils.waitForEvent(
        PopupNotifications.panel,
        "popuppositioned"
      );

      // Move the window.
      window.moveTo(200, 200);

      // Wait for the panel to reposition and the PopupNotifications listener to run.
      await promisePopupPositioned;
      await new Promise(resolve => setTimeout(resolve, 0));
    },
    cleanupFn: async () => {
      // Reset window position
      window.moveTo(screenX, screenY);
    },
  });
});

/**
 * Tests that the security delay gets extended if a notification is shown during
 * a full screen transition.
 */
add_task(async function test_notificationDuringFullScreenTransition() {
  // Log full screen transition messages.
  let loggingObserver = {
    observe(subject, topic) {
      info("Observed topic: " + topic);
    },
  };
  Services.obs.addObserver(loggingObserver, "fullscreen-transition-start");
  Services.obs.addObserver(loggingObserver, "fullscreen-transition-end");
  // Unregister observers when the test ends:
  registerCleanupFunction(() => {
    Services.obs.removeObserver(loggingObserver, "fullscreen-transition-start");
    Services.obs.removeObserver(loggingObserver, "fullscreen-transition-end");
  });

  if (Services.appinfo.OS == "Linux") {
    ok(
      "Skipping test on Linux because of disabled full screen transition in CI."
    );
    return;
  }
  // Bug 1882527: Intermittent failures on macOS.
  if (Services.appinfo.OS == "Darwin") {
    ok("Skipping test on macOS because of intermittent failures.");
    return;
  }

  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
    await SpecialPowers.pushPrefEnv({
      set: [
        // Set a short security delay so we can observe it being extended.
        ["security.notification_enable_delay", 1],
        // Set a longer full screen exit transition so the test works on slow builds.
        ["full-screen-api.transition-duration.leave", "1000 1000"],
        // Waive the user activation requirement for full screen requests.
        // The PoC this test is based on relies on spam clicking which grants
        // user activation in the popup that requests full screen.
        // This isn't reliable in automation.
        ["full-screen-api.allow-trusted-requests-only", false],
        // macOS native full screen is not affected by the full screen
        // transition overlap. Test with the old full screen implementation.
        ["full-screen-api.macos-native-full-screen", false],
      ],
    });

    await ensureSecurityDelayReady();

    ok(
      !PopupNotifications.isPanelOpen,
      "PopupNotification panel should not be open initially."
    );

    info("Open a notification.");
    let popupShownPromise = waitForNotificationPanel();
    showNotification();
    await popupShownPromise;
    ok(
      PopupNotifications.isPanelOpen,
      "PopupNotification should be open after show call."
    );

    let notification = PopupNotifications.getNotification("foo", browser);
    is(notification?.id, "foo", "There should be a notification with id foo");

    info(
      "Open a new tab via window.open, enter full screen and remove the tab."
    );

    // There are two transitions, one for full screen entry and one for full screen exit.
    let transitionStartCount = 0;
    let transitionEndCount = 0;
    let promiseFullScreenTransitionStart = TestUtils.topicObserved(
      "fullscreen-transition-start",
      () => {
        transitionStartCount++;
        return transitionStartCount == 2;
      }
    );
    let promiseFullScreenTransitionEnd = TestUtils.topicObserved(
      "fullscreen-transition-end",
      () => {
        transitionEndCount++;
        return transitionEndCount == 2;
      }
    );
    let notificationShownPromise = waitForNotificationPanel();

    await SpecialPowers.spawn(browser, [], () => {
      // Use eval to execute in the privilege context of the website.
      content.eval(`
           let button = document.createElement("button");
           button.id = "triggerBtn";
           button.innerText = "Open Popup";
           button.addEventListener("click", () => {
             let popup = window.open("about:blank");
             popup.document.write(
               "<script>setTimeout(() => document.documentElement.requestFullscreen(), 500)</script>"
             );
             popup.document.write(
               "<script>setTimeout(() => window.close(), 1500)</script>"
             );
           });
           // Insert button at the top so the synthesized click works. Otherwise
           // the button may be outside of the viewport.
           document.body.prepend(button);
         `);
    });

    let timeClick = performance.now();
    await BrowserTestUtils.synthesizeMouseAtCenter("#triggerBtn", {}, browser);

    info("Wait for the exit transition to start. It's the second transition.");
    await promiseFullScreenTransitionStart;
    info("Full screen transition start");
    ok(true, "Full screen transition started");
    ok(
      window.isInFullScreenTransition,
      "Full screen transition is still running."
    );

    info(
      "Wait for notification to re-show on tab switch, after the popup has been closed"
    );
    await notificationShownPromise;
    ok(
      window.isInFullScreenTransition,
      "Full screen transition is still running."
    );
    info(
      "about to trigger notification. time between btn click and notification show: " +
        (performance.now() - timeClick)
    );

    info(
      "Trigger main action via button click during the extended security delay."
    );
    triggerMainCommand(PopupNotifications.panel);

    await new Promise(resolve => setTimeout(resolve, 0));

    ok(
      PopupNotifications.isPanelOpen,
      "PopupNotification should still be open."
    );
    notification = PopupNotifications.getNotification(
      "foo",
      gBrowser.selectedBrowser
    );
    ok(
      notification,
      "Notification should still be open because we clicked during the security delay."
    );

    info("Wait for full screen transition end.");
    await promiseFullScreenTransitionEnd;
    info("Full screen transition end");

    await SpecialPowers.popPrefEnv();
  });
});

/**
 * Tests that the security delay gets extended when pointer lock is entered.
 */
add_task(async function test_notificationPointerLock() {
  // We need a tab to enter pointer lock.
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.com"
  );

  await runPopupNotificationSecurityDelayTest({
    onSecurityDelayExpired: async () => {
      info("Enter pointer lock");
      // Move focus to the browser to ensure pointer lock request succeeds.
      gBrowser.selectedBrowser.focus();
      let pointerLockEnterPromise = TestUtils.topicObserved(
        "pointer-lock-entered"
      );
      await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
        SpecialPowers.wrap(content.document).notifyUserGestureActivation();
        await content.document.body.requestPointerLock();
      });

      // Wait for pointer lock to be entered and the PopupNotifications listener to run.
      await pointerLockEnterPromise;
      await new Promise(resolve => setTimeout(resolve, 0));
    },
    cleanupFn: async () => {
      // Cleanup.
      await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
        SpecialPowers.wrap(content.document).notifyUserGestureActivation();
        await content.document.exitPointerLock();
      });
      await TestUtils.waitForCondition(
        () => !window.PointerLock.isActive,
        "Wait for pointer lock exit."
      );
      BrowserTestUtils.removeTab(tab);
    },
  });
});