1 /* 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/sound/automaticallychosensoundsystem.h" 12 13 #include "webrtc/base/arraysize.h" 14 #include "webrtc/base/gunit.h" 15 #include "webrtc/sound/nullsoundsystem.h" 16 17 namespace rtc { 18 19 class NeverFailsToFailSoundSystem : public NullSoundSystem { 20 public: 21 // Overrides superclass. 22 virtual bool Init() { 23 return false; 24 } 25 26 static SoundSystemInterface *Create() { 27 return new NeverFailsToFailSoundSystem(); 28 } 29 }; 30 31 class InitCheckingSoundSystem1 : public NullSoundSystem { 32 public: 33 // Overrides superclass. 34 virtual bool Init() { 35 created_ = true; 36 return true; 37 } 38 39 static SoundSystemInterface *Create() { 40 return new InitCheckingSoundSystem1(); 41 } 42 43 static bool created_; 44 }; 45 46 bool InitCheckingSoundSystem1::created_ = false; 47 48 class InitCheckingSoundSystem2 : public NullSoundSystem { 49 public: 50 // Overrides superclass. 51 virtual bool Init() { 52 created_ = true; 53 return true; 54 } 55 56 static SoundSystemInterface *Create() { 57 return new InitCheckingSoundSystem2(); 58 } 59 60 static bool created_; 61 }; 62 63 bool InitCheckingSoundSystem2::created_ = false; 64 65 class DeletionCheckingSoundSystem1 : public NeverFailsToFailSoundSystem { 66 public: 67 virtual ~DeletionCheckingSoundSystem1() { 68 deleted_ = true; 69 } 70 71 static SoundSystemInterface *Create() { 72 return new DeletionCheckingSoundSystem1(); 73 } 74 75 static bool deleted_; 76 }; 77 78 bool DeletionCheckingSoundSystem1::deleted_ = false; 79 80 class DeletionCheckingSoundSystem2 : public NeverFailsToFailSoundSystem { 81 public: 82 virtual ~DeletionCheckingSoundSystem2() { 83 deleted_ = true; 84 } 85 86 static SoundSystemInterface *Create() { 87 return new DeletionCheckingSoundSystem2(); 88 } 89 90 static bool deleted_; 91 }; 92 93 bool DeletionCheckingSoundSystem2::deleted_ = false; 94 95 class DeletionCheckingSoundSystem3 : public NullSoundSystem { 96 public: 97 virtual ~DeletionCheckingSoundSystem3() { 98 deleted_ = true; 99 } 100 101 static SoundSystemInterface *Create() { 102 return new DeletionCheckingSoundSystem3(); 103 } 104 105 static bool deleted_; 106 }; 107 108 bool DeletionCheckingSoundSystem3::deleted_ = false; 109 110 extern const SoundSystemCreator kSingleSystemFailingCreators[] = { 111 &NeverFailsToFailSoundSystem::Create, 112 }; 113 114 TEST(AutomaticallyChosenSoundSystem, SingleSystemFailing) { 115 AutomaticallyChosenSoundSystem< 116 kSingleSystemFailingCreators, 117 arraysize(kSingleSystemFailingCreators)> sound_system; 118 EXPECT_FALSE(sound_system.Init()); 119 } 120 121 extern const SoundSystemCreator kSingleSystemSucceedingCreators[] = { 122 &NullSoundSystem::Create, 123 }; 124 125 TEST(AutomaticallyChosenSoundSystem, SingleSystemSucceeding) { 126 AutomaticallyChosenSoundSystem< 127 kSingleSystemSucceedingCreators, 128 arraysize(kSingleSystemSucceedingCreators)> sound_system; 129 EXPECT_TRUE(sound_system.Init()); 130 } 131 132 extern const SoundSystemCreator 133 kFailedFirstSystemResultsInUsingSecondCreators[] = { 134 &NeverFailsToFailSoundSystem::Create, 135 &NullSoundSystem::Create, 136 }; 137 138 TEST(AutomaticallyChosenSoundSystem, FailedFirstSystemResultsInUsingSecond) { 139 AutomaticallyChosenSoundSystem< 140 kFailedFirstSystemResultsInUsingSecondCreators, 141 arraysize(kFailedFirstSystemResultsInUsingSecondCreators)> sound_system; 142 EXPECT_TRUE(sound_system.Init()); 143 } 144 145 extern const SoundSystemCreator kEarlierEntriesHavePriorityCreators[] = { 146 &InitCheckingSoundSystem1::Create, 147 &InitCheckingSoundSystem2::Create, 148 }; 149 150 TEST(AutomaticallyChosenSoundSystem, EarlierEntriesHavePriority) { 151 AutomaticallyChosenSoundSystem< 152 kEarlierEntriesHavePriorityCreators, 153 arraysize(kEarlierEntriesHavePriorityCreators)> sound_system; 154 InitCheckingSoundSystem1::created_ = false; 155 InitCheckingSoundSystem2::created_ = false; 156 EXPECT_TRUE(sound_system.Init()); 157 EXPECT_TRUE(InitCheckingSoundSystem1::created_); 158 EXPECT_FALSE(InitCheckingSoundSystem2::created_); 159 } 160 161 extern const SoundSystemCreator kManySoundSystemsCreators[] = { 162 &NullSoundSystem::Create, 163 &NullSoundSystem::Create, 164 &NullSoundSystem::Create, 165 &NullSoundSystem::Create, 166 &NullSoundSystem::Create, 167 &NullSoundSystem::Create, 168 &NullSoundSystem::Create, 169 }; 170 171 TEST(AutomaticallyChosenSoundSystem, ManySoundSystems) { 172 AutomaticallyChosenSoundSystem< 173 kManySoundSystemsCreators, 174 arraysize(kManySoundSystemsCreators)> sound_system; 175 EXPECT_TRUE(sound_system.Init()); 176 } 177 178 extern const SoundSystemCreator kDeletesAllCreatedSoundSystemsCreators[] = { 179 &DeletionCheckingSoundSystem1::Create, 180 &DeletionCheckingSoundSystem2::Create, 181 &DeletionCheckingSoundSystem3::Create, 182 }; 183 184 TEST(AutomaticallyChosenSoundSystem, DeletesAllCreatedSoundSystems) { 185 typedef AutomaticallyChosenSoundSystem< 186 kDeletesAllCreatedSoundSystemsCreators, 187 arraysize(kDeletesAllCreatedSoundSystemsCreators)> TestSoundSystem; 188 TestSoundSystem *sound_system = new TestSoundSystem(); 189 DeletionCheckingSoundSystem1::deleted_ = false; 190 DeletionCheckingSoundSystem2::deleted_ = false; 191 DeletionCheckingSoundSystem3::deleted_ = false; 192 EXPECT_TRUE(sound_system->Init()); 193 delete sound_system; 194 EXPECT_TRUE(DeletionCheckingSoundSystem1::deleted_); 195 EXPECT_TRUE(DeletionCheckingSoundSystem2::deleted_); 196 EXPECT_TRUE(DeletionCheckingSoundSystem3::deleted_); 197 } 198 199 } // namespace rtc 200