summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_multiselect_tabs_play.js
blob: 281ed50c1b36769fdd69febf075ab752b0d8e223 (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Ensure multiselected tabs that are active media blocked act correctly
 * when we try to unblock them using the "Play Tabs" icon or by calling
 * resumeDelayedMediaOnMultiSelectedTabs()
 */

"use strict";

const PREF_DELAY_AUTOPLAY = "media.block-autoplay-until-in-foreground";

add_task(async function setPref() {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_DELAY_AUTOPLAY, true]],
  });
});

/*
 * Playing blocked media will not mute the selected tabs
 */
add_task(async function testDelayPlayWontAffectUnmuteStatus() {
  info("Add media tabs");
  let tab0 = await addMediaTab();
  let tab1 = await addMediaTab();

  info("Play both tabs");
  await play(tab0, false);
  await play(tab1, false);

  info("Multiselect tabs");
  await triggerClickOn(tab1, { shiftKey: true });

  // Check multiselection
  ok(tab0.multiselected, "tab0 is multiselected");
  ok(tab1.multiselected, "tab1 is multiselected");

  // Check tabs are unmuted
  ok(!muted(tab0), "Tab0 is unmuted");
  ok(!muted(tab1), "Tab1 is unmuted");

  let tab0BlockPromise = wait_for_tab_media_blocked_event(tab0, false);
  let tab1BlockPromise = wait_for_tab_media_blocked_event(tab1, false);

  // Play media on selected tabs
  gBrowser.resumeDelayedMediaOnMultiSelectedTabs();

  info("Wait for media to play");
  await tab0BlockPromise;
  await tab1BlockPromise;

  // Check tabs are still unmuted
  ok(!muted(tab0), "Tab0 is unmuted");
  ok(!muted(tab1), "Tab1 is unmuted");

  BrowserTestUtils.removeTab(tab0);
  BrowserTestUtils.removeTab(tab1);
});

/*
 * Playing blocked media will not unmute the selected tabs
 */
add_task(async function testDelayPlayWontAffectMuteStatus() {
  info("Add media tabs");
  let tab0 = await addMediaTab();
  let tab1 = await addMediaTab();

  info("Play both tabs");
  await play(tab0, false);
  await play(tab1, false);

  // Mute both tabs
  toggleMuteAudio(tab0, true);
  toggleMuteAudio(tab1, true);

  info("Multiselect tabs");
  await triggerClickOn(tab1, { shiftKey: true });

  // Check multiselection
  ok(tab0.multiselected, "tab0 is multiselected");
  ok(tab1.multiselected, "tab1 is multiselected");

  // Check tabs are muted
  ok(muted(tab0), "Tab0 is muted");
  ok(muted(tab1), "Tab1 is muted");

  let tab0BlockPromise = wait_for_tab_media_blocked_event(tab0, false);
  let tab1BlockPromise = wait_for_tab_media_blocked_event(tab1, false);

  // Play media on selected tabs
  gBrowser.resumeDelayedMediaOnMultiSelectedTabs();

  info("Wait for media to play");
  await tab0BlockPromise;
  await tab1BlockPromise;

  // Check tabs are still muted
  ok(muted(tab0), "Tab0 is muted");
  ok(muted(tab1), "Tab1 is muted");

  BrowserTestUtils.removeTab(tab0);
  BrowserTestUtils.removeTab(tab1);
});

/*
 * The "Play Tabs" icon unblocks media
 */
add_task(async function testDelayPlayWhenUsingButton() {
  info("Add media tabs");
  let tab0 = await addMediaTab();
  let tab1 = await addMediaTab();
  let tab2 = await addMediaTab();
  let tab3 = await addMediaTab();
  let tab4 = await addMediaTab();

  let tabs = [tab0, tab1, tab2, tab3, tab4];

  // All tabs are initially unblocked due to not being played yet
  ok(!activeMediaBlocked(tab0), "Tab0 is not activemedia-blocked");
  ok(!activeMediaBlocked(tab1), "Tab1 is not activemedia-blocked");
  ok(!activeMediaBlocked(tab2), "Tab2 is not activemedia-blocked");
  ok(!activeMediaBlocked(tab3), "Tab3 is not activemedia-blocked");
  ok(!activeMediaBlocked(tab4), "Tab4 is not activemedia-blocked");

  // Playing tabs 0, 1, and 2 will block them
  info("Play tabs 0, 1, and 2");
  await play(tab0, false);
  await play(tab1, false);
  await play(tab2, false);
  ok(activeMediaBlocked(tab0), "Tab0 is activemedia-blocked");
  ok(activeMediaBlocked(tab1), "Tab1 is activemedia-blocked");
  ok(activeMediaBlocked(tab2), "Tab2 is activemedia-blocked");

  // tab3 and tab4 are still unblocked
  ok(!activeMediaBlocked(tab3), "Tab3 is not activemedia-blocked");
  ok(!activeMediaBlocked(tab4), "Tab4 is not activemedia-blocked");

  // Multiselect tab0, tab1, tab2, and tab3.
  info("Multiselect tabs");
  await triggerClickOn(tab3, { shiftKey: true });

  // Check multiselection
  for (let i = 0; i <= 3; i++) {
    ok(tabs[i].multiselected, `tab${i} is multiselected`);
  }
  ok(!tab4.multiselected, "tab4 is not multiselected");

  let tab0BlockPromise = wait_for_tab_media_blocked_event(tab0, false);
  let tab1BlockPromise = wait_for_tab_media_blocked_event(tab1, false);
  let tab2BlockPromise = wait_for_tab_media_blocked_event(tab2, false);

  // Use the overlay icon on tab2 to play media on the selected tabs
  info("Press play tab2 icon");
  await pressIcon(tab2.overlayIcon);

  // tab0, tab1, and tab2 were played and multiselected
  // They will now be unblocked and playing media
  info("Wait for tabs to play");
  await tab0BlockPromise;
  await tab1BlockPromise;
  await tab2BlockPromise;
  ok(!activeMediaBlocked(tab0), "Tab0 is not activemedia-blocked");
  ok(!activeMediaBlocked(tab1), "Tab1 is not activemedia-blocked");
  ok(!activeMediaBlocked(tab2), "Tab2 is not activemedia-blocked");
  // tab3 was also multiselected but never played
  // It will be unblocked but not playing media
  ok(!activeMediaBlocked(tab3), "Tab3 is not activemedia-blocked");
  // tab4 was not multiselected and was never played
  // It remains in its original state
  ok(!activeMediaBlocked(tab4), "Tab4 is not activemedia-blocked");

  for (let tab of tabs) {
    BrowserTestUtils.removeTab(tab);
  }
});

/*
 * Tab context menus have to show the menu icons "Play Tab" or "Play Tabs"
 * depending on the number of tabs selected, and whether blocked media is present
 */
add_task(async function testTabContextMenu() {
  info("Add media tabs");
  let tab0 = await addMediaTab();
  let tab1 = await addMediaTab();
  let tab2 = await addMediaTab();
  let tab3 = await addMediaTab();
  let tabs = [tab0, tab1, tab2, tab3];

  let menuItemPlayTab = document.getElementById("context_playTab");
  let menuItemPlaySelectedTabs = document.getElementById(
    "context_playSelectedTabs"
  );

  // Multiselect tab0, tab1, and tab2.
  info("Multiselect tabs");
  await triggerClickOn(tab0, { ctrlKey: true });
  await triggerClickOn(tab1, { ctrlKey: true });
  await triggerClickOn(tab2, { ctrlKey: true });

  // Check multiselected tabs
  for (let i = 0; i <= 2; i++) {
    ok(tabs[i].multiselected, `tab${i} is multi-selected`);
  }
  ok(!tab3.multiselected, "tab3 is not multiselected");

  // No active media yet:
  // - "Play Tab" is hidden
  // - "Play Tabs" is hidden
  for (let i = 0; i <= 2; i++) {
    updateTabContextMenu(tabs[i]);
    ok(menuItemPlayTab.hidden, `tab${i} "Play Tab" is hidden`);
    ok(menuItemPlaySelectedTabs.hidden, `tab${i} "Play Tabs" is hidden`);
    ok(!activeMediaBlocked(tabs[i]), `tab${i} is not active media blocked`);
  }

  info("Play tabs 0, 1, and 2");
  await play(tab0, false);
  await play(tab1, false);
  await play(tab2, false);

  // Active media blocked:
  // - "Play Tab" is hidden
  // - "Play Tabs" is visible
  for (let i = 0; i <= 2; i++) {
    updateTabContextMenu(tabs[i]);
    ok(menuItemPlayTab.hidden, `tab${i} "Play Tab" is hidden`);
    ok(!menuItemPlaySelectedTabs.hidden, `tab${i} "Play Tabs" is visible`);
    ok(activeMediaBlocked(tabs[i]), `tab${i} is active media blocked`);
  }

  info("Play Media on tabs 0, 1, and 2");
  gBrowser.resumeDelayedMediaOnMultiSelectedTabs();

  // Active media is unblocked:
  // - "Play Tab" is hidden
  // - "Play Tabs" is hidden
  for (let i = 0; i <= 2; i++) {
    updateTabContextMenu(tabs[i]);
    ok(menuItemPlayTab.hidden, `tab${i} "Play Tab" is hidden`);
    ok(menuItemPlaySelectedTabs.hidden, `tab${i} "Play Tabs" is hidden`);
    ok(!activeMediaBlocked(tabs[i]), `tab${i} is not active media blocked`);
  }

  // tab3 is untouched
  updateTabContextMenu(tab3);
  ok(menuItemPlayTab.hidden, 'tab3 "Play Tab" is hidden');
  ok(menuItemPlaySelectedTabs.hidden, 'tab3 "Play Tabs" is hidden');
  ok(!activeMediaBlocked(tab3), "tab3 is not active media blocked");

  for (let tab of tabs) {
    BrowserTestUtils.removeTab(tab);
  }
});