OLD | NEW |
(Empty) | |
| 1 // Copyright 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 <memory> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "media/base/mock_filters.h" |
| 13 #include "media/mojo/clients/mojo_android_overlay.h" |
| 14 #include "mojo/public/cpp/bindings/interface_request.h" |
| 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using ::testing::_; |
| 19 using ::testing::StrictMock; |
| 20 |
| 21 namespace media { |
| 22 |
| 23 class MockAndroidOverlay : public StrictMock<mojom::AndroidOverlay> { |
| 24 public: |
| 25 MOCK_METHOD1(ScheduleLayout, void(const gfx::Rect& rect)); |
| 26 }; |
| 27 |
| 28 // Handy class with client-level callback mocks. |
| 29 class ClientCallbacks { |
| 30 public: |
| 31 virtual void OnReady() = 0; |
| 32 virtual void OnDestroyed() = 0; |
| 33 }; |
| 34 |
| 35 class MockClientCallbacks : public StrictMock<ClientCallbacks> { |
| 36 public: |
| 37 MOCK_METHOD0(OnReady, void()); |
| 38 MOCK_METHOD0(OnDestroyed, void()); |
| 39 }; |
| 40 |
| 41 class MockAndroidOverlayProvider |
| 42 : public StrictMock<mojom::AndroidOverlayProvider> { |
| 43 public: |
| 44 // These argument types lack default constructors, so gmock can't mock them. |
| 45 void CreateOverlay(mojom::AndroidOverlayClientPtr client, |
| 46 mojom::AndroidOverlayConfigPtr config) override { |
| 47 client_ = std::move(client); |
| 48 config_ = std::move(config); |
| 49 OverlayCreated(); |
| 50 } |
| 51 |
| 52 MOCK_METHOD0(OverlayCreated, void(void)); |
| 53 |
| 54 mojom::AndroidOverlayClientPtr client_; |
| 55 mojom::AndroidOverlayConfigPtr config_; |
| 56 }; |
| 57 |
| 58 class MojoAndroidOverlayTest : public ::testing::Test { |
| 59 public: |
| 60 MojoAndroidOverlayTest() |
| 61 : provider_binding_(&mock_provider_), overlay_binding_(&mock_overlay_) {} |
| 62 |
| 63 ~MojoAndroidOverlayTest() override {} |
| 64 |
| 65 void SetUp() override { |
| 66 provider_ = provider_binding_.CreateInterfacePtrAndBind(); |
| 67 |
| 68 // Set up default config. |
| 69 config_.rect = gfx::Rect(100, 200, 300, 400); |
| 70 config_.ready_cb = base::Bind(&MockClientCallbacks::OnReady, |
| 71 base::Unretained(&callbacks_)); |
| 72 config_.destroyed_cb = base::Bind(&MockClientCallbacks::OnDestroyed, |
| 73 base::Unretained(&callbacks_)); |
| 74 } |
| 75 |
| 76 void TearDown() override { |
| 77 overlay_client_.reset(); |
| 78 base::RunLoop().RunUntilIdle(); |
| 79 } |
| 80 |
| 81 // Create an overlay in |overlay_client_| using the current config, but do |
| 82 // not initialize it with an overlay impl. |
| 83 void CreateOverlay() { |
| 84 EXPECT_CALL(mock_provider_, OverlayCreated()); |
| 85 overlay_client_.reset(new MojoAndroidOverlay( |
| 86 std::move(provider_), render_frame_id_, renderer_pid_, config_)); |
| 87 base::RunLoop().RunUntilIdle(); |
| 88 } |
| 89 |
| 90 // Create an overlay, then provide it with |mock_overlay_|. |
| 91 void CreateAndInitializeOverlay() { |
| 92 CreateOverlay(); |
| 93 |
| 94 // Notify the client about the overlay mock. |
| 95 mock_provider_.client_->OnInitialized( |
| 96 overlay_binding_.CreateInterfacePtrAndBind()); |
| 97 base::RunLoop().RunUntilIdle(); |
| 98 } |
| 99 |
| 100 // Notify |overlay_client_| that the surface is ready. |
| 101 void CreateSurface() { |
| 102 EXPECT_CALL(callbacks_, OnReady()); |
| 103 const int surface_key = 123; |
| 104 mock_provider_.client_->OnSurfaceReady(surface_key); |
| 105 base::RunLoop().RunUntilIdle(); |
| 106 } |
| 107 |
| 108 // Destroy the overlay. This includes onSurfaceDestroyed cases. |
| 109 void DestroyOverlay() { |
| 110 EXPECT_CALL(callbacks_, OnDestroyed()); |
| 111 mock_provider_.client_->OnDestroyed(); |
| 112 base::RunLoop().RunUntilIdle(); |
| 113 } |
| 114 |
| 115 // Mojo stuff. |
| 116 base::MessageLoop loop; |
| 117 mojom::AndroidOverlayProviderPtr provider_; |
| 118 mojo::Binding<mojom::AndroidOverlayProvider> provider_binding_; |
| 119 mojo::Binding<mojom::AndroidOverlay> overlay_binding_; |
| 120 |
| 121 // The mock provider that |overlay_client_| will talk to. |
| 122 MockAndroidOverlayProvider mock_provider_; |
| 123 |
| 124 // The mock overlay impl that |mock_provider_| will provide. |
| 125 MockAndroidOverlay mock_overlay_; |
| 126 |
| 127 // The client under test. |
| 128 std::unique_ptr<AndroidOverlay> overlay_client_; |
| 129 |
| 130 // Inital config for |CreateOverlay|. |
| 131 // Set to sane values, but feel free to modify before CreateOverlay(). |
| 132 int render_frame_id_ = 1; |
| 133 int renderer_pid_ = 2; |
| 134 AndroidOverlay::Config config_; |
| 135 MockClientCallbacks callbacks_; |
| 136 }; |
| 137 |
| 138 // Verify basic create => init => ready => destroyed. |
| 139 TEST_F(MojoAndroidOverlayTest, CreateInitReadyDestroy) { |
| 140 CreateAndInitializeOverlay(); |
| 141 CreateSurface(); |
| 142 DestroyOverlay(); |
| 143 } |
| 144 |
| 145 // Verify that initialization failure results in an onDestroyed callback. |
| 146 TEST_F(MojoAndroidOverlayTest, InitFailure) { |
| 147 CreateOverlay(); |
| 148 DestroyOverlay(); |
| 149 } |
| 150 |
| 151 // Verify that we can destroy the overlay before providing a surface. |
| 152 TEST_F(MojoAndroidOverlayTest, CreateInitDestroy) { |
| 153 CreateAndInitializeOverlay(); |
| 154 DestroyOverlay(); |
| 155 } |
| 156 |
| 157 // Test that layouts happen. |
| 158 TEST_F(MojoAndroidOverlayTest, LayoutOverlay) { |
| 159 CreateAndInitializeOverlay(); |
| 160 CreateSurface(); |
| 161 |
| 162 gfx::Rect new_layout(5, 6, 7, 8); |
| 163 EXPECT_CALL(mock_overlay_, ScheduleLayout(new_layout)); |
| 164 overlay_client_->ScheduleLayout(new_layout); |
| 165 } |
| 166 |
| 167 // Test that layouts are ignored before the client is notified about a surface. |
| 168 TEST_F(MojoAndroidOverlayTest, LayoutBeforeSurfaceIsIgnored) { |
| 169 CreateAndInitializeOverlay(); |
| 170 |
| 171 gfx::Rect new_layout(5, 6, 7, 8); |
| 172 EXPECT_CALL(mock_overlay_, ScheduleLayout(_)).Times(0); |
| 173 overlay_client_->ScheduleLayout(new_layout); |
| 174 } |
| 175 |
| 176 } // namespace media |
OLD | NEW |