File: capture_device_ranking_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (533 lines) | stat: -rw-r--r-- 21,461 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
526
527
528
529
530
531
532
533
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media/prefs/capture_device_ranking.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

MATCHER(VideoCaptureDeviceInfoEq, "") {
  return std::get<0>(arg).descriptor.device_id ==
         std::get<1>(arg).descriptor.device_id;
}

MATCHER(MediaStreamDeviceInfoEq, "") {
  return std::get<0>(arg).id == std::get<1>(arg).id &&
         std::get<0>(arg).type == std::get<1>(arg).type;
}

template <typename T>
std::vector<T>::const_iterator GetIterForInfo(const std::vector<T>& infos,
                                              const T& info) {
  for (auto iter = infos.begin(); iter < infos.end(); ++iter) {
    if (media_prefs::internal::DeviceInfoToUniqueId(*iter) ==
        media_prefs::internal::DeviceInfoToUniqueId(info)) {
      return iter;
    }
  }

  return infos.end();
}

}  // namespace

class CaptureDeviceRankingTest : public testing::Test {
  void SetUp() override { media_prefs::RegisterUserPrefs(prefs_.registry()); }

 protected:
  void UpdateAudioRanking(
      const media::AudioDeviceDescription& preferred_device,
      const std::vector<media::AudioDeviceDescription>& infos) {
    media_prefs::UpdateAudioDevicePreferenceRanking(
        prefs_, GetIterForInfo(infos, preferred_device), infos);
  }

  void UpdateVideoRanking(
      const media::VideoCaptureDeviceInfo& preferred_device,
      const std::vector<media::VideoCaptureDeviceInfo>& infos) {
    media_prefs::UpdateVideoDevicePreferenceRanking(
        prefs_, GetIterForInfo(infos, preferred_device), infos);
  }

  void ExpectAudioRanking(
      std::vector<media::AudioDeviceDescription> infos,
      const std::vector<media::AudioDeviceDescription>& expected_output) {
    media_prefs::PreferenceRankAudioDeviceInfos(prefs_, infos);
    EXPECT_THAT(infos, expected_output);
  }

  void ExpectVideoRanking(
      std::vector<media::VideoCaptureDeviceInfo> infos,
      const std::vector<media::VideoCaptureDeviceInfo>& expected_output) {
    media_prefs::PreferenceRankVideoDeviceInfos(prefs_, infos);
    EXPECT_THAT(
        infos, testing::Pointwise(VideoCaptureDeviceInfoEq(), expected_output));
  }

  TestingPrefServiceSimple prefs_;
};

TEST_F(CaptureDeviceRankingTest, PreferenceRankVideoDeviceInfos) {
  const media::VideoCaptureDeviceInfo kIntegratedCamera(
      {/*display_name=*/"Integrated Camera",
       /*device_id=*/"integrated_camera"});
  const media::VideoCaptureDeviceInfo kUsbCamera({/*display_name=*/"USB Camera",
                                                  /*device_id=*/"usb_camera"});
  const media::VideoCaptureDeviceInfo kDevice3({/*display_name=*/"Device 3",
                                                /*device_id=*/"device_3"});
  const media::VideoCaptureDeviceInfo kDeviceLost(
      {/*display_name=*/"Device Lost",
       /*device_id=*/"device_lost"});
  const media::VideoCaptureDeviceInfo kDeviceExtra(
      {/*display_name=*/"Device Extra",
       /*device_id=*/"device_extra"});

  UpdateVideoRanking(
      /*preferred_device=*/kIntegratedCamera,
      /*infos=*/{kDeviceLost, kDevice3, kIntegratedCamera, kUsbCamera});

  UpdateVideoRanking(
      /*preferred_device=*/kUsbCamera,
      /*infos=*/{kDevice3, kIntegratedCamera, kUsbCamera});

  ExpectVideoRanking(
      /*infos=*/{kDevice3, kIntegratedCamera, kUsbCamera},
      /*expected_output=*/{kUsbCamera, kIntegratedCamera, kDevice3});

  // Remove USB camera and select the integrated camera. Integrated
  // should stay below USB in the ranking  because the USB camera isn't
  // present.
  UpdateVideoRanking(/*preferred_device=*/kIntegratedCamera,
                     /*infos=*/{kDevice3, kIntegratedCamera});

  ExpectVideoRanking(
      /*infos=*/{kDeviceExtra, kDevice3, kIntegratedCamera},
      /*expected_output=*/{kIntegratedCamera, kDevice3, kDeviceExtra});

  // Bring back USB camera. The USB camera should be ranked as the most
  // preferred device even without an update.
  ExpectVideoRanking(
      /*infos=*/{kDeviceExtra, kDevice3, kIntegratedCamera, kUsbCamera},
      /*expected_output=*/{kUsbCamera, kIntegratedCamera, kDevice3,
                           kDeviceExtra});
}

TEST_F(CaptureDeviceRankingTest, PreferenceRankAudioDeviceInfos) {
  const media::AudioDeviceDescription kIntegratedMic(
      {/*device_name=*/"Integrated Mic",
       /*unique_id=*/"integrated_mic",
       /*group_id=*/"integrated_group"});
  const media::AudioDeviceDescription kUsbMic({
      /*device_name=*/"USB Mic",
      /*unique_id=*/"usb_mic",
      /*group_id=*/"usb_group",
  });
  const media::AudioDeviceDescription kDefaultMic({
      /*device_name=*/"",
      /*unique_id=*/media::AudioDeviceDescription::kDefaultDeviceId,
      /*group_id=*/"default_group",
  });
  const media::AudioDeviceDescription kDevice3({/*device_name=*/"Device 3",
                                                /*unique_id=*/"device_3",
                                                /*group_id=*/"device_group"});
  const media::AudioDeviceDescription kDeviceLost(
      {/*device_name=*/"Device Lost",
       /*unique_id=*/"device_lost",
       /*group_id=*/"device_group"});
  const media::AudioDeviceDescription kDeviceExtra(
      {/*device_name=*/"Device Extra",
       /*unique_id=*/"device_extra",
       /*group_id=*/"device_group"});

  UpdateAudioRanking(
      /*preferred_device=*/kIntegratedMic,
      /*infos=*/{kDeviceLost, kDevice3, kIntegratedMic, kUsbMic});

  UpdateAudioRanking(/*preferred_device=*/kUsbMic,
                     /*infos=*/{kDevice3, kIntegratedMic, kUsbMic});

  ExpectAudioRanking(
      /*infos=*/{kDevice3, kIntegratedMic, kUsbMic},
      /*expected_output=*/{kUsbMic, kIntegratedMic, kDevice3});

  // Remove USB mic and select the integrated mic. Integrated
  // should stay below USB in the ranking  because the USB mic isn't
  // present.
  UpdateAudioRanking(/*preferred_device=*/kIntegratedMic,
                     /*infos=*/{kDevice3, kIntegratedMic});

  ExpectAudioRanking(
      /*infos=*/{kDeviceExtra, kDevice3, kIntegratedMic},
      /*expected_output=*/{kIntegratedMic, kDevice3, kDeviceExtra});

  // Bring back USB mic. The USB mic should be ranked as the most
  // preferred device even without an update.
  ExpectAudioRanking(
      /*infos=*/{kDeviceExtra, kDevice3, kIntegratedMic, kUsbMic},
      /*expected_output=*/{kUsbMic, kIntegratedMic, kDevice3, kDeviceExtra});

  // Include the default mic, which has an empty name.
  UpdateAudioRanking(/*preferred_device=*/kDefaultMic, /*infos=*/{
                         kUsbMic, kIntegratedMic, kDefaultMic, kDevice3});
  ExpectAudioRanking(
      /*infos=*/{kUsbMic, kIntegratedMic, kDefaultMic, kDevice3},
      /*expected_output=*/{kDefaultMic, kUsbMic, kIntegratedMic, kDevice3});
}

TEST_F(CaptureDeviceRankingTest, PreferenceRankDefaultAudioDevice) {
  const media::AudioDeviceDescription kIntegratedMic(
      {/*device_name=*/"Integrated Mic",
       /*unique_id=*/"integrated_mic",
       /*group_id=*/"integrated_group"});
  const media::AudioDeviceDescription kUsbMic({
      /*device_name=*/"USB Mic",
      /*unique_id=*/"usb_mic",
      /*group_id=*/"usb_group",
  });
  const media::AudioDeviceDescription kDefaultMic({
      /*device_name=*/"",
      /*unique_id=*/media::AudioDeviceDescription::kDefaultDeviceId,
      /*group_id=*/"default_group",
  });

  // Include the default mic, which has an empty name. Then expect that
  // preference ranking works correctly for the other device info types.
  UpdateAudioRanking(/*preferred_device=*/kDefaultMic,
                     /*infos=*/{kUsbMic, kIntegratedMic, kDefaultMic});
  ExpectAudioRanking(
      /*infos=*/{kUsbMic, kDefaultMic, kIntegratedMic},
      /*expected_output=*/{kDefaultMic, kUsbMic, kIntegratedMic});

  {
    const blink::WebMediaDeviceInfo kUsbDevice(
        /*device_id=*/"usb_device",
        /*label=*/"USB Device",
        /*group_id=*/"usb_group");
    const blink::WebMediaDeviceInfo kDefaultDevice(
        /*device_id=*/media::AudioDeviceDescription::kDefaultDeviceId,
        /*label=*/"Default",
        /*group_id=*/"default_group");
    std::vector infos = {kUsbDevice, kDefaultDevice};
    media_prefs::PreferenceRankAudioDeviceInfos(prefs_, infos);
    std::vector expected = {kDefaultDevice, kUsbDevice};
    EXPECT_EQ(infos, expected);
  }

  {
    const blink::MediaStreamDevice kUsbDevice(
        /*type=*/blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE,
        /*id=*/"usb_device",
        /*name=*/"USB Device");
    const blink::MediaStreamDevice kDefaultDevice(
        /*type=*/blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE,
        /*id=*/media::AudioDeviceDescription::kDefaultDeviceId,
        /*name=*/"Default");
    std::vector infos = {kUsbDevice, kDefaultDevice};
    media_prefs::PreferenceRankAudioDeviceInfos(prefs_, infos);
    std::vector expected = {kDefaultDevice, kUsbDevice};
    EXPECT_EQ(infos, expected);
  }
}

TEST_F(CaptureDeviceRankingTest, InitializeAudioDeviceInfosRanking) {
  const media::AudioDeviceDescription kIntegratedMic(
      {/*device_name=*/"Integrated Mic",
       /*unique_id=*/"integrated_mic",
       /*group_id=*/"integrated_group"});
  const media::AudioDeviceDescription kUsbMic({
      /*device_name=*/"USB Mic",
      /*unique_id=*/"usb_mic",
      /*group_id=*/"usb_group",
  });

  prefs_.registry()->RegisterStringPref(
      prefs::kDefaultAudioCaptureDeviceDeprecated, "");

  // The default device is set to "" so the output should be unmodified.
  ExpectAudioRanking({kIntegratedMic, kUsbMic}, {kIntegratedMic, kUsbMic});

  // The default device isn't in the passed device list, so the output should be
  // unmodified.
  prefs_.SetString(prefs::kDefaultAudioCaptureDeviceDeprecated, "not_found_id");
  ExpectAudioRanking({kIntegratedMic, kUsbMic}, {kIntegratedMic, kUsbMic});

  // The default device is USB mic so the device ranking gets initialized to put
  // that first.
  prefs_.SetString(prefs::kDefaultAudioCaptureDeviceDeprecated,
                   kUsbMic.unique_id);
  ExpectAudioRanking({kIntegratedMic, kUsbMic}, {kUsbMic, kIntegratedMic});

  // The device ranking pref now has a value, so use that instead of the default
  // device pref.
  UpdateAudioRanking(kIntegratedMic, {kIntegratedMic, kUsbMic});
  ExpectAudioRanking({kUsbMic, kIntegratedMic}, {kIntegratedMic, kUsbMic});
}

TEST_F(CaptureDeviceRankingTest, InitializeVideoDeviceInfosRanking) {
  const media::VideoCaptureDeviceInfo kIntegratedCamera(
      {/*display_name=*/"Integrated Camera",
       /*device_id=*/"integrated_camera"});
  const media::VideoCaptureDeviceInfo kUsbCamera({/*display_name=*/"USB Camera",
                                                  /*device_id=*/"usb_camera"});

  prefs_.registry()->RegisterStringPref(
      prefs::kDefaultVideoCaptureDeviceDeprecated, "");

  // The default device is set to "" so the output should be unmodified.
  ExpectVideoRanking({kIntegratedCamera, kUsbCamera},
                     {kIntegratedCamera, kUsbCamera});

  // The default device isn't in the passed device list, so the output should be
  // unmodified.
  prefs_.SetString(prefs::kDefaultVideoCaptureDeviceDeprecated, "not_found_id");
  ExpectVideoRanking({kIntegratedCamera, kUsbCamera},
                     {kIntegratedCamera, kUsbCamera});

  // The default device is usb camera so the device ranking gets Initialized to
  // put that first.
  prefs_.SetString(prefs::kDefaultVideoCaptureDeviceDeprecated,
                   kUsbCamera.descriptor.device_id);
  ExpectVideoRanking({kIntegratedCamera, kUsbCamera},
                     {kUsbCamera, kIntegratedCamera});

  // The device ranking pref now has a value, so use that instead of the default
  // device pref.
  UpdateVideoRanking(kIntegratedCamera, {kIntegratedCamera, kUsbCamera});
  ExpectVideoRanking({kUsbCamera, kIntegratedCamera},
                     {kIntegratedCamera, kUsbCamera});
}

class CaptureDeviceRankingWebMediaDeviceTest
    : public CaptureDeviceRankingTest,
      public testing::WithParamInterface<blink::mojom::MediaDeviceType> {
 protected:
  void UpdateDeviceRanking(
      blink::mojom::MediaDeviceType device_type,
      const blink::WebMediaDeviceInfo& preferred_device,
      const std::vector<blink::WebMediaDeviceInfo>& infos) {
    if (device_type == blink::mojom::MediaDeviceType::kMediaAudioInput) {
      media_prefs::UpdateAudioDevicePreferenceRanking(
          prefs_, GetIterForInfo(infos, preferred_device), infos);
    } else if (device_type == blink::mojom::MediaDeviceType::kMediaVideoInput) {
      media_prefs::UpdateVideoDevicePreferenceRanking(
          prefs_, GetIterForInfo(infos, preferred_device), infos);
    } else {
      FAIL() << "Called with an unsupported type: " << device_type;
    }
  }

  void ExpectDeviceRanking(
      blink::mojom::MediaDeviceType device_type,
      std::vector<blink::WebMediaDeviceInfo> infos,
      const std::vector<blink::WebMediaDeviceInfo>& expected_output) {
    if (device_type == blink::mojom::MediaDeviceType::kMediaAudioInput) {
      media_prefs::PreferenceRankAudioDeviceInfos(prefs_, infos);
      EXPECT_EQ(infos, expected_output);
    } else if (device_type == blink::mojom::MediaDeviceType::kMediaVideoInput) {
      media_prefs::PreferenceRankVideoDeviceInfos(prefs_, infos);
      EXPECT_EQ(infos, expected_output);
    } else {
      FAIL() << "Called with an unsupported type: " << device_type;
    }
  }
};

INSTANTIATE_TEST_SUITE_P(
    WebMediaDeviceAudioVideoInput,
    CaptureDeviceRankingWebMediaDeviceTest,
    ::testing::Values(blink::mojom::MediaDeviceType::kMediaAudioInput,
                      blink::mojom::MediaDeviceType::kMediaVideoInput));

TEST_P(CaptureDeviceRankingWebMediaDeviceTest, PreferenceWebMediaDeviceInfos) {
  const blink::WebMediaDeviceInfo kIntegratedDevice(
      /*device_id=*/"integrated_device",
      /*label=*/"Integrated Device",
      /*group_id=*/"integrated_group");
  const blink::WebMediaDeviceInfo kUsbDevice(
      /*device_id=*/"usb_device",
      /*label=*/"USB Device",
      /*group_id=*/"usb_group");
  const blink::WebMediaDeviceInfo kDefaultDevice(
      /*device_id=*/media::AudioDeviceDescription::kDefaultDeviceId,
      /*label=*/"Default",
      /*group_id=*/"default_group");
  const blink::WebMediaDeviceInfo kDevice3(
      /*device_id=*/"device_3",
      /*label=*/"Device 3",
      /*group_id=*/"device_group");
  const blink::WebMediaDeviceInfo kDeviceLost(
      /*device_id=*/"device_lost",
      /*label=*/"Device Lost",
      /*group_id=*/"device_group");
  const blink::WebMediaDeviceInfo kDeviceExtra(
      /*device_id=*/"device_extra",
      /*label=*/"Device Extra",
      /*group_id=*/"device_group");

  const blink::mojom::MediaDeviceType device_type = GetParam();

  UpdateDeviceRanking(
      device_type,
      /*preferred_device=*/kIntegratedDevice,
      /*infos=*/{kDeviceLost, kDevice3, kIntegratedDevice, kUsbDevice});

  UpdateDeviceRanking(device_type,
                      /*preferred_device=*/kUsbDevice,
                      /*infos=*/{kDevice3, kIntegratedDevice, kUsbDevice});

  ExpectDeviceRanking(
      device_type,
      /*infos=*/{kDevice3, kIntegratedDevice, kUsbDevice},
      /*expected_output=*/{kUsbDevice, kIntegratedDevice, kDevice3});

  // Remove USB device and select the integrated device. Integrated
  // should stay below USB in the ranking  because the USB device isn't
  // present.
  UpdateDeviceRanking(device_type,
                      /*preferred_device=*/kIntegratedDevice,
                      /*infos=*/{kDevice3, kIntegratedDevice});

  ExpectDeviceRanking(
      device_type,
      /*infos=*/{kDeviceExtra, kDevice3, kIntegratedDevice},
      /*expected_output=*/{kIntegratedDevice, kDevice3, kDeviceExtra});

  // Bring back USB device. The USB device should be ranked as the most
  // preferred device even without an update.
  ExpectDeviceRanking(
      device_type,
      /*infos=*/{kDeviceExtra, kDevice3, kIntegratedDevice, kUsbDevice},
      /*expected_output=*/
      {kUsbDevice, kIntegratedDevice, kDevice3, kDeviceExtra});

  // Include the default device.
  UpdateDeviceRanking(
      device_type, /*preferred_device=*/kDefaultDevice,
      /*infos=*/{kUsbDevice, kIntegratedDevice, kDefaultDevice, kDevice3});
  ExpectDeviceRanking(
      device_type,
      /*infos=*/{kUsbDevice, kIntegratedDevice, kDefaultDevice, kDevice3},
      /*expected_output=*/
      {kDefaultDevice, kUsbDevice, kIntegratedDevice, kDevice3});
}

class CaptureDeviceRankingMediaStreamDeviceTest
    : public CaptureDeviceRankingTest,
      public testing::WithParamInterface<blink::mojom::MediaStreamType> {
 protected:
  void UpdateDeviceRanking(blink::mojom::MediaStreamType device_type,
                           const blink::MediaStreamDevice& preferred_device,
                           const std::vector<blink::MediaStreamDevice>& infos) {
    if (device_type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE) {
      media_prefs::UpdateAudioDevicePreferenceRanking(
          prefs_, GetIterForInfo(infos, preferred_device), infos);
    } else if (device_type ==
               blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE) {
      media_prefs::UpdateVideoDevicePreferenceRanking(
          prefs_, GetIterForInfo(infos, preferred_device), infos);
    } else {
      FAIL() << "Called with an unsupported type: " << device_type;
    }
  }

  void ExpectDeviceRanking(
      blink::mojom::MediaStreamType device_type,
      std::vector<blink::MediaStreamDevice> infos,
      const std::vector<blink::MediaStreamDevice>& expected_output) {
    if (device_type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE) {
      media_prefs::PreferenceRankAudioDeviceInfos(prefs_, infos);
      EXPECT_THAT(infos, testing::Pointwise(MediaStreamDeviceInfoEq(),
                                            expected_output));
    } else if (device_type ==
               blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE) {
      media_prefs::PreferenceRankVideoDeviceInfos(prefs_, infos);
      EXPECT_THAT(infos, testing::Pointwise(MediaStreamDeviceInfoEq(),
                                            expected_output));
    } else {
      FAIL() << "Called with an unsupported type: " << device_type;
    }
  }
};

INSTANTIATE_TEST_SUITE_P(
    MediaStreamDeviceAudioVideoInput,
    CaptureDeviceRankingMediaStreamDeviceTest,
    ::testing::Values(blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE,
                      blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE));

TEST_P(CaptureDeviceRankingMediaStreamDeviceTest,
       PreferenceMediaStreamDevices) {
  const blink::mojom::MediaStreamType device_type = GetParam();

  const blink::MediaStreamDevice kIntegratedDevice(
      /*type=*/device_type,
      /*id=*/"integrated_device",
      /*name=*/"Integrated Device");
  const blink::MediaStreamDevice kUsbDevice(
      /*type=*/device_type,
      /*id=*/"usb_device",
      /*name=*/"USB Device");
  const blink::MediaStreamDevice kDefaultDevice(
      /*type=*/device_type,
      /*id=*/media::AudioDeviceDescription::kDefaultDeviceId,
      /*name=*/"Default");
  const blink::MediaStreamDevice kDevice3(
      /*type=*/device_type,
      /*id=*/"device_3",
      /*name=*/"Device 3");
  const blink::MediaStreamDevice kDeviceLost(
      /*type=*/device_type,
      /*id=*/"device_lost",
      /*name=*/"Device Lost");
  const blink::MediaStreamDevice kDeviceExtra(
      /*type=*/device_type,
      /*id=*/"device_extra",
      /*name=*/"Device Extra");

  UpdateDeviceRanking(
      device_type,
      /*preferred_device=*/kIntegratedDevice,
      /*infos=*/{kDeviceLost, kDevice3, kIntegratedDevice, kUsbDevice});

  UpdateDeviceRanking(device_type,
                      /*preferred_device=*/kUsbDevice,
                      /*infos=*/{kDevice3, kIntegratedDevice, kUsbDevice});

  ExpectDeviceRanking(
      device_type,
      /*infos=*/{kDevice3, kIntegratedDevice, kUsbDevice},
      /*expected_output=*/{kUsbDevice, kIntegratedDevice, kDevice3});

  // Remove USB device and select the integrated device. Integrated
  // should stay below USB in the ranking  because the USB device isn't
  // present.
  UpdateDeviceRanking(device_type,
                      /*preferred_device=*/kIntegratedDevice,
                      /*infos=*/{kDevice3, kIntegratedDevice});

  ExpectDeviceRanking(
      device_type,
      /*infos=*/{kDeviceExtra, kDevice3, kIntegratedDevice},
      /*expected_output=*/{kIntegratedDevice, kDevice3, kDeviceExtra});

  // Bring back USB device. The USB device should be ranked as the most
  // preferred device even without an update.
  ExpectDeviceRanking(
      device_type,
      /*infos=*/{kDeviceExtra, kDevice3, kIntegratedDevice, kUsbDevice},
      /*expected_output=*/
      {kUsbDevice, kIntegratedDevice, kDevice3, kDeviceExtra});

  // Include the default device.
  UpdateDeviceRanking(
      device_type, /*preferred_device=*/kDefaultDevice,
      /*infos=*/{kUsbDevice, kIntegratedDevice, kDefaultDevice, kDevice3});
  ExpectDeviceRanking(
      device_type,
      /*infos=*/{kUsbDevice, kIntegratedDevice, kDefaultDevice, kDevice3},
      /*expected_output=*/
      {kDefaultDevice, kUsbDevice, kIntegratedDevice, kDevice3});
}