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

Commit de4f3db

Browse files
committed
fix unittests
1 parent 3ccb566 commit de4f3db

File tree

6 files changed

+88
-53
lines changed

6 files changed

+88
-53
lines changed

shell/platform/android/external_view_embedder/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ executable("android_external_view_embedder_unittests") {
4444
"//flutter/flow",
4545
"//flutter/shell/gpu:gpu_surface_gl",
4646
"//flutter/shell/platform/android/jni:jni_mock",
47+
"//flutter/shell/platform/android/surface",
4748
"//flutter/shell/platform/android/surface:surface_mock",
4849
"//flutter/testing",
4950
"//flutter/testing:dart",

shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc

Lines changed: 28 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,24 @@ 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+
surface_producer_ = surface_producer;
31+
}
32+
33+
~TestAndroidSurfaceFactory() override = default;
34+
35+
std::unique_ptr<AndroidSurface> CreateSurface() override {
36+
return surface_producer_();
37+
}
38+
39+
private:
40+
TestSurfaceProducer surface_producer_;
41+
};
42+
2443
class SurfaceMock : public Surface {
2544
public:
2645
MOCK_METHOD(bool, IsValid, (), (override));
@@ -263,10 +282,8 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame) {
263282
auto window = fml::MakeRefCounted<AndroidNativeWindow>(nullptr);
264283
auto gr_context = GrDirectContext::MakeMock(nullptr);
265284
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) {
285+
auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
286+
[gr_context, window, frame_size]() {
270287
auto surface_frame_1 = std::make_unique<SurfaceFrame>(
271288
SkSurface::MakeNull(1000, 1000), false,
272289
[](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
@@ -293,7 +310,7 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame) {
293310
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
294311

295312
return android_surface_mock;
296-
};
313+
});
297314
auto embedder = std::make_unique<AndroidExternalViewEmbedder>(
298315
android_context, jni_mock, surface_factory);
299316

@@ -482,10 +499,8 @@ TEST(AndroidExternalViewEmbedder, DestroyOverlayLayersOnSizeChange) {
482499
auto window = fml::MakeRefCounted<AndroidNativeWindow>(nullptr);
483500
auto gr_context = GrDirectContext::MakeMock(nullptr);
484501
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) {
502+
auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
503+
[gr_context, window, frame_size]() {
489504
auto surface_frame_1 = std::make_unique<SurfaceFrame>(
490505
SkSurface::MakeNull(1000, 1000), false,
491506
[](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
@@ -505,7 +520,7 @@ TEST(AndroidExternalViewEmbedder, DestroyOverlayLayersOnSizeChange) {
505520
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
506521

507522
return android_surface_mock;
508-
};
523+
});
509524

510525
auto embedder = std::make_unique<AndroidExternalViewEmbedder>(
511526
android_context, jni_mock, surface_factory);
@@ -565,10 +580,8 @@ TEST(AndroidExternalViewEmbedder, DoesNotDestroyOverlayLayersOnSizeChange) {
565580
auto window = fml::MakeRefCounted<AndroidNativeWindow>(nullptr);
566581
auto gr_context = GrDirectContext::MakeMock(nullptr);
567582
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) {
583+
auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
584+
[gr_context, window, frame_size]() {
572585
auto surface_frame_1 = std::make_unique<SurfaceFrame>(
573586
SkSurface::MakeNull(1000, 1000), false,
574587
[](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
@@ -588,7 +601,7 @@ TEST(AndroidExternalViewEmbedder, DoesNotDestroyOverlayLayersOnSizeChange) {
588601
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
589602

590603
return android_surface_mock;
591-
};
604+
});
592605

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

shell/platform/android/external_view_embedder/surface_pool_unittests.cc

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ 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+
surface_producer_ = surface_producer;
27+
}
28+
29+
~TestAndroidSurfaceFactory() override = default;
30+
31+
std::unique_ptr<AndroidSurface> CreateSurface() override {
32+
return surface_producer_();
33+
}
34+
35+
private:
36+
TestSurfaceProducer surface_producer_;
37+
};
38+
2139
TEST(SurfacePool, GetLayer__AllocateOneLayer) {
2240
auto pool = std::make_unique<SurfacePool>();
2341

@@ -33,14 +51,13 @@ TEST(SurfacePool, GetLayer__AllocateOneLayer) {
3351
0, window))));
3452

3553
auto surface_factory =
36-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
37-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
54+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
3855
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
3956
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
4057
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
4158
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
4259
return android_surface_mock;
43-
};
60+
});
4461
auto layer = pool->GetLayer(gr_context.get(), android_context, jni_mock,
4562
surface_factory);
4663

@@ -64,14 +81,13 @@ TEST(SurfacePool, GetUnusedLayers) {
6481
0, window))));
6582

6683
auto surface_factory =
67-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
68-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
84+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
6985
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
7086
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
7187
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
7288
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
7389
return android_surface_mock;
74-
};
90+
});
7591
auto layer = pool->GetLayer(gr_context.get(), android_context, jni_mock,
7692
surface_factory);
7793
ASSERT_EQ(0UL, pool->GetUnusedLayers().size());
@@ -97,10 +113,8 @@ TEST(SurfacePool, GetLayer__Recycle) {
97113
std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
98114

99115
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) {
116+
auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
117+
[gr_context_1, gr_context_2, window]() {
104118
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
105119
// Allocate two GPU surfaces for each gr context.
106120
EXPECT_CALL(*android_surface_mock,
@@ -111,7 +125,7 @@ TEST(SurfacePool, GetLayer__Recycle) {
111125
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
112126
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
113127
return android_surface_mock;
114-
};
128+
});
115129
auto layer_1 = pool->GetLayer(gr_context_1.get(), android_context, jni_mock,
116130
surface_factory);
117131

@@ -147,14 +161,13 @@ TEST(SurfacePool, GetLayer__AllocateTwoLayers) {
147161
1, window))));
148162

149163
auto surface_factory =
150-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
151-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
164+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
152165
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
153166
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
154167
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
155168
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
156169
return android_surface_mock;
157-
};
170+
});
158171
auto layer_1 = pool->GetLayer(gr_context.get(), android_context, jni_mock,
159172
surface_factory);
160173
auto layer_2 = pool->GetLayer(gr_context.get(), android_context, jni_mock,
@@ -185,14 +198,13 @@ TEST(SurfacePool, DestroyLayers) {
185198
0, window))));
186199

187200
auto surface_factory =
188-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
189-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
201+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
190202
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
191203
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
192204
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
193205
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
194206
return android_surface_mock;
195-
};
207+
});
196208
pool->GetLayer(gr_context.get(), android_context, jni_mock, surface_factory);
197209

198210
EXPECT_CALL(*jni_mock, FlutterViewDestroyOverlaySurfaces());
@@ -213,14 +225,13 @@ TEST(SurfacePool, DestroyLayers__frameSizeChanged) {
213225
auto window = fml::MakeRefCounted<AndroidNativeWindow>(nullptr);
214226

215227
auto surface_factory =
216-
[gr_context, window](std::shared_ptr<AndroidContext> android_context,
217-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
228+
std::make_shared<TestAndroidSurfaceFactory>([gr_context, window]() {
218229
auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
219230
EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
220231
EXPECT_CALL(*android_surface_mock, SetNativeWindow(window));
221232
EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(true));
222233
return android_surface_mock;
223-
};
234+
});
224235
pool->SetFrameSize(SkISize::Make(10, 10));
225236
EXPECT_CALL(*jni_mock, FlutterViewDestroyOverlaySurfaces()).Times(0);
226237
EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())

shell/platform/android/platform_view_android.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@
2828

2929
namespace flutter {
3030

31-
AndroidSurfaceFactory::AndroidSurfaceFactory(
31+
AndroidSurfaceFactoryImpl::AndroidSurfaceFactoryImpl(
3232
std::shared_ptr<AndroidContext> context,
3333
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
3434
android_context_ = context;
3535
jni_facade_ = jni_facade;
3636
}
3737

38-
AndroidSurfaceFactory::~AndroidSurfaceFactory() = default;
38+
AndroidSurfaceFactoryImpl::~AndroidSurfaceFactoryImpl() = default;
3939

40-
void AndroidSurfaceFactory::SetExternalViewEmbedder(
40+
void AndroidSurfaceFactoryImpl::SetExternalViewEmbedder(
4141
std::shared_ptr<AndroidExternalViewEmbedder> external_view_embedder) {
4242
external_view_embedder_ = external_view_embedder;
4343
}
4444

45-
std::unique_ptr<AndroidSurface> AndroidSurfaceFactory::CreateSurface() {
45+
std::unique_ptr<AndroidSurface> AndroidSurfaceFactoryImpl::CreateSurface() {
4646
FML_CHECK(external_view_embedder_);
4747
switch (android_context_->RenderingApi()) {
4848
case AndroidRenderingAPI::kSoftware:
@@ -88,7 +88,7 @@ PlatformViewAndroid::PlatformViewAndroid(
8888
<< "Could not create an Android context.";
8989

9090
surface_factory_ =
91-
std::make_shared<AndroidSurfaceFactory>(android_context, jni_facade);
91+
std::make_shared<AndroidSurfaceFactoryImpl>(android_context, jni_facade);
9292
surface_factory_->SetExternalViewEmbedder(
9393
std::make_shared<AndroidExternalViewEmbedder>(android_context, jni_facade,
9494
surface_factory_));

shell/platform/android/platform_view_android.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@
2222

2323
namespace flutter {
2424

25+
class AndroidSurfaceFactoryImpl : public AndroidSurfaceFactory {
26+
public:
27+
AndroidSurfaceFactoryImpl(std::shared_ptr<AndroidContext> context,
28+
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);
29+
30+
~AndroidSurfaceFactoryImpl() override;
31+
32+
std::unique_ptr<AndroidSurface> CreateSurface() override;
33+
34+
void SetExternalViewEmbedder(
35+
std::shared_ptr<AndroidExternalViewEmbedder> external_view_embedder);
36+
37+
private:
38+
std::shared_ptr<AndroidContext> android_context_;
39+
std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
40+
std::shared_ptr<AndroidExternalViewEmbedder> external_view_embedder_;
41+
42+
};
43+
2544
class PlatformViewAndroid final : public PlatformView {
2645
public:
2746
static bool Register(JNIEnv* env);
@@ -80,7 +99,7 @@ class PlatformViewAndroid final : public PlatformView {
8099

81100
private:
82101
const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
83-
std::shared_ptr<AndroidSurfaceFactory> surface_factory_;
102+
std::shared_ptr<AndroidSurfaceFactoryImpl> surface_factory_;
84103

85104
PlatformViewAndroidDelegate platform_view_android_delegate_;
86105

shell/platform/android/surface/android_surface.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,11 @@ class AndroidSurface {
3939

4040
class AndroidSurfaceFactory {
4141
public:
42-
AndroidSurfaceFactory(std::shared_ptr<AndroidContext> context,
43-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);
42+
AndroidSurfaceFactory() = default;
4443

45-
~AndroidSurfaceFactory();
44+
virtual ~AndroidSurfaceFactory() = default;
4645

47-
void SetExternalViewEmbedder(
48-
std::shared_ptr<AndroidExternalViewEmbedder> external_view_embedder);
49-
50-
std::unique_ptr<AndroidSurface> CreateSurface();
51-
52-
private:
53-
std::shared_ptr<AndroidContext> android_context_;
54-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
55-
std::shared_ptr<AndroidExternalViewEmbedder> external_view_embedder_;
46+
virtual std::unique_ptr<AndroidSurface> CreateSurface() = 0;
5647
};
5748

5849
} // namespace flutter

0 commit comments

Comments
 (0)