summaryrefslogtreecommitdiffstats
path: root/dom/media/gtest/TestGroupId.cpp
blob: efd0bae20a3745a64725ae0ce7228f3bf77ba383 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "AudioDeviceInfo.h"
#include "MediaManager.h"
#include "gmock/gmock.h"
#include "gtest/gtest-printers.h"
#include "gtest/gtest.h"
#include "mozilla/Attributes.h"
#include "mozilla/UniquePtr.h"
#include "nsTArray.h"
#include "webrtc/MediaEngineFake.h"

using ::testing::Return;
using namespace mozilla;

void PrintTo(const nsString& aValue, ::std::ostream* aStream) {
  NS_ConvertUTF16toUTF8 str(aValue);
  (*aStream) << str.get();
}
void PrintTo(const nsCString& aValue, ::std::ostream* aStream) {
  (*aStream) << aValue.get();
}

RefPtr<AudioDeviceInfo> MakeAudioDeviceInfo(const nsAString& aName,
                                            const nsAString& aGroupId,
                                            uint16_t aType) {
  return MakeRefPtr<AudioDeviceInfo>(
      nullptr, aName, aGroupId, u"Vendor"_ns, aType,
      AudioDeviceInfo::STATE_ENABLED, AudioDeviceInfo::PREF_NONE,
      AudioDeviceInfo::FMT_F32LE, AudioDeviceInfo::FMT_F32LE, 2u, 44100u,
      44100u, 44100u, 0, 0);
}

RefPtr<MediaDevice> MakeCameraDevice(const nsString& aName,
                                     const nsString& aGroupId) {
  return new MediaDevice(new MediaEngineFake(), dom::MediaSourceEnum::Camera,
                         aName, u""_ns, aGroupId, MediaDevice::IsScary::No,
                         MediaDevice::OsPromptable::No);
}

RefPtr<MediaDevice> MakeMicDevice(const nsString& aName,
                                  const nsString& aGroupId) {
  return new MediaDevice(
      new MediaEngineFake(),
      MakeAudioDeviceInfo(aName, aGroupId, AudioDeviceInfo::TYPE_INPUT),
      u""_ns);
}

RefPtr<MediaDevice> MakeSpeakerDevice(const nsString& aName,
                                      const nsString& aGroupId) {
  return new MediaDevice(
      new MediaEngineFake(),
      MakeAudioDeviceInfo(aName, aGroupId, AudioDeviceInfo::TYPE_OUTPUT),
      u"ID"_ns);
}

/* Verify that when an audio input device name contains the video input device
 * name the video device group id is updated to become equal to the audio
 * device group id. */
TEST(TestGroupId, MatchInput_PartOfName)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));

  auto mic =
      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns);
  devices.AppendElement(mic);
  audios.AppendElement(mic);

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, devices[1]->mRawGroupID)
      << "Video group id is the same as audio input group id.";
}

/* Verify that when an audio input device name is the same as the video input
 * device name the video device group id is updated to become equal to the audio
 * device group id. */
TEST(TestGroupId, MatchInput_FullName)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));

  auto mic = MakeMicDevice(u"Vendor Model"_ns, u"Mic-Model-GroupId"_ns);
  devices.AppendElement(mic);
  audios.AppendElement(mic);

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, devices[1]->mRawGroupID)
      << "Video group id is the same as audio input group id.";
}

/* Verify that when an audio input device name does not contain the video input
 * device name the video device group id does not change. */
TEST(TestGroupId, NoMatchInput)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  nsString Cam_Model_GroupId = u"Cam-Model-GroupId"_ns;
  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, Cam_Model_GroupId));

  audios.AppendElement(
      MakeMicDevice(u"Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, Cam_Model_GroupId)
      << "Video group id has not been updated.";
  EXPECT_NE(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
      << "Video group id is different than audio input group id.";
}

/* Verify that when more that one audio input and more than one audio output
 * device name contain the video input device name the video device group id
 * does not change. */
TEST(TestGroupId, NoMatch_TwoIdenticalDevices)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  nsString Cam_Model_GroupId = u"Cam-Model-GroupId"_ns;
  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, Cam_Model_GroupId));

  audios.AppendElement(
      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
  audios.AppendElement(
      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));

  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
                                         u"Speaker-Model-GroupId"_ns));
  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
                                         u"Speaker-Model-GroupId"_ns));

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, Cam_Model_GroupId)
      << "Video group id has not been updated.";
  EXPECT_NE(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
      << "Video group id is different from audio input group id.";
  EXPECT_NE(devices[0]->mRawGroupID, audios[2]->mRawGroupID)
      << "Video group id is different from audio output group id.";
}

/* Verify that when more that one audio input device name contain the video
 * input device name the video device group id is not updated by audio input
 * device group id but it continues looking at audio output devices where it
 * finds a match so video input group id is updated by audio output group id. */
TEST(TestGroupId, Match_TwoIdenticalInputsMatchOutput)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  nsString Cam_Model_GroupId = u"Cam-Model-GroupId"_ns;
  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, Cam_Model_GroupId));

  audios.AppendElement(
      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
  audios.AppendElement(
      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));

  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
                                         u"Speaker-Model-GroupId"_ns));

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, audios[2]->mRawGroupID)
      << "Video group id is the same as audio output group id.";
}

/* Verify that when more that one audio input and more than one audio output
 * device names contain the video input device name the video device group id
 * does not change. */
TEST(TestGroupId, NoMatch_ThreeIdenticalDevices)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  nsString Cam_Model_GroupId = u"Cam-Model-GroupId"_ns;
  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, Cam_Model_GroupId));

  audios.AppendElement(
      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
  audios.AppendElement(
      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
  audios.AppendElement(
      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));

  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
                                         u"Speaker-Model-GroupId"_ns));
  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
                                         u"Speaker-Model-GroupId"_ns));
  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
                                         u"Speaker-Model-GroupId"_ns));

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, Cam_Model_GroupId)
      << "Video group id has not been updated.";
  EXPECT_NE(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
      << "Video group id is different from audio input group id.";
  EXPECT_NE(devices[0]->mRawGroupID, audios[3]->mRawGroupID)
      << "Video group id is different from audio output group id.";
}

/* Verify that when an audio output device name contains the video input device
 * name the video device group id is updated to become equal to the audio
 * device group id. */
TEST(TestGroupId, MatchOutput)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));

  audios.AppendElement(
      MakeMicDevice(u"Mic Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));

  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
                                         u"Speaker-Model-GroupId"_ns));

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, audios[1]->mRawGroupID)
      << "Video group id is the same as audio output group id.";
}

/* Verify that when an audio input device name is the same as audio output
 * device and video input device name the video device group id is updated to
 * become equal to the audio input device group id. */
TEST(TestGroupId, InputOutputSameName)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));

  audios.AppendElement(
      MakeMicDevice(u"Vendor Model"_ns, u"Mic-Model-GroupId"_ns));

  audios.AppendElement(
      MakeSpeakerDevice(u"Vendor Model"_ns, u"Speaker-Model-GroupId"_ns));

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
      << "Video input group id is the same as audio input group id.";
}

/* Verify that when an audio input device name contains the video input device
 * and the audio input group id is an empty string, the video device group id
 * is updated to become equal to the audio device group id. */
TEST(TestGroupId, InputEmptyGroupId)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));

  audios.AppendElement(MakeMicDevice(u"Vendor Model"_ns, u""_ns));

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
      << "Video input group id is the same as audio input group id.";
}

/* Verify that when an audio output device name contains the video input device
 * and the audio output group id is an empty string, the video device group id
 * is updated to become equal to the audio output device group id. */
TEST(TestGroupId, OutputEmptyGroupId)
{
  MediaManager::MediaDeviceSet devices;
  MediaManager::MediaDeviceSet audios;

  devices.AppendElement(
      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));

  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model"_ns, u""_ns));

  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);

  EXPECT_EQ(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
      << "Video input group id is the same as audio output group id.";
}