File: webrtc_mediadevices_interactive_uitest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (525 lines) | stat: -rw-r--r-- 21,244 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/json/json_reader.h"
#include "base/strings/string_util.h"
#include "base/test/bind.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/media/webrtc/media_device_salt_service_factory.h"
#include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
#include "chrome/browser/media/webrtc/webrtc_browsertest_common.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/media_device_salt/media_device_salt_service.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/test/permission_request_observer.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/tracking_protection_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browsing_data_remover.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/browsing_data_remover_test_util.h"
#include "content/public/test/prerender_test_util.h"
#include "media/audio/audio_device_description.h"
#include "media/audio/audio_manager.h"
#include "media/base/media_switches.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

#if BUILDFLAG(IS_MAC)
#include <CoreGraphics/CoreGraphics.h>
#endif

namespace {

const char kMainWebrtcTestHtmlPage[] = "/webrtc/webrtc_jsep01_test.html";
const char kClearCookiesPage[] = "/clear_cookies";

const char kDeviceKindAudioInput[] = "audioinput";
const char kDeviceKindVideoInput[] = "videoinput";
const char kDeviceKindAudioOutput[] = "audiooutput";

}  // namespace

// Integration test for WebRTC enumerateDevices. It always uses fake devices.
// It needs to be a browser test (and not content browser test) to be able to
// test that labels are cleared or not depending on if access to devices has
// been granted.
class WebRtcMediaDevicesInteractiveUITest : public WebRtcTestBase {
 public:
  WebRtcMediaDevicesInteractiveUITest()
      : has_audio_output_devices_initialized_(false),
        has_audio_output_devices_(false) {
    scoped_feature_list_.InitWithFeatures({features::kUserMediaCaptureOnFocus},
                                          {});
  }

  void SetUpInProcessBrowserTestFixture() override {
    DetectErrorsInJavaScript();  // Look for errors in our rather complex js.
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    // Ensure the infobar is enabled, since we expect that in this test.
    EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream));
  }

 protected:
  // This is used for media devices and sources.
  struct MediaDeviceInfo {
    std::string device_id;  // Domain specific device ID.
    std::string kind;
    std::string label;
    std::string group_id;
  };

  void EnumerateDevices(content::WebContents* tab,
                        std::vector<MediaDeviceInfo>* devices) {
    std::string devices_as_json = ExecuteJavascript("enumerateDevices()", tab);
    EXPECT_FALSE(devices_as_json.empty());

    ASSERT_OK_AND_ASSIGN(
        auto parsed_json,
        base::JSONReader::ReadAndReturnValueWithError(
            devices_as_json, base::JSON_ALLOW_TRAILING_COMMAS));
    ASSERT_TRUE(parsed_json.is_list());
    ASSERT_FALSE(parsed_json.GetList().empty());
    bool found_audio_input = false;
    bool found_video_input = false;

    for (const auto& value : parsed_json.GetList()) {
      const base::Value::Dict* dict = value.GetIfDict();
      ASSERT_TRUE(dict);
      MediaDeviceInfo device;
      ASSERT_TRUE(dict->FindString("deviceId"));
      device.device_id = *dict->FindString("deviceId");

      ASSERT_TRUE(dict->FindString("kind"));
      device.kind = *dict->FindString("kind");

      ASSERT_TRUE(dict->FindString("label"));
      device.label = *dict->FindString("label");

      ASSERT_TRUE(dict->FindString("groupId"));
      device.group_id = *dict->FindString("groupId");

      // Should be HMAC SHA256.
      if (!media::AudioDeviceDescription::IsDefaultDevice(device.device_id) &&
          !(device.device_id ==
            media::AudioDeviceDescription::kCommunicationsDeviceId)) {
        EXPECT_EQ(64ul, device.device_id.length());
        EXPECT_TRUE(
            base::ContainsOnlyChars(device.device_id, "0123456789abcdef"));
      }

      EXPECT_TRUE(device.kind == kDeviceKindAudioInput ||
                  device.kind == kDeviceKindVideoInput ||
                  device.kind == kDeviceKindAudioOutput);
      if (device.kind == kDeviceKindAudioInput) {
        found_audio_input = true;
      } else if (device.kind == kDeviceKindVideoInput) {
        found_video_input = true;
      }

      devices->push_back(device);
    }

    EXPECT_TRUE(found_audio_input);
    EXPECT_TRUE(found_video_input);
  }

  static void CheckEnumerationsAreDifferent(
      const std::vector<MediaDeviceInfo>& devices,
      const std::vector<MediaDeviceInfo>& devices2) {
    for (auto& device : devices) {
      bool found = base::Contains(devices2, device.device_id,
                                  &MediaDeviceInfo::device_id);
      if (device.device_id == media::AudioDeviceDescription::kDefaultDeviceId ||
          device.device_id ==
              media::AudioDeviceDescription::kCommunicationsDeviceId) {
        EXPECT_TRUE(found);
      } else {
        EXPECT_FALSE(found);
      }

      EXPECT_FALSE(base::Contains(devices2, device.group_id,
                                  &MediaDeviceInfo::group_id));
    }
  }

  bool has_audio_output_devices_initialized_;
  bool has_audio_output_devices_;

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       EnumerateDevicesWithoutAccess) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();

  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab, &devices);

  // Label, deviceId and groupId should be empty if access has not been allowed.
  for (const auto& device_info : devices) {
    EXPECT_TRUE(device_info.label.empty());
    EXPECT_TRUE(device_info.device_id.empty());
    EXPECT_TRUE(device_info.group_id.empty());
  }
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       EnumerateDevicesWithAccess) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();

  EXPECT_TRUE(GetUserMediaAndAccept(tab));

  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab, &devices);

  // Labels, deviceId and groupId should be non-empty if access has been
  // allowed.
  for (const auto& device_info : devices) {
    EXPECT_TRUE(!device_info.label.empty());
    EXPECT_TRUE(!device_info.device_id.empty());
    EXPECT_TRUE(!device_info.group_id.empty());
  }
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       GetUserMediaOnUnFocusedTab) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  ASSERT_TRUE(AddTabAtIndexToBrowser(browser(), 1, url,
                                     ui::PAGE_TRANSITION_LINK, true));

  content::WebContents* focused_tab =
      browser()->tab_strip_model()->GetWebContentsAt(1);
  content::WebContents* unfocused_tab =
      browser()->tab_strip_model()->GetWebContentsAt(0);
  EXPECT_TRUE(GetUserMediaAndAccept(focused_tab));
  GetUserMediaReturnsFalseIfWaitIsTooLong(unfocused_tab,
                                          kAudioVideoCallConstraints);
}

// Flakes on Linux TSan Tests; crbug.com/1396123.
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_GetUserMediaTabRegainsFocus DISABLED_GetUserMediaTabRegainsFocus
#else
#define MAYBE_GetUserMediaTabRegainsFocus GetUserMediaTabRegainsFocus
#endif
IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       MAYBE_GetUserMediaTabRegainsFocus) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  ASSERT_TRUE(AddTabAtIndexToBrowser(browser(), 1, url,
                                     ui::PAGE_TRANSITION_LINK, true));

  content::WebContents* tab = browser()->tab_strip_model()->GetWebContentsAt(0);
  GetUserMediaReturnsFalseIfWaitIsTooLong(tab, kAudioVideoCallConstraints);
  // |tab| gains focus.
  browser()->tab_strip_model()->ActivateTabAt(0);
  EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted(
      tab, kAudioVideoCallConstraints));
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       DeviceIdSameGroupIdDiffersAcrossTabs) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  content::WebContents* tab1 =
      browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_TRUE(GetUserMediaAndAccept(tab1));
  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab1, &devices);

  chrome::AddTabAt(browser(), GURL(url::kAboutBlankURL), -1, true);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  content::WebContents* tab2 =
      browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted(
      tab2, kAudioVideoCallConstraints));
  std::vector<MediaDeviceInfo> devices2;
  EnumerateDevices(tab2, &devices2);

  EXPECT_NE(tab1, tab2);
  EXPECT_EQ(devices.size(), devices2.size());
  for (auto& device : devices) {
    EXPECT_TRUE(base::Contains(devices2, device.device_id,
                               &MediaDeviceInfo::device_id));

    EXPECT_FALSE(
        base::Contains(devices2, device.group_id, &MediaDeviceInfo::group_id));
  }
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       DeviceIdDiffersAfterClearingCookies) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();

  EXPECT_TRUE(GetUserMediaAndAccept(tab));

  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab, &devices);

  auto* remover = browser()->profile()->GetBrowsingDataRemover();
  content::BrowsingDataRemoverCompletionObserver completion_observer(remover);
  remover->RemoveAndReply(
      base::Time(), base::Time::Max(),
      content::BrowsingDataRemover::DATA_TYPE_COOKIES,
      content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB,
      &completion_observer);
  completion_observer.BlockUntilCompletion();

  std::vector<MediaDeviceInfo> devices2;
  EnumerateDevices(tab, &devices2);

  EXPECT_EQ(devices.size(), devices2.size());
  CheckEnumerationsAreDifferent(devices, devices2);
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       DeviceIdDiffersAcrossTabsWithCookiesDisabled) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  CookieSettingsFactory::GetForProfile(browser()->profile())
      ->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockAll3pcToggleEnabled,
                                               true);
  content::WebContents* tab1 =
      browser()->tab_strip_model()->GetActiveWebContents();

  EXPECT_TRUE(GetUserMediaAndAccept(tab1));

  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab1, &devices);

  chrome::AddTabAt(browser(), GURL(url::kAboutBlankURL), -1, true);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  content::WebContents* tab2 =
      browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted(
      tab2, kAudioVideoCallConstraints));
  std::vector<MediaDeviceInfo> devices2;
  EnumerateDevices(tab2, &devices2);

  EXPECT_NE(tab1, tab2);
  EXPECT_EQ(devices.size(), devices2.size());
  CheckEnumerationsAreDifferent(devices, devices2);
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       DeviceIdDiffersSameTabAfterReloadWithCookiesDisabled) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  CookieSettingsFactory::GetForProfile(browser()->profile())
      ->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockAll3pcToggleEnabled,
                                               true);
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();

  EXPECT_TRUE(GetUserMediaAndAccept(tab));
  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab, &devices);

  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  tab = browser()->tab_strip_model()->GetActiveWebContents();

  EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted(
      tab, kAudioVideoCallConstraints));
  std::vector<MediaDeviceInfo> devices2;
  EnumerateDevices(tab, &devices2);

  EXPECT_EQ(devices.size(), devices2.size());
  CheckEnumerationsAreDifferent(devices, devices2);
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       DeviceIdDiffersAfterClearSiteDataHeader) {
  embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
      [](const net::test_server::HttpRequest& request)
          -> std::unique_ptr<net::test_server::HttpResponse> {
        if (request.GetURL().path() == kClearCookiesPage) {
          auto response =
              std::make_unique<net::test_server::BasicHttpResponse>();
          response->AddCustomHeader("Clear-Site-Data", "\"cookies\"");
          response->set_code(net::HTTP_OK);
          response->set_content_type("text/html");
          response->set_content(std::string());
          return response;
        }

        // Use the default handler for other requests.
        return nullptr;
      }));

  ASSERT_TRUE(embedded_test_server()->Start());
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage)));
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();

  EXPECT_TRUE(GetUserMediaAndAccept(tab));
  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab, &devices);

  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL(kClearCookiesPage)));

  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage)));

  std::vector<MediaDeviceInfo> devices2;
  EnumerateDevices(tab, &devices2);
  EXPECT_EQ(devices.size(), devices2.size());
  CheckEnumerationsAreDifferent(devices, devices2);
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       PRE_SaltsDeletedForSessionOnlyCookies) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  CookieSettingsFactory::GetForProfile(browser()->profile())
      ->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab, &devices);

  media_device_salt::MediaDeviceSaltService* salt_service =
      MediaDeviceSaltServiceFactory::GetForBrowserContext(browser()->profile());
  base::test::TestFuture<std::vector<blink::StorageKey>> keys_future;
  salt_service->GetAllStorageKeys(keys_future.GetCallback());
  EXPECT_FALSE(keys_future.Get().empty());
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       SaltsDeletedForSessionOnlyCookies) {
  media_device_salt::MediaDeviceSaltService* salt_service =
      MediaDeviceSaltServiceFactory::GetForBrowserContext(browser()->profile());
  base::test::TestFuture<std::vector<blink::StorageKey>> keys_future;
  salt_service->GetAllStorageKeys(keys_future.GetCallback());
  EXPECT_TRUE(keys_future.Get().empty());
}

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesInteractiveUITest,
                       NoPersistentSaltStoredWithCookiesDisabled) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  CookieSettingsFactory::GetForProfile(browser()->profile())
      ->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockAll3pcToggleEnabled,
                                               true);
  content::WebContents* tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  std::vector<MediaDeviceInfo> devices;
  EnumerateDevices(tab, &devices);

  media_device_salt::MediaDeviceSaltService* salt_service =
      MediaDeviceSaltServiceFactory::GetForBrowserContext(browser()->profile());
  base::test::TestFuture<std::vector<blink::StorageKey>> keys_future;
  salt_service->GetAllStorageKeys(keys_future.GetCallback());
  EXPECT_TRUE(keys_future.Get().empty());
}

class WebRtcMediaDevicesPrerenderingBrowserTest
    : public WebRtcMediaDevicesInteractiveUITest {
 public:
  WebRtcMediaDevicesPrerenderingBrowserTest()
      : prerender_helper_(base::BindRepeating(
            &WebRtcMediaDevicesPrerenderingBrowserTest::web_contents,
            base::Unretained(this))) {}
  ~WebRtcMediaDevicesPrerenderingBrowserTest() override = default;

  content::test::PrerenderTestHelper* prerender_helper() {
    return &prerender_helper_;
  }

  content::WebContents* web_contents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

 private:
  content::test::PrerenderTestHelper prerender_helper_;
};

IN_PROC_BROWSER_TEST_F(WebRtcMediaDevicesPrerenderingBrowserTest,
                       EnumerateDevicesInPrerendering) {
#if BUILDFLAG(IS_MAC)
  // Test will fail if the window it's running in contains the mouse pointer.
  // Here we warp the cursor, hopefully, out of the window.
  CGWarpMouseCursorPosition({0, 0});
#endif
  ASSERT_TRUE(embedded_test_server()->Start());

  // Loads a simple page as a primary page.
  GURL url = embedded_test_server()->GetURL("/empty.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  // Loads a page in the prerender.
  auto prerender_url = embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage);
  content::FrameTreeNodeId host_id =
      prerender_helper()->AddPrerender(prerender_url);
  content::test::PrerenderHostObserver host_observer(*web_contents(), host_id);
  content::RenderFrameHost* prerender_rfh =
      prerender_helper()->GetPrerenderedMainFrameHost(host_id);

  CookieSettingsFactory::GetForProfile(browser()->profile())
      ->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);

  base::RunLoop run_loop;
  permissions::PermissionRequestObserver observer(web_contents());
  prerender_rfh->ExecuteJavaScriptForTests(
      u"doGetUserMedia({audio: true, video: true});", base::NullCallback(),
      content::ISOLATED_WORLD_ID_GLOBAL);

  // The prerendering page should not show a permission request's bubble UI.
  EXPECT_FALSE(observer.request_shown());

  // Activates the page from the prerendering.
  prerender_helper()->NavigatePrimaryPage(prerender_url);
  observer.Wait();

  // Makes sure that the page is activated from the prerendering.
  EXPECT_TRUE(host_observer.was_activated());

  // The prerendered page should show the permission request's bubble UI.
  EXPECT_TRUE(observer.request_shown());
}