tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestGroupId.cpp (11598B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "AudioDeviceInfo.h"
      8 #include "MediaManager.h"
      9 #include "gmock/gmock.h"
     10 #include "gtest/gtest-printers.h"
     11 #include "gtest/gtest.h"
     12 #include "nsTArray.h"
     13 #include "webrtc/MediaEngineFake.h"
     14 
     15 using ::testing::Return;
     16 using namespace mozilla;
     17 
     18 void PrintTo(const nsString& aValue, ::std::ostream* aStream) {
     19  NS_ConvertUTF16toUTF8 str(aValue);
     20  (*aStream) << str.get();
     21 }
     22 void PrintTo(const nsCString& aValue, ::std::ostream* aStream) {
     23  (*aStream) << aValue.get();
     24 }
     25 
     26 RefPtr<AudioDeviceInfo> MakeAudioDeviceInfo(const nsAString& aName,
     27                                            const nsAString& aGroupId,
     28                                            uint16_t aType) {
     29  return MakeRefPtr<AudioDeviceInfo>(
     30      nullptr, aName, aGroupId, u"Vendor"_ns, aType,
     31      AudioDeviceInfo::STATE_ENABLED, AudioDeviceInfo::PREF_NONE,
     32      AudioDeviceInfo::FMT_F32LE, AudioDeviceInfo::FMT_F32LE, 2u, 44100u,
     33      44100u, 44100u, 0, 0);
     34 }
     35 
     36 RefPtr<MediaDevice> MakeCameraDevice(const nsString& aName,
     37                                     const nsString& aGroupId) {
     38  return new MediaDevice(new MediaEngineFake(), dom::MediaSourceEnum::Camera,
     39                         aName, u""_ns, aGroupId, MediaDevice::IsScary::No,
     40                         MediaDevice::OsPromptable::No);
     41 }
     42 
     43 RefPtr<MediaDevice> MakeMicDevice(const nsString& aName,
     44                                  const nsString& aGroupId) {
     45  return new MediaDevice(
     46      new MediaEngineFake(),
     47      MakeAudioDeviceInfo(aName, aGroupId, AudioDeviceInfo::TYPE_INPUT),
     48      u""_ns);
     49 }
     50 
     51 RefPtr<MediaDevice> MakeSpeakerDevice(const nsString& aName,
     52                                      const nsString& aGroupId) {
     53  return new MediaDevice(
     54      new MediaEngineFake(),
     55      MakeAudioDeviceInfo(aName, aGroupId, AudioDeviceInfo::TYPE_OUTPUT),
     56      u"ID"_ns);
     57 }
     58 
     59 /* Verify that when an audio input device name contains the video input device
     60 * name the video device group id is updated to become equal to the audio
     61 * device group id. */
     62 TEST(TestGroupId, MatchInput_PartOfName)
     63 {
     64  MediaManager::MediaDeviceSet devices;
     65  MediaManager::MediaDeviceSet audios;
     66 
     67  devices.AppendElement(
     68      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));
     69 
     70  auto mic =
     71      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns);
     72  devices.AppendElement(mic);
     73  audios.AppendElement(mic);
     74 
     75  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
     76 
     77  EXPECT_EQ(devices[0]->mRawGroupID, devices[1]->mRawGroupID)
     78      << "Video group id is the same as audio input group id.";
     79 }
     80 
     81 /* Verify that when an audio input device name is the same as the video input
     82 * device name the video device group id is updated to become equal to the audio
     83 * device group id. */
     84 TEST(TestGroupId, MatchInput_FullName)
     85 {
     86  MediaManager::MediaDeviceSet devices;
     87  MediaManager::MediaDeviceSet audios;
     88 
     89  devices.AppendElement(
     90      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));
     91 
     92  auto mic = MakeMicDevice(u"Vendor Model"_ns, u"Mic-Model-GroupId"_ns);
     93  devices.AppendElement(mic);
     94  audios.AppendElement(mic);
     95 
     96  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
     97 
     98  EXPECT_EQ(devices[0]->mRawGroupID, devices[1]->mRawGroupID)
     99      << "Video group id is the same as audio input group id.";
    100 }
    101 
    102 /* Verify that when an audio input device name does not contain the video input
    103 * device name the video device group id does not change. */
    104 TEST(TestGroupId, NoMatchInput)
    105 {
    106  MediaManager::MediaDeviceSet devices;
    107  MediaManager::MediaDeviceSet audios;
    108 
    109  nsString Cam_Model_GroupId = u"Cam-Model-GroupId"_ns;
    110  devices.AppendElement(
    111      MakeCameraDevice(u"Vendor Model"_ns, Cam_Model_GroupId));
    112 
    113  audios.AppendElement(
    114      MakeMicDevice(u"Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    115 
    116  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
    117 
    118  EXPECT_EQ(devices[0]->mRawGroupID, Cam_Model_GroupId)
    119      << "Video group id has not been updated.";
    120  EXPECT_NE(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
    121      << "Video group id is different than audio input group id.";
    122 }
    123 
    124 /* Verify that when more that one audio input and more than one audio output
    125 * device name contain the video input device name the video device group id
    126 * does not change. */
    127 TEST(TestGroupId, NoMatch_TwoIdenticalDevices)
    128 {
    129  MediaManager::MediaDeviceSet devices;
    130  MediaManager::MediaDeviceSet audios;
    131 
    132  nsString Cam_Model_GroupId = u"Cam-Model-GroupId"_ns;
    133  devices.AppendElement(
    134      MakeCameraDevice(u"Vendor Model"_ns, Cam_Model_GroupId));
    135 
    136  audios.AppendElement(
    137      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    138  audios.AppendElement(
    139      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    140 
    141  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
    142                                         u"Speaker-Model-GroupId"_ns));
    143  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
    144                                         u"Speaker-Model-GroupId"_ns));
    145 
    146  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
    147 
    148  EXPECT_EQ(devices[0]->mRawGroupID, Cam_Model_GroupId)
    149      << "Video group id has not been updated.";
    150  EXPECT_NE(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
    151      << "Video group id is different from audio input group id.";
    152  EXPECT_NE(devices[0]->mRawGroupID, audios[2]->mRawGroupID)
    153      << "Video group id is different from audio output group id.";
    154 }
    155 
    156 /* Verify that when more that one audio input device name contain the video
    157 * input device name the video device group id is not updated by audio input
    158 * device group id but it continues looking at audio output devices where it
    159 * finds a match so video input group id is updated by audio output group id. */
    160 TEST(TestGroupId, Match_TwoIdenticalInputsMatchOutput)
    161 {
    162  MediaManager::MediaDeviceSet devices;
    163  MediaManager::MediaDeviceSet audios;
    164 
    165  nsString Cam_Model_GroupId = u"Cam-Model-GroupId"_ns;
    166  devices.AppendElement(
    167      MakeCameraDevice(u"Vendor Model"_ns, Cam_Model_GroupId));
    168 
    169  audios.AppendElement(
    170      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    171  audios.AppendElement(
    172      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    173 
    174  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
    175                                         u"Speaker-Model-GroupId"_ns));
    176 
    177  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
    178 
    179  EXPECT_EQ(devices[0]->mRawGroupID, audios[2]->mRawGroupID)
    180      << "Video group id is the same as audio output group id.";
    181 }
    182 
    183 /* Verify that when more that one audio input and more than one audio output
    184 * device names contain the video input device name the video device group id
    185 * does not change. */
    186 TEST(TestGroupId, NoMatch_ThreeIdenticalDevices)
    187 {
    188  MediaManager::MediaDeviceSet devices;
    189  MediaManager::MediaDeviceSet audios;
    190 
    191  nsString Cam_Model_GroupId = u"Cam-Model-GroupId"_ns;
    192  devices.AppendElement(
    193      MakeCameraDevice(u"Vendor Model"_ns, Cam_Model_GroupId));
    194 
    195  audios.AppendElement(
    196      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    197  audios.AppendElement(
    198      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    199  audios.AppendElement(
    200      MakeMicDevice(u"Vendor Model Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    201 
    202  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
    203                                         u"Speaker-Model-GroupId"_ns));
    204  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
    205                                         u"Speaker-Model-GroupId"_ns));
    206  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
    207                                         u"Speaker-Model-GroupId"_ns));
    208 
    209  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
    210 
    211  EXPECT_EQ(devices[0]->mRawGroupID, Cam_Model_GroupId)
    212      << "Video group id has not been updated.";
    213  EXPECT_NE(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
    214      << "Video group id is different from audio input group id.";
    215  EXPECT_NE(devices[0]->mRawGroupID, audios[3]->mRawGroupID)
    216      << "Video group id is different from audio output group id.";
    217 }
    218 
    219 /* Verify that when an audio output device name contains the video input device
    220 * name the video device group id is updated to become equal to the audio
    221 * device group id. */
    222 TEST(TestGroupId, MatchOutput)
    223 {
    224  MediaManager::MediaDeviceSet devices;
    225  MediaManager::MediaDeviceSet audios;
    226 
    227  devices.AppendElement(
    228      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));
    229 
    230  audios.AppendElement(
    231      MakeMicDevice(u"Mic Analog Stereo"_ns, u"Mic-Model-GroupId"_ns));
    232 
    233  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model Analog Stereo"_ns,
    234                                         u"Speaker-Model-GroupId"_ns));
    235 
    236  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
    237 
    238  EXPECT_EQ(devices[0]->mRawGroupID, audios[1]->mRawGroupID)
    239      << "Video group id is the same as audio output group id.";
    240 }
    241 
    242 /* Verify that when an audio input device name is the same as audio output
    243 * device and video input device name the video device group id is updated to
    244 * become equal to the audio input device group id. */
    245 TEST(TestGroupId, InputOutputSameName)
    246 {
    247  MediaManager::MediaDeviceSet devices;
    248  MediaManager::MediaDeviceSet audios;
    249 
    250  devices.AppendElement(
    251      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));
    252 
    253  audios.AppendElement(
    254      MakeMicDevice(u"Vendor Model"_ns, u"Mic-Model-GroupId"_ns));
    255 
    256  audios.AppendElement(
    257      MakeSpeakerDevice(u"Vendor Model"_ns, u"Speaker-Model-GroupId"_ns));
    258 
    259  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
    260 
    261  EXPECT_EQ(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
    262      << "Video input group id is the same as audio input group id.";
    263 }
    264 
    265 /* Verify that when an audio input device name contains the video input device
    266 * and the audio input group id is an empty string, the video device group id
    267 * is updated to become equal to the audio device group id. */
    268 TEST(TestGroupId, InputEmptyGroupId)
    269 {
    270  MediaManager::MediaDeviceSet devices;
    271  MediaManager::MediaDeviceSet audios;
    272 
    273  devices.AppendElement(
    274      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));
    275 
    276  audios.AppendElement(MakeMicDevice(u"Vendor Model"_ns, u""_ns));
    277 
    278  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
    279 
    280  EXPECT_EQ(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
    281      << "Video input group id is the same as audio input group id.";
    282 }
    283 
    284 /* Verify that when an audio output device name contains the video input device
    285 * and the audio output group id is an empty string, the video device group id
    286 * is updated to become equal to the audio output device group id. */
    287 TEST(TestGroupId, OutputEmptyGroupId)
    288 {
    289  MediaManager::MediaDeviceSet devices;
    290  MediaManager::MediaDeviceSet audios;
    291 
    292  devices.AppendElement(
    293      MakeCameraDevice(u"Vendor Model"_ns, u"Cam-Model-GroupId"_ns));
    294 
    295  audios.AppendElement(MakeSpeakerDevice(u"Vendor Model"_ns, u""_ns));
    296 
    297  MediaManager::GuessVideoDeviceGroupIDs(devices, audios);
    298 
    299  EXPECT_EQ(devices[0]->mRawGroupID, audios[0]->mRawGroupID)
    300      << "Video input group id is the same as audio output group id.";
    301 }