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; |
33 using testing::Return; | 35 using testing::Return; |
34 using testing::WithArgs; | 36 using testing::WithArgs; |
35 using testing::StrictMock; | 37 using testing::StrictMock; |
36 using testing::Values; | 38 using testing::Values; |
37 | 39 |
38 namespace media { | 40 namespace media { |
39 | 41 |
40 namespace { | 42 namespace { |
41 | 43 |
42 const char kDefaultDeviceId[] = ""; | 44 const char kDefaultDeviceId[] = ""; |
43 const char kNonDefaultDeviceId[] = "valid-nondefault-device-id"; | 45 const char kNonDefaultDeviceId[] = "valid-nondefault-device-id"; |
44 const char kUnauthorizedDeviceId[] = "unauthorized-device-id"; | 46 const char kUnauthorizedDeviceId[] = "unauthorized-device-id"; |
| 47 const int kAuthTimeoutForTestingMs = 500; |
45 | 48 |
46 class MockRenderCallback : public AudioRendererSink::RenderCallback { | 49 class MockRenderCallback : public AudioRendererSink::RenderCallback { |
47 public: | 50 public: |
48 MockRenderCallback() {} | 51 MockRenderCallback() {} |
49 virtual ~MockRenderCallback() {} | 52 virtual ~MockRenderCallback() {} |
50 | 53 |
51 MOCK_METHOD3(Render, | 54 MOCK_METHOD3(Render, |
52 int(AudioBus* dest, | 55 int(AudioBus* dest, |
53 uint32_t frames_delayed, | 56 uint32_t frames_delayed, |
54 uint32_t frames_skipped)); | 57 uint32_t frames_skipped)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 public: | 95 public: |
93 AudioOutputDeviceTest(); | 96 AudioOutputDeviceTest(); |
94 ~AudioOutputDeviceTest(); | 97 ~AudioOutputDeviceTest(); |
95 | 98 |
96 void ReceiveAuthorization(OutputDeviceStatus device_status); | 99 void ReceiveAuthorization(OutputDeviceStatus device_status); |
97 void StartAudioDevice(); | 100 void StartAudioDevice(); |
98 void CreateStream(); | 101 void CreateStream(); |
99 void ExpectRenderCallback(); | 102 void ExpectRenderCallback(); |
100 void WaitUntilRenderCallback(); | 103 void WaitUntilRenderCallback(); |
101 void StopAudioDevice(); | 104 void StopAudioDevice(); |
| 105 void CreateDevice(const std::string& device_id); |
102 void SetDevice(const std::string& device_id); | 106 void SetDevice(const std::string& device_id); |
| 107 void CheckDeviceStatus(OutputDeviceStatus device_status); |
103 | 108 |
104 protected: | 109 protected: |
105 // Used to clean up TLS pointers that the test(s) will initialize. | 110 // Used to clean up TLS pointers that the test(s) will initialize. |
106 // Must remain the first member of this class. | 111 // Must remain the first member of this class. |
107 base::ShadowingAtExitManager at_exit_manager_; | 112 base::ShadowingAtExitManager at_exit_manager_; |
108 base::MessageLoopForIO io_loop_; | 113 base::MessageLoopForIO io_loop_; |
109 AudioParameters default_audio_parameters_; | 114 AudioParameters default_audio_parameters_; |
110 StrictMock<MockRenderCallback> callback_; | 115 StrictMock<MockRenderCallback> callback_; |
111 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ | 116 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ |
112 scoped_refptr<AudioOutputDevice> audio_device_; | 117 scoped_refptr<AudioOutputDevice> audio_device_; |
(...skipping 19 matching lines...) Expand all Loading... |
132 : device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL) { | 137 : device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL) { |
133 default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR, | 138 default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR, |
134 CHANNEL_LAYOUT_STEREO, 48000, 16, 1024); | 139 CHANNEL_LAYOUT_STEREO, 48000, 16, 1024); |
135 SetDevice(kDefaultDeviceId); | 140 SetDevice(kDefaultDeviceId); |
136 } | 141 } |
137 | 142 |
138 AudioOutputDeviceTest::~AudioOutputDeviceTest() { | 143 AudioOutputDeviceTest::~AudioOutputDeviceTest() { |
139 audio_device_ = NULL; | 144 audio_device_ = NULL; |
140 } | 145 } |
141 | 146 |
| 147 void AudioOutputDeviceTest::CreateDevice(const std::string& device_id) { |
| 148 audio_output_ipc_ = new MockAudioOutputIPC(); |
| 149 audio_device_ = new AudioOutputDevice( |
| 150 base::WrapUnique(audio_output_ipc_), io_loop_.task_runner(), 0, device_id, |
| 151 url::Origin(), |
| 152 base::TimeDelta::FromMilliseconds(kAuthTimeoutForTestingMs)); |
| 153 } |
| 154 |
142 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) { | 155 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) { |
143 audio_output_ipc_ = new MockAudioOutputIPC(); | 156 CreateDevice(device_id); |
144 audio_device_ = new AudioOutputDevice(base::WrapUnique(audio_output_ipc_), | |
145 io_loop_.task_runner(), 0, device_id, | |
146 url::Origin()); | |
147 EXPECT_CALL(*audio_output_ipc_, | 157 EXPECT_CALL(*audio_output_ipc_, |
148 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, _)); | 158 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, _)); |
149 audio_device_->RequestDeviceAuthorization(); | 159 audio_device_->RequestDeviceAuthorization(); |
150 io_loop_.RunUntilIdle(); | 160 io_loop_.RunUntilIdle(); |
151 | 161 |
152 // Simulate response from browser | 162 // Simulate response from browser |
153 OutputDeviceStatus device_status = | 163 OutputDeviceStatus device_status = |
154 (device_id == kUnauthorizedDeviceId) | 164 (device_id == kUnauthorizedDeviceId) |
155 ? OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED | 165 ? OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED |
156 : OUTPUT_DEVICE_STATUS_OK; | 166 : OUTPUT_DEVICE_STATUS_OK; |
157 ReceiveAuthorization(device_status); | 167 ReceiveAuthorization(device_status); |
158 | 168 |
159 audio_device_->Initialize(default_audio_parameters_, | 169 audio_device_->Initialize(default_audio_parameters_, |
160 &callback_); | 170 &callback_); |
161 } | 171 } |
162 | 172 |
| 173 void AudioOutputDeviceTest::CheckDeviceStatus(OutputDeviceStatus status) { |
| 174 DCHECK(!io_loop_.task_runner()->BelongsToCurrentThread()); |
| 175 EXPECT_EQ(status, audio_device_->GetOutputDeviceInfo().device_status()); |
| 176 } |
| 177 |
163 void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) { | 178 void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) { |
164 device_status_ = status; | 179 device_status_ = status; |
165 if (device_status_ != OUTPUT_DEVICE_STATUS_OK) | 180 if (device_status_ != OUTPUT_DEVICE_STATUS_OK) |
166 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | 181 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
167 | 182 |
168 audio_device_->OnDeviceAuthorized(device_status_, default_audio_parameters_, | 183 audio_device_->OnDeviceAuthorized(device_status_, default_audio_parameters_, |
169 kDefaultDeviceId); | 184 kDefaultDeviceId); |
170 io_loop_.RunUntilIdle(); | 185 io_loop_.RunUntilIdle(); |
171 } | 186 } |
172 | 187 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 | 326 |
312 StopAudioDevice(); | 327 StopAudioDevice(); |
313 } | 328 } |
314 | 329 |
315 TEST_P(AudioOutputDeviceTest, UnauthorizedDevice) { | 330 TEST_P(AudioOutputDeviceTest, UnauthorizedDevice) { |
316 SetDevice(kUnauthorizedDeviceId); | 331 SetDevice(kUnauthorizedDeviceId); |
317 StartAudioDevice(); | 332 StartAudioDevice(); |
318 StopAudioDevice(); | 333 StopAudioDevice(); |
319 } | 334 } |
320 | 335 |
| 336 TEST_P(AudioOutputDeviceTest, AuthorizationTimedOut) { |
| 337 base::Thread thread("DeviceInfo"); |
| 338 thread.Start(); |
| 339 |
| 340 CreateDevice(kNonDefaultDeviceId); |
| 341 EXPECT_CALL(*audio_output_ipc_, |
| 342 RequestDeviceAuthorization(audio_device_.get(), 0, |
| 343 kNonDefaultDeviceId, _)); |
| 344 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
| 345 |
| 346 // Request authorization; no reply from the browser. |
| 347 audio_device_->RequestDeviceAuthorization(); |
| 348 |
| 349 media::WaitableMessageLoopEvent event; |
| 350 |
| 351 // Request device info on another thread. |
| 352 thread.task_runner()->PostTaskAndReply( |
| 353 FROM_HERE, |
| 354 base::Bind(&AudioOutputDeviceTest::CheckDeviceStatus, |
| 355 base::Unretained(this), OUTPUT_DEVICE_STATUS_ERROR_TIMED_OUT), |
| 356 event.GetClosure()); |
| 357 |
| 358 io_loop_.RunUntilIdle(); |
| 359 |
| 360 // Runs the loop and waits for |thread| to call event's closure. |
| 361 event.RunAndWait(); |
| 362 |
| 363 audio_device_->Stop(); |
| 364 io_loop_.RunUntilIdle(); |
| 365 } |
| 366 |
321 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 367 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
322 | 368 |
323 } // namespace media. | 369 } // namespace media. |
OLD | NEW |