Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(772)

Side by Side Diff: media/mojo/clients/mojo_android_overlay_unittest.cc

Issue 2688193002: Mojo framework for AndroidOverlay. (Closed)
Patch Set: BUILD.gn fixes Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // gmock can't mock gl::ScopedJavaSurface
32 void RealOnReady(gl::ScopedJavaSurface surface) { OnReady(); }
33 virtual void OnReady() = 0;
34 virtual void OnDestroyed() = 0;
35 };
36
37 class MockClientCallbacks : public StrictMock<ClientCallbacks> {
38 public:
39 void RealOnReady(gl::ScopedJavaSurface surface) { OnReady(); }
40 MOCK_METHOD0(OnReady, void());
41 MOCK_METHOD0(OnDestroyed, void());
42 };
43
44 class MockAndroidOverlayProvider
45 : public StrictMock<mojom::AndroidOverlayProvider> {
46 public:
47 // These argument types lack default constructors, so gmock can't mock them.
48 void CreateOverlay(mojom::AndroidOverlayClientPtr client,
49 mojom::AndroidOverlayConfigPtr config) override {
50 client_ = std::move(client);
51 config_ = std::move(config);
52 OverlayCreated();
53 }
54
55 MOCK_METHOD0(OverlayCreated, void(void));
56
57 mojom::AndroidOverlayClientPtr client_;
58 mojom::AndroidOverlayConfigPtr config_;
59 };
60
61 class MojoAndroidOverlayTest : public ::testing::Test {
62 public:
63 MojoAndroidOverlayTest()
64 : provider_binding_(&mock_provider_), overlay_binding_(&mock_overlay_) {}
65
66 ~MojoAndroidOverlayTest() override {}
67
68 void SetUp() override {
69 provider_ = provider_binding_.CreateInterfacePtrAndBind();
70
71 // Set up default config.
72 config_.rect = gfx::Rect(100, 200, 300, 400);
73 config_.ready_cb = base::Bind(&MockClientCallbacks::RealOnReady,
74 base::Unretained(&callbacks_));
75 config_.destroyed_cb = base::Bind(&MockClientCallbacks::OnDestroyed,
76 base::Unretained(&callbacks_));
77 }
78
79 void TearDown() override {
80 overlay_client_.reset();
81 base::RunLoop().RunUntilIdle();
82 }
83
84 // Create an overlay in |overlay_client_| using the current config, but do
85 // not initialize it with an overlay impl.
86 void CreateOverlay() {
87 EXPECT_CALL(mock_provider_, OverlayCreated());
88 overlay_client_.reset(new MojoAndroidOverlay(
89 std::move(provider_), render_frame_id_, renderer_pid_, config_));
90 base::RunLoop().RunUntilIdle();
91 }
92
93 // Create an overlay, then provide it with |mock_overlay_|.
94 void CreateAndInitializeOverlay() {
95 CreateOverlay();
96
97 // Notify the client about the overlay mock.
98 mock_provider_.client_->OnInitialized(
99 overlay_binding_.CreateInterfacePtrAndBind());
100 base::RunLoop().RunUntilIdle();
101 }
102
103 // Notify |overlay_client_| that the surface is ready.
104 void CreateSurface() {
105 EXPECT_CALL(callbacks_, OnReady());
106 const int surface_key = 123;
107 mock_provider_.client_->OnSurfaceReady(surface_key);
108 base::RunLoop().RunUntilIdle();
109 }
110
111 // Destroy the overlay. This includes onSurfaceDestroyed cases.
112 void DestroyOverlay() {
113 EXPECT_CALL(callbacks_, OnDestroyed());
114 mock_provider_.client_->OnDestroyed();
115 base::RunLoop().RunUntilIdle();
116 }
117
118 // Mojo stuff.
119 base::MessageLoop loop;
120 mojom::AndroidOverlayProviderPtr provider_;
121 mojo::Binding<mojom::AndroidOverlayProvider> provider_binding_;
122 mojo::Binding<mojom::AndroidOverlay> overlay_binding_;
123
124 // The mock provider that |overlay_client_| will talk to.
125 MockAndroidOverlayProvider mock_provider_;
126
127 // The mock overlay impl that |mock_provider_| will provide.
128 MockAndroidOverlay mock_overlay_;
129
130 // The client under test.
131 std::unique_ptr<AndroidOverlay> overlay_client_;
132
133 // Inital config for |CreateOverlay|.
134 // Set to sane values, but feel free to modify before CreateOverlay().
135 int render_frame_id_ = 1;
136 int renderer_pid_ = 2;
137 AndroidOverlay::Config config_;
138 MockClientCallbacks callbacks_;
139 };
140
141 // Verify basic create => init => ready => destroyed.
142 TEST_F(MojoAndroidOverlayTest, CreateInitReadyDestroy) {
143 CreateAndInitializeOverlay();
144 CreateSurface();
145 DestroyOverlay();
146 }
147
148 // Verify that initialization failure results in an onDestroyed callback.
149 TEST_F(MojoAndroidOverlayTest, InitFailure) {
150 CreateOverlay();
151 DestroyOverlay();
152 }
153
154 // Verify that we can destroy the overlay before providing a surface.
155 TEST_F(MojoAndroidOverlayTest, CreateInitDestroy) {
156 CreateAndInitializeOverlay();
157 DestroyOverlay();
158 }
159
160 // Test that layouts happen.
161 TEST_F(MojoAndroidOverlayTest, LayoutOverlay) {
162 CreateAndInitializeOverlay();
163 CreateSurface();
164
165 gfx::Rect new_layout(5, 6, 7, 8);
166 EXPECT_CALL(mock_overlay_, ScheduleLayout(new_layout));
167 overlay_client_->ScheduleLayout(new_layout);
168 }
169
170 // Test that layouts are ignored before the client is notified about a surface.
171 TEST_F(MojoAndroidOverlayTest, LayoutBeforeSurfaceIsIgnored) {
172 CreateAndInitializeOverlay();
173
174 gfx::Rect new_layout(5, 6, 7, 8);
175 EXPECT_CALL(mock_overlay_, ScheduleLayout(_)).Times(0);
176 overlay_client_->ScheduleLayout(new_layout);
177 }
178
179 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698