Skip to content

Commit 12fdf44

Browse files
collinjacksonEgor
authored andcommitted
Make FlutterRunner launch deterministic when running other tests. (flutter#2584)
* Make FlutterRunner launch deterministic when running other tests. * Switch to using FlutterTestRunner
1 parent 1bde91c commit 12fdf44

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

packages/e2e/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.3.0
2+
3+
* Updates documentation to instruct developers not to launch the activity since
4+
we are doing it for them.
5+
* Renames `FlutterRunner` to `FlutterTestRunner` to avoid conflict with Fuchsia.
6+
17
## 0.2.4+4
28

39
* Fixed a hang that occurred on platforms that don't have a `MethodChannel` listener registered..

packages/e2e/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ import dev.flutter.plugins.e2e.FlutterRunner;
9393
import org.junit.Rule;
9494
import org.junit.runner.RunWith;
9595

96-
@RunWith(FlutterRunner.class)
96+
@RunWith(FlutterTestRunner.class)
9797
public class MainActivityTest {
9898
@Rule
99-
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
99+
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, true, false);
100100
}
101101
```
102102

packages/e2e/android/src/main/java/dev/flutter/plugins/e2e/FlutterRunner.java renamed to packages/e2e/android/src/main/java/dev/flutter/plugins/e2e/FlutterTestRunner.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package dev.flutter.plugins.e2e;
66

77
import android.app.Activity;
8+
import android.util.Log;
89
import androidx.test.rule.ActivityTestRule;
910
import java.lang.reflect.Field;
1011
import java.util.Map;
@@ -15,11 +16,13 @@
1516
import org.junit.runner.notification.Failure;
1617
import org.junit.runner.notification.RunNotifier;
1718

18-
public class FlutterRunner extends Runner {
19+
public class FlutterTestRunner extends Runner {
20+
private static final String TAG = "FlutterTestRunner";
1921

2022
final Class testClass;
23+
ActivityTestRule<Activity> rule = null;
2124

22-
public FlutterRunner(Class<?> testClass) {
25+
public FlutterTestRunner(Class<?> testClass) {
2326
super();
2427
this.testClass = testClass;
2528

@@ -29,8 +32,7 @@ public FlutterRunner(Class<?> testClass) {
2932
if (field.isAnnotationPresent(Rule.class)) {
3033
try {
3134
Object instance = testClass.newInstance();
32-
ActivityTestRule<Activity> rule = (ActivityTestRule<Activity>) field.get(instance);
33-
rule.launchActivity(null);
35+
rule = (ActivityTestRule<Activity>) field.get(instance);
3436
} catch (InstantiationException | IllegalAccessException e) {
3537
// This might occur if the developer did not make the rule public.
3638
// We could call field.setAccessible(true) but it seems better to throw.
@@ -47,6 +49,17 @@ public Description getDescription() {
4749

4850
@Override
4951
public void run(RunNotifier notifier) {
52+
if (rule == null) {
53+
throw new RuntimeException("Unable to run tests due to missing activity rule");
54+
}
55+
try {
56+
rule.launchActivity(null);
57+
} catch (RuntimeException e) {
58+
Log.v(TAG, "launchActivity failed, possibly because the activity was already running. " + e);
59+
Log.v(
60+
TAG,
61+
"Try disabling auto-launch of the activity, e.g. ActivityTestRule<>(MainActivity.class, true, false);");
62+
}
5063
Map<String, String> results = null;
5164
try {
5265
results = E2EPlugin.testResults.get();
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.example.e2e_example;
22

33
import androidx.test.rule.ActivityTestRule;
4-
import dev.flutter.plugins.e2e.FlutterRunner;
4+
import dev.flutter.plugins.e2e.FlutterTestRunner;
55
import org.junit.Rule;
66
import org.junit.runner.RunWith;
77

8-
@RunWith(FlutterRunner.class)
8+
@RunWith(FlutterTestRunner.class)
99
public class EmbedderV1ActivityTest {
1010
@Rule
1111
public ActivityTestRule<EmbedderV1Activity> rule =
12-
new ActivityTestRule<>(EmbedderV1Activity.class);
12+
new ActivityTestRule<>(EmbedderV1Activity.class, true, false);
1313
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.example.e2e_example;
22

33
import androidx.test.rule.ActivityTestRule;
4-
import dev.flutter.plugins.e2e.FlutterRunner;
4+
import dev.flutter.plugins.e2e.FlutterTestRunner;
55
import org.junit.Rule;
66
import org.junit.runner.RunWith;
77

8-
@RunWith(FlutterRunner.class)
8+
@RunWith(FlutterTestRunner.class)
99
public class MainActivityTest {
10-
@Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
10+
@Rule
11+
public ActivityTestRule<MainActivity> rule =
12+
new ActivityTestRule<>(MainActivity.class, true, false);
1113
}

packages/e2e/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: e2e
22
description: Runs tests that use the flutter_test API as integration tests.
3-
version: 0.2.4+4
3+
version: 0.3.0
44
homepage: https://github.com/flutter/plugins/tree/master/packages/e2e
55

66
environment:

0 commit comments

Comments
 (0)