OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
10 #include "media/audio/audio_input_controller.h" | 10 #include "media/audio/audio_input_controller.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
| 14 #if defined(OS_ANDROID) |
| 15 #include "base/android/jni_android.h" |
| 16 #include "media/audio/audio_manager_base.h" |
| 17 #endif |
| 18 |
14 using ::testing::_; | 19 using ::testing::_; |
15 using ::testing::AtLeast; | 20 using ::testing::AtLeast; |
16 using ::testing::Exactly; | 21 using ::testing::Exactly; |
17 using ::testing::InvokeWithoutArgs; | 22 using ::testing::InvokeWithoutArgs; |
18 using ::testing::NotNull; | 23 using ::testing::NotNull; |
19 | 24 |
20 namespace media { | 25 namespace media { |
21 | 26 |
22 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; | 27 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; |
23 static const int kBitsPerSample = 16; | 28 static const int kBitsPerSample = 16; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); | 63 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); |
59 }; | 64 }; |
60 | 65 |
61 // Test fixture. | 66 // Test fixture. |
62 class AudioInputControllerTest : public testing::Test { | 67 class AudioInputControllerTest : public testing::Test { |
63 public: | 68 public: |
64 AudioInputControllerTest() {} | 69 AudioInputControllerTest() {} |
65 virtual ~AudioInputControllerTest() {} | 70 virtual ~AudioInputControllerTest() {} |
66 | 71 |
67 protected: | 72 protected: |
| 73 virtual void SetUp() { |
| 74 #if defined(OS_ANDROID) |
| 75 media::AudioManagerBase::RegisterAudioManager( |
| 76 base::android::AttachCurrentThread()); |
| 77 #endif |
| 78 } |
| 79 |
68 MessageLoop message_loop_; | 80 MessageLoop message_loop_; |
69 | 81 |
70 private: | 82 private: |
71 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); | 83 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); |
72 }; | 84 }; |
73 | 85 |
74 // Test AudioInputController for create and close without recording audio. | 86 // Test AudioInputController for create and close without recording audio. |
75 // TODO(leozwang): Because java calls were introduced in audio_manager_base, | 87 TEST_F(AudioInputControllerTest, CreateAndClose) { |
76 // unit test has to register jni first, else it will crash. | |
77 #if defined(OS_ANDROID) | |
78 #define MAYBE_CreateAndClose DISABLED_CreateAndClose | |
79 #else | |
80 #define MAYBE_CreateAndClose CreateAndClose | |
81 #endif | |
82 TEST_F(AudioInputControllerTest, MAYBE_CreateAndClose) { | |
83 MockAudioInputControllerEventHandler event_handler; | 88 MockAudioInputControllerEventHandler event_handler; |
84 | 89 |
85 // OnCreated() will be posted once. | 90 // OnCreated() will be posted once. |
86 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 91 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
87 .WillOnce(QuitMessageLoop(&message_loop_)); | 92 .WillOnce(QuitMessageLoop(&message_loop_)); |
88 | 93 |
89 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); | 94 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); |
90 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 95 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
91 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 96 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
92 scoped_refptr<AudioInputController> controller = | 97 scoped_refptr<AudioInputController> controller = |
93 AudioInputController::Create(audio_manager.get(), &event_handler, params); | 98 AudioInputController::Create(audio_manager.get(), &event_handler, params); |
94 ASSERT_TRUE(controller.get()); | 99 ASSERT_TRUE(controller.get()); |
95 | 100 |
96 // Wait for OnCreated() to fire. | 101 // Wait for OnCreated() to fire. |
97 message_loop_.Run(); | 102 message_loop_.Run(); |
98 | 103 |
99 // Close the AudioInputController synchronously. | 104 // Close the AudioInputController synchronously. |
100 CloseAudioController(controller); | 105 CloseAudioController(controller); |
101 } | 106 } |
102 | 107 |
103 // Test a normal call sequence of create, record and close. | 108 // Test a normal call sequence of create, record and close. |
104 // TODO(leozwang): Because java calls were introduced in audio_manager_base, | 109 TEST_F(AudioInputControllerTest, RecordAndClose) { |
105 // unit test has to register jni first, else it will crash. | |
106 #if defined(OS_ANDROID) | |
107 #define MAYBE_RecordAndClose DISABLED_RecordAndClose | |
108 #else | |
109 #define MAYBE_RecordAndClose RecordAndClose | |
110 #endif | |
111 TEST_F(AudioInputControllerTest, MAYBE_RecordAndClose) { | |
112 MockAudioInputControllerEventHandler event_handler; | 110 MockAudioInputControllerEventHandler event_handler; |
113 int count = 0; | 111 int count = 0; |
114 | 112 |
115 // OnCreated() will be called once. | 113 // OnCreated() will be called once. |
116 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 114 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
117 .Times(Exactly(1)); | 115 .Times(Exactly(1)); |
118 | 116 |
119 // OnRecording() will be called only once. | 117 // OnRecording() will be called only once. |
120 EXPECT_CALL(event_handler, OnRecording(NotNull())) | 118 EXPECT_CALL(event_handler, OnRecording(NotNull())) |
121 .Times(Exactly(1)); | 119 .Times(Exactly(1)); |
(...skipping 19 matching lines...) Expand all Loading... |
141 // Record and wait until ten OnData() callbacks are received. | 139 // Record and wait until ten OnData() callbacks are received. |
142 message_loop_.Run(); | 140 message_loop_.Run(); |
143 | 141 |
144 // Close the AudioInputController synchronously. | 142 // Close the AudioInputController synchronously. |
145 CloseAudioController(controller); | 143 CloseAudioController(controller); |
146 } | 144 } |
147 | 145 |
148 // Test that the AudioInputController reports an error when the input stream | 146 // Test that the AudioInputController reports an error when the input stream |
149 // stops without an OnClose() callback. This can happen when the underlying | 147 // stops without an OnClose() callback. This can happen when the underlying |
150 // audio layer stops feeding data as a result of a removed microphone device. | 148 // audio layer stops feeding data as a result of a removed microphone device. |
151 // TODO(leozwang): Because java calls were introduced in audio_manager_base, | 149 TEST_F(AudioInputControllerTest, RecordAndError) { |
152 // unit test has to register jni first to make unit test run. | |
153 #if defined(OS_ANDROID) | |
154 #define MAYBE_RecordAndError DISABLED_RecordAndError | |
155 #else | |
156 #define MAYBE_RecordAndError RecordAndError | |
157 #endif | |
158 TEST_F(AudioInputControllerTest, MAYBE_RecordAndError) { | |
159 MockAudioInputControllerEventHandler event_handler; | 150 MockAudioInputControllerEventHandler event_handler; |
160 int count = 0; | 151 int count = 0; |
161 | 152 |
162 // OnCreated() will be called once. | 153 // OnCreated() will be called once. |
163 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 154 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
164 .Times(Exactly(1)); | 155 .Times(Exactly(1)); |
165 | 156 |
166 // OnRecording() will be called only once. | 157 // OnRecording() will be called only once. |
167 EXPECT_CALL(event_handler, OnRecording(NotNull())) | 158 EXPECT_CALL(event_handler, OnRecording(NotNull())) |
168 .Times(Exactly(1)); | 159 .Times(Exactly(1)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 205 |
215 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); | 206 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); |
216 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 207 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
217 kSampleRate, kBitsPerSample, kSamplesPerPacket * 1000); | 208 kSampleRate, kBitsPerSample, kSamplesPerPacket * 1000); |
218 scoped_refptr<AudioInputController> controller = | 209 scoped_refptr<AudioInputController> controller = |
219 AudioInputController::Create(audio_manager.get(), &event_handler, params); | 210 AudioInputController::Create(audio_manager.get(), &event_handler, params); |
220 ASSERT_FALSE(controller); | 211 ASSERT_FALSE(controller); |
221 } | 212 } |
222 | 213 |
223 // Test calling AudioInputController::Close multiple times. | 214 // Test calling AudioInputController::Close multiple times. |
224 #if defined(OS_ANDROID) | 215 TEST_F(AudioInputControllerTest, CloseTwice) { |
225 #define MAYBE_CloseTwice DISABLED_CloseTwice | |
226 #else | |
227 #define MAYBE_CloseTwice CloseTwice | |
228 #endif | |
229 TEST_F(AudioInputControllerTest, MAYBE_CloseTwice) { | |
230 MockAudioInputControllerEventHandler event_handler; | 216 MockAudioInputControllerEventHandler event_handler; |
231 | 217 |
232 // OnRecording() will be called only once. | 218 // OnRecording() will be called only once. |
233 EXPECT_CALL(event_handler, OnCreated(NotNull())); | 219 EXPECT_CALL(event_handler, OnCreated(NotNull())); |
234 | 220 |
235 // OnRecording() will be called only once. | 221 // OnRecording() will be called only once. |
236 EXPECT_CALL(event_handler, OnRecording(NotNull())) | 222 EXPECT_CALL(event_handler, OnRecording(NotNull())) |
237 .Times(Exactly(1)); | 223 .Times(Exactly(1)); |
238 | 224 |
239 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); | 225 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); |
240 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 226 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
241 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 227 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
242 scoped_refptr<AudioInputController> controller = | 228 scoped_refptr<AudioInputController> controller = |
243 AudioInputController::Create(audio_manager.get(), &event_handler, params); | 229 AudioInputController::Create(audio_manager.get(), &event_handler, params); |
244 ASSERT_TRUE(controller.get()); | 230 ASSERT_TRUE(controller.get()); |
245 | 231 |
246 controller->Record(); | 232 controller->Record(); |
247 | 233 |
248 controller->Close(MessageLoop::QuitClosure()); | 234 controller->Close(MessageLoop::QuitClosure()); |
249 MessageLoop::current()->Run(); | 235 MessageLoop::current()->Run(); |
250 | 236 |
251 controller->Close(MessageLoop::QuitClosure()); | 237 controller->Close(MessageLoop::QuitClosure()); |
252 MessageLoop::current()->Run(); | 238 MessageLoop::current()->Run(); |
253 } | 239 } |
254 | 240 |
255 } // namespace media | 241 } // namespace media |
OLD | NEW |