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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/process/process_handle.h" | 16 #include "base/process/process_handle.h" |
17 #include "base/sync_socket.h" | 17 #include "base/sync_socket.h" |
18 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
19 #include "base/test/test_timeouts.h" | 19 #include "base/test/test_timeouts.h" |
20 #include "base/threading/thread.h" | |
20 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
21 #include "media/audio/audio_output_device.h" | 22 #include "media/audio/audio_output_device.h" |
22 #include "media/audio/sample_rates.h" | 23 #include "media/audio/sample_rates.h" |
24 #include "media/base/test_helpers.h" | |
23 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gmock_mutant.h" | 26 #include "testing/gmock_mutant.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
26 | 28 |
27 using base::CancelableSyncSocket; | 29 using base::CancelableSyncSocket; |
28 using base::SharedMemory; | 30 using base::SharedMemory; |
29 using base::SyncSocket; | 31 using base::SyncSocket; |
30 using testing::_; | 32 using testing::_; |
31 using testing::DoAll; | 33 using testing::DoAll; |
32 using testing::Invoke; | 34 using testing::Invoke; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 public: | 94 public: |
93 AudioOutputDeviceTest(); | 95 AudioOutputDeviceTest(); |
94 ~AudioOutputDeviceTest(); | 96 ~AudioOutputDeviceTest(); |
95 | 97 |
96 void ReceiveAuthorization(OutputDeviceStatus device_status); | 98 void ReceiveAuthorization(OutputDeviceStatus device_status); |
97 void StartAudioDevice(); | 99 void StartAudioDevice(); |
98 void CreateStream(); | 100 void CreateStream(); |
99 void ExpectRenderCallback(); | 101 void ExpectRenderCallback(); |
100 void WaitUntilRenderCallback(); | 102 void WaitUntilRenderCallback(); |
101 void StopAudioDevice(); | 103 void StopAudioDevice(); |
104 void CreateDevice(const std::string& device_id); | |
102 void SetDevice(const std::string& device_id); | 105 void SetDevice(const std::string& device_id); |
106 void CheckDeviceStatus(OutputDeviceStatus device_status); | |
103 | 107 |
104 protected: | 108 protected: |
105 // Used to clean up TLS pointers that the test(s) will initialize. | 109 // Used to clean up TLS pointers that the test(s) will initialize. |
106 // Must remain the first member of this class. | 110 // Must remain the first member of this class. |
107 base::ShadowingAtExitManager at_exit_manager_; | 111 base::ShadowingAtExitManager at_exit_manager_; |
108 base::MessageLoopForIO io_loop_; | 112 base::MessageLoopForIO io_loop_; |
109 AudioParameters default_audio_parameters_; | 113 AudioParameters default_audio_parameters_; |
110 StrictMock<MockRenderCallback> callback_; | 114 StrictMock<MockRenderCallback> callback_; |
111 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ | 115 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ |
112 scoped_refptr<AudioOutputDevice> audio_device_; | 116 scoped_refptr<AudioOutputDevice> audio_device_; |
(...skipping 19 matching lines...) Expand all Loading... | |
132 : device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL) { | 136 : device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL) { |
133 default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR, | 137 default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR, |
134 CHANNEL_LAYOUT_STEREO, 48000, 16, 1024); | 138 CHANNEL_LAYOUT_STEREO, 48000, 16, 1024); |
135 SetDevice(kDefaultDeviceId); | 139 SetDevice(kDefaultDeviceId); |
136 } | 140 } |
137 | 141 |
138 AudioOutputDeviceTest::~AudioOutputDeviceTest() { | 142 AudioOutputDeviceTest::~AudioOutputDeviceTest() { |
139 audio_device_ = NULL; | 143 audio_device_ = NULL; |
140 } | 144 } |
141 | 145 |
142 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) { | 146 void AudioOutputDeviceTest::CreateDevice(const std::string& device_id) { |
143 audio_output_ipc_ = new MockAudioOutputIPC(); | 147 audio_output_ipc_ = new MockAudioOutputIPC(); |
144 audio_device_ = new AudioOutputDevice(base::WrapUnique(audio_output_ipc_), | 148 audio_device_ = new AudioOutputDevice(base::WrapUnique(audio_output_ipc_), |
145 io_loop_.task_runner(), 0, device_id, | 149 io_loop_.task_runner(), 0, device_id, |
146 url::Origin()); | 150 url::Origin()); |
151 } | |
152 | |
153 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) { | |
154 CreateDevice(device_id); | |
147 EXPECT_CALL(*audio_output_ipc_, | 155 EXPECT_CALL(*audio_output_ipc_, |
148 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, _)); | 156 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, _)); |
149 audio_device_->RequestDeviceAuthorization(); | 157 audio_device_->RequestDeviceAuthorization(); |
150 io_loop_.RunUntilIdle(); | 158 io_loop_.RunUntilIdle(); |
151 | 159 |
152 // Simulate response from browser | 160 // Simulate response from browser |
153 OutputDeviceStatus device_status = | 161 OutputDeviceStatus device_status = |
154 (device_id == kUnauthorizedDeviceId) | 162 (device_id == kUnauthorizedDeviceId) |
155 ? OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED | 163 ? OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED |
156 : OUTPUT_DEVICE_STATUS_OK; | 164 : OUTPUT_DEVICE_STATUS_OK; |
157 ReceiveAuthorization(device_status); | 165 ReceiveAuthorization(device_status); |
158 | 166 |
159 audio_device_->Initialize(default_audio_parameters_, | 167 audio_device_->Initialize(default_audio_parameters_, |
160 &callback_); | 168 &callback_); |
161 } | 169 } |
162 | 170 |
171 void AudioOutputDeviceTest::CheckDeviceStatus(OutputDeviceStatus status) { | |
172 EXPECT_EQ(status, audio_device_->GetOutputDeviceInfo().device_status()); | |
173 } | |
174 | |
163 void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) { | 175 void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) { |
164 device_status_ = status; | 176 device_status_ = status; |
165 if (device_status_ != OUTPUT_DEVICE_STATUS_OK) | 177 if (device_status_ != OUTPUT_DEVICE_STATUS_OK) |
166 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | 178 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
167 | 179 |
168 audio_device_->OnDeviceAuthorized(device_status_, default_audio_parameters_, | 180 audio_device_->OnDeviceAuthorized(device_status_, default_audio_parameters_, |
169 kDefaultDeviceId); | 181 kDefaultDeviceId); |
170 io_loop_.RunUntilIdle(); | 182 io_loop_.RunUntilIdle(); |
171 } | 183 } |
172 | 184 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
311 | 323 |
312 StopAudioDevice(); | 324 StopAudioDevice(); |
313 } | 325 } |
314 | 326 |
315 TEST_P(AudioOutputDeviceTest, UnauthorizedDevice) { | 327 TEST_P(AudioOutputDeviceTest, UnauthorizedDevice) { |
316 SetDevice(kUnauthorizedDeviceId); | 328 SetDevice(kUnauthorizedDeviceId); |
317 StartAudioDevice(); | 329 StartAudioDevice(); |
318 StopAudioDevice(); | 330 StopAudioDevice(); |
319 } | 331 } |
320 | 332 |
333 TEST_P(AudioOutputDeviceTest, AuthorizationTimedOut) { | |
o1ka
2016/06/10 14:21:09
Since it takes 4 seconds (= auth timeout value), p
| |
334 base::Thread thread("DeviceInfo"); | |
335 thread.Start(); | |
336 | |
337 CreateDevice(kNonDefaultDeviceId); | |
338 EXPECT_CALL(*audio_output_ipc_, | |
339 RequestDeviceAuthorization(audio_device_.get(), 0, | |
340 kNonDefaultDeviceId, _)); | |
341 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | |
342 audio_device_->RequestDeviceAuthorization(); | |
343 | |
344 media::WaitableMessageLoopEvent event; | |
345 // Request device info on another thread. | |
346 thread.task_runner()->PostTaskAndReply( | |
347 FROM_HERE, | |
348 base::Bind(&AudioOutputDeviceTest::CheckDeviceStatus, | |
349 base::Unretained(this), OUTPUT_DEVICE_STATUS_ERROR_INTERNAL), | |
350 event.GetClosure()); | |
351 | |
352 io_loop_.RunUntilIdle(); | |
353 | |
354 // Runs the loop and waits for |thread| to call event's closure. | |
DaleCurtis
2016/06/10 18:29:19
Don't use real time if you want to add a test like
o1ka
2016/06/13 13:22:13
Introduced a timeout parameter in AOD constructor.
| |
355 event.RunAndWait(); | |
356 | |
357 audio_device_->Stop(); | |
358 io_loop_.RunUntilIdle(); | |
359 } | |
360 | |
321 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 361 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
322 | 362 |
323 } // namespace media. | 363 } // namespace media. |
OLD | NEW |