Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7db1607

Browse files
committed
fix unittests
1 parent 3ccb566 commit 7db1607

5 files changed

Lines changed: 72 additions & 46 deletions

File tree

shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "flutter/shell/platform/android/surface/android_surface_mock.h"
1414
#include "gmock/gmock.h"
1515
#include "gtest/gtest.h"
16+
#include "shell/platform/android/surface/android_surface.h"
1617
#include "third_party/skia/include/gpu/GrDirectContext.h"
1718

1819
namespace flutter {
@@ -21,6 +22,25 @@ namespace testing {
2122
using ::testing::ByMove;
2223
using ::testing::Return;
2324

25+
class TestAndroidSurfaceFactory : public AndroidSurfaceFactory {
26+
public:
27+
using TestSurfaceProducer =
28+
std::function<std::unique_ptr<AndroidSurface>(void)>;
29+
explicit TestAndroidSurfaceFactory(TestSurfaceProducer&& surface_producer)
30+
: AndroidSurfaceFactory(nullptr, nullptr) {
31+
surface_producer_ = surface_producer;
32+
}
33+
34+
~TestAndroidSurfaceFactory() override = default;
35+
36+
std::unique_ptr<AndroidSurface> CreateSurface() override {
37+
return surface_producer_();
38+
}
39+
40+
private:
41+
TestSurfaceProducer surface_producer_;
42+
};
43+
2444
class SurfaceMock : public Surface {
2545
public:
2646
MOCK_METHOD(bool, IsValid, (), (override));
@@ -263,10 +283,8 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame) {
263283
auto window = fml::MakeRefCounted<AndroidNativeWindow>(nullptr);
264284
auto gr_context = GrDirectContext::MakeMock(nullptr);
265285
auto frame_size = SkISize::Make(1000, 1000);
266-
auto surface_factory =
267-
[gr_context, window, frame_size](
268-
std::shared_ptr<AndroidContext> android_context,
269-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
286+
auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
287+
[gr_context, window, frame_size]() {
270288
auto surface_frame_1 = std::make_unique<SurfaceFrame>(
271289
SkSurface::MakeNull(1000, 1000), false,
272290
[](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
@@ -293,7 +311,7 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame) {
293311
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
294312

295313
return android_surface_mock;
296-
};
314+
});
297315
auto embedder = std::make_unique<AndroidExternalViewEmbedder>(
298316
android_context, jni_mock, surface_factory);
299317

@@ -482,10 +500,8 @@ TEST(AndroidExternalViewEmbedder, DestroyOverlayLayersOnSizeChange) {
482500
auto window = fml::MakeRefCounted<AndroidNativeWindow>(nullptr);
483501
auto gr_context = GrDirectContext::MakeMock(nullptr);
484502
auto frame_size = SkISize::Make(1000, 1000);
485-
auto surface_factory =
486-
[gr_context, window, frame_size](
487-
std::shared_ptr<AndroidContext> android_context,
488-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
503+
auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
504+
[gr_context, window, frame_size]() {
489505
auto surface_frame_1 = std::make_unique<SurfaceFrame>(
490506
SkSurface::MakeNull(1000, 1000), false,
491507
[](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
@@ -505,7 +521,7 @@ TEST(AndroidExternalViewEmbedder, DestroyOverlayLayersOnSizeChange) {
505521
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
506522

507523
return android_surface_mock;
508-
};
524+
});
509525

510526
auto embedder = std::make_unique<AndroidExternalViewEmbedder>(
511527
android_context, jni_mock, surface_factory);
@@ -565,10 +581,8 @@ TEST(AndroidExternalViewEmbedder, DoesNotDestroyOverlayLayersOnSizeChange) {
565581
auto window = fml::MakeRefCounted<AndroidNativeWindow>(nullptr);
566582
auto gr_context = GrDirectContext::MakeMock(nullptr);
567583
auto frame_size = SkISize::Make(1000, 1000);
568-
auto surface_factory =
569-
[gr_context, window, frame_size](
570-
std::shared_ptr<AndroidContext> android_context,
571-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
584+
auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
585+
[gr_context, window, frame_size]() {
572586
auto surface_frame_1 = std::make_unique<SurfaceFrame>(
573587
SkSurface::MakeNull(1000, 1000), false,
574588
[](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
@@ -588,7 +602,7 @@ TEST(AndroidExternalViewEmbedder, DoesNotDestroyOverlayLayersOnSizeChange) {
588602
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
589603

590604
return android_surface_mock;
591-
};
605+
});
592606

593607
auto embedder = std::make_unique<AndroidExternalViewEmbedder>(
594608
android_context, jni_mock, surface_factory);

shell/platform/android/external_view_embedder/surface_pool_unittests.cc

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ namespace testing {
1818
using ::testing::ByMove;
1919
using ::testing::Return;
2020

21+
class TestAndroidSurfaceFactory : public AndroidSurfaceFactory {
22+
public:
23+
using TestSurfaceProducer =
24+
std::function<std::unique_ptr<AndroidSurface>(void)>;
25+
explicit TestAndroidSurfaceFactory(TestSurfaceProducer&& surface_producer)
26+
: AndroidSurfaceFactory(nullptr, nullptr) {
27+
surface_producer_ = surface_producer;
28+
}
29+
30+
~TestAndroidSurfaceFactory() override = default;
31+
32+
std::unique_ptr<AndroidSurface> CreateSurface() override {
33+
return surface_producer_();
34+
}
35+
36+
private:
37+
TestSurfaceProducer surface_producer_;
38+
};
39+
2140
TEST(SurfacePool, GetLayer__AllocateOneLayer) {
2241
auto pool = std::make_unique<SurfacePool>();
2342

@@ -33,14 +52,13 @@ TEST(SurfacePool, GetLayer__AllocateOneLayer) {
3352
0, window))));
3453

3554
auto surface_factory =
36-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
37-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
55+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
3856
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
3957
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
4058
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
4159
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
4260
return android_surface_mock;
43-
};
61+
});
4462
auto layer = pool->GetLayer(gr_context.get(), android_context, jni_mock,
4563
surface_factory);
4664

@@ -64,14 +82,13 @@ TEST(SurfacePool, GetUnusedLayers) {
6482
0, window))));
6583

6684
auto surface_factory =
67-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
68-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
85+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
6986
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
7087
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
7188
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
7289
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
7390
return android_surface_mock;
74-
};
91+
});
7592
auto layer = pool->GetLayer(gr_context.get(), android_context, jni_mock,
7693
surface_factory);
7794
ASSERT_EQ(0UL, pool->GetUnusedLayers().size());
@@ -97,10 +114,8 @@ TEST(SurfacePool, GetLayer__Recycle) {
97114
std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
98115

99116
auto gr_context_2 = GrDirectContext::MakeMock(nullptr);
100-
auto surface_factory =
101-
[gr_context_1, gr_context_2, window](
102-
std::shared_ptr<AndroidContext> android_context,
103-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
117+
auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
118+
[gr_context_1, gr_context_2, window]() {
104119
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
105120
// Allocate two GPU surfaces for each gr context.
106121
EXPECT_CALL(*android_surface_mock,
@@ -111,7 +126,7 @@ TEST(SurfacePool, GetLayer__Recycle) {
111126
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
112127
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
113128
return android_surface_mock;
114-
};
129+
});
115130
auto layer_1 = pool->GetLayer(gr_context_1.get(), android_context, jni_mock,
116131
surface_factory);
117132

@@ -147,14 +162,13 @@ TEST(SurfacePool, GetLayer__AllocateTwoLayers) {
147162
1, window))));
148163

149164
auto surface_factory =
150-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
151-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
165+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
152166
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
153167
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
154168
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
155169
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
156170
return android_surface_mock;
157-
};
171+
});
158172
auto layer_1 = pool->GetLayer(gr_context.get(), android_context, jni_mock,
159173
surface_factory);
160174
auto layer_2 = pool->GetLayer(gr_context.get(), android_context, jni_mock,
@@ -185,14 +199,13 @@ TEST(SurfacePool, DestroyLayers) {
185199
0, window))));
186200

187201
auto surface_factory =
188-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
189-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
202+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
190203
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
191204
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
192205
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
193206
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
194207
return android_surface_mock;
195-
};
208+
});
196209
pool->GetLayer(gr_context.get(), android_context, jni_mock, surface_factory);
197210

198211
EXPECT_CALL(*jni_mock, FlutterViewDestroyOverlaySurfaces());
@@ -213,14 +226,13 @@ TEST(SurfacePool, DestroyLayers__frameSizeChanged) {
213226
auto window = fml::MakeRefCounted<AndroidNativeWindow>(nullptr);
214227

215228
auto surface_factory =
216-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
217-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
229+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
218230
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
219231
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
220232
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
221233
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
222234
return android_surface_mock;
223-
};
235+
});
224236
pool->SetFrameSize(SkISize::Make(10, 10));
225237
EXPECT_CALL(*jni_mock, FlutterViewDestroyOverlaySurfaces()).Times(0);
226238
EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())

shell/platform/android/platform_view_android.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@
2828

2929
namespace flutter {
3030

31-
AndroidSurfaceFactory::AndroidSurfaceFactory(
32-
std::shared_ptr<AndroidContext> context,
33-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
34-
android_context_ = context;
35-
jni_facade_ = jni_facade;
36-
}
37-
38-
AndroidSurfaceFactory::~AndroidSurfaceFactory() = default;
39-
4031
void AndroidSurfaceFactory::SetExternalViewEmbedder(
4132
std::shared_ptr<AndroidExternalViewEmbedder> external_view_embedder) {
4233
external_view_embedder_ = external_view_embedder;

shell/platform/android/surface/android_surface.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ namespace flutter {
88

99
AndroidSurface::~AndroidSurface() = default;
1010

11+
AndroidSurfaceFactory::AndroidSurfaceFactory(
12+
std::shared_ptr<AndroidContext> context,
13+
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
14+
android_context_ = context;
15+
jni_facade_ = jni_facade;
16+
}
17+
18+
AndroidSurfaceFactory::~AndroidSurfaceFactory() = default;
19+
1120
} // namespace flutter

shell/platform/android/surface/android_surface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ class AndroidSurfaceFactory {
4242
AndroidSurfaceFactory(std::shared_ptr<AndroidContext> context,
4343
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);
4444

45-
~AndroidSurfaceFactory();
45+
virtual ~AndroidSurfaceFactory();
4646

4747
void SetExternalViewEmbedder(
4848
std::shared_ptr<AndroidExternalViewEmbedder> external_view_embedder);
4949

50-
std::unique_ptr<AndroidSurface> CreateSurface();
50+
virtual std::unique_ptr<AndroidSurface> CreateSurface() = 0;
5151

5252
private:
5353
std::shared_ptr<AndroidContext> android_context_;

0 commit comments

Comments
 (0)