OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "content/browser/renderer_host/media/audio_stream_registry_impl.h" |
| 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "content/public/test/mock_render_process_host.h" |
| 9 #include "content/public/test/test_browser_context.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 using testing::StrictMock; |
| 15 using testing::Mock; |
| 16 |
| 17 namespace content { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class MockStream : public AudioStreamRegistry::Stream { |
| 22 public: |
| 23 MOCK_METHOD1(EnableDebugRecording, void(const base::FilePath&)); |
| 24 MOCK_METHOD0(DisableDebugRecording, void()); |
| 25 }; |
| 26 |
| 27 class MockRenderProcessHostHandler { |
| 28 public: |
| 29 MOCK_METHOD0(AudioStateChanged, void()); |
| 30 }; |
| 31 |
| 32 #if defined(OS_WIN) |
| 33 const wchar_t kBaseFileName[] = L"some_file_name"; |
| 34 #else |
| 35 const char kBaseFileName[] = "some_file_name"; |
| 36 #endif |
| 37 } // namespace |
| 38 |
| 39 class AudioStreamRegistryImplTest : public testing::Test { |
| 40 public: |
| 41 AudioStreamRegistryImplTest() = default; |
| 42 ~AudioStreamRegistryImplTest() override = default; |
| 43 |
| 44 private: |
| 45 TestBrowserThreadBundle thread_bundle_; |
| 46 }; |
| 47 |
| 48 TEST_F(AudioStreamRegistryImplTest, RegisterUnregister) { |
| 49 AudioStreamRegistryImpl registry(0); |
| 50 StrictMock<MockStream> s; |
| 51 |
| 52 EXPECT_FALSE(registry.HasActiveAudio()); |
| 53 registry.RegisterOutputStream(&s); |
| 54 EXPECT_FALSE(registry.HasActiveAudio()); |
| 55 registry.DeregisterOutputStream(&s); |
| 56 EXPECT_FALSE(registry.HasActiveAudio()); |
| 57 } |
| 58 |
| 59 TEST_F(AudioStreamRegistryImplTest, RegisterPlayUnregister) { |
| 60 AudioStreamRegistryImpl registry(0); |
| 61 StrictMock<MockStream> s; |
| 62 |
| 63 registry.RegisterOutputStream(&s); |
| 64 registry.OutputStreamStateChanged(&s, true); |
| 65 EXPECT_TRUE(registry.HasActiveAudio()); |
| 66 registry.OutputStreamStateChanged(&s, false); |
| 67 EXPECT_FALSE(registry.HasActiveAudio()); |
| 68 registry.DeregisterOutputStream(&s); |
| 69 EXPECT_FALSE(registry.HasActiveAudio()); |
| 70 } |
| 71 |
| 72 TEST_F(AudioStreamRegistryImplTest, DebugRecording) { |
| 73 base::FilePath path(kBaseFileName); |
| 74 AudioStreamRegistryImpl registry(0); |
| 75 StrictMock<MockStream> s1; |
| 76 StrictMock<MockStream> s2; |
| 77 StrictMock<MockStream> s3; |
| 78 |
| 79 registry.RegisterOutputStream(&s1); |
| 80 registry.RegisterOutputStream(&s2); |
| 81 registry.DeregisterOutputStream(&s1); |
| 82 // s1 should not get a call since it's not registered. |
| 83 #if BUILDFLAG(ENABLE_WEBRTC) |
| 84 { |
| 85 testing::InSequence s; |
| 86 EXPECT_CALL(s2, EnableDebugRecording(path)); |
| 87 EXPECT_CALL(s2, DisableDebugRecording()); |
| 88 } |
| 89 { |
| 90 testing::InSequence s; |
| 91 EXPECT_CALL(s3, EnableDebugRecording(path)); |
| 92 EXPECT_CALL(s3, DisableDebugRecording()); |
| 93 } |
| 94 registry.EnableDebugRecording(path); |
| 95 #endif // BUILDFLAG(ENABLE_WEBRTC) |
| 96 registry.RegisterOutputStream(&s3); |
| 97 registry.DeregisterOutputStream(&s2); |
| 98 #if BUILDFLAG(ENABLE_WEBRTC) |
| 99 registry.DisableDebugRecording(); |
| 100 #endif // BUILDFLAG(ENABLE_WEBRTC) |
| 101 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&s2)); |
| 102 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&s3)); |
| 103 registry.DeregisterOutputStream(&s3); |
| 104 } |
| 105 |
| 106 TEST_F(AudioStreamRegistryImplTest, StateChangeNotifiesRenderProcessHost) { |
| 107 TestBrowserContext context; |
| 108 MockRenderProcessHost rph(&context); |
| 109 StrictMock<MockRenderProcessHostHandler> mock; |
| 110 rph.SetAudioStateChangedCallback( |
| 111 base::Bind(&MockRenderProcessHostHandler::AudioStateChanged, |
| 112 base::Unretained(&mock))); |
| 113 AudioStreamRegistryImpl registry(rph.GetID()); |
| 114 |
| 115 StrictMock<MockStream> s1; |
| 116 registry.RegisterOutputStream(&s1); |
| 117 base::RunLoop().RunUntilIdle(); |
| 118 |
| 119 EXPECT_CALL(mock, AudioStateChanged()); |
| 120 registry.OutputStreamStateChanged(&s1, true); |
| 121 base::RunLoop().RunUntilIdle(); |
| 122 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock)); |
| 123 EXPECT_CALL(mock, AudioStateChanged()); |
| 124 registry.OutputStreamStateChanged(&s1, false); |
| 125 base::RunLoop().RunUntilIdle(); |
| 126 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock)); |
| 127 registry.DeregisterOutputStream(&s1); |
| 128 } |
| 129 |
| 130 } // namespace content |
OLD | NEW |