1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
media / mojo / clients / mojo_android_overlay_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/android/jni_android.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "gpu/ipc/common/gpu_surface_tracker.h"
#include "media/base/mock_filters.h"
#include "media/mojo/clients/mojo_android_overlay.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/service_manager/public/mojom/interface_provider.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gl/android/scoped_java_surface.h"
#include "ui/gl/android/surface_texture.h"
using ::testing::StrictMock;
using ::testing::_;
namespace media {
class MojoAndroidOverlayTest : public ::testing::Test {
public:
class MockAndroidOverlay : public StrictMock<mojom::AndroidOverlay> {
public:
MOCK_METHOD1(ScheduleLayout, void(const gfx::Rect& rect));
};
// Handy class with client-level callback mocks.
class ClientCallbacks {
public:
virtual void OnReady(AndroidOverlay*) = 0;
virtual void OnFailed(AndroidOverlay*) = 0;
virtual void OnDestroyed(AndroidOverlay*) = 0;
};
class MockClientCallbacks : public StrictMock<ClientCallbacks> {
public:
MOCK_METHOD1(OnReady, void(AndroidOverlay*));
MOCK_METHOD1(OnFailed, void(AndroidOverlay*));
MOCK_METHOD1(OnDestroyed, void(AndroidOverlay*));
MOCK_METHOD2(OnPowerEfficient, void(AndroidOverlay*, bool));
};
class MockAndroidOverlayProvider
: public StrictMock<mojom::AndroidOverlayProvider> {
public:
// These argument types lack default constructors, so gmock can't mock them.
void CreateOverlay(
mojo::PendingReceiver<mojom::AndroidOverlay> overlay_receiver,
mojo::PendingRemote<mojom::AndroidOverlayClient> client,
mojom::AndroidOverlayConfigPtr config) override {
overlay_receiver_ = std::move(overlay_receiver);
client_.Bind(std::move(client));
config_ = std::move(config);
OverlayCreated();
}
MOCK_METHOD0(OverlayCreated, void(void));
mojo::PendingReceiver<mojom::AndroidOverlay> overlay_receiver_;
mojo::Remote<mojom::AndroidOverlayClient> client_;
mojom::AndroidOverlayConfigPtr config_;
};
public:
MojoAndroidOverlayTest()
: provider_receiver_(&mock_provider_),
overlay_receiver_(&mock_overlay_) {}
~MojoAndroidOverlayTest() override {}
void SetUp() override {
// Set up default config.
config_.rect = gfx::Rect(100, 200, 300, 400);
config_.ready_cb = base::BindOnce(&MockClientCallbacks::OnReady,
base::Unretained(&callbacks_));
config_.failed_cb = base::BindOnce(&MockClientCallbacks::OnFailed,
base::Unretained(&callbacks_));
config_.power_cb = base::BindRepeating(
&MockClientCallbacks::OnPowerEfficient, base::Unretained(&callbacks_));
// Make sure that we have an implementation of GpuSurfaceLookup.
gpu::GpuSurfaceTracker::Get();
}
void TearDown() override {
overlay_client_.reset();
// If we registered a surface, then unregister it.
if (surface_texture_) {
gpu::GpuSurfaceTracker::Get()->RemoveSurface(surface_key_);
// Drop the surface before the surface texture.
surface_ = gl::ScopedJavaSurface();
}
base::RunLoop().RunUntilIdle();
}
// Create an overlay in |overlay_client_| using the current config, but do
// not bind anything to |overlay_receiver_| yet.
void CreateOverlay() {
EXPECT_CALL(mock_provider_, OverlayCreated());
base::UnguessableToken routing_token = base::UnguessableToken::Create();
overlay_client_ = std::make_unique<MojoAndroidOverlay>(
provider_receiver_.BindNewPipeAndPassRemote(), std::move(config_),
routing_token);
overlay_client_->AddSurfaceDestroyedCallback(base::BindOnce(
&MockClientCallbacks::OnDestroyed, base::Unretained(&callbacks_)));
base::RunLoop().RunUntilIdle();
}
// Create an overlay, then provide it with |mock_overlay_|.
void CreateAndInitializeOverlay() {
CreateOverlay();
// Bind an overlay to the request.
overlay_receiver_.Bind(std::move(mock_provider_.overlay_receiver_));
base::RunLoop().RunUntilIdle();
}
// Notify |overlay_client_| that the surface is ready.
void CreateSurface() {
EXPECT_CALL(callbacks_, OnReady(overlay_client_.get()));
// We have to actually add a valid surface, else the client will get mad
// when it tries to retrieve it.
surface_texture_ = gl::SurfaceTexture::Create(0);
surface_ = gl::ScopedJavaSurface(surface_texture_.get());
surface_key_ = gpu::GpuSurfaceTracker::Get()->AddSurfaceForNativeWidget(
gpu::SurfaceRecord(surface_.CopyRetainOwnership(),
false /* can_be_used_with_surface_control */));
mock_provider_.client_->OnSurfaceReady(surface_key_);
base::RunLoop().RunUntilIdle();
// Verify that we actually got back the right surface.
JNIEnv* env = base::android::AttachCurrentThread();
ASSERT_TRUE(env->IsSameObject(surface_.j_surface().obj(),
overlay_client_->GetJavaSurface().obj()));
}
// Destroy the overlay. This includes onSurfaceDestroyed cases.
void DestroyOverlay() {
mock_provider_.client_->OnDestroyed();
base::RunLoop().RunUntilIdle();
}
// Mojo stuff.
base::test::SingleThreadTaskEnvironment task_environment;
// The mock provider that |overlay_client_| will talk to.
// |interface_provider_| will bind it.
MockAndroidOverlayProvider mock_provider_;
// Receiver for |mock_provider_|.
mojo::Receiver<mojom::AndroidOverlayProvider> provider_receiver_;
// The mock overlay impl that |mock_provider_| will provide.
MockAndroidOverlay mock_overlay_;
mojo::Receiver<mojom::AndroidOverlay> overlay_receiver_;
// The client under test.
std::unique_ptr<AndroidOverlay> overlay_client_;
// If we create a surface, then these are the SurfaceTexture that owns it,
// the surface itself, and the key that we registered with GpuSurfaceLookup,
// respectively. We could probably mock out GpuSurfaceLookup, but we'd still
// have to provide a (mock) ScopedJavaSurface, which isn't easy.
scoped_refptr<gl::SurfaceTexture> surface_texture_;
gl::ScopedJavaSurface surface_;
int surface_key_ = 0;
// Initial config for |CreateOverlay|.
// Set to sane values, but feel free to modify before CreateOverlay().
AndroidOverlayConfig config_;
MockClientCallbacks callbacks_;
};
// Verify basic create => init => ready => destroyed.
TEST_F(MojoAndroidOverlayTest, CreateInitReadyDestroy) {
CreateAndInitializeOverlay();
CreateSurface();
EXPECT_CALL(callbacks_, OnDestroyed(overlay_client_.get()));
DestroyOverlay();
}
// Verify that initialization failure results in an onDestroyed callback.
TEST_F(MojoAndroidOverlayTest, InitFailure) {
CreateOverlay();
EXPECT_CALL(callbacks_, OnFailed(overlay_client_.get()));
DestroyOverlay();
}
// Verify that we can destroy the overlay before providing a surface.
TEST_F(MojoAndroidOverlayTest, CreateInitDestroy) {
CreateAndInitializeOverlay();
EXPECT_CALL(callbacks_, OnFailed(overlay_client_.get()));
DestroyOverlay();
}
// Test that layouts happen.
TEST_F(MojoAndroidOverlayTest, LayoutOverlay) {
CreateAndInitializeOverlay();
CreateSurface();
gfx::Rect new_layout(5, 6, 7, 8);
EXPECT_CALL(mock_overlay_, ScheduleLayout(new_layout));
overlay_client_->ScheduleLayout(new_layout);
}
// Test that layouts are ignored before the client is notified about a surface.
TEST_F(MojoAndroidOverlayTest, LayoutBeforeSurfaceIsIgnored) {
CreateAndInitializeOverlay();
gfx::Rect new_layout(5, 6, 7, 8);
EXPECT_CALL(mock_overlay_, ScheduleLayout(_)).Times(0);
overlay_client_->ScheduleLayout(new_layout);
}
// Test |secure| makes it to the mojo config when it is true
TEST_F(MojoAndroidOverlayTest, FlagsAreSentViaMojoWhenTrue) {
config_.secure = true;
config_.power_efficient = true;
CreateOverlay();
ASSERT_TRUE(mock_provider_.config_->secure);
ASSERT_TRUE(mock_provider_.config_->power_efficient);
}
// Test |secure| makes it to the mojo config when it is false
TEST_F(MojoAndroidOverlayTest, FlagsAreSentViaMojoWhenFalse) {
config_.secure = false;
config_.power_efficient = false;
CreateOverlay();
ASSERT_FALSE(mock_provider_.config_->secure);
ASSERT_FALSE(mock_provider_.config_->power_efficient);
}
// Make sure that power efficient cbs are relayed to the application.
TEST_F(MojoAndroidOverlayTest, PowerEfficientCallbackWorks) {
CreateOverlay();
EXPECT_CALL(callbacks_, OnPowerEfficient(overlay_client_.get(), true));
mock_provider_.client_->OnPowerEfficientState(true);
base::RunLoop().RunUntilIdle();
EXPECT_CALL(callbacks_, OnPowerEfficient(overlay_client_.get(), false));
mock_provider_.client_->OnPowerEfficientState(false);
base::RunLoop().RunUntilIdle();
}
} // namespace media