1 /* 2 * libjingle 3 * Copyright 2004--2010, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "talk/base/gunit.h" 29 #include "talk/sound/automaticallychosensoundsystem.h" 30 #include "talk/sound/nullsoundsystem.h" 31 32 namespace cricket { 33 34 class NeverFailsToFailSoundSystem : public NullSoundSystem { 35 public: 36 // Overrides superclass. 37 virtual bool Init() { 38 return false; 39 } 40 41 static SoundSystemInterface *Create() { 42 return new NeverFailsToFailSoundSystem(); 43 } 44 }; 45 46 class InitCheckingSoundSystem1 : public NullSoundSystem { 47 public: 48 // Overrides superclass. 49 virtual bool Init() { 50 created_ = true; 51 return true; 52 } 53 54 static SoundSystemInterface *Create() { 55 return new InitCheckingSoundSystem1(); 56 } 57 58 static bool created_; 59 }; 60 61 bool InitCheckingSoundSystem1::created_ = false; 62 63 class InitCheckingSoundSystem2 : public NullSoundSystem { 64 public: 65 // Overrides superclass. 66 virtual bool Init() { 67 created_ = true; 68 return true; 69 } 70 71 static SoundSystemInterface *Create() { 72 return new InitCheckingSoundSystem2(); 73 } 74 75 static bool created_; 76 }; 77 78 bool InitCheckingSoundSystem2::created_ = false; 79 80 class DeletionCheckingSoundSystem1 : public NeverFailsToFailSoundSystem { 81 public: 82 virtual ~DeletionCheckingSoundSystem1() { 83 deleted_ = true; 84 } 85 86 static SoundSystemInterface *Create() { 87 return new DeletionCheckingSoundSystem1(); 88 } 89 90 static bool deleted_; 91 }; 92 93 bool DeletionCheckingSoundSystem1::deleted_ = false; 94 95 class DeletionCheckingSoundSystem2 : public NeverFailsToFailSoundSystem { 96 public: 97 virtual ~DeletionCheckingSoundSystem2() { 98 deleted_ = true; 99 } 100 101 static SoundSystemInterface *Create() { 102 return new DeletionCheckingSoundSystem2(); 103 } 104 105 static bool deleted_; 106 }; 107 108 bool DeletionCheckingSoundSystem2::deleted_ = false; 109 110 class DeletionCheckingSoundSystem3 : public NullSoundSystem { 111 public: 112 virtual ~DeletionCheckingSoundSystem3() { 113 deleted_ = true; 114 } 115 116 static SoundSystemInterface *Create() { 117 return new DeletionCheckingSoundSystem3(); 118 } 119 120 static bool deleted_; 121 }; 122 123 bool DeletionCheckingSoundSystem3::deleted_ = false; 124 125 extern const SoundSystemCreator kSingleSystemFailingCreators[] = { 126 &NeverFailsToFailSoundSystem::Create, 127 }; 128 129 TEST(AutomaticallyChosenSoundSystem, SingleSystemFailing) { 130 AutomaticallyChosenSoundSystem< 131 kSingleSystemFailingCreators, 132 ARRAY_SIZE(kSingleSystemFailingCreators)> sound_system; 133 EXPECT_FALSE(sound_system.Init()); 134 } 135 136 extern const SoundSystemCreator kSingleSystemSucceedingCreators[] = { 137 &NullSoundSystem::Create, 138 }; 139 140 TEST(AutomaticallyChosenSoundSystem, SingleSystemSucceeding) { 141 AutomaticallyChosenSoundSystem< 142 kSingleSystemSucceedingCreators, 143 ARRAY_SIZE(kSingleSystemSucceedingCreators)> sound_system; 144 EXPECT_TRUE(sound_system.Init()); 145 } 146 147 extern const SoundSystemCreator 148 kFailedFirstSystemResultsInUsingSecondCreators[] = { 149 &NeverFailsToFailSoundSystem::Create, 150 &NullSoundSystem::Create, 151 }; 152 153 TEST(AutomaticallyChosenSoundSystem, FailedFirstSystemResultsInUsingSecond) { 154 AutomaticallyChosenSoundSystem< 155 kFailedFirstSystemResultsInUsingSecondCreators, 156 ARRAY_SIZE(kFailedFirstSystemResultsInUsingSecondCreators)> sound_system; 157 EXPECT_TRUE(sound_system.Init()); 158 } 159 160 extern const SoundSystemCreator kEarlierEntriesHavePriorityCreators[] = { 161 &InitCheckingSoundSystem1::Create, 162 &InitCheckingSoundSystem2::Create, 163 }; 164 165 TEST(AutomaticallyChosenSoundSystem, EarlierEntriesHavePriority) { 166 AutomaticallyChosenSoundSystem< 167 kEarlierEntriesHavePriorityCreators, 168 ARRAY_SIZE(kEarlierEntriesHavePriorityCreators)> sound_system; 169 InitCheckingSoundSystem1::created_ = false; 170 InitCheckingSoundSystem2::created_ = false; 171 EXPECT_TRUE(sound_system.Init()); 172 EXPECT_TRUE(InitCheckingSoundSystem1::created_); 173 EXPECT_FALSE(InitCheckingSoundSystem2::created_); 174 } 175 176 extern const SoundSystemCreator kManySoundSystemsCreators[] = { 177 &NullSoundSystem::Create, 178 &NullSoundSystem::Create, 179 &NullSoundSystem::Create, 180 &NullSoundSystem::Create, 181 &NullSoundSystem::Create, 182 &NullSoundSystem::Create, 183 &NullSoundSystem::Create, 184 }; 185 186 TEST(AutomaticallyChosenSoundSystem, ManySoundSystems) { 187 AutomaticallyChosenSoundSystem< 188 kManySoundSystemsCreators, 189 ARRAY_SIZE(kManySoundSystemsCreators)> sound_system; 190 EXPECT_TRUE(sound_system.Init()); 191 } 192 193 extern const SoundSystemCreator kDeletesAllCreatedSoundSystemsCreators[] = { 194 &DeletionCheckingSoundSystem1::Create, 195 &DeletionCheckingSoundSystem2::Create, 196 &DeletionCheckingSoundSystem3::Create, 197 }; 198 199 TEST(AutomaticallyChosenSoundSystem, DeletesAllCreatedSoundSystems) { 200 typedef AutomaticallyChosenSoundSystem< 201 kDeletesAllCreatedSoundSystemsCreators, 202 ARRAY_SIZE(kDeletesAllCreatedSoundSystemsCreators)> TestSoundSystem; 203 TestSoundSystem *sound_system = new TestSoundSystem(); 204 DeletionCheckingSoundSystem1::deleted_ = false; 205 DeletionCheckingSoundSystem2::deleted_ = false; 206 DeletionCheckingSoundSystem3::deleted_ = false; 207 EXPECT_TRUE(sound_system->Init()); 208 delete sound_system; 209 EXPECT_TRUE(DeletionCheckingSoundSystem1::deleted_); 210 EXPECT_TRUE(DeletionCheckingSoundSystem2::deleted_); 211 EXPECT_TRUE(DeletionCheckingSoundSystem3::deleted_); 212 } 213 214 } // namespace cricket 215