Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / sound / automaticallychosensoundsystem_unittest.cc
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 #include "webrtc/sound/nullsoundsystem.h"
13 #include "webrtc/base/gunit.h"
14
15 namespace rtc {
16
17 class NeverFailsToFailSoundSystem : public NullSoundSystem {
18  public:
19   // Overrides superclass.
20   virtual bool Init() {
21     return false;
22   }
23
24   static SoundSystemInterface *Create() {
25     return new NeverFailsToFailSoundSystem();
26   }
27 };
28
29 class InitCheckingSoundSystem1 : public NullSoundSystem {
30  public:
31   // Overrides superclass.
32   virtual bool Init() {
33     created_ = true;
34     return true;
35   }
36
37   static SoundSystemInterface *Create() {
38     return new InitCheckingSoundSystem1();
39   }
40
41   static bool created_;
42 };
43
44 bool InitCheckingSoundSystem1::created_ = false;
45
46 class InitCheckingSoundSystem2 : 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 InitCheckingSoundSystem2();
56   }
57
58   static bool created_;
59 };
60
61 bool InitCheckingSoundSystem2::created_ = false;
62
63 class DeletionCheckingSoundSystem1 : public NeverFailsToFailSoundSystem {
64  public:
65   virtual ~DeletionCheckingSoundSystem1() {
66     deleted_ = true;
67   }
68
69   static SoundSystemInterface *Create() {
70     return new DeletionCheckingSoundSystem1();
71   }
72
73   static bool deleted_;
74 };
75
76 bool DeletionCheckingSoundSystem1::deleted_ = false;
77
78 class DeletionCheckingSoundSystem2 : public NeverFailsToFailSoundSystem {
79  public:
80   virtual ~DeletionCheckingSoundSystem2() {
81     deleted_ = true;
82   }
83
84   static SoundSystemInterface *Create() {
85     return new DeletionCheckingSoundSystem2();
86   }
87
88   static bool deleted_;
89 };
90
91 bool DeletionCheckingSoundSystem2::deleted_ = false;
92
93 class DeletionCheckingSoundSystem3 : public NullSoundSystem {
94  public:
95   virtual ~DeletionCheckingSoundSystem3() {
96     deleted_ = true;
97   }
98
99   static SoundSystemInterface *Create() {
100     return new DeletionCheckingSoundSystem3();
101   }
102
103   static bool deleted_;
104 };
105
106 bool DeletionCheckingSoundSystem3::deleted_ = false;
107
108 extern const SoundSystemCreator kSingleSystemFailingCreators[] = {
109   &NeverFailsToFailSoundSystem::Create,
110 };
111
112 TEST(AutomaticallyChosenSoundSystem, SingleSystemFailing) {
113   AutomaticallyChosenSoundSystem<
114       kSingleSystemFailingCreators,
115       ARRAY_SIZE(kSingleSystemFailingCreators)> sound_system;
116   EXPECT_FALSE(sound_system.Init());
117 }
118
119 extern const SoundSystemCreator kSingleSystemSucceedingCreators[] = {
120   &NullSoundSystem::Create,
121 };
122
123 TEST(AutomaticallyChosenSoundSystem, SingleSystemSucceeding) {
124   AutomaticallyChosenSoundSystem<
125       kSingleSystemSucceedingCreators,
126       ARRAY_SIZE(kSingleSystemSucceedingCreators)> sound_system;
127   EXPECT_TRUE(sound_system.Init());
128 }
129
130 extern const SoundSystemCreator
131     kFailedFirstSystemResultsInUsingSecondCreators[] = {
132   &NeverFailsToFailSoundSystem::Create,
133   &NullSoundSystem::Create,
134 };
135
136 TEST(AutomaticallyChosenSoundSystem, FailedFirstSystemResultsInUsingSecond) {
137   AutomaticallyChosenSoundSystem<
138       kFailedFirstSystemResultsInUsingSecondCreators,
139       ARRAY_SIZE(kFailedFirstSystemResultsInUsingSecondCreators)> sound_system;
140   EXPECT_TRUE(sound_system.Init());
141 }
142
143 extern const SoundSystemCreator kEarlierEntriesHavePriorityCreators[] = {
144   &InitCheckingSoundSystem1::Create,
145   &InitCheckingSoundSystem2::Create,
146 };
147
148 TEST(AutomaticallyChosenSoundSystem, EarlierEntriesHavePriority) {
149   AutomaticallyChosenSoundSystem<
150       kEarlierEntriesHavePriorityCreators,
151       ARRAY_SIZE(kEarlierEntriesHavePriorityCreators)> sound_system;
152   InitCheckingSoundSystem1::created_ = false;
153   InitCheckingSoundSystem2::created_ = false;
154   EXPECT_TRUE(sound_system.Init());
155   EXPECT_TRUE(InitCheckingSoundSystem1::created_);
156   EXPECT_FALSE(InitCheckingSoundSystem2::created_);
157 }
158
159 extern const SoundSystemCreator kManySoundSystemsCreators[] = {
160   &NullSoundSystem::Create,
161   &NullSoundSystem::Create,
162   &NullSoundSystem::Create,
163   &NullSoundSystem::Create,
164   &NullSoundSystem::Create,
165   &NullSoundSystem::Create,
166   &NullSoundSystem::Create,
167 };
168
169 TEST(AutomaticallyChosenSoundSystem, ManySoundSystems) {
170   AutomaticallyChosenSoundSystem<
171       kManySoundSystemsCreators,
172       ARRAY_SIZE(kManySoundSystemsCreators)> sound_system;
173   EXPECT_TRUE(sound_system.Init());
174 }
175
176 extern const SoundSystemCreator kDeletesAllCreatedSoundSystemsCreators[] = {
177   &DeletionCheckingSoundSystem1::Create,
178   &DeletionCheckingSoundSystem2::Create,
179   &DeletionCheckingSoundSystem3::Create,
180 };
181
182 TEST(AutomaticallyChosenSoundSystem, DeletesAllCreatedSoundSystems) {
183   typedef AutomaticallyChosenSoundSystem<
184       kDeletesAllCreatedSoundSystemsCreators,
185       ARRAY_SIZE(kDeletesAllCreatedSoundSystemsCreators)> TestSoundSystem;
186   TestSoundSystem *sound_system = new TestSoundSystem();
187   DeletionCheckingSoundSystem1::deleted_ = false;
188   DeletionCheckingSoundSystem2::deleted_ = false;
189   DeletionCheckingSoundSystem3::deleted_ = false;
190   EXPECT_TRUE(sound_system->Init());
191   delete sound_system;
192   EXPECT_TRUE(DeletionCheckingSoundSystem1::deleted_);
193   EXPECT_TRUE(DeletionCheckingSoundSystem2::deleted_);
194   EXPECT_TRUE(DeletionCheckingSoundSystem3::deleted_);
195 }
196
197 }  // namespace rtc