Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
platform_view_android_jni_impl_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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 "gmock/gmock.h"
6#include "gtest/gtest.h"
7
16
17namespace flutter {
18namespace testing {
19
20using ::testing::_;
21using ::testing::Return;
22using ::testing::ReturnArg;
23
24class PlatformViewAndroidJNIImplTest : public ::testing::Test {
25 public:
26 static void SetUpTestSuite() {
27 static std::once_flag jvm_init_flag;
28 std::call_once(jvm_init_flag, SetUpJVM);
29 }
30
31 private:
32 friend class MockJNIEnvProvider;
33 static MockJavaVM jvm_;
34 static void SetUpJVM();
35};
36
37MockJavaVM PlatformViewAndroidJNIImplTest::jvm_;
38
40 public:
42 PlatformViewAndroidJNIImplTest::jvm_.SetJNIEnv(&env_);
43 }
45 PlatformViewAndroidJNIImplTest::jvm_.SetJNIEnv(nullptr);
46 }
47 MockJNIEnv& env() { return env_; }
48
49 private:
50 MockJNIEnv env_;
51};
52
53void PlatformViewAndroidJNIImplTest::SetUpJVM() {
55
56 MockJNIEnvProvider env_provider;
57 MockJNIEnv& mock_env = env_provider.env();
58
59 const jclass kPlaceholderClass = reinterpret_cast<jclass>(100);
60 const jfieldID kPlaceholderFieldID = reinterpret_cast<jfieldID>(200);
61 const jmethodID kPlaceholderMethodID = reinterpret_cast<jmethodID>(300);
62
63 EXPECT_CALL(mock_env, GetObjectRefType(_))
64 .WillRepeatedly(Return(JNILocalRefType));
65 EXPECT_CALL(mock_env, NewLocalRef(_)).WillRepeatedly(ReturnArg<0>());
66 EXPECT_CALL(mock_env, DeleteLocalRef(_)).WillRepeatedly(Return());
67 EXPECT_CALL(mock_env, NewGlobalRef(_)).WillRepeatedly(ReturnArg<0>());
68 EXPECT_CALL(mock_env, DeleteGlobalRef(_)).WillRepeatedly(Return());
69 EXPECT_CALL(mock_env, FindClass(_)).WillRepeatedly(Return(kPlaceholderClass));
70 EXPECT_CALL(mock_env, GetFieldID(_, _, _))
71 .WillRepeatedly(Return(kPlaceholderFieldID));
72 EXPECT_CALL(mock_env, GetMethodID(_, _, _))
73 .WillRepeatedly(Return(kPlaceholderMethodID));
74 EXPECT_CALL(mock_env, GetStaticFieldID(_, _, _))
75 .WillRepeatedly(Return(kPlaceholderFieldID));
76 EXPECT_CALL(mock_env, GetStaticMethodID(_, _, _))
77 .WillRepeatedly(Return(kPlaceholderMethodID));
78 EXPECT_CALL(mock_env, ExceptionCheck()).WillRepeatedly(Return(JNI_FALSE));
79 EXPECT_CALL(mock_env, RegisterNatives(_, _, _)).WillRepeatedly(Return(0));
80
82}
83
84TEST_F(PlatformViewAndroidJNIImplTest, ImageGetHardwareBufferException) {
85 MockJNIEnvProvider env_provider;
86 MockJNIEnv& mock_env = env_provider.env();
87
88 // Call ImageGetHardwareBuffer and simulate throwing an exception.
89 // Verify that it clears the exception and does not abort the process.
90 EXPECT_CALL(mock_env, GetObjectRefType(_))
91 .WillRepeatedly(Return(JNILocalRefType));
92 EXPECT_CALL(mock_env, NewLocalRef(_)).WillRepeatedly(ReturnArg<0>());
93 EXPECT_CALL(mock_env, DeleteLocalRef(_)).WillRepeatedly(Return());
94 EXPECT_CALL(mock_env, CallObjectMethodV(_, _, _))
95 .WillRepeatedly(Return(nullptr));
96 EXPECT_CALL(mock_env, ExceptionCheck()).WillOnce(Return(JNI_TRUE));
97 EXPECT_CALL(mock_env, ExceptionDescribe()).WillOnce(Return());
98 EXPECT_CALL(mock_env, ExceptionClear()).Times(1).WillOnce(Return());
99
100 fml::jni::JavaObjectWeakGlobalRef flutter_jni_object;
101 PlatformViewAndroidJNIImpl android_jni(flutter_jni_object);
102
104 reinterpret_cast<jobject>(123));
105 android_jni.ImageGetHardwareBuffer(image);
106}
107
108TEST_F(PlatformViewAndroidJNIImplTest, SetViewportMetricsEmptyArrays) {
109 MockJNIEnvProvider env_provider;
110 MockJNIEnv& mock_env = env_provider.env();
111
112 typedef void (*SetViewportMetricsFn)(
113 JNIEnv*, jobject, jlong, jfloat, jint, jint, jint, jint, jint, jint, jint,
114 jint, jint, jint, jint, jint, jint, jint, jint, jintArray, jintArray,
115 jintArray, jint, jint, jint, jint, jint, jint, jint, jint);
116
117 SetViewportMetricsFn set_viewport_metrics = nullptr;
118
119 const jclass kPlaceholderClass = reinterpret_cast<jclass>(100);
120 const jfieldID kPlaceholderFieldID = reinterpret_cast<jfieldID>(200);
121 const jmethodID kPlaceholderMethodID = reinterpret_cast<jmethodID>(300);
122
123 EXPECT_CALL(mock_env, GetObjectRefType(_))
124 .WillRepeatedly(Return(JNILocalRefType));
125 EXPECT_CALL(mock_env, NewLocalRef(_)).WillRepeatedly(ReturnArg<0>());
126 EXPECT_CALL(mock_env, DeleteLocalRef(_)).WillRepeatedly(Return());
127 EXPECT_CALL(mock_env, NewGlobalRef(_)).WillRepeatedly(ReturnArg<0>());
128 EXPECT_CALL(mock_env, DeleteGlobalRef(_)).WillRepeatedly(Return());
129 EXPECT_CALL(mock_env, FindClass(_)).WillRepeatedly(Return(kPlaceholderClass));
130 EXPECT_CALL(mock_env, GetFieldID(_, _, _))
131 .WillRepeatedly(Return(kPlaceholderFieldID));
132 EXPECT_CALL(mock_env, GetMethodID(_, _, _))
133 .WillRepeatedly(Return(kPlaceholderMethodID));
134 EXPECT_CALL(mock_env, GetStaticFieldID(_, _, _))
135 .WillRepeatedly(Return(kPlaceholderFieldID));
136 EXPECT_CALL(mock_env, GetStaticMethodID(_, _, _))
137 .WillRepeatedly(Return(kPlaceholderMethodID));
138 EXPECT_CALL(mock_env, ExceptionCheck()).WillRepeatedly(Return(JNI_FALSE));
139
140 EXPECT_CALL(mock_env, RegisterNatives(_, _, _))
141 .WillRepeatedly(
142 [&](jclass clazz, const JNINativeMethod* methods, jint nMethods) {
143 for (jint i = 0; i < nMethods; ++i) {
144 if (strcmp(methods[i].name, "nativeSetViewportMetrics") == 0) {
145 set_viewport_metrics =
146 reinterpret_cast<SetViewportMetricsFn>(methods[i].fnPtr);
147 }
148 }
149 return 0;
150 });
151
153
154 ASSERT_NE(set_viewport_metrics, nullptr);
155
156 EXPECT_CALL(mock_env, GetArrayLength(_)).WillRepeatedly(Return(0));
157 EXPECT_CALL(mock_env, GetIntArrayRegion(_, _, _, _)).Times(0);
158
159 Settings settings;
160 settings.enable_software_rendering = false;
161 auto jni = std::make_shared<JNIMock>();
162 auto holder = std::make_unique<AndroidShellHolder>(
164
165 jobject jcaller = reinterpret_cast<jobject>(123);
166 jintArray bounds = reinterpret_cast<jintArray>(456);
167 jintArray type = reinterpret_cast<jintArray>(789);
168 jintArray state = reinterpret_cast<jintArray>(1011);
169
170 set_viewport_metrics(&mock_env, jcaller,
171 reinterpret_cast<jlong>(holder.get()), 1.0f, 100, 100, 0,
172 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, bounds, type, state,
173 0, 0, 0, 0, 0, 0, 0, 0);
174}
175
176} // namespace testing
177} // namespace flutter
void SetJNIEnv(JNIEnv *env)
Concrete implementation of PlatformViewAndroidJNI that is compiled with the Android toolchain.
JavaLocalRef ImageGetHardwareBuffer(JavaLocalRef image) override
Grab the HardwareBuffer from image.
FlutterVulkanImage * image
TEST_F(DisplayListTest, Defaults)
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
void InitJavaVM(JavaVM *vm)
Definition jni_util.cc:29
impeller::ShaderType type
bool enable_software_rendering
Definition settings.h:320