Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 5 | #include "components/media_effects/media_effects_service.h" |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 6 | |
| 7 | #include "base/run_loop.h" |
| 8 | #include "base/test/test_future.h" |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 9 | #include "components/media_effects/test/test_browser_context_with_prefs.h" |
| 10 | #include "components/prefs/testing_pref_service.h" |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 11 | #include "content/public/test/browser_task_environment.h" |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 12 | #include "content/public/test/test_browser_context.h" |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "ui/gfx/geometry/insets_f.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | constexpr char kDeviceId[] = "test_device"; |
| 19 | |
| 20 | video_capture::mojom::VideoEffectsConfigurationPtr GetConfigurationSync( |
| 21 | mojo::Remote<video_capture::mojom::VideoEffectsManager>& effects_manager) { |
| 22 | base::test::TestFuture<video_capture::mojom::VideoEffectsConfigurationPtr> |
| 23 | output_configuration; |
| 24 | effects_manager->GetConfiguration(output_configuration.GetCallback()); |
| 25 | return output_configuration.Take(); |
| 26 | } |
| 27 | |
| 28 | void SetFramingSync( |
| 29 | mojo::Remote<video_capture::mojom::VideoEffectsManager>& effects_manager, |
| 30 | float framing_padding_ratio) { |
| 31 | base::test::TestFuture<video_capture::mojom::SetConfigurationResult> |
| 32 | result_future; |
| 33 | effects_manager->SetConfiguration( |
| 34 | video_capture::mojom::VideoEffectsConfiguration::New( |
| 35 | nullptr, nullptr, |
| 36 | video_capture::mojom::Framing::New( |
| 37 | gfx::InsetsF{framing_padding_ratio})), |
| 38 | result_future.GetCallback()); |
| 39 | EXPECT_EQ(video_capture::mojom::SetConfigurationResult::kOk, |
| 40 | result_future.Get()); |
| 41 | } |
| 42 | } // namespace |
| 43 | |
| 44 | class MediaEffectsServiceTest : public testing::Test { |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 45 | protected: |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 46 | content::BrowserTaskEnvironment task_environment_; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 47 | media_effects::TestBrowserContextWithPrefs browser_context_; |
| 48 | MediaEffectsService service_{browser_context_.prefs()}; |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | TEST_F(MediaEffectsServiceTest, BindVideoEffectsManager) { |
| 52 | mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 53 | service_.BindVideoEffectsManager( |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 54 | kDeviceId, effects_manager.BindNewPipeAndPassReceiver()); |
| 55 | |
| 56 | EXPECT_TRUE(GetConfigurationSync(effects_manager)->framing.is_null()); |
| 57 | |
| 58 | const float kFramingPaddingRatio = 0.2; |
| 59 | SetFramingSync(effects_manager, kFramingPaddingRatio); |
| 60 | |
| 61 | auto configuration = GetConfigurationSync(effects_manager); |
| 62 | EXPECT_EQ(gfx::InsetsF{kFramingPaddingRatio}, |
| 63 | configuration->framing->padding_ratios); |
| 64 | } |
| 65 | |
| 66 | TEST_F(MediaEffectsServiceTest, |
| 67 | BindVideoEffectsManager_TwoRegistrantsWithSameIdConnectToSameManager) { |
| 68 | mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager1; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 69 | service_.BindVideoEffectsManager( |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 70 | kDeviceId, effects_manager1.BindNewPipeAndPassReceiver()); |
| 71 | |
| 72 | const float kFramingPaddingRatio = 0.234; |
| 73 | SetFramingSync(effects_manager1, kFramingPaddingRatio); |
| 74 | |
| 75 | EXPECT_EQ(gfx::InsetsF{kFramingPaddingRatio}, |
| 76 | GetConfigurationSync(effects_manager1)->framing->padding_ratios); |
| 77 | |
| 78 | mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager2; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 79 | service_.BindVideoEffectsManager( |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 80 | kDeviceId, effects_manager2.BindNewPipeAndPassReceiver()); |
| 81 | |
| 82 | EXPECT_EQ(gfx::InsetsF{kFramingPaddingRatio}, |
| 83 | GetConfigurationSync(effects_manager2)->framing->padding_ratios); |
| 84 | } |
| 85 | |
| 86 | TEST_F( |
| 87 | MediaEffectsServiceTest, |
| 88 | BindVideoEffectsManager_TwoRegistrantsWithDifferentIdConnectToDifferentManager) { |
| 89 | mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager1; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 90 | service_.BindVideoEffectsManager( |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 91 | "test_device_1", effects_manager1.BindNewPipeAndPassReceiver()); |
| 92 | |
| 93 | const float kFramingPaddingRatio = 0.234; |
| 94 | SetFramingSync(effects_manager1, kFramingPaddingRatio); |
| 95 | |
| 96 | EXPECT_EQ(gfx::InsetsF{kFramingPaddingRatio}, |
| 97 | GetConfigurationSync(effects_manager1)->framing->padding_ratios); |
| 98 | |
| 99 | mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager2; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 100 | service_.BindVideoEffectsManager( |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 101 | "test_device_2", effects_manager2.BindNewPipeAndPassReceiver()); |
| 102 | |
| 103 | // Expect `framing` to be unset because it is a separate instance of |
| 104 | // `VideoEffectsManager`. |
| 105 | auto framing = std::move(GetConfigurationSync(effects_manager2)->framing); |
| 106 | EXPECT_TRUE(framing.is_null()); |
| 107 | } |
| 108 | |
| 109 | TEST_F( |
| 110 | MediaEffectsServiceTest, |
| 111 | OnLastReceiverDisconnected_ErasesTheManagerWhenAllReceiversAreDisconnected) { |
| 112 | mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager1; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 113 | service_.BindVideoEffectsManager( |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 114 | kDeviceId, effects_manager1.BindNewPipeAndPassReceiver()); |
| 115 | mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager2; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 116 | service_.BindVideoEffectsManager( |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 117 | kDeviceId, effects_manager2.BindNewPipeAndPassReceiver()); |
| 118 | |
| 119 | const float kFramingPaddingRatio = 0.234; |
| 120 | |
| 121 | SetFramingSync(effects_manager1, kFramingPaddingRatio); |
| 122 | |
| 123 | EXPECT_EQ(gfx::InsetsF{kFramingPaddingRatio}, |
| 124 | GetConfigurationSync(effects_manager1)->framing->padding_ratios); |
| 125 | |
| 126 | EXPECT_EQ(gfx::InsetsF{kFramingPaddingRatio}, |
| 127 | GetConfigurationSync(effects_manager2)->framing->padding_ratios); |
| 128 | |
| 129 | effects_manager1.reset(); |
| 130 | effects_manager2.reset(); |
| 131 | // Wait for the reset to complete |
| 132 | base::RunLoop().RunUntilIdle(); |
| 133 | |
| 134 | mojo::Remote<video_capture::mojom::VideoEffectsManager> effects_manager3; |
Bryant Chandler | d5890afb | 2023-09-11 19:12:13 | [diff] [blame^] | 135 | service_.BindVideoEffectsManager( |
Bryant Chandler | 32ca638 | 2023-08-02 21:52:31 | [diff] [blame] | 136 | kDeviceId, effects_manager3.BindNewPipeAndPassReceiver()); |
| 137 | |
| 138 | // Expect `framing` to be unset because it is a new instance of |
| 139 | // `VideoEffectsManager`. |
| 140 | EXPECT_TRUE(GetConfigurationSync(effects_manager3)->framing.is_null()); |
| 141 | } |