Commit 72ef4485 authored by Amir Hardon's avatar Amir Hardon Committed by amirh

Integration test for embeded Android Views touch support.

The test places an embedded Android view at the top left, and verifies
that motion events that get to FlutterView are equivalent to the
synthesized motion events that gets to the embedded view.

See the README.md for more high level details.
parent ad1eaff4
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: 19aa6f8de8c34af7e62f4791393a98d7decf1b65
channel: unknown
# Integration test for touch events on embedded Android views
This test verifies that the synthesized motion events that get to embedded
Android view are equal to the motion events that originally hit the FlutterView.
The test app's Android code listens to MotionEvents that get to FlutterView and
to an embedded Android view and sends them over a platform channel to the Dart
code, where the events are matched.
This is what the app looks like:
![android_views test app](https://flutter.github.io/assets-for-api-docs/assets/readme-assets/android_views_test.png)
The blue part is the embedded Android view, because it is positioned at the top
left corner, the coordinate systems for FlutterView and for the embedded view's
virtual display has the same origin (this makes the MotionEvent comparison
easier as we don't need to translate the coordinates).
The app includes the following control buttons:
* RECORD - Start listening for MotionEvents for 3 seconds, matched/unmatched events are
displayed in the listview as they arrive.
* CLEAR - Clears the events that were recorded so far.
* SAVE - Saves the events that hit FlutterView to a file.
* PLAY FILE - Send a list of events from a bundled asset file to FlutterView.
A recorded touch events sequence is bundled as an asset in the
assets_for_android_view package which lives in the goldens repository.
When running this test with `flutter drive` the record touch sequences is
replayed and the test asserts that the events that got to FlutterView are
equivalent to the ones that got to the embedded view.
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
throw new GradleException("versionCode not found. Define flutter.versionCode in the local.properties file.")
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
throw new GradleException("versionName not found. Define flutter.versionName in the local.properties file.")
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 27
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "io.flutter.integration.androidviews"
minSdkVersion 16
targetSdkVersion 27
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.integration.androidviews">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name="io.flutter.app.FlutterApplication"
android:label="android_views">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.integration.androidviews;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import java.util.HashMap;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler {
final static int STORAGE_PERMISSION_CODE = 1;
MethodChannel mMethodChannel;
TouchPipe mFlutterViewTouchPipe;
// The method result to complete with the Android permission request result.
// This is null when not waiting for the Android permission request;
private MethodChannel.Result permissionResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
getFlutterView().getPluginRegistry()
.registrarFor("io.flutter.integration.android_views").platformViewRegistry()
.registerViewFactory("simple_view", new SimpleViewFactory(getFlutterView()));
mMethodChannel = new MethodChannel(this.getFlutterView(), "android_views_integration");
mMethodChannel.setMethodCallHandler(this);
mFlutterViewTouchPipe = new TouchPipe(mMethodChannel, getFlutterView());
}
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
switch(methodCall.method) {
case "pipeFlutterViewEvents":
mFlutterViewTouchPipe.enable();
result.success(null);
return;
case "stopFlutterViewEvents":
mFlutterViewTouchPipe.disable();
result.success(null);
return;
case "getStoragePermission":
if (permissionResult != null) {
result.error("error", "already waiting for permissions", null);
return;
}
permissionResult = result;
getExternalStoragePermissions();
return;
case "synthesizeEvent":
synthesizeEvent(methodCall, result);
return;
}
result.notImplemented();
}
@SuppressWarnings("unchecked")
public void synthesizeEvent(MethodCall methodCall, MethodChannel.Result result) {
MotionEvent event = MotionEventCodec.decode((HashMap<String, Object>) methodCall.arguments());
getFlutterView().dispatchTouchEvent(event);
result.success(null);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode != STORAGE_PERMISSION_CODE || permissionResult == null)
return;
boolean permisisonGranted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
sendPermissionResult(permisisonGranted);
}
private void getExternalStoragePermissions() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return;
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
sendPermissionResult(true);
return;
}
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
private void sendPermissionResult(boolean result) {
if (permissionResult == null)
return;
permissionResult.success(result);
permissionResult = null;
}
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.integration.androidviews;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.MotionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static android.view.MotionEvent.PointerCoords;
import static android.view.MotionEvent.PointerProperties;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class MotionEventCodec {
public static HashMap<String, Object> encode(MotionEvent event) {
ArrayList<HashMap<String,Object>> pointerProperties = new ArrayList<>();
ArrayList<HashMap<String,Object>> pointerCoords = new ArrayList<>();
for (int i = 0; i < event.getPointerCount(); i++) {
MotionEvent.PointerProperties properties = new MotionEvent.PointerProperties();
event.getPointerProperties(i, properties);
pointerProperties.add(encodePointerProperties(properties));
MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
event.getPointerCoords(i, coords);
pointerCoords.add(encodePointerCoords(coords));
}
HashMap<String, Object> eventMap = new HashMap<>();
eventMap.put("downTime", event.getDownTime());
eventMap.put("eventTime", event.getEventTime());
eventMap.put("action", event.getAction());
eventMap.put("pointerCount", event.getPointerCount());
eventMap.put("pointerProperties", pointerProperties);
eventMap.put("pointerCoords", pointerCoords);
eventMap.put("metaState", event.getMetaState());
eventMap.put("buttonState", event.getButtonState());
eventMap.put("xPrecision", event.getXPrecision());
eventMap.put("yPrecision", event.getYPrecision());
eventMap.put("deviceId", event.getDeviceId());
eventMap.put("edgeFlags", event.getEdgeFlags());
eventMap.put("source", event.getSource());
eventMap.put("flags", event.getFlags());
return eventMap;
}
private static HashMap<String, Object> encodePointerProperties(PointerProperties properties) {
HashMap<String, Object> map = new HashMap<>();
map.put("id", properties.id);
map.put("toolType", properties.toolType);
return map;
}
private static HashMap<String, Object> encodePointerCoords(PointerCoords coords) {
HashMap<String, Object> map = new HashMap<>();
map.put("orientation", coords.orientation);
map.put("pressure", coords.pressure);
map.put("size", coords.size);
map.put("toolMajor", coords.toolMajor);
map.put("toolMinor", coords.toolMinor);
map.put("touchMajor", coords.touchMajor);
map.put("touchMinor", coords.touchMinor);
map.put("x", coords.x);
map.put("y", coords.y);
return map;
}
@SuppressWarnings("unchecked")
public static MotionEvent decode(HashMap<String, Object> data) {
List<PointerProperties> pointerProperties = new ArrayList<>();
List<PointerCoords> pointerCoords = new ArrayList<>();
for (HashMap<String, Object> property : (List<HashMap<String, Object>>) data.get("pointerProperties")) {
pointerProperties.add(decodePointerProperties(property)) ;
}
for (HashMap<String, Object> coord : (List<HashMap<String, Object>>) data.get("pointerCoords")) {
pointerCoords.add(decodePointerCoords(coord)) ;
}
return MotionEvent.obtain(
(int) data.get("downTime"),
(int) data.get("eventTime"),
(int) data.get("action"),
(int) data.get("pointerCount"),
pointerProperties.toArray(new PointerProperties[pointerProperties.size()]),
pointerCoords.toArray(new PointerCoords[pointerCoords.size()]),
(int) data.get("metaState"),
(int) data.get("buttonState"),
(float) (double) data.get("xPrecision"),
(float) (double) data.get("yPrecision"),
(int) data.get("deviceId"),
(int) data.get("edgeFlags"),
(int) data.get("source"),
(int) data.get("flags")
);
}
private static PointerProperties decodePointerProperties(HashMap<String, Object> data) {
PointerProperties properties = new PointerProperties();
properties.id = (int) data.get("id");
properties.toolType = (int) data.get("toolType");
return properties;
}
private static PointerCoords decodePointerCoords(HashMap<String, Object> data) {
PointerCoords coords = new PointerCoords();
coords.orientation = (float) (double) data.get("orientation");
coords.pressure = (float) (double) data.get("pressure");
coords.size = (float) (double) data.get("size");
coords.toolMajor = (float) (double) data.get("toolMajor");
coords.toolMinor = (float) (double) data.get("toolMinor");
coords.touchMajor = (float) (double) data.get("touchMajor");
coords.touchMinor = (float) (double) data.get("touchMinor");
coords.x = (float) (double) data.get("x");
coords.y = (float) (double) data.get("y");
return coords;
}
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.integration.androidviews;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
public class SimplePlatformView implements PlatformView, MethodChannel.MethodCallHandler {
private final View mView;
private final MethodChannel mMethodChannel;
private final TouchPipe mTouchPipe;
SimplePlatformView(Context context, MethodChannel methodChannel) {
mMethodChannel = methodChannel;
mView = new View(context) {
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
};
mView.setBackgroundColor(0xff0000ff);
mMethodChannel.setMethodCallHandler(this);
mTouchPipe = new TouchPipe(mMethodChannel, mView);
}
@Override
public View getView() {
return mView;
}
@Override
public void dispose() {
}
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
switch(methodCall.method) {
case "pipeTouchEvents":
mTouchPipe.enable();
result.success(null);
return;
case "stopTouchEvents":
mTouchPipe.disable();
result.success(null);
return;
}
result.notImplemented();
}
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.integration.androidviews;
import android.content.Context;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
public class SimpleViewFactory implements PlatformViewFactory {
final BinaryMessenger messenger;
public SimpleViewFactory(BinaryMessenger messenger) {
this.messenger = messenger;
}
@Override
public PlatformView create(Context context, int id) {
MethodChannel methodChannel = new MethodChannel(messenger, "simple_view/" + id);
return new SimplePlatformView(context, methodChannel);
}
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.integration.androidviews;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import io.flutter.plugin.common.MethodChannel;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
class TouchPipe implements View.OnTouchListener {
private final MethodChannel mMethodChannel;
private final View mView;
private boolean mEnabled;
TouchPipe(MethodChannel methodChannel, View view) {
mMethodChannel = methodChannel;
mView = view;
}
public void enable() {
if (mEnabled)
return;
mEnabled = true;
mView.setOnTouchListener(this);
}
public void disable() {
if(!mEnabled)
return;
mEnabled = false;
mView.setOnTouchListener(null);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
mMethodChannel.invokeMethod("onTouch", MotionEventCodec.encode(event));
return false;
}
}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
include ':app'
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}
plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
include ":$name"
project(":$name").projectDir = pluginDirectory
}
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
profile
DerivedData/
build/
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
.generated/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/app.flx
/Flutter/app.zip
/Flutter/flutter_assets/
/Flutter/App.framework
/Flutter/Flutter.framework
/Flutter/Generated.xcconfig
/ServiceDefinitions.json
Pods/
.symlinks/
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:Runner.xcodeproj">
</FileRef>
</Workspace>
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0910"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "97C146ED1CF9000F007C117D"
BuildableName = "Runner.app"
BlueprintName = "Runner"
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "97C146ED1CF9000F007C117D"
BuildableName = "Runner.app"
BlueprintName = "Runner"
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "97C146ED1CF9000F007C117D"
BuildableName = "Runner.app"
BlueprintName = "Runner"
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "97C146ED1CF9000F007C117D"
BuildableName = "Runner.app"
BlueprintName = "Runner"
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'package:path_provider/path_provider.dart';
import 'motion_event_diff.dart';
MethodChannel channel = const MethodChannel('android_views_integration');
const String kEventsFileName = 'touchEvents';
/// Wraps a flutter driver [DataHandler] with one that waits until a delegate is set.
///
/// This allows the driver test to call [FlutterDriver.requestData] before the handler was
/// set by the app in which case the requestData call will only complete once the app is ready
/// for it.
class FutureDataHandler {
final Completer<DataHandler> handlerCompleter = new Completer<DataHandler>();
Future<String> handleMessage(String message) async {
final DataHandler handler = await handlerCompleter.future;
return handler(message);
}
}
FutureDataHandler driverDataHandler = new FutureDataHandler();
void main() {
enableFlutterDriverExtension(handler: driverDataHandler.handleMessage);
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Android Views Integration Test',
home: new Scaffold(
body: new PlatformViewPage(),
),
);
}
}
class PlatformViewPage extends StatefulWidget {
@override
State createState() => new PlatformViewState();
}
class PlatformViewState extends State<PlatformViewPage> {
static const int kEventsBufferSize = 1000;
MethodChannel viewChannel;
/// The list of motion events that were passed to the FlutterView.
List<Map<String, dynamic>> flutterViewEvents = <Map<String, dynamic>>[];
/// The list of motion events that were passed to the embedded view.
List<Map<String, dynamic>> embeddedViewEvents = <Map<String, dynamic>>[];
@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
new SizedBox(
height: 300.0,
child: new AndroidView(
viewType: 'simple_view',
onPlatformViewCreated: onPlatformViewCreated),
),
new Expanded(
child: new ListView.builder(
itemBuilder: buildEventTile,
itemCount: flutterViewEvents.length,
),
),
new Row(
children: <Widget>[
new RaisedButton(
child: const Text('RECORD'),
onPressed: listenToFlutterViewEvents,
),
new RaisedButton(
child: const Text('CLEAR'),
onPressed: () {
setState(() {
flutterViewEvents.clear();
embeddedViewEvents.clear();
});
},
),
new RaisedButton(
child: const Text('SAVE'),
onPressed: () {
const StandardMessageCodec codec = StandardMessageCodec();
saveRecordedEvents(
codec.encodeMessage(flutterViewEvents), context);
},
),
new RaisedButton(
key: const ValueKey<String>('play'),
child: const Text('PLAY FILE'),
onPressed: () { playEventsFile(); },
)
],
)
],
);
}
Future<String> playEventsFile() async {
const StandardMessageCodec codec = StandardMessageCodec();
try {
final ByteData data = await rootBundle.load('packages/assets_for_android_views/assets/touchEvents');
final List<dynamic> unTypedRecordedEvents = codec.decodeMessage(data);
final List<Map<String, dynamic>> recordedEvents = unTypedRecordedEvents
.cast<Map<dynamic, dynamic>>()
.map((Map<dynamic, dynamic> e) =>e.cast<String, dynamic>())
.toList();
await channel.invokeMethod('pipeFlutterViewEvents');
await viewChannel.invokeMethod('pipeTouchEvents');
print('replaying ${recordedEvents.length} motion events');
for (Map<String, dynamic> event in recordedEvents.reversed) {
await channel.invokeMethod('synthesizeEvent', event);
}
await channel.invokeMethod('stopFlutterViewEvents');
await viewChannel.invokeMethod('stopTouchEvents');
if (flutterViewEvents.length != embeddedViewEvents.length)
return 'Synthesized ${flutterViewEvents.length} events but the embedded view received ${embeddedViewEvents.length} events';
final StringBuffer diff = new StringBuffer();
for (int i = 0; i < flutterViewEvents.length; ++i) {
final String currentDiff = diffMotionEvents(flutterViewEvents[i], embeddedViewEvents[i]);
if (currentDiff.isEmpty)
continue;
if (diff.isNotEmpty)
diff.write(', ');
diff.write(currentDiff);
}
return diff.toString();
} catch(e) {
return e.toString();
}
}
@override
void initState() {
super.initState();
channel.setMethodCallHandler(onMethodChannelCall);
}
Future<void> saveRecordedEvents(ByteData data, BuildContext context) async {
if (!await channel.invokeMethod('getStoragePermission')) {
showMessage(
context, 'External storage permissions are required to save events');
return;
}
try {
final Directory outDir = await getExternalStorageDirectory();
// This test only runs on Android so we can assume path separator is '/'.
final File file = new File('${outDir.path}/$kEventsFileName');
await file.writeAsBytes(data.buffer.asUint8List(0, data.lengthInBytes), flush: true);
showMessage(context, 'Saved original events to ${file.path}');
} catch (e) {
showMessage(context, 'Failed saving ${e.toString()}');
}
}
void showMessage(BuildContext context, String message) {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text(message),
duration: const Duration(seconds: 3),
));
}
void onPlatformViewCreated(int id) {
viewChannel = new MethodChannel('simple_view/$id');
viewChannel.setMethodCallHandler(onViewMethodChannelCall);
driverDataHandler.handlerCompleter.complete(handleDriverMessage);
}
void listenToFlutterViewEvents() {
channel.invokeMethod('pipeFlutterViewEvents');
viewChannel.invokeMethod('pipeTouchEvents');
new Timer(const Duration(seconds: 3), () {
channel.invokeMethod('stopFlutterViewEvents');
viewChannel.invokeMethod('stopTouchEvents');
});
}
Future<String> handleDriverMessage(String message) async {
switch (message) {
case 'run test':
return playEventsFile();
}
return 'unknown message: "$message"';
}
Future<dynamic> onMethodChannelCall(MethodCall call) {
switch (call.method) {
case 'onTouch':
final Map<dynamic, dynamic> map = call.arguments;
flutterViewEvents.insert(0, map.cast<String, dynamic>());
if (flutterViewEvents.length > kEventsBufferSize)
flutterViewEvents.removeLast();
setState(() {});
break;
}
return new Future<dynamic>.sync(null);
}
Future<dynamic> onViewMethodChannelCall(MethodCall call) {
switch (call.method) {
case 'onTouch':
final Map<dynamic, dynamic> map = call.arguments;
embeddedViewEvents.insert(0, map.cast<String, dynamic>());
if (embeddedViewEvents.length > kEventsBufferSize)
embeddedViewEvents.removeLast();
setState(() {});
break;
}
return new Future<dynamic>.sync(null);
}
Widget buildEventTile(BuildContext context, int index) {
if (embeddedViewEvents.length > index)
return new TouchEventDiff(
flutterViewEvents[index], embeddedViewEvents[index]);
return new Text(
'Unmatched event, action: ${flutterViewEvents[index]['action']}');
}
}
class TouchEventDiff extends StatelessWidget {
const TouchEventDiff(this.originalEvent, this.synthesizedEvent);
final Map<String, dynamic> originalEvent;
final Map<String, dynamic> synthesizedEvent;
@override
Widget build(BuildContext context) {
Color color;
final String diff = diffMotionEvents(originalEvent, synthesizedEvent);
String msg;
final int action = synthesizedEvent['action'];
final String actionName = getActionName(getActionMasked(action), action);
if (diff.isEmpty) {
color = Colors.green;
msg = 'Matched event (action $actionName)';
} else {
color = Colors.red;
msg = '[$actionName] $diff';
}
return new GestureDetector(
onLongPress: () {
print('expected:');
prettyPrintEvent(originalEvent);
print('\nactual:');
prettyPrintEvent(synthesizedEvent);
},
child: new Container(
color: color,
margin: const EdgeInsets.only(bottom: 2.0),
child: new Text(msg),
),
);
}
void prettyPrintEvent(Map<String, dynamic> event) {
final StringBuffer buffer = new StringBuffer();
final int action = event['action'];
final int maskedAction = getActionMasked(action);
final String actionName = getActionName(maskedAction, action);
buffer.write('$actionName ');
if (maskedAction == 5 || maskedAction == 6) {
buffer.write('pointer: ${getPointerIdx(action)} ');
}
final List<Map<dynamic, dynamic>> coords = event['pointerCoords'].cast<Map<dynamic, dynamic>>();
for (int i = 0; i < coords.length; i++) {
buffer.write('p$i x: ${coords[i]['x']} y: ${coords[i]['y']}, pressure: ${coords[i]['pressure']} ');
}
print(buffer.toString());
}
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:collection/collection.dart';
// Android MotionEvent actions for which a pointer index is encoded in the
// unmasked action code.
const List<int> kPointerActions = <int>[
0, // DOWN
1, // UP
5, // POINTER_DOWN
6 // POINTER_UP
];
const double kDoubleErrorMargin = 0.0001;
String diffMotionEvents(
Map<String, dynamic> originalEvent,
Map<String, dynamic> synthesizedEvent,
) {
final StringBuffer diff = new StringBuffer();
diffMaps(originalEvent, synthesizedEvent, diff, excludeKeys: const <String>[
'pointerProperties', // Compared separately.
'pointerCoords', // Compared separately.
'source', // Unused by Flutter.
'deviceId', // Android documentation says that's an arbitrary number that shouldn't be depended on.
'action', // Compared separately.
]);
diffActions(diff, originalEvent, synthesizedEvent);
diffPointerProperties(diff, originalEvent, synthesizedEvent);
diffPointerCoordsList(diff, originalEvent, synthesizedEvent);
return diff.toString();
}
void diffActions(StringBuffer diffBuffer, Map<String, dynamic> originalEvent,
Map<String, dynamic> synthesizedEvent) {
final int synthesizedActionMasked =
getActionMasked(synthesizedEvent['action']);
final int originalActionMasked = getActionMasked(originalEvent['action']);
final String synthesizedActionName =
getActionName(synthesizedActionMasked, synthesizedEvent['action']);
final String originalActionName =
getActionName(originalActionMasked, originalEvent['action']);
if (synthesizedActionMasked != originalActionMasked)
diffBuffer.write(
'action (expected: $originalActionName actual: $synthesizedActionName) ');
if (kPointerActions.contains(originalActionMasked) &&
originalActionMasked == synthesizedActionMasked) {
final int originalPointer = getPointerIdx(originalEvent['action']);
final int synthesizedPointer = getPointerIdx(synthesizedEvent['action']);
if (originalPointer != synthesizedPointer)
diffBuffer.write(
'pointerIdx (expected: $originalPointer actual: $synthesizedPointer action: $originalActionName ');
}
}
void diffPointerProperties(StringBuffer diffBuffer,
Map<String, dynamic> originalEvent, Map<String, dynamic> synthesizedEvent) {
final List<Map<dynamic, dynamic>> expectedList =
originalEvent['pointerProperties'].cast<Map<dynamic, dynamic>>();
final List<Map<dynamic, dynamic>> actualList =
synthesizedEvent['pointerProperties'].cast<Map<dynamic, dynamic>>();
if (expectedList.length != actualList.length) {
diffBuffer.write(
'pointerProperties (actual length: ${actualList.length}, expected length: ${expectedList.length} ');
return;
}
for (int i = 0; i < expectedList.length; i++) {
final Map<String, dynamic> expected =
expectedList[i].cast<String, dynamic>();
final Map<String, dynamic> actual = actualList[i].cast<String, dynamic>();
diffMaps(expected, actual, diffBuffer,
messagePrefix: '[pointerProperty $i] ');
}
}
void diffPointerCoordsList(StringBuffer diffBuffer,
Map<String, dynamic> originalEvent, Map<String, dynamic> synthesizedEvent) {
final List<Map<dynamic, dynamic>> expectedList =
originalEvent['pointerCoords'].cast<Map<dynamic, dynamic>>();
final List<Map<dynamic, dynamic>> actualList =
synthesizedEvent['pointerCoords'].cast<Map<dynamic, dynamic>>();
if (expectedList.length != actualList.length) {
diffBuffer.write(
'pointerCoords (actual length: ${actualList.length}, expected length: ${expectedList.length} ');
return;
}
if (isSinglePointerAction(originalEvent['action'])) {
final int idx = getPointerIdx(originalEvent['action']);
final Map<String, dynamic> expected =
expectedList[idx].cast<String, dynamic>();
final Map<String, dynamic> actual = actualList[idx].cast<String, dynamic>();
diffPointerCoords(expected, actual, idx, diffBuffer);
// For POINTER_UP and POINTER_DOWN events the engine drops the data for all pointers
// but for the pointer that was taken up/down.
// See: https://github.com/flutter/flutter/issues/19882
//
// Until that issue is resolved, we only compare the pointer for which the action
// applies to here.
//
// TODO(amirh): Compare all pointers once the issue mentioned above is resolved.
return;
}
for (int i = 0; i < expectedList.length; i++) {
final Map<String, dynamic> expected =
expectedList[i].cast<String, dynamic>();
final Map<String, dynamic> actual = actualList[i].cast<String, dynamic>();
diffPointerCoords(expected, actual, i, diffBuffer);
}
}
void diffPointerCoords(Map<String, dynamic> expected,
Map<String, dynamic> actual, int pointerIdx, StringBuffer diffBuffer) {
diffMaps(expected, actual, diffBuffer,
messagePrefix: '[pointerCoord $pointerIdx] ',
excludeKeys: <String>[
'size', // Currently the framework doesn't get the size from the engine.
]);
}
void diffMaps(
Map<String, dynamic> expected,
Map<String, dynamic> actual,
StringBuffer diffBuffer, {
List<String> excludeKeys = const <String>[],
String messagePrefix = '',
}) {
const IterableEquality<String> eq = IterableEquality<String>();
if (!eq.equals(expected.keys, actual.keys)) {
diffBuffer.write(
'${messagePrefix}keys (expected: ${expected.keys} actual: ${actual.keys} ');
return;
}
for (String key in expected.keys) {
if (excludeKeys.contains(key))
continue;
if (doublesApproximatelyMatch(expected[key], actual[key]))
continue;
if (expected[key] != actual[key]) {
diffBuffer.write(
'$messagePrefix$key (expected: ${expected[key]} actual: ${actual[key]}) ');
}
}
}
bool isSinglePointerAction(int action) {
final int actionMasked = getActionMasked(action);
return actionMasked == 5 || // POINTER_DOWN
actionMasked == 6; // POINTER_UP
}
int getActionMasked(int action) => action & 0xff;
int getPointerIdx(int action) => (action >> 8) & 0xff;
String getActionName(int actionMasked, int action) {
const List<String> actionNames = <String>[
'DOWN',
'UP',
'MOVE',
'CANCEL',
'OUTSIDE',
'POINTER_DOWN',
'POINTER_UP',
'HOVER_MOVE',
'SCROLL',
'HOVER_ENTER',
'HOVER_EXIT',
'BUTTON_PRESS',
'BUTTON_RELEASE'
];
if (actionMasked < actionNames.length)
return '${actionNames[actionMasked]}($action)';
else
return 'ACTION_$actionMasked';
}
bool doublesApproximatelyMatch(dynamic a, dynamic b) =>
a is double && b is double && (a - b).abs() < kDoubleErrorMargin;
name: android_views
description: An integration test for embedded Android views
version: 1.0.0+1
dependencies:
flutter:
sdk: flutter
flutter_driver:
sdk: flutter
path_provider: ^0.4.1
collection: ^1.14.6
assets_for_android_views:
path: ../../../bin/cache/pkg/goldens/dev/integration_tests/assets_for_android_views
dev_dependencies:
flutter_test:
sdk: flutter
flutter_goldens:
sdk: flutter
flutter:
uses-material-design: true
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter_driver/flutter_driver.dart';
import 'package:flutter_goldens_client/client.dart';
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
Future<void> main() async {
setUpAll(() async {
print('Cloning goldens repository...');
final GoldensClient goldensClient = new GoldensClient();
await goldensClient.prepare();
});
test('MotionEvents recomposition', () async {
final FlutterDriver driver = await FlutterDriver.connect();
final String errorMessage = await driver.requestData('run test');
expect(errorMessage, '');
driver?.close();
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment