Move package:integration_test to flutter/flutter (#69622)

This commit is contained in:
Dan Field
2020-11-05 17:28:47 -08:00
committed by GitHub
parent a3f6ea6f38
commit 76784651d4
174 changed files with 6042 additions and 71 deletions

View File

@@ -0,0 +1,7 @@
<!-- Copyright 2014 The Flutter Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.flutter.integration_test">
</manifest>

View File

@@ -0,0 +1,87 @@
// Copyright 2014 The Flutter 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 dev.flutter.plugins.integration_test;
import android.util.Log;
import androidx.test.rule.ActivityTestRule;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
public class FlutterTestRunner extends Runner {
private static final String TAG = "FlutterTestRunner";
final Class testClass;
TestRule rule = null;
public FlutterTestRunner(Class<?> testClass) {
super();
this.testClass = testClass;
// Look for an `ActivityTestRule` annotated `@Rule` and invoke `launchActivity()`
Field[] fields = testClass.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Rule.class)) {
try {
Object instance = testClass.newInstance();
if (field.get(instance) instanceof ActivityTestRule) {
rule = (TestRule) field.get(instance);
break;
}
} catch (InstantiationException | IllegalAccessException e) {
// This might occur if the developer did not make the rule public.
// We could call field.setAccessible(true) but it seems better to throw.
throw new RuntimeException("Unable to access activity rule", e);
}
}
}
}
@Override
public Description getDescription() {
return Description.createTestDescription(testClass, "Flutter Tests");
}
@Override
public void run(RunNotifier notifier) {
if (rule == null) {
throw new RuntimeException("Unable to run tests due to missing activity rule");
}
try {
if (rule instanceof ActivityTestRule) {
((ActivityTestRule) rule).launchActivity(null);
}
} catch (RuntimeException e) {
Log.v(TAG, "launchActivity failed, possibly because the activity was already running. " + e);
Log.v(
TAG,
"Try disabling auto-launch of the activity, e.g. ActivityTestRule<>(MainActivity.class, true, false);");
}
Map<String, String> results = null;
try {
results = IntegrationTestPlugin.testResults.get();
} catch (ExecutionException | InterruptedException e) {
throw new IllegalThreadStateException("Unable to get test results");
}
for (String name : results.keySet()) {
Description d = Description.createTestDescription(testClass, name);
notifier.fireTestStarted(d);
String outcome = results.get(name);
if (!outcome.equals("success")) {
Exception dummyException = new Exception(outcome);
notifier.fireTestFailure(new Failure(d, dummyException));
}
notifier.fireTestFinished(d);
}
}
}

View File

@@ -0,0 +1,61 @@
// Copyright 2014 The Flutter 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 dev.flutter.plugins.integration_test;
import android.content.Context;
import com.google.common.util.concurrent.SettableFuture;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import java.util.Map;
import java.util.concurrent.Future;
/** IntegrationTestPlugin */
public class IntegrationTestPlugin implements MethodCallHandler, FlutterPlugin {
private MethodChannel methodChannel;
private static final SettableFuture<Map<String, String>> testResultsSettable =
SettableFuture.create();
public static final Future<Map<String, String>> testResults = testResultsSettable;
private static final String CHANNEL = "plugins.flutter.io/integration_test";
/** Plugin registration. */
@SuppressWarnings("deprecation")
public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
final IntegrationTestPlugin instance = new IntegrationTestPlugin();
instance.onAttachedToEngine(registrar.context(), registrar.messenger());
}
@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
}
private void onAttachedToEngine(Context unusedApplicationContext, BinaryMessenger messenger) {
methodChannel = new MethodChannel(messenger, CHANNEL);
methodChannel.setMethodCallHandler(this);
}
@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
methodChannel.setMethodCallHandler(null);
methodChannel = null;
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("allTestsFinished")) {
Map<String, String> results = call.argument("results");
testResultsSettable.set(results);
result.success(null);
} else {
result.notImplemented();
}
}
}