Skip to content

Commit 112f170

Browse files
Reland hybrid views test (flutter#181336)
This test was deleted in flutter#162903 under the claim that it wasn't running, but it was actually (just only in bringup). In fact the [glue code](https://github.com/flutter/flutter/blob/dbed04aa32e5f0319898d29c8996919ee6ae8e2e/dev/devicelab/lib/tasks/integration_tests.dart#L75) and [ci.yaml](https://github.com/flutter/flutter/blob/master/.ci.yaml#L2874) target are still around. It tested stuff that isn't covered by the current testing landscape, like alert dialogue popup and event types being preserved. The original pr was not a pure deletion so I maintained the good changes (mostly a change to use shared code for the app under test). Had to make some changes (described in comments below) but the test is passing in its current state. --------- Co-authored-by: Gray Mackall <[email protected]>
1 parent 91a8eb9 commit 112f170

28 files changed

Lines changed: 2433 additions & 0 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2014 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+
import 'package:flutter_devicelab/framework/devices.dart';
6+
import 'package:flutter_devicelab/framework/framework.dart';
7+
import 'package:flutter_devicelab/tasks/integration_tests.dart';
8+
9+
Future<void> main() async {
10+
deviceOperatingSystem = DeviceOperatingSystem.android;
11+
await task(createHybridAndroidViewsIntegrationTest());
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Integration test for hybrid composition on Android
2+
3+
This test verifies that the synthesized motion events that get to embedded
4+
Android view are equal to the motion events that originally hit the FlutterView.
5+
6+
The test app's Android code listens to MotionEvents that get to FlutterView and
7+
to an embedded Android view and sends them over a platform channel to the Dart
8+
code where the events are matched.
9+
10+
This is what the app looks like:
11+
12+
![android_views test app](https://flutter.github.io/assets-for-api-docs/assets/readme-assets/android_views_test.png)
13+
14+
The blue part is the embedded Android view, because it is positioned at the top
15+
left corner, the coordinate systems for FlutterView and for the embedded view's
16+
virtual display has the same origin (this makes the MotionEvent comparison
17+
easier as we don't need to translate the coordinates).
18+
19+
The app includes the following control buttons:
20+
* RECORD - Start listening for MotionEvents for 3 seconds, matched/unmatched events are
21+
displayed in the listview as they arrive.
22+
* CLEAR - Clears the events that were recorded so far.
23+
* SAVE - Saves the events that hit FlutterView to a file.
24+
* PLAY FILE - Send a list of events from a bundled asset file to FlutterView.
25+
26+
A recorded touch events sequence is bundled as an asset in the
27+
assets_for_android_view package which lives in the goldens repository.
28+
29+
When running this test with `flutter drive` the record touch sequences is
30+
replayed and the test asserts that the events that got to FlutterView are
31+
equivalent to the ones that got to the embedded view.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2014 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+
plugins {
6+
id "com.android.application"
7+
id "dev.flutter.flutter-gradle-plugin"
8+
}
9+
10+
def localProperties = new Properties()
11+
def localPropertiesFile = rootProject.file('local.properties')
12+
if (localPropertiesFile.exists()) {
13+
localPropertiesFile.withReader('UTF-8') { reader ->
14+
localProperties.load(reader)
15+
}
16+
}
17+
18+
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
19+
if (flutterVersionCode == null) {
20+
flutterVersionCode = '1'
21+
}
22+
23+
def flutterVersionName = localProperties.getProperty('flutter.versionName')
24+
if (flutterVersionName == null) {
25+
flutterVersionName = '1.0'
26+
}
27+
28+
android {
29+
namespace = "io.flutter.integration.platformviews"
30+
compileSdk flutter.compileSdkVersion
31+
32+
// Flutter's CI installs the NDK at a non-standard path.
33+
// This non-standard structure is initially created by
34+
// https://github.com/flutter/engine/blob/3.27.0/tools/android_sdk/create_cipd_packages.sh.
35+
String systemNdkPath = System.getenv("ANDROID_NDK_PATH")
36+
if (systemNdkPath != null) {
37+
ndkVersion = flutter.ndkVersion
38+
ndkPath = systemNdkPath
39+
}
40+
41+
compileOptions {
42+
sourceCompatibility JavaVersion.VERSION_1_8
43+
targetCompatibility JavaVersion.VERSION_1_8
44+
}
45+
46+
defaultConfig {
47+
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
48+
applicationId "io.flutter.integration.platformviews"
49+
minSdkVersion flutter.minSdkVersion
50+
targetSdkVersion flutter.targetSdkVersion
51+
versionCode flutterVersionCode.toInteger()
52+
versionName flutterVersionName
53+
}
54+
55+
buildTypes {
56+
release {
57+
// TODO: Add your own signing config for the release build.
58+
// Signing with the debug keys for now, so `flutter run --release` works.
59+
signingConfig signingConfigs.debug
60+
}
61+
}
62+
}
63+
64+
flutter {
65+
source '../..'
66+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!-- Copyright 2014 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+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
6+
7+
<uses-permission android:name="android.permission.INTERNET"/>
8+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
9+
10+
<application
11+
android:name="${applicationName}"
12+
android:label="platform_views">
13+
<activity
14+
android:name=".MainActivity"
15+
android:launchMode="singleTop"
16+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
17+
android:hardwareAccelerated="true"
18+
android:windowSoftInputMode="adjustResize"
19+
android:exported="true">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN"/>
22+
<category android:name="android.intent.category.LAUNCHER"/>
23+
</intent-filter>
24+
</activity>
25+
<meta-data
26+
android:name="flutterEmbedding"
27+
android:value="2" />
28+
</application>
29+
<!-- Required to query activities that can process text, see:
30+
https://developer.android.com/training/package-visibility and
31+
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
32+
33+
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
34+
<queries>
35+
<intent>
36+
<action android:name="android.intent.action.PROCESS_TEXT"/>
37+
<data android:mimeType="text/plain"/>
38+
</intent>
39+
</queries>
40+
</manifest>
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Copyright 2014 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+
package io.flutter.integration.platformviews;
6+
7+
import android.Manifest;
8+
import android.content.pm.PackageManager;
9+
import android.os.Build;
10+
import android.os.Bundle;
11+
import android.view.MotionEvent;
12+
import android.view.View;
13+
import android.view.ViewGroup;
14+
15+
import java.lang.StringBuilder;
16+
import java.util.HashMap;
17+
18+
import io.flutter.embedding.android.FlutterActivity;
19+
import io.flutter.embedding.android.FlutterImageView;
20+
import io.flutter.embedding.android.FlutterSurfaceView;
21+
import io.flutter.embedding.android.FlutterTextureView;
22+
import io.flutter.embedding.android.FlutterView;
23+
import io.flutter.embedding.engine.dart.DartExecutor;
24+
import io.flutter.embedding.engine.FlutterEngine;
25+
import io.flutter.plugin.common.MethodCall;
26+
import io.flutter.plugin.common.MethodChannel;
27+
import io.flutter.plugins.GeneratedPluginRegistrant;
28+
29+
public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler {
30+
final static int STORAGE_PERMISSION_CODE = 1;
31+
32+
MethodChannel mMethodChannel;
33+
34+
// The method result to complete with the Android permission request result.
35+
// This is null when not waiting for the Android permission request;
36+
private MethodChannel.Result permissionResult;
37+
38+
private View getFlutterView() {
39+
return findViewById(FLUTTER_VIEW_ID);
40+
}
41+
42+
private String getViewName(View view) {
43+
if (view instanceof FlutterImageView) {
44+
return "FlutterImageView";
45+
}
46+
if (view instanceof FlutterSurfaceView) {
47+
return "FlutterSurfaceView";
48+
}
49+
if (view instanceof FlutterTextureView) {
50+
return "FlutterTextureView";
51+
}
52+
if (view instanceof FlutterView) {
53+
return "FlutterView";
54+
}
55+
if (view instanceof ViewGroup) {
56+
return "ViewGroup";
57+
}
58+
return "View";
59+
}
60+
61+
private void recurseViewHierarchy(View current, String padding, StringBuilder builder) {
62+
if (current.getVisibility() != View.VISIBLE || current.getAlpha() == 0) {
63+
return;
64+
}
65+
String name = getViewName(current);
66+
builder.append(padding);
67+
builder.append("|-");
68+
builder.append(name);
69+
builder.append("\n");
70+
71+
if (current instanceof ViewGroup) {
72+
ViewGroup viewGroup = (ViewGroup) current;
73+
for (int index = 0; index < viewGroup.getChildCount(); index++) {
74+
recurseViewHierarchy(viewGroup.getChildAt(index), padding + " ", builder);
75+
}
76+
}
77+
}
78+
79+
/**
80+
* Serializes the view hierarchy, so it can be sent to Dart over the method channel.
81+
*
82+
* Notation:
83+
* |- <view-name>
84+
* |- ... child view ordered by z order.
85+
*
86+
* Example output:
87+
* |- FlutterView
88+
* |- FlutterImageView
89+
* |- ViewGroup
90+
* |- View
91+
*/
92+
private String getSerializedViewHierarchy() {
93+
View root = getFlutterView();
94+
StringBuilder builder = new StringBuilder();
95+
recurseViewHierarchy(root, "", builder);
96+
return builder.toString();
97+
}
98+
99+
@Override
100+
public void configureFlutterEngine(FlutterEngine flutterEngine) {
101+
DartExecutor executor = flutterEngine.getDartExecutor();
102+
flutterEngine
103+
.getPlatformViewsController()
104+
.getRegistry()
105+
.registerViewFactory("simple_view", new SimpleViewFactory(executor));
106+
mMethodChannel = new MethodChannel(executor, "android_views_integration");
107+
mMethodChannel.setMethodCallHandler(this);
108+
GeneratedPluginRegistrant.registerWith(flutterEngine);
109+
}
110+
111+
@Override
112+
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
113+
switch (methodCall.method) {
114+
case "getStoragePermission":
115+
if (permissionResult != null) {
116+
result.error("error", "already waiting for permissions", null);
117+
return;
118+
}
119+
permissionResult = result;
120+
getExternalStoragePermissions();
121+
return;
122+
case "synthesizeEvent":
123+
synthesizeEvent(methodCall);
124+
result.success(null);
125+
return;
126+
case "getViewHierarchy":
127+
String viewHierarchy = getSerializedViewHierarchy();
128+
result.success(viewHierarchy);
129+
return;
130+
}
131+
result.notImplemented();
132+
}
133+
134+
@SuppressWarnings("unchecked")
135+
public void synthesizeEvent(MethodCall methodCall) {
136+
MotionEvent event = MotionEventCodec.decode((HashMap<String, Object>) methodCall.arguments());
137+
getFlutterView().dispatchTouchEvent(event);
138+
// TODO(egarciad): Remove invokeMethod since it is not necessary.
139+
mMethodChannel.invokeMethod("onTouch", MotionEventCodec.encode(event));
140+
}
141+
142+
@Override
143+
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
144+
if (requestCode != STORAGE_PERMISSION_CODE || permissionResult == null)
145+
return;
146+
boolean permissionGranted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
147+
sendPermissionResult(permissionGranted);
148+
}
149+
150+
private void getExternalStoragePermissions() {
151+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
152+
return;
153+
154+
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
155+
== PackageManager.PERMISSION_GRANTED) {
156+
sendPermissionResult(true);
157+
return;
158+
}
159+
160+
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
161+
}
162+
163+
private void sendPermissionResult(boolean result) {
164+
if (permissionResult == null)
165+
return;
166+
permissionResult.success(result);
167+
permissionResult = null;
168+
}
169+
}

0 commit comments

Comments
 (0)