Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Make FlutterRunner launch deterministic when running other tests. (#2584
Browse files Browse the repository at this point in the history
)

* Make FlutterRunner launch deterministic when running other tests.
* Switch to using FlutterTestRunner
  • Loading branch information
collinjackson committed Mar 6, 2020
1 parent 1dbca58 commit 1d7e12e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/e2e/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.3.0

* Updates documentation to instruct developers not to launch the activity since
we are doing it for them.
* Renames `FlutterRunner` to `FlutterTestRunner` to avoid conflict with Fuchsia.

## 0.2.4+4

* Fixed a hang that occurred on platforms that don't have a `MethodChannel` listener registered..
Expand Down
4 changes: 2 additions & 2 deletions packages/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ import dev.flutter.plugins.e2e.FlutterRunner;
import org.junit.Rule;
import org.junit.runner.RunWith;

@RunWith(FlutterRunner.class)
@RunWith(FlutterTestRunner.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, true, false);
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package dev.flutter.plugins.e2e;

import android.app.Activity;
import android.util.Log;
import androidx.test.rule.ActivityTestRule;
import java.lang.reflect.Field;
import java.util.Map;
Expand All @@ -15,11 +16,13 @@
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

public class FlutterRunner extends Runner {
public class FlutterTestRunner extends Runner {
private static final String TAG = "FlutterTestRunner";

final Class testClass;
ActivityTestRule<Activity> rule = null;

public FlutterRunner(Class<?> testClass) {
public FlutterTestRunner(Class<?> testClass) {
super();
this.testClass = testClass;

Expand All @@ -29,8 +32,7 @@ public FlutterRunner(Class<?> testClass) {
if (field.isAnnotationPresent(Rule.class)) {
try {
Object instance = testClass.newInstance();
ActivityTestRule<Activity> rule = (ActivityTestRule<Activity>) field.get(instance);
rule.launchActivity(null);
rule = (ActivityTestRule<Activity>) field.get(instance);
} 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.
Expand All @@ -47,6 +49,17 @@ public Description getDescription() {

@Override
public void run(RunNotifier notifier) {
if (rule == null) {
throw new RuntimeException("Unable to run tests due to missing activity rule");
}
try {
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 = E2EPlugin.testResults.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.e2e_example;

import androidx.test.rule.ActivityTestRule;
import dev.flutter.plugins.e2e.FlutterRunner;
import dev.flutter.plugins.e2e.FlutterTestRunner;
import org.junit.Rule;
import org.junit.runner.RunWith;

@RunWith(FlutterRunner.class)
@RunWith(FlutterTestRunner.class)
public class EmbedderV1ActivityTest {
@Rule
public ActivityTestRule<EmbedderV1Activity> rule =
new ActivityTestRule<>(EmbedderV1Activity.class);
new ActivityTestRule<>(EmbedderV1Activity.class, true, false);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.e2e_example;

import androidx.test.rule.ActivityTestRule;
import dev.flutter.plugins.e2e.FlutterRunner;
import dev.flutter.plugins.e2e.FlutterTestRunner;
import org.junit.Rule;
import org.junit.runner.RunWith;

@RunWith(FlutterRunner.class)
@RunWith(FlutterTestRunner.class)
public class MainActivityTest {
@Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
@Rule
public ActivityTestRule<MainActivity> rule =
new ActivityTestRule<>(MainActivity.class, true, false);
}
2 changes: 1 addition & 1 deletion packages/e2e/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: e2e
description: Runs tests that use the flutter_test API as integration tests.
version: 0.2.4+4
version: 0.3.0
homepage: https://github.com/flutter/plugins/tree/master/packages/e2e

environment:
Expand Down

0 comments on commit 1d7e12e

Please sign in to comment.