summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_audioTabIcon.js
blob: 3e3db58f06ddeca6edeca74bfc047cb62876a02b (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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/* eslint-disable mozilla/no-arbitrary-setTimeout */
const PAGE =
  "https://example.com/browser/browser/base/content/test/tabs/file_mediaPlayback.html";
const TABATTR_REMOVAL_PREFNAME = "browser.tabs.delayHidingAudioPlayingIconMS";
const INITIAL_TABATTR_REMOVAL_DELAY_MS = Services.prefs.getIntPref(
  TABATTR_REMOVAL_PREFNAME
);

async function pause(tab, options) {
  let extendedDelay = options && options.extendedDelay;
  if (extendedDelay) {
    // Use 10s to remove possibility of race condition with attr removal.
    Services.prefs.setIntPref(TABATTR_REMOVAL_PREFNAME, 10000);
  }

  try {
    let browser = tab.linkedBrowser;
    let awaitDOMAudioPlaybackStopped;
    if (!browser.audioMuted) {
      awaitDOMAudioPlaybackStopped = BrowserTestUtils.waitForEvent(
        browser,
        "DOMAudioPlaybackStopped",
        "DOMAudioPlaybackStopped event should get fired after pause"
      );
    }
    await SpecialPowers.spawn(browser, [], async function () {
      let audio = content.document.querySelector("audio");
      audio.pause();
    });

    // If the tab has already be muted, it means the tab won't have soundplaying,
    // so we don't need to check this attribute.
    if (browser.audioMuted) {
      return;
    }

    if (extendedDelay) {
      ok(
        tab.hasAttribute("soundplaying"),
        "The tab should still have the soundplaying attribute immediately after pausing"
      );

      await awaitDOMAudioPlaybackStopped;
      ok(
        tab.hasAttribute("soundplaying"),
        "The tab should still have the soundplaying attribute immediately after DOMAudioPlaybackStopped"
      );
    }

    await wait_for_tab_playing_event(tab, false);
    ok(
      !tab.hasAttribute("soundplaying"),
      "The tab should not have the soundplaying attribute after the timeout has resolved"
    );
  } finally {
    // Make sure other tests don't timeout if an exception gets thrown above.
    // Need to use setIntPref instead of clearUserPref because
    // testing/profiles/common/user.js overrides the default value to help this and
    // other tests run faster.
    Services.prefs.setIntPref(
      TABATTR_REMOVAL_PREFNAME,
      INITIAL_TABATTR_REMOVAL_DELAY_MS
    );
  }
}

async function hide_tab(tab) {
  let tabHidden = BrowserTestUtils.waitForEvent(tab, "TabHide");
  gBrowser.hideTab(tab);
  return tabHidden;
}

async function show_tab(tab) {
  let tabShown = BrowserTestUtils.waitForEvent(tab, "TabShow");
  gBrowser.showTab(tab);
  return tabShown;
}

async function test_tooltip(icon, expectedTooltip, isActiveTab, tab) {
  let tooltip = document.getElementById("tabbrowser-tab-tooltip");

  let tabContent = tab.querySelector(".tab-content");
  await hover_icon(tabContent, tooltip);

  await hover_icon(icon, tooltip);
  if (isActiveTab) {
    // The active tab should have the keybinding shortcut in the tooltip.
    // We check this by ensuring that the strings are not equal but the expected
    // message appears in the beginning.
    isnot(
      tooltip.getAttribute("label"),
      expectedTooltip,
      "Tooltips should not be equal"
    );
    is(
      tooltip.getAttribute("label").indexOf(expectedTooltip),
      0,
      "Correct tooltip expected"
    );
  } else {
    is(
      tooltip.getAttribute("label"),
      expectedTooltip,
      "Tooltips should not be equal"
    );
  }
  leave_icon(icon);
}

function get_tab_state(tab) {
  return JSON.parse(SessionStore.getTabState(tab));
}

async function test_muting_using_menu(tab, expectMuted) {
  // Show the popup menu
  let contextMenu = document.getElementById("tabContextMenu");
  let popupShownPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popupshown"
  );
  EventUtils.synthesizeMouseAtCenter(tab, { type: "contextmenu", button: 2 });
  await popupShownPromise;

  // Check the menu
  let expectedLabel = expectMuted ? "Unmute Tab" : "Mute Tab";
  let expectedAccessKey = expectMuted ? "m" : "M";
  let toggleMute = document.getElementById("context_toggleMuteTab");
  is(toggleMute.label, expectedLabel, "Correct label expected");
  is(toggleMute.accessKey, expectedAccessKey, "Correct accessKey expected");

  is(
    toggleMute.hasAttribute("muted"),
    expectMuted,
    "Should have the correct state for the muted attribute"
  );
  ok(
    !toggleMute.hasAttribute("soundplaying"),
    "Should not have the soundplaying attribute"
  );

  await play(tab);

  is(
    toggleMute.hasAttribute("muted"),
    expectMuted,
    "Should have the correct state for the muted attribute"
  );
  is(
    !toggleMute.hasAttribute("soundplaying"),
    expectMuted,
    "The value of soundplaying attribute is incorrect"
  );

  await pause(tab);

  is(
    toggleMute.hasAttribute("muted"),
    expectMuted,
    "Should have the correct state for the muted attribute"
  );
  ok(
    !toggleMute.hasAttribute("soundplaying"),
    "Should not have the soundplaying attribute"
  );

  // Click on the menu and wait for the tab to be muted.
  let mutedPromise = get_wait_for_mute_promise(tab, !expectMuted);
  let popupHiddenPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popuphidden"
  );
  contextMenu.activateItem(toggleMute);
  await popupHiddenPromise;
  await mutedPromise;
}

async function test_playing_icon_on_tab(tab, browser, isPinned) {
  let icon = isPinned ? tab.overlayIcon : tab.overlayIcon;
  let isActiveTab = tab === gBrowser.selectedTab;

  await play(tab);

  await test_tooltip(icon, "Mute tab", isActiveTab, tab);

  ok(
    !("muted" in get_tab_state(tab)),
    "No muted attribute should be persisted"
  );
  ok(
    !("muteReason" in get_tab_state(tab)),
    "No muteReason property should be persisted"
  );

  await test_mute_tab(tab, icon, true);

  ok("muted" in get_tab_state(tab), "Muted attribute should be persisted");
  ok(
    "muteReason" in get_tab_state(tab),
    "muteReason property should be persisted"
  );

  await test_tooltip(icon, "Unmute tab", isActiveTab, tab);

  await test_mute_tab(tab, icon, false);

  ok(
    !("muted" in get_tab_state(tab)),
    "No muted attribute should be persisted"
  );
  ok(
    !("muteReason" in get_tab_state(tab)),
    "No muteReason property should be persisted"
  );

  await test_tooltip(icon, "Mute tab", isActiveTab, tab);

  await test_mute_tab(tab, icon, true);

  await pause(tab);

  ok(
    tab.hasAttribute("muted") && !tab.hasAttribute("soundplaying"),
    "Tab should still be muted but not playing"
  );
  ok(
    tab.muted && !tab.soundPlaying,
    "Tab should still be muted but not playing"
  );

  await test_tooltip(icon, "Unmute tab", isActiveTab, tab);

  await test_mute_tab(tab, icon, false);

  ok(
    !tab.hasAttribute("muted") && !tab.hasAttribute("soundplaying"),
    "Tab should not be be muted or playing"
  );
  ok(!tab.muted && !tab.soundPlaying, "Tab should not be be muted or playing");

  // Make sure it's possible to mute using the context menu.
  await test_muting_using_menu(tab, false);

  // Make sure it's possible to unmute using the context menu.
  await test_muting_using_menu(tab, true);
}

async function test_playing_icon_on_hidden_tab(tab) {
  let oldSelectedTab = gBrowser.selectedTab;
  let otherTabs = [
    await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE, true, true),
    await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE, true, true),
  ];
  let tabContainer = tab.container;
  let alltabsButton = document.getElementById("alltabs-button");
  let alltabsBadge = alltabsButton.badgeLabel;

  function assertIconShowing() {
    is(
      getComputedStyle(alltabsBadge).backgroundImage,
      'url("chrome://browser/skin/tabbrowser/tab-audio-playing-small.svg")',
      "The audio playing icon is shown"
    );
    is(
      tabContainer.getAttribute("hiddensoundplaying"),
      "true",
      "There are hidden audio tabs"
    );
  }

  function assertIconHidden() {
    is(
      getComputedStyle(alltabsBadge).backgroundImage,
      "none",
      "The audio playing icon is hidden"
    );
    ok(
      !tabContainer.hasAttribute("hiddensoundplaying"),
      "There are no hidden audio tabs"
    );
  }

  // Keep the passed in tab selected.
  gBrowser.selectedTab = tab;

  // Play sound in the other two (visible) tabs.
  await play(otherTabs[0]);
  await play(otherTabs[1]);
  assertIconHidden();

  // Hide one of the noisy tabs, we see the icon.
  await hide_tab(otherTabs[0]);
  assertIconShowing();

  // Hiding the other tab keeps the icon.
  await hide_tab(otherTabs[1]);
  assertIconShowing();

  // Pausing both tabs will hide the icon.
  await pause(otherTabs[0]);
  assertIconShowing();
  await pause(otherTabs[1]);
  assertIconHidden();

  // The icon returns when audio starts again.
  await play(otherTabs[0]);
  await play(otherTabs[1]);
  assertIconShowing();

  // There is still an icon after hiding one tab.
  await show_tab(otherTabs[0]);
  assertIconShowing();

  // The icon is hidden when both of the tabs are shown.
  await show_tab(otherTabs[1]);
  assertIconHidden();

  await BrowserTestUtils.removeTab(otherTabs[0]);
  await BrowserTestUtils.removeTab(otherTabs[1]);

  // Make sure we didn't change the selected tab.
  gBrowser.selectedTab = oldSelectedTab;
}

async function test_swapped_browser_while_playing(oldTab, newBrowser) {
  // The tab was muted so it won't have soundplaying attribute even it's playing.
  ok(
    oldTab.hasAttribute("muted"),
    "Expected the correct muted attribute on the old tab"
  );
  is(
    oldTab.muteReason,
    null,
    "Expected the correct muteReason attribute on the old tab"
  );
  ok(
    !oldTab.hasAttribute("soundplaying"),
    "Expected the correct soundplaying attribute on the old tab"
  );

  let newTab = gBrowser.getTabForBrowser(newBrowser);
  let AttrChangePromise = BrowserTestUtils.waitForEvent(
    newTab,
    "TabAttrModified",
    false,
    event => {
      return event.detail.changed.includes("muted");
    }
  );

  gBrowser.swapBrowsersAndCloseOther(newTab, oldTab);
  await AttrChangePromise;

  ok(
    newTab.hasAttribute("muted"),
    "Expected the correct muted attribute on the new tab"
  );
  is(
    newTab.muteReason,
    null,
    "Expected the correct muteReason property on the new tab"
  );
  ok(
    !newTab.hasAttribute("soundplaying"),
    "Expected the correct soundplaying attribute on the new tab"
  );

  await test_tooltip(newTab.overlayIcon, "Unmute tab", true, newTab);
}

async function test_swapped_browser_while_not_playing(oldTab, newBrowser) {
  ok(
    oldTab.hasAttribute("muted"),
    "Expected the correct muted attribute on the old tab"
  );
  is(
    oldTab.muteReason,
    null,
    "Expected the correct muteReason property on the old tab"
  );
  ok(
    !oldTab.hasAttribute("soundplaying"),
    "Expected the correct soundplaying attribute on the old tab"
  );

  let newTab = gBrowser.getTabForBrowser(newBrowser);
  let AttrChangePromise = BrowserTestUtils.waitForEvent(
    newTab,
    "TabAttrModified",
    false,
    event => {
      return event.detail.changed.includes("muted");
    }
  );

  let AudioPlaybackPromise = new Promise(resolve => {
    let observer = (subject, topic, data) => {
      ok(false, "Should not see an audio-playback notification");
    };
    Services.obs.addObserver(observer, "audio-playback");
    setTimeout(() => {
      Services.obs.removeObserver(observer, "audio-playback");
      resolve();
    }, 100);
  });

  gBrowser.swapBrowsersAndCloseOther(newTab, oldTab);
  await AttrChangePromise;

  ok(
    newTab.hasAttribute("muted"),
    "Expected the correct muted attribute on the new tab"
  );
  is(
    newTab.muteReason,
    null,
    "Expected the correct muteReason property on the new tab"
  );
  ok(
    !newTab.hasAttribute("soundplaying"),
    "Expected the correct soundplaying attribute on the new tab"
  );

  // Wait to see if an audio-playback event is dispatched.
  await AudioPlaybackPromise;

  ok(
    newTab.hasAttribute("muted"),
    "Expected the correct muted attribute on the new tab"
  );
  is(
    newTab.muteReason,
    null,
    "Expected the correct muteReason property on the new tab"
  );
  ok(
    !newTab.hasAttribute("soundplaying"),
    "Expected the correct soundplaying attribute on the new tab"
  );

  await test_tooltip(newTab.overlayIcon, "Unmute tab", true, newTab);
}

async function test_browser_swapping(tab, browser) {
  // First, test swapping with a playing but muted tab.
  await play(tab);

  await test_mute_tab(tab, tab.overlayIcon, true);

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:blank",
    },
    async function (newBrowser) {
      await test_swapped_browser_while_playing(tab, newBrowser);

      // Now, test swapping with a muted but not playing tab.
      // Note that the tab remains muted, so we only need to pause playback.
      tab = gBrowser.getTabForBrowser(newBrowser);
      await pause(tab);

      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: "about:blank",
        },
        secondAboutBlankBrowser =>
          test_swapped_browser_while_not_playing(tab, secondAboutBlankBrowser)
      );
    }
  );
}

async function test_click_on_pinned_tab_after_mute() {
  async function taskFn(browser) {
    let tab = gBrowser.getTabForBrowser(browser);

    gBrowser.selectedTab = originallySelectedTab;
    isnot(
      tab,
      gBrowser.selectedTab,
      "Sanity check, the tab should not be selected!"
    );

    // Steps to reproduce the bug:
    //   Pin the tab.
    gBrowser.pinTab(tab);

    //   Start playback and wait for it to finish.
    await play(tab);

    //   Mute the tab.
    let icon = tab.overlayIcon;
    await test_mute_tab(tab, icon, true);

    // Pause playback and wait for it to finish.
    await pause(tab);

    // Unmute tab.
    await test_mute_tab(tab, icon, false);

    // Now click on the tab.
    EventUtils.synthesizeMouseAtCenter(tab.iconImage, { button: 0 });

    is(tab, gBrowser.selectedTab, "Tab switch should be successful");

    // Cleanup.
    gBrowser.unpinTab(tab);
    gBrowser.selectedTab = originallySelectedTab;
  }

  let originallySelectedTab = gBrowser.selectedTab;

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: PAGE,
    },
    taskFn
  );
}

// This test only does something useful in e10s!
async function test_cross_process_load() {
  async function taskFn(browser) {
    let tab = gBrowser.getTabForBrowser(browser);

    //   Start playback and wait for it to finish.
    await play(tab);

    let soundPlayingStoppedPromise = BrowserTestUtils.waitForEvent(
      tab,
      "TabAttrModified",
      false,
      event => event.detail.changed.includes("soundplaying")
    );

    // Go to a different process.
    BrowserTestUtils.loadURIString(browser, "about:mozilla");
    await BrowserTestUtils.browserLoaded(browser);

    await soundPlayingStoppedPromise;

    ok(
      !tab.hasAttribute("soundplaying"),
      "Tab should not be playing sound any more"
    );
    ok(!tab.soundPlaying, "Tab should not be playing sound any more");
  }

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: PAGE,
    },
    taskFn
  );
}

async function test_mute_keybinding() {
  async function test_muting_using_keyboard(tab) {
    let mutedPromise = get_wait_for_mute_promise(tab, true);
    EventUtils.synthesizeKey("m", { ctrlKey: true });
    await mutedPromise;
    mutedPromise = get_wait_for_mute_promise(tab, false);
    EventUtils.synthesizeKey("m", { ctrlKey: true });
    await mutedPromise;
  }
  async function taskFn(browser) {
    let tab = gBrowser.getTabForBrowser(browser);

    // Make sure it's possible to mute before the tab is playing.
    await test_muting_using_keyboard(tab);

    //   Start playback and wait for it to finish.
    await play(tab);

    // Make sure it's possible to mute after the tab is playing.
    await test_muting_using_keyboard(tab);

    // Pause playback and wait for it to finish.
    await pause(tab);

    // Make sure things work if the tab is pinned.
    gBrowser.pinTab(tab);

    // Make sure it's possible to mute before the tab is playing.
    await test_muting_using_keyboard(tab);

    //   Start playback and wait for it to finish.
    await play(tab);

    // Make sure it's possible to mute after the tab is playing.
    await test_muting_using_keyboard(tab);

    gBrowser.unpinTab(tab);
  }

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: PAGE,
    },
    taskFn
  );
}

async function test_on_browser(browser) {
  let tab = gBrowser.getTabForBrowser(browser);

  // Test the icon in a normal tab.
  await test_playing_icon_on_tab(tab, browser, false);

  gBrowser.pinTab(tab);

  // Test the icon in a pinned tab.
  await test_playing_icon_on_tab(tab, browser, true);

  gBrowser.unpinTab(tab);

  // Test the sound playing icon for hidden tabs.
  await test_playing_icon_on_hidden_tab(tab);

  // Retest with another browser in the foreground tab
  if (gBrowser.selectedBrowser.currentURI.spec == PAGE) {
    await BrowserTestUtils.withNewTab(
      {
        gBrowser,
        url: "data:text/html,test",
      },
      () => test_on_browser(browser)
    );
  } else {
    await test_browser_swapping(tab, browser);
  }
}

async function test_delayed_tabattr_removal() {
  async function taskFn(browser) {
    let tab = gBrowser.getTabForBrowser(browser);
    await play(tab);

    // Extend the delay to guarantee the soundplaying attribute
    // is not removed from the tab when audio is stopped. Without
    // the extended delay the attribute could be removed in the
    // same tick and the test wouldn't catch that this broke.
    await pause(tab, { extendedDelay: true });
  }

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: PAGE,
    },
    taskFn
  );
}

requestLongerTimeout(2);
add_task(async function test_page() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: PAGE,
    },
    test_on_browser
  );
});

add_task(test_click_on_pinned_tab_after_mute);

add_task(test_cross_process_load);

add_task(test_mute_keybinding);

add_task(test_delayed_tabattr_removal);