Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: media/audio/audio_input_controller_unittest.cc

Issue 2624403002: Refactor AudioInputController and related interfaces. (Closed)
Patch Set: Address comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/audio_input_controller.cc ('k') | media/audio/audio_io.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/bind.h" 5 #include "base/bind.h"
6 #include "base/macros.h" 6 #include "base/macros.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/test/test_timeouts.h" 10 #include "base/test/test_timeouts.h"
11 #include "base/threading/thread.h"
11 #include "media/audio/audio_device_description.h" 12 #include "media/audio/audio_device_description.h"
12 #include "media/audio/audio_input_controller.h" 13 #include "media/audio/audio_input_controller.h"
13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 using ::testing::_; 17 using ::testing::_;
18 using ::testing::AnyNumber;
17 using ::testing::AtLeast; 19 using ::testing::AtLeast;
18 using ::testing::Exactly; 20 using ::testing::Exactly;
19 using ::testing::InvokeWithoutArgs; 21 using ::testing::InvokeWithoutArgs;
20 using ::testing::NotNull; 22 using ::testing::NotNull;
23 using base::WaitableEvent;
21 24
22 namespace media { 25 namespace media {
23 26
24 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; 27 static const int kSampleRate = AudioParameters::kAudioCDSampleRate;
25 static const int kBitsPerSample = 16; 28 static const int kBitsPerSample = 16;
26 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; 29 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
27 static const int kSamplesPerPacket = kSampleRate / 10; 30 static const int kSamplesPerPacket = kSampleRate / 10;
28 31
29 ACTION_P(QuitRunLoop, run_loop) { 32 ACTION_P(QuitRunLoop, run_loop) {
30 run_loop->QuitWhenIdle(); 33 run_loop->QuitWhenIdle();
(...skipping 15 matching lines...) Expand all
46 } 49 }
47 50
48 class MockAudioInputControllerEventHandler 51 class MockAudioInputControllerEventHandler
49 : public AudioInputController::EventHandler { 52 : public AudioInputController::EventHandler {
50 public: 53 public:
51 MockAudioInputControllerEventHandler() {} 54 MockAudioInputControllerEventHandler() {}
52 55
53 MOCK_METHOD1(OnCreated, void(AudioInputController* controller)); 56 MOCK_METHOD1(OnCreated, void(AudioInputController* controller));
54 MOCK_METHOD2(OnError, void(AudioInputController* controller, 57 MOCK_METHOD2(OnError, void(AudioInputController* controller,
55 AudioInputController::ErrorCode error_code)); 58 AudioInputController::ErrorCode error_code));
56 MOCK_METHOD2(OnData,
57 void(AudioInputController* controller, const AudioBus* data));
58 MOCK_METHOD2(OnLog, 59 MOCK_METHOD2(OnLog,
59 void(AudioInputController* controller, 60 void(AudioInputController* controller,
60 const std::string& message)); 61 const std::string& message));
61 62
62 private: 63 private:
63 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); 64 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler);
64 }; 65 };
65 66
67 class MockSyncWriter : public AudioInputController::SyncWriter {
68 public:
69 MockSyncWriter() {}
70
71 MOCK_METHOD4(Write,
72 void(const AudioBus* data,
73 double volume,
74 bool key_pressed,
75 uint32_t hardware_delay_bytes));
76 MOCK_METHOD0(Close, void());
77 };
78
66 // Test fixture. 79 // Test fixture.
67 class AudioInputControllerTest : public testing::Test { 80 class AudioInputControllerTest : public testing::Test {
68 public: 81 public:
69 AudioInputControllerTest() 82 AudioInputControllerTest()
70 : audio_manager_( 83 : audio_thread_("AudioThread"),
71 AudioManager::CreateForTesting(message_loop_.task_runner())) { 84 suspend_event_(WaitableEvent::ResetPolicy::AUTOMATIC,
72 // Flush the message loop to ensure that AudioManager is fully initialized. 85 WaitableEvent::InitialState::NOT_SIGNALED) {
73 base::RunLoop().RunUntilIdle(); 86 audio_thread_.StartAndWaitForTesting();
87 audio_manager_ =
88 AudioManager::CreateForTesting(audio_thread_.task_runner());
74 } 89 }
75 ~AudioInputControllerTest() override { 90 ~AudioInputControllerTest() override {
76 audio_manager_.reset(); 91 audio_task_runner()->PostTask(
77 base::RunLoop().RunUntilIdle(); 92 FROM_HERE, base::Bind(&AudioInputControllerTest::DeleteAudioManager,
93 base::Unretained(this)));
94 audio_thread_.Stop();
78 } 95 }
79 96
97 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner() const {
98 return audio_thread_.task_runner();
99 }
100
101 void SuspendAudioThread() {
102 audio_task_runner()->PostTask(
103 FROM_HERE, base::Bind(&AudioInputControllerTest::WaitForResume,
104 base::Unretained(this)));
105 }
106
107 void ResumeAudioThread() { suspend_event_.Signal(); }
108
109 private:
110 void DeleteAudioManager() { audio_manager_.reset(); }
111 void WaitForResume() { suspend_event_.Wait(); }
112
80 protected: 113 protected:
114 base::Thread audio_thread_;
81 base::MessageLoop message_loop_; 115 base::MessageLoop message_loop_;
82 ScopedAudioManagerPtr audio_manager_; 116 ScopedAudioManagerPtr audio_manager_;
117 WaitableEvent suspend_event_;
83 118
84 private: 119 private:
85 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); 120 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest);
86 }; 121 };
87 122
88 // Test AudioInputController for create and close without recording audio. 123 // Test AudioInputController for create and close without recording audio.
89 TEST_F(AudioInputControllerTest, CreateAndClose) { 124 TEST_F(AudioInputControllerTest, CreateAndClose) {
90 base::RunLoop run_loop; 125 base::RunLoop run_loop;
91 126
92 MockAudioInputControllerEventHandler event_handler; 127 MockAudioInputControllerEventHandler event_handler;
93 128 MockSyncWriter sync_writer;
94 // OnCreated() will be posted once. 129 scoped_refptr<AudioInputController> controller;
95 EXPECT_CALL(event_handler, OnCreated(NotNull()))
96 .WillOnce(QuitRunLoop(&run_loop));
97 130
98 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, 131 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
99 kSampleRate, kBitsPerSample, kSamplesPerPacket); 132 kSampleRate, kBitsPerSample, kSamplesPerPacket);
100 133
101 scoped_refptr<AudioInputController> controller = AudioInputController::Create( 134 SuspendAudioThread();
102 audio_manager_.get(), &event_handler, params, 135 controller = AudioInputController::Create(
136 audio_manager_.get(), &event_handler, &sync_writer, params,
103 AudioDeviceDescription::kDefaultDeviceId, NULL); 137 AudioDeviceDescription::kDefaultDeviceId, NULL);
104 ASSERT_TRUE(controller.get()); 138 ASSERT_TRUE(controller.get());
139 EXPECT_CALL(event_handler, OnCreated(controller.get())).Times(Exactly(1));
140 EXPECT_CALL(event_handler, OnLog(controller.get(), _));
141 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
142 ResumeAudioThread();
105 143
106 // Wait for OnCreated() to fire. 144 CloseAudioController(controller.get());
107 run_loop.Run();
108 145
109 // Close the AudioInputController synchronously. 146 audio_thread_.FlushForTesting();
110 CloseAudioController(controller.get());
111 } 147 }
112 148
113 // Test a normal call sequence of create, record and close. 149 // Test a normal call sequence of create, record and close.
114 TEST_F(AudioInputControllerTest, RecordAndClose) { 150 TEST_F(AudioInputControllerTest, RecordAndClose) {
115 MockAudioInputControllerEventHandler event_handler; 151 MockAudioInputControllerEventHandler event_handler;
152 MockSyncWriter sync_writer;
116 int count = 0; 153 int count = 0;
117 154
118 // OnCreated() will be called once. 155 // OnCreated() will be called once.
119 EXPECT_CALL(event_handler, OnCreated(NotNull())) 156 EXPECT_CALL(event_handler, OnCreated(NotNull()))
120 .Times(Exactly(1)); 157 .Times(Exactly(1));
121 158
122 // OnData() shall be called ten times. 159 // Write() should be called ten times.
123 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull())) 160 EXPECT_CALL(sync_writer, Write(NotNull(), _, _, _))
124 .Times(AtLeast(10)) 161 .Times(AtLeast(10))
125 .WillRepeatedly(CheckCountAndPostQuitTask( 162 .WillRepeatedly(
126 &count, 10, message_loop_.task_runner())); 163 CheckCountAndPostQuitTask(&count, 10, message_loop_.task_runner()));
164
165 EXPECT_CALL(event_handler, OnLog(_, _)).Times(AnyNumber());
166 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
127 167
128 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, 168 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
129 kSampleRate, kBitsPerSample, kSamplesPerPacket); 169 kSampleRate, kBitsPerSample, kSamplesPerPacket);
130 170
131 // Creating the AudioInputController should render an OnCreated() call. 171 // Creating the AudioInputController should render an OnCreated() call.
132 scoped_refptr<AudioInputController> controller = AudioInputController::Create( 172 scoped_refptr<AudioInputController> controller = AudioInputController::Create(
133 audio_manager_.get(), &event_handler, params, 173 audio_manager_.get(), &event_handler, &sync_writer, params,
134 AudioDeviceDescription::kDefaultDeviceId, NULL); 174 AudioDeviceDescription::kDefaultDeviceId, NULL);
135 ASSERT_TRUE(controller.get()); 175 ASSERT_TRUE(controller.get());
136 176
137 controller->Record(); 177 controller->Record();
138 178
139 // Record and wait until ten OnData() callbacks are received. 179 // Record and wait until ten Write() callbacks are received.
140 base::RunLoop().Run(); 180 base::RunLoop().Run();
141
142 // Close the AudioInputController synchronously.
143 CloseAudioController(controller.get()); 181 CloseAudioController(controller.get());
144 } 182 }
145 183
146 // Test that AudioInputController rejects insanely large packet sizes. 184 // Test that AudioInputController rejects insanely large packet sizes.
147 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { 185 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) {
148 // Create an audio device with a very large packet size. 186 // Create an audio device with a very large packet size.
149 MockAudioInputControllerEventHandler event_handler; 187 MockAudioInputControllerEventHandler event_handler;
188 MockSyncWriter sync_writer;
150 189
151 // OnCreated() shall not be called in this test. 190 // OnCreated() shall not be called in this test.
152 EXPECT_CALL(event_handler, OnCreated(NotNull())) 191 EXPECT_CALL(event_handler, OnCreated(NotNull())).Times(Exactly(0));
153 .Times(Exactly(0));
154 192
155 AudioParameters params(AudioParameters::AUDIO_FAKE, 193 AudioParameters params(AudioParameters::AUDIO_FAKE,
156 kChannelLayout, 194 kChannelLayout,
157 kSampleRate, 195 kSampleRate,
158 kBitsPerSample, 196 kBitsPerSample,
159 kSamplesPerPacket * 1000); 197 kSamplesPerPacket * 1000);
160 scoped_refptr<AudioInputController> controller = AudioInputController::Create( 198 scoped_refptr<AudioInputController> controller = AudioInputController::Create(
161 audio_manager_.get(), &event_handler, params, 199 audio_manager_.get(), &event_handler, &sync_writer, params,
162 AudioDeviceDescription::kDefaultDeviceId, NULL); 200 AudioDeviceDescription::kDefaultDeviceId, NULL);
163 ASSERT_FALSE(controller.get()); 201 ASSERT_FALSE(controller.get());
164 } 202 }
165 203
166 // Test calling AudioInputController::Close multiple times. 204 // Test calling AudioInputController::Close multiple times.
167 TEST_F(AudioInputControllerTest, CloseTwice) { 205 TEST_F(AudioInputControllerTest, CloseTwice) {
168 MockAudioInputControllerEventHandler event_handler; 206 MockAudioInputControllerEventHandler event_handler;
207 MockSyncWriter sync_writer;
169 208
170 // OnCreated() will be called only once. 209 // OnCreated() will be called only once.
171 EXPECT_CALL(event_handler, OnCreated(NotNull())); 210 EXPECT_CALL(event_handler, OnCreated(NotNull())).Times(Exactly(1));
211 EXPECT_CALL(event_handler, OnLog(_, _)).Times(AnyNumber());
212 // This callback should still only be called once.
213 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
172 214
173 AudioParameters params(AudioParameters::AUDIO_FAKE, 215 AudioParameters params(AudioParameters::AUDIO_FAKE,
174 kChannelLayout, 216 kChannelLayout,
175 kSampleRate, 217 kSampleRate,
176 kBitsPerSample, 218 kBitsPerSample,
177 kSamplesPerPacket); 219 kSamplesPerPacket);
178 scoped_refptr<AudioInputController> controller = AudioInputController::Create( 220 scoped_refptr<AudioInputController> controller = AudioInputController::Create(
179 audio_manager_.get(), &event_handler, params, 221 audio_manager_.get(), &event_handler, &sync_writer, params,
180 AudioDeviceDescription::kDefaultDeviceId, NULL); 222 AudioDeviceDescription::kDefaultDeviceId, NULL);
181 ASSERT_TRUE(controller.get()); 223 ASSERT_TRUE(controller.get());
182 224
183 controller->Record(); 225 controller->Record();
184 226
185 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); 227 controller->Close(base::MessageLoop::QuitWhenIdleClosure());
186 base::RunLoop().Run(); 228 base::RunLoop().Run();
187 229
188 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); 230 controller->Close(base::MessageLoop::QuitWhenIdleClosure());
189 base::RunLoop().Run(); 231 base::RunLoop().Run();
190 } 232 }
191 233
192 } // namespace media 234 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/audio_input_controller.cc ('k') | media/audio/audio_io.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698