OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/macros.h" | 6 #include "base/macros.h" |
6 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
7 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
8 #include "base/test/test_message_loop.h" | 9 #include "base/test/test_message_loop.h" |
9 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
10 #include "media/audio/audio_io.h" | 11 #include "media/audio/audio_io.h" |
11 #include "media/audio/audio_manager.h" | 12 #include "media/audio/audio_manager.h" |
12 #include "media/audio/audio_unittest_util.h" | 13 #include "media/audio/audio_unittest_util.h" |
13 #include "media/audio/mock_audio_source_callback.h" | 14 #include "media/audio/mock_audio_source_callback.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 using testing::_; | 18 using testing::_; |
18 using testing::DoAll; | 19 using testing::DoAll; |
19 using testing::Return; | 20 using testing::Return; |
20 | 21 |
21 // TODO(crogers): Most of these tests can be made platform agnostic. | 22 // TODO(crogers): Most of these tests can be made platform agnostic. |
22 // http://crbug.com/223242 | 23 // http://crbug.com/223242 |
23 | 24 |
24 namespace media { | 25 namespace media { |
25 | 26 |
26 ACTION(ZeroBuffer) { | 27 ACTION(ZeroBuffer) { |
27 arg0->Zero(); | 28 arg0->Zero(); |
28 } | 29 } |
29 | 30 |
30 ACTION_P(SignalEvent, event) { | 31 ACTION_P3(MaybeSignalEvent, counter, signal_at_count, event) { |
31 event->Signal(); | 32 if (++(*counter) == signal_at_count) |
| 33 event->Signal(); |
32 } | 34 } |
33 | 35 |
34 class AUHALStreamTest : public testing::Test { | 36 class AUHALStreamTest : public testing::Test { |
35 public: | 37 public: |
36 AUHALStreamTest() | 38 AUHALStreamTest() |
37 : message_loop_(base::MessageLoop::TYPE_UI), | 39 : message_loop_(base::MessageLoop::TYPE_UI), |
38 manager_(AudioManager::CreateForTesting( | 40 manager_(AudioManager::CreateForTesting( |
39 base::ThreadTaskRunnerHandle::Get())) { | 41 base::ThreadTaskRunnerHandle::Get())) { |
40 // Wait for the AudioManager to finish any initialization on the audio loop. | 42 // Wait for the AudioManager to finish any initialization on the audio loop. |
41 base::RunLoop().RunUntilIdle(); | 43 base::RunLoop().RunUntilIdle(); |
42 } | 44 } |
43 | 45 |
44 ~AUHALStreamTest() override {} | 46 ~AUHALStreamTest() override {} |
45 | 47 |
46 AudioOutputStream* Create() { | 48 AudioOutputStream* Create() { |
47 return manager_->MakeAudioOutputStream( | 49 return manager_->MakeAudioOutputStream( |
48 manager_->GetDefaultOutputStreamParameters(), ""); | 50 manager_->GetDefaultOutputStreamParameters(), "", |
| 51 base::Bind(&AUHALStreamTest::OnLogMessage, base::Unretained(this))); |
49 } | 52 } |
50 | 53 |
51 bool OutputDevicesAvailable() { | 54 bool OutputDevicesAvailable() { |
52 return manager_->HasAudioOutputDevices(); | 55 return manager_->HasAudioOutputDevices(); |
53 } | 56 } |
54 | 57 |
| 58 void OnLogMessage(const std::string& message) { log_message_ = message; } |
| 59 |
55 protected: | 60 protected: |
56 base::TestMessageLoop message_loop_; | 61 base::TestMessageLoop message_loop_; |
57 ScopedAudioManagerPtr manager_; | 62 ScopedAudioManagerPtr manager_; |
58 MockAudioSourceCallback source_; | 63 MockAudioSourceCallback source_; |
| 64 std::string log_message_; |
59 | 65 |
60 private: | 66 private: |
61 DISALLOW_COPY_AND_ASSIGN(AUHALStreamTest); | 67 DISALLOW_COPY_AND_ASSIGN(AUHALStreamTest); |
62 }; | 68 }; |
63 | 69 |
64 TEST_F(AUHALStreamTest, HardwareSampleRate) { | 70 TEST_F(AUHALStreamTest, HardwareSampleRate) { |
65 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); | 71 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); |
66 const AudioParameters preferred_params = | 72 const AudioParameters preferred_params = |
67 manager_->GetDefaultOutputStreamParameters(); | 73 manager_->GetDefaultOutputStreamParameters(); |
68 EXPECT_GE(preferred_params.sample_rate(), 16000); | 74 EXPECT_GE(preferred_params.sample_rate(), 16000); |
(...skipping 11 matching lines...) Expand all Loading... |
80 EXPECT_TRUE(stream->Open()); | 86 EXPECT_TRUE(stream->Open()); |
81 stream->Close(); | 87 stream->Close(); |
82 } | 88 } |
83 | 89 |
84 TEST_F(AUHALStreamTest, CreateOpenStartStopClose) { | 90 TEST_F(AUHALStreamTest, CreateOpenStartStopClose) { |
85 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); | 91 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); |
86 | 92 |
87 AudioOutputStream* stream = Create(); | 93 AudioOutputStream* stream = Create(); |
88 EXPECT_TRUE(stream->Open()); | 94 EXPECT_TRUE(stream->Open()); |
89 | 95 |
90 // Wait for the first data callback from the OS. | 96 // Wait for the first two data callback from the OS. |
91 base::WaitableEvent event(false, false); | 97 base::WaitableEvent event(false, false); |
| 98 int callback_counter = 0; |
| 99 const int number_of_callbacks = 2; |
92 EXPECT_CALL(source_, OnMoreData(_, _, _)) | 100 EXPECT_CALL(source_, OnMoreData(_, _, _)) |
93 .WillOnce(DoAll(ZeroBuffer(), SignalEvent(&event), Return(0))); | 101 .Times(number_of_callbacks) |
| 102 .WillRepeatedly(DoAll( |
| 103 ZeroBuffer(), |
| 104 MaybeSignalEvent(&callback_counter, number_of_callbacks, &event), |
| 105 Return(0))); |
94 EXPECT_CALL(source_, OnError(_)).Times(0); | 106 EXPECT_CALL(source_, OnError(_)).Times(0); |
95 stream->Start(&source_); | 107 stream->Start(&source_); |
96 event.Wait(); | 108 event.Wait(); |
97 | 109 |
98 stream->Stop(); | 110 stream->Stop(); |
99 stream->Close(); | 111 stream->Close(); |
| 112 |
| 113 EXPECT_FALSE(log_message_.empty()); |
100 } | 114 } |
101 | 115 |
102 } // namespace media | 116 } // namespace media |
OLD | NEW |