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
15namespace rtc {
16
17class 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
29class 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
44bool InitCheckingSoundSystem1::created_ = false;
45
46class 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
61bool InitCheckingSoundSystem2::created_ = false;
62
63class 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
76bool DeletionCheckingSoundSystem1::deleted_ = false;
77
78class 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
91bool DeletionCheckingSoundSystem2::deleted_ = false;
92
93class 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
106bool DeletionCheckingSoundSystem3::deleted_ = false;
107
108extern const SoundSystemCreator kSingleSystemFailingCreators[] = {
109  &NeverFailsToFailSoundSystem::Create,
110};
111
112TEST(AutomaticallyChosenSoundSystem, SingleSystemFailing) {
113  AutomaticallyChosenSoundSystem<
114      kSingleSystemFailingCreators,
115      ARRAY_SIZE(kSingleSystemFailingCreators)> sound_system;
116  EXPECT_FALSE(sound_system.Init());
117}
118
119extern const SoundSystemCreator kSingleSystemSucceedingCreators[] = {
120  &NullSoundSystem::Create,
121};
122
123TEST(AutomaticallyChosenSoundSystem, SingleSystemSucceeding) {
124  AutomaticallyChosenSoundSystem<
125      kSingleSystemSucceedingCreators,
126      ARRAY_SIZE(kSingleSystemSucceedingCreators)> sound_system;
127  EXPECT_TRUE(sound_system.Init());
128}
129
130extern const SoundSystemCreator
131    kFailedFirstSystemResultsInUsingSecondCreators[] = {
132  &NeverFailsToFailSoundSystem::Create,
133  &NullSoundSystem::Create,
134};
135
136TEST(AutomaticallyChosenSoundSystem, FailedFirstSystemResultsInUsingSecond) {
137  AutomaticallyChosenSoundSystem<
138      kFailedFirstSystemResultsInUsingSecondCreators,
139      ARRAY_SIZE(kFailedFirstSystemResultsInUsingSecondCreators)> sound_system;
140  EXPECT_TRUE(sound_system.Init());
141}
142
143extern const SoundSystemCreator kEarlierEntriesHavePriorityCreators[] = {
144  &InitCheckingSoundSystem1::Create,
145  &InitCheckingSoundSystem2::Create,
146};
147
148TEST(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
159extern 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
169TEST(AutomaticallyChosenSoundSystem, ManySoundSystems) {
170  AutomaticallyChosenSoundSystem<
171      kManySoundSystemsCreators,
172      ARRAY_SIZE(kManySoundSystemsCreators)> sound_system;
173  EXPECT_TRUE(sound_system.Init());
174}
175
176extern const SoundSystemCreator kDeletesAllCreatedSoundSystemsCreators[] = {
177  &DeletionCheckingSoundSystem1::Create,
178  &DeletionCheckingSoundSystem2::Create,
179  &DeletionCheckingSoundSystem3::Create,
180};
181
182TEST(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
198