Integration test for channel communication (#9621)

This commit is contained in:
Mikkel Nygaard Ravn
2017-04-27 08:44:28 +02:00
committed by GitHub
parent 0770c3c14f
commit 70ff50f929
69 changed files with 2420 additions and 186 deletions

View File

@@ -0,0 +1,12 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
/gradle
/gradlew
/gradlew.bat

View File

@@ -0,0 +1,46 @@
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withInputStream { stream ->
localProperties.load(stream)
}
}
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.")
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
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 {
androidTestCompile 'com.android.support:support-annotations:25.0.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
}

View File

@@ -0,0 +1,32 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourcompany.channels"
android:versionCode="1"
android:versionName="0.0.1">
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application android:name="io.flutter.app.FlutterApplication" android:label="channels" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,142 @@
// Copyright 2017 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 com.yourcompany.channels;
import java.nio.ByteBuffer;
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.*;
import io.flutter.plugins.PluginRegistry;
public class MainActivity extends FlutterActivity {
PluginRegistry pluginRegistry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pluginRegistry = new PluginRegistry();
pluginRegistry.registerAll(this);
setupMessageHandshake(new BasicMessageChannel<>(getFlutterView(), "binary-msg", BinaryCodec.INSTANCE));
setupMessageHandshake(new BasicMessageChannel<>(getFlutterView(), "string-msg", StringCodec.INSTANCE));
setupMessageHandshake(new BasicMessageChannel<>(getFlutterView(), "json-msg", JSONMessageCodec.INSTANCE));
setupMessageHandshake(new BasicMessageChannel<>(getFlutterView(), "std-msg", StandardMessageCodec.INSTANCE));
setupMethodHandshake(new MethodChannel(getFlutterView(), "json-method", JSONMethodCodec.INSTANCE));
setupMethodHandshake(new MethodChannel(getFlutterView(), "std-method", StandardMethodCodec.INSTANCE));
}
private <T> void setupMessageHandshake(final BasicMessageChannel<T> channel) {
// On message receipt, do a send/reply/send round-trip in the other direction,
// then reply to the first message.
channel.setMessageHandler(new BasicMessageChannel.MessageHandler<T>() {
@Override
public void onMessage(final T message, final BasicMessageChannel.Reply<T> reply) {
final T messageEcho = echo(message);
channel.send(messageEcho, new BasicMessageChannel.Reply<T>() {
@Override
public void reply(T replyMessage) {
channel.send(echo(replyMessage));
reply.reply(messageEcho);
}
});
}
});
}
// Outgoing ByteBuffer messages must be direct-allocated and payload placed between
// positon 0 and current position.
@SuppressWarnings("unchecked")
private <T> T echo(T message) {
if (message instanceof ByteBuffer) {
final ByteBuffer buffer = (ByteBuffer) message;
final ByteBuffer echo = ByteBuffer.allocateDirect(buffer.remaining());
echo.put(buffer);
return (T) echo;
}
return message;
}
private void setupMethodHandshake(final MethodChannel channel) {
channel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(final MethodCall methodCall, final MethodChannel.Result result) {
switch (methodCall.method) {
case "success":
doSuccessHandshake(channel, methodCall, result);
break;
case "error":
doErrorHandshake(channel, methodCall, result);
break;
default:
doNotImplementedHandshake(channel, methodCall, result);
break;
}
}
});
}
private void doSuccessHandshake(final MethodChannel channel, final MethodCall methodCall, final MethodChannel.Result result) {
channel.invokeMethod(methodCall.method, methodCall.arguments, new MethodChannel.Result() {
@Override
public void success(Object o) {
channel.invokeMethod(methodCall.method, o);
result.success(methodCall.arguments);
}
@Override
public void error(String code, String message, Object details) {
throw new AssertionError("Should not be called");
}
@Override
public void notImplemented() {
throw new AssertionError("Should not be called");
}
});
}
private void doErrorHandshake(final MethodChannel channel, final MethodCall methodCall, final MethodChannel.Result result) {
channel.invokeMethod(methodCall.method, methodCall.arguments, new MethodChannel.Result() {
@Override
public void success(Object o) {
throw new AssertionError("Should not be called");
}
@Override
public void error(String code, String message, Object details) {
channel.invokeMethod(methodCall.method, details);
result.error(code, message, methodCall.arguments);
}
@Override
public void notImplemented() {
throw new AssertionError("Should not be called");
}
});
}
private void doNotImplementedHandshake(final MethodChannel channel, final MethodCall methodCall, final MethodChannel.Result result) {
channel.invokeMethod(methodCall.method, methodCall.arguments, new MethodChannel.Result() {
@Override
public void success(Object o) {
throw new AssertionError("Should not be called");
}
@Override
public void error(String code, String message, Object details) {
throw new AssertionError("Should not be called");
}
@Override
public void notImplemented() {
channel.invokeMethod(methodCall.method, null);
result.notImplemented();
}
});
}
}

View File

@@ -0,0 +1,14 @@
package io.flutter.plugins;
import io.flutter.app.FlutterActivity;
/**
* Generated file. Do not edit.
*/
public class PluginRegistry {
public void registerAll(FlutterActivity activity) {
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 544 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 721 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@@ -0,0 +1,29 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
task wrapper(type: Wrapper) {
gradleVersion = '2.14.1'
}

View File

@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx1536M

View File

@@ -0,0 +1,15 @@
include ':app'
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withInputStream { stream -> plugins.load(stream) }
}
plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
include ":$name"
project(":$name").projectDir = pluginDirectory
}