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

Commit 20ec34e

Browse files
authored
added unit tests for the android embedder that run on android devices (#28784)
1 parent 1f74de8 commit 20ec34e

File tree

11 files changed

+153
-13
lines changed

11 files changed

+153
-13
lines changed

BUILD.gn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ group("flutter") {
145145
"//flutter/third_party/txt:txt_unittests",
146146
]
147147

148+
if (is_android) {
149+
public_deps +=
150+
[ "//flutter/shell/platform/android:flutter_shell_native_unittests" ]
151+
}
152+
148153
# The accessibility library only supports Mac and Windows at the moment.
149154
if (is_mac || is_win) {
150155
public_deps +=

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h
770770
FILE: ../../../flutter/shell/platform/android/AndroidManifest.xml
771771
FILE: ../../../flutter/shell/platform/android/android_context_gl.cc
772772
FILE: ../../../flutter/shell/platform/android/android_context_gl.h
773+
FILE: ../../../flutter/shell/platform/android/android_context_gl_unittests.cc
773774
FILE: ../../../flutter/shell/platform/android/android_environment_gl.cc
774775
FILE: ../../../flutter/shell/platform/android/android_environment_gl.h
775776
FILE: ../../../flutter/shell/platform/android/android_exports.lst
@@ -779,6 +780,7 @@ FILE: ../../../flutter/shell/platform/android/android_image_generator.cc
779780
FILE: ../../../flutter/shell/platform/android/android_image_generator.h
780781
FILE: ../../../flutter/shell/platform/android/android_shell_holder.cc
781782
FILE: ../../../flutter/shell/platform/android/android_shell_holder.h
783+
FILE: ../../../flutter/shell/platform/android/android_shell_holder_unittests.cc
782784
FILE: ../../../flutter/shell/platform/android/android_surface_gl.cc
783785
FILE: ../../../flutter/shell/platform/android/android_surface_gl.h
784786
FILE: ../../../flutter/shell/platform/android/android_surface_software.cc
@@ -795,6 +797,7 @@ FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_poo
795797
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool_unittests.cc
796798
FILE: ../../../flutter/shell/platform/android/flutter_main.cc
797799
FILE: ../../../flutter/shell/platform/android/flutter_main.h
800+
FILE: ../../../flutter/shell/platform/android/flutter_shell_native_unittests.cc
798801
FILE: ../../../flutter/shell/platform/android/io/flutter/FlutterInjector.java
799802
FILE: ../../../flutter/shell/platform/android/io/flutter/Log.java
800803
FILE: ../../../flutter/shell/platform/android/io/flutter/app/FlutterActivity.java

shell/common/platform_view.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include <functional>
99
#include <memory>
1010

11-
#include "flow/embedded_views.h"
1211
#include "flutter/common/graphics/texture.h"
1312
#include "flutter/common/task_runners.h"
13+
#include "flutter/flow/embedded_views.h"
1414
#include "flutter/flow/surface.h"
1515
#include "flutter/fml/macros.h"
1616
#include "flutter/fml/mapping.h"

shell/platform/android/BUILD.gn

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,30 @@ source_set("image_generator") {
3535
]
3636
}
3737

38-
shared_library("flutter_shell_native") {
39-
visibility = [ ":*" ]
38+
executable("flutter_shell_native_unittests") {
39+
visibility = [ "*" ]
40+
testonly = true
41+
sources = [
42+
"android_context_gl_unittests.cc",
43+
"android_shell_holder_unittests.cc",
44+
"flutter_shell_native_unittests.cc",
45+
]
46+
public_configs = [ "//flutter:config" ]
47+
deps = [
48+
":flutter_shell_native_src",
49+
"//third_party/googletest:gmock",
50+
"//third_party/googletest:gtest",
51+
]
52+
}
4053

54+
shared_library("flutter_shell_native") {
4155
output_name = "flutter"
56+
deps = [ ":flutter_shell_native_src" ]
57+
ldflags = [ "-Wl,--version-script=" + rebase_path("android_exports.lst") ]
58+
}
59+
60+
source_set("flutter_shell_native_src") {
61+
visibility = [ ":*" ]
4262

4363
sources = [
4464
"$root_build_dir/flutter_icu/icudtl.o",
@@ -69,7 +89,7 @@ shared_library("flutter_shell_native") {
6989
"vsync_waiter_android.h",
7090
]
7191

72-
deps = [
92+
public_deps = [
7393
":android_gpu_configuration",
7494
":icudtl_object",
7595
":image_generator",
@@ -100,8 +120,6 @@ shared_library("flutter_shell_native") {
100120
"EGL",
101121
"GLESv2",
102122
]
103-
104-
ldflags = [ "-Wl,--version-script=" + rebase_path("android_exports.lst") ]
105123
}
106124

107125
action("gen_android_build_config_java") {

shell/platform/android/android_context_gl.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,18 @@ AndroidContextGL::~AndroidContextGL() {
204204

205205
std::unique_ptr<AndroidEGLSurface> AndroidContextGL::CreateOnscreenSurface(
206206
fml::RefPtr<AndroidNativeWindow> window) const {
207-
EGLDisplay display = environment_->Display();
207+
if (window->IsFakeWindow()) {
208+
return CreatePbufferSurface();
209+
} else {
210+
EGLDisplay display = environment_->Display();
208211

209-
const EGLint attribs[] = {EGL_NONE};
212+
const EGLint attribs[] = {EGL_NONE};
210213

211-
EGLSurface surface = eglCreateWindowSurface(
212-
display, config_, reinterpret_cast<EGLNativeWindowType>(window->handle()),
213-
attribs);
214-
return std::make_unique<AndroidEGLSurface>(surface, display, context_);
214+
EGLSurface surface = eglCreateWindowSurface(
215+
display, config_,
216+
reinterpret_cast<EGLNativeWindowType>(window->handle()), attribs);
217+
return std::make_unique<AndroidEGLSurface>(surface, display, context_);
218+
}
215219
}
216220

217221
std::unique_ptr<AndroidEGLSurface> AndroidContextGL::CreateOffscreenSurface()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <memory>
2+
#include "flutter/shell/platform/android/android_context_gl.h"
3+
#include "flutter/shell/platform/android/android_environment_gl.h"
4+
#include "gtest/gtest.h"
5+
6+
TEST(AndroidContextGl, Create) {
7+
auto environment = fml::MakeRefCounted<flutter::AndroidEnvironmentGL>();
8+
auto context = std::make_unique<flutter::AndroidContextGL>(
9+
flutter::AndroidRenderingAPI::kOpenGLES, environment);
10+
EXPECT_NE(context.get(), nullptr);
11+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <memory>
2+
#include "flutter/shell/platform/android/android_shell_holder.h"
3+
#include "gmock/gmock.h"
4+
#include "gtest/gtest.h"
5+
6+
namespace flutter {
7+
namespace testing {
8+
namespace {
9+
class MockPlatformViewAndroidJNI : public PlatformViewAndroidJNI {
10+
MOCK_METHOD2(FlutterViewHandlePlatformMessage,
11+
void(std::unique_ptr<flutter::PlatformMessage> message,
12+
int responseId));
13+
MOCK_METHOD2(FlutterViewHandlePlatformMessageResponse,
14+
void(int responseId, std::unique_ptr<fml::Mapping> data));
15+
MOCK_METHOD3(FlutterViewUpdateSemantics,
16+
void(std::vector<uint8_t> buffer,
17+
std::vector<std::string> strings,
18+
std::vector<std::vector<uint8_t>> string_attribute_args));
19+
MOCK_METHOD2(FlutterViewUpdateCustomAccessibilityActions,
20+
void(std::vector<uint8_t> actions_buffer,
21+
std::vector<std::string> strings));
22+
MOCK_METHOD0(FlutterViewOnFirstFrame, void());
23+
MOCK_METHOD0(FlutterViewOnPreEngineRestart, void());
24+
MOCK_METHOD2(SurfaceTextureAttachToGLContext,
25+
void(JavaLocalRef surface_texture, int textureId));
26+
MOCK_METHOD1(SurfaceTextureUpdateTexImage,
27+
void(JavaLocalRef surface_texture));
28+
MOCK_METHOD2(SurfaceTextureGetTransformMatrix,
29+
void(JavaLocalRef surface_texture, SkMatrix& transform));
30+
MOCK_METHOD1(SurfaceTextureDetachFromGLContext,
31+
void(JavaLocalRef surface_texture));
32+
MOCK_METHOD8(FlutterViewOnDisplayPlatformView,
33+
void(int view_id,
34+
int x,
35+
int y,
36+
int width,
37+
int height,
38+
int viewWidth,
39+
int viewHeight,
40+
MutatorsStack mutators_stack));
41+
MOCK_METHOD5(FlutterViewDisplayOverlaySurface,
42+
void(int surface_id, int x, int y, int width, int height));
43+
MOCK_METHOD0(FlutterViewBeginFrame, void());
44+
MOCK_METHOD0(FlutterViewEndFrame, void());
45+
MOCK_METHOD0(FlutterViewCreateOverlaySurface,
46+
std::unique_ptr<PlatformViewAndroidJNI::OverlayMetadata>());
47+
MOCK_METHOD0(FlutterViewDestroyOverlaySurfaces, void());
48+
MOCK_METHOD1(FlutterViewComputePlatformResolvedLocale,
49+
std::unique_ptr<std::vector<std::string>>(
50+
std::vector<std::string> supported_locales_data));
51+
MOCK_METHOD0(GetDisplayRefreshRate, double());
52+
MOCK_METHOD1(RequestDartDeferredLibrary, bool(int loading_unit_id));
53+
};
54+
} // namespace
55+
56+
TEST(AndroidShellHolder, Create) {
57+
Settings settings;
58+
settings.enable_software_rendering = false;
59+
auto jni = std::make_shared<MockPlatformViewAndroidJNI>();
60+
auto holder = std::make_unique<AndroidShellHolder>(
61+
settings, jni, /*is_background_view=*/false);
62+
EXPECT_NE(holder.get(), nullptr);
63+
EXPECT_TRUE(holder->IsValid());
64+
EXPECT_NE(holder->GetPlatformView().get(), nullptr);
65+
auto window =
66+
fml::MakeRefCounted<AndroidNativeWindow>(nullptr, /*is_offscreen=*/true);
67+
holder->GetPlatformView()->NotifyCreated(window);
68+
}
69+
} // namespace testing
70+
} // namespace flutter
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "gtest/gtest.h"
2+
3+
int main(int argc, char* argv[]) {
4+
::testing::InitGoogleTest(&argc, argv);
5+
return RUN_ALL_TESTS();
6+
}

shell/platform/android/surface/android_native_window.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
namespace flutter {
88

9-
AndroidNativeWindow::AndroidNativeWindow(Handle window) : window_(window) {}
9+
AndroidNativeWindow::AndroidNativeWindow(Handle window, bool is_fake_window)
10+
: window_(window), is_fake_window_(is_fake_window) {}
11+
12+
AndroidNativeWindow::AndroidNativeWindow(Handle window)
13+
: AndroidNativeWindow(window, /*is_fake_window=*/false) {}
1014

1115
AndroidNativeWindow::~AndroidNativeWindow() {
1216
if (window_ != nullptr) {

shell/platform/android/surface/android_native_window.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ class AndroidNativeWindow
3232

3333
SkISize GetSize() const;
3434

35+
/// Returns true when this AndroidNativeWindow is not backed by a real window
36+
/// (used for testing).
37+
bool IsFakeWindow() const { return is_fake_window_; }
38+
3539
private:
3640
Handle window_;
41+
const bool is_fake_window_;
3742

3843
/// Creates a native window with the given handle. Handle ownership is assumed
3944
/// by this instance of the native window.
4045
explicit AndroidNativeWindow(Handle window);
4146

47+
explicit AndroidNativeWindow(Handle window, bool is_fake_window);
48+
4249
~AndroidNativeWindow();
4350

4451
FML_FRIEND_MAKE_REF_COUNTED(AndroidNativeWindow);

0 commit comments

Comments
 (0)