OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/ptr_util.h" |
| 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "base/single_thread_task_runner.h" |
| 9 #include "media/audio/audio_input_device.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gmock_mutant.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 using testing::_; |
| 15 using testing::DoAll; |
| 16 |
| 17 namespace media { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class MockAudioInputIPC : public AudioInputIPC { |
| 22 public: |
| 23 MockAudioInputIPC() {} |
| 24 ~MockAudioInputIPC() override {} |
| 25 |
| 26 MOCK_METHOD5(CreateStream, |
| 27 void(AudioInputIPCDelegate* delegate, |
| 28 int session_id, |
| 29 const AudioParameters& params, |
| 30 bool automatic_gain_control, |
| 31 uint32_t total_segments)); |
| 32 MOCK_METHOD0(RecordStream, void()); |
| 33 MOCK_METHOD1(SetVolume, void(double volume)); |
| 34 MOCK_METHOD0(CloseStream, void()); |
| 35 }; |
| 36 |
| 37 class MockCaptureCallback : public AudioCapturerSource::CaptureCallback { |
| 38 public: |
| 39 MockCaptureCallback() {} |
| 40 ~MockCaptureCallback() override {} |
| 41 |
| 42 MOCK_METHOD4(Capture, |
| 43 void(const AudioBus* audio_source, |
| 44 int audio_delay_milliseconds, |
| 45 double volume, |
| 46 bool key_pressed)); |
| 47 |
| 48 MOCK_METHOD1(OnCaptureError, void(const std::string& message)); |
| 49 }; |
| 50 |
| 51 // Used to terminate a loop from a different thread than the loop belongs to. |
| 52 // |task_runner| should be a SingleThreadTaskRunner. |
| 53 ACTION_P(QuitLoop, task_runner) { |
| 54 task_runner->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 55 } |
| 56 |
| 57 } // namespace. |
| 58 |
| 59 // Regular construction. |
| 60 TEST(AudioInputDeviceTest, Noop) { |
| 61 base::MessageLoopForIO io_loop; |
| 62 MockAudioInputIPC* input_ipc = new MockAudioInputIPC(); |
| 63 scoped_refptr<AudioInputDevice> device( |
| 64 new AudioInputDevice(base::WrapUnique(input_ipc), io_loop.task_runner())); |
| 65 } |
| 66 |
| 67 ACTION_P2(ReportStateChange, device, state) { |
| 68 static_cast<AudioInputIPCDelegate*>(device)->OnStateChanged(state); |
| 69 } |
| 70 |
| 71 // Verify that we get an OnCaptureError() callback if CreateStream fails. |
| 72 TEST(AudioInputDeviceTest, FailToCreateStream) { |
| 73 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 74 CHANNEL_LAYOUT_STEREO, 48000, 16, 480); |
| 75 |
| 76 base::MessageLoopForIO io_loop; |
| 77 MockCaptureCallback callback; |
| 78 MockAudioInputIPC* input_ipc = new MockAudioInputIPC(); |
| 79 scoped_refptr<AudioInputDevice> device( |
| 80 new AudioInputDevice(base::WrapUnique(input_ipc), io_loop.task_runner())); |
| 81 device->Initialize(params, &callback, 1); |
| 82 device->Start(); |
| 83 EXPECT_CALL(*input_ipc, CreateStream(_, _, _, _, _)) |
| 84 .WillOnce(ReportStateChange(device.get(), |
| 85 AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR)); |
| 86 EXPECT_CALL(callback, OnCaptureError(_)) |
| 87 .WillOnce(QuitLoop(io_loop.task_runner())); |
| 88 base::RunLoop().Run(); |
| 89 } |
| 90 |
| 91 } // namespace media. |
OLD | NEW |