Index: content/browser/media/audio_debug_recording_impl_unittest.cc |
diff --git a/content/browser/media/audio_debug_recording_impl_unittest.cc b/content/browser/media/audio_debug_recording_impl_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7d4bd4ec658a94106afdd123e20406126dce2d8b |
--- /dev/null |
+++ b/content/browser/media/audio_debug_recording_impl_unittest.cc |
@@ -0,0 +1,221 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/media/audio_debug_recording_impl.h" |
+ |
+#include <utility> |
+ |
+#include "base/bind.h" |
+#include "base/files/file.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/time/time.h" |
+#include "content/browser/renderer_host/media/audio_input_renderer_host.h" |
+#include "content/common/media/audio_debug_recording.mojom.h" |
+#include "content/public/test/mock_render_process_host.h" |
+#include "content/public/test/test_browser_context.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "mojo/public/cpp/bindings/binding.h" |
+#include "mojo/public/cpp/system/handle.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using ::testing::_; |
+ |
+namespace content { |
+namespace { |
+ |
+class TestObserverImpl : public AudioDebugRecordingObserver { |
+ public: |
+ enum LastCalled { |
+ NONE = 0, |
+ ENABLE = 1, |
+ DISABLE = 2, |
+ }; |
+ |
+ explicit TestObserverImpl( |
+ mojo::InterfaceRequest<AudioDebugRecordingObserver> request) |
+ : last_called_(NONE), binding_(this, std::move(request)) {} |
+ |
+ // |AudioDebugRecordingObserver| implementation. |
+ void Enable(mojo::ScopedHandle handle) override { |
+ EXPECT_TRUE(handle.is_valid()); |
+ last_called_ = ENABLE; |
+ if (!quit_closure_.is_null()) |
+ quit_closure_.Run(); |
+ } |
+ |
+ void Disable() override { |
+ last_called_ = DISABLE; |
+ if (!quit_closure_.is_null()) |
+ quit_closure_.Run(); |
+ } |
+ |
+ void WaitForCall(LastCalled expected) { |
+ while (last_called_ != expected) { |
+ base::RunLoop run_loop; |
+ quit_closure_ = run_loop.QuitClosure(); |
+ run_loop.Run(); |
+ quit_closure_ = base::Closure(); |
+ } |
+ } |
+ |
+ private: |
+ LastCalled last_called_; |
+ base::Closure quit_closure_; |
+ mojo::Binding<AudioDebugRecordingObserver> binding_; |
+}; |
+ |
+class MockAudioInputRendererHost : public AudioInputRendererHost { |
+ public: |
+ MockAudioInputRendererHost() |
+ : AudioInputRendererHost(0, 0, nullptr, nullptr, nullptr, nullptr) {} |
+ |
+ MOCK_METHOD1(EnableDebugRecording, void(const base::FilePath& file)); |
+ MOCK_METHOD0(DisableDebugRecording, void()); |
+ |
+ private: |
+ ~MockAudioInputRendererHost() override {} |
+}; |
+ |
+} // namespace |
+ |
+class AudioDebugRecordingImplTest : public ::testing::Test { |
+ public: |
+ AudioDebugRecordingImplTest() |
+ : file_counter_(1), mock_render_process_(&test_browser_context_) {} |
+ |
+ void SetUp() override { |
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ mock_audio_input_renderer_host_ = new MockAudioInputRendererHost; |
+ impl_.reset(new AudioDebugRecordingImpl( |
+ mock_render_process_.GetID(), mock_audio_input_renderer_host_, |
+ base::Bind(&AudioDebugRecordingImplTest::DisconnectCallback, |
+ base::Unretained(this)), |
+ mojo::GetProxy(&client_))); |
+ } |
+ |
+ base::FilePath CreateTempFilePath() { |
+ return temp_dir_.path().AppendASCII( |
+ "temp_file" + base::IntToString(file_counter_)); |
+ } |
+ |
+ void DisconnectCallback(AudioDebugRecordingImpl* impl) { |
+ EXPECT_EQ(impl_.get(), impl); |
+ quit_closure_.Run(); |
+ } |
+ |
+ void WaitForDisconnect() { |
+ base::RunLoop run_loop; |
+ quit_closure_ = run_loop.QuitClosure(); |
+ run_loop.Run(); |
+ quit_closure_ = base::Closure(); |
+ } |
+ |
+ void WaitForNumObservers(size_t num_observers) { |
+ // Poll for the number of observers to reach |num_observers|. |
+ while (impl_->observers_.size() != num_observers) { |
+ base::RunLoop run_loop; |
+ base::MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, run_loop.QuitClosure(), |
+ base::TimeDelta::FromMilliseconds(1)); |
+ run_loop.Run(); |
+ } |
+ } |
+ |
+ private: |
+ // Must be destroyed last. |
+ TestBrowserThreadBundle thread_bundle_; |
+ |
+ protected: |
+ AudioDebugRecordingPtr client_; |
+ scoped_refptr<MockAudioInputRendererHost> mock_audio_input_renderer_host_; |
+ scoped_ptr<AudioDebugRecordingImpl> impl_; |
+ |
+ private: |
+ base::ScopedTempDir temp_dir_; |
+ int file_counter_; |
+ base::Closure quit_closure_; |
+ TestBrowserContext test_browser_context_; |
+ MockRenderProcessHost mock_render_process_; |
+}; |
+ |
+TEST_F(AudioDebugRecordingImplTest, Basic) { |
+ EXPECT_CALL(*mock_audio_input_renderer_host_, EnableDebugRecording(_)); |
+ EXPECT_CALL(*mock_audio_input_renderer_host_, DisableDebugRecording()); |
+ |
+ // Does nothing, since there are no observers. |
+ impl_->EnableAudioDebugRecording(CreateTempFilePath()); |
+ impl_->DisableAudioDebugRecording(); |
+ |
+ // Disconnect client. Should run callback. |
+ client_.reset(); |
+ WaitForDisconnect(); |
+ |
+ // Calls to Enable/DisableDebugRecording are posted to the IO thread, which by |
+ // default in |TestBrowserThreadBundle|, is the main thread. Wait for the task |
+ // to run in order to satisfy the mock expectation. |
+ base::RunLoop().RunUntilIdle(); |
+} |
+ |
+TEST_F(AudioDebugRecordingImplTest, ObserverDisconnect) { |
+ AudioDebugRecordingObserverPtr observer_ptr; |
+ scoped_ptr<TestObserverImpl> observer( |
+ new TestObserverImpl(mojo::GetProxy(&observer_ptr))); |
+ client_->RegisterObserver(1, std::move(observer_ptr)); |
+ WaitForNumObservers(1); |
+ observer.reset(); |
+ WaitForNumObservers(0); |
+} |
+ |
+TEST_F(AudioDebugRecordingImplTest, ObserverEnableDisable) { |
+ AudioDebugRecordingObserverPtr observer_ptr; |
+ scoped_ptr<TestObserverImpl> observer( |
+ new TestObserverImpl(mojo::GetProxy(&observer_ptr))); |
+ client_->RegisterObserver(1, std::move(observer_ptr)); |
+ WaitForNumObservers(1); |
+ |
+ EXPECT_CALL(*mock_audio_input_renderer_host_, EnableDebugRecording(_)); |
+ EXPECT_CALL(*mock_audio_input_renderer_host_, DisableDebugRecording()); |
+ |
+ impl_->EnableAudioDebugRecording(CreateTempFilePath()); |
+ observer->WaitForCall(TestObserverImpl::ENABLE); |
+ |
+ impl_->DisableAudioDebugRecording(); |
+ observer->WaitForCall(TestObserverImpl::DISABLE); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+} |
+ |
+TEST_F(AudioDebugRecordingImplTest, MultipleObservers) { |
+ AudioDebugRecordingObserverPtr observer1_ptr; |
+ scoped_ptr<TestObserverImpl> observer1( |
+ new TestObserverImpl(mojo::GetProxy(&observer1_ptr))); |
+ client_->RegisterObserver(1, std::move(observer1_ptr)); |
+ |
+ AudioDebugRecordingObserverPtr observer2_ptr; |
+ scoped_ptr<TestObserverImpl> observer2( |
+ new TestObserverImpl(mojo::GetProxy(&observer2_ptr))); |
+ client_->RegisterObserver(2, std::move(observer2_ptr)); |
+ |
+ WaitForNumObservers(2); |
+ |
+ EXPECT_CALL(*mock_audio_input_renderer_host_, EnableDebugRecording(_)); |
+ EXPECT_CALL(*mock_audio_input_renderer_host_, DisableDebugRecording()); |
+ |
+ impl_->EnableAudioDebugRecording(CreateTempFilePath()); |
+ observer1->WaitForCall(TestObserverImpl::ENABLE); |
+ observer2->WaitForCall(TestObserverImpl::ENABLE); |
+ |
+ impl_->DisableAudioDebugRecording(); |
+ observer1->WaitForCall(TestObserverImpl::DISABLE); |
+ observer2->WaitForCall(TestObserverImpl::DISABLE); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+} |
+ |
+} // namespace content |