Avi Drissman | d878a501 | 2022-09-12 19:13:30 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 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 "chromecast/browser/application_media_info_manager.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
Avi Drissman | 710fdab | 2023-01-11 04:37:36 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
Yuchen Liu | 3b9cc5a | 2024-01-09 18:24:14 | [diff] [blame] | 11 | #include "base/task/sequenced_task_runner.h" |
| 12 | #include "chromecast/browser/cast_session_id_map.h" |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 13 | #include "content/public/test/test_content_client_initializer.h" |
| 14 | #include "content/public/test/test_renderer_host.h" |
Miyoung Shin | 5c2d61a | 2019-11-02 01:08:40 | [diff] [blame] | 15 | #include "mojo/public/cpp/bindings/remote.h" |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 16 | #include "testing/gmock/include/gmock/gmock.h" |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace chromecast { |
| 20 | namespace media { |
| 21 | |
| 22 | using ::testing::_; |
| 23 | using ::testing::Invoke; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | const char kSessionId[] = "test-session-id"; |
| 28 | constexpr bool kMixedAudioEnabled = true; |
| 29 | |
| 30 | } // namespace |
| 31 | |
| 32 | class ApplicationMediaInfoManagerTest |
| 33 | : public content::RenderViewHostTestHarness { |
| 34 | public: |
| 35 | ApplicationMediaInfoManagerTest() : started_(false) {} |
| 36 | ~ApplicationMediaInfoManagerTest() override {} |
| 37 | |
| 38 | void SetUp() override { |
| 39 | initializer_ = std::make_unique<content::TestContentClientInitializer>(); |
| 40 | content::RenderViewHostTestHarness::SetUp(); |
Yuchen Liu | 3b9cc5a | 2024-01-09 18:24:14 | [diff] [blame] | 41 | shell::CastSessionIdMap::GetInstance( |
| 42 | base::SequencedTaskRunner::GetCurrentDefault().get()); |
danakj | c70aec1f | 2022-07-07 15:48:19 | [diff] [blame] | 43 | application_media_info_manager_ = |
| 44 | &ApplicationMediaInfoManager::CreateForTesting( |
| 45 | *main_rfh(), kSessionId, kMixedAudioEnabled, |
| 46 | application_media_info_manager_remote_ |
| 47 | .BindNewPipeAndPassReceiver()); |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void OnCastApplicationMediaInfo( |
| 51 | ::media::mojom::CastApplicationMediaInfoPtr ptr) { |
| 52 | EXPECT_EQ(ptr->application_session_id, kSessionId); |
| 53 | EXPECT_EQ(ptr->mixer_audio_enabled, kMixedAudioEnabled); |
| 54 | started_ = true; |
| 55 | } |
| 56 | |
Miyoung Shin | 5c2d61a | 2019-11-02 01:08:40 | [diff] [blame] | 57 | mojo::Remote<::media::mojom::CastApplicationMediaInfoManager> |
| 58 | application_media_info_manager_remote_; |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 59 | std::unique_ptr<content::TestContentClientInitializer> initializer_; |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 60 | // `ApplicationMediaInfoManager` is a `DocumentService` and manages its |
Daniel Cheng | 2809eff | 2021-09-28 00:20:31 | [diff] [blame] | 61 | // own lifetime. |
| 62 | ApplicationMediaInfoManager* application_media_info_manager_; |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 63 | bool started_; |
| 64 | }; |
| 65 | |
| 66 | TEST_F(ApplicationMediaInfoManagerTest, NoBlock_GetMediaInfo) { |
Miyoung Shin | 5c2d61a | 2019-11-02 01:08:40 | [diff] [blame] | 67 | application_media_info_manager_remote_->GetCastApplicationMediaInfo( |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 68 | base::BindOnce( |
| 69 | &ApplicationMediaInfoManagerTest::OnCastApplicationMediaInfo, |
| 70 | base::Unretained(this))); |
| 71 | base::RunLoop().RunUntilIdle(); |
| 72 | EXPECT_TRUE(started_); |
| 73 | } |
| 74 | |
| 75 | TEST_F(ApplicationMediaInfoManagerTest, Block_GetMediaInfo_Unblock) { |
| 76 | application_media_info_manager_->SetRendererBlock(true); |
| 77 | base::RunLoop().RunUntilIdle(); |
Miyoung Shin | 5c2d61a | 2019-11-02 01:08:40 | [diff] [blame] | 78 | application_media_info_manager_remote_->GetCastApplicationMediaInfo( |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 79 | base::BindOnce( |
| 80 | &ApplicationMediaInfoManagerTest::OnCastApplicationMediaInfo, |
| 81 | base::Unretained(this))); |
| 82 | base::RunLoop().RunUntilIdle(); |
| 83 | EXPECT_FALSE(started_); |
| 84 | application_media_info_manager_->SetRendererBlock(false); |
| 85 | base::RunLoop().RunUntilIdle(); |
| 86 | EXPECT_TRUE(started_); |
| 87 | } |
| 88 | |
| 89 | TEST_F(ApplicationMediaInfoManagerTest, Block_Unblock_GetMediaInfo) { |
| 90 | application_media_info_manager_->SetRendererBlock(true); |
| 91 | base::RunLoop().RunUntilIdle(); |
| 92 | application_media_info_manager_->SetRendererBlock(false); |
| 93 | base::RunLoop().RunUntilIdle(); |
| 94 | EXPECT_FALSE(started_); |
Miyoung Shin | 5c2d61a | 2019-11-02 01:08:40 | [diff] [blame] | 95 | application_media_info_manager_remote_->GetCastApplicationMediaInfo( |
Andres Medina | a65ad1b | 2019-06-26 22:18:19 | [diff] [blame] | 96 | base::BindOnce( |
| 97 | &ApplicationMediaInfoManagerTest::OnCastApplicationMediaInfo, |
| 98 | base::Unretained(this))); |
| 99 | base::RunLoop().RunUntilIdle(); |
| 100 | EXPECT_TRUE(started_); |
| 101 | } |
| 102 | |
| 103 | } // namespace media |
| 104 | } // namespace chromecast |