Skip to content

Commit 71a9d55

Browse files
enedEgor
authored andcommitted
[e2e] Replaces the check for ActivityTestRule (flutter#2633)
Android: Replaces the check for ActivityTestRule and adds a second test for GrantPermissionRule enabled tests
1 parent 363cdf9 commit 71a9d55

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

packages/e2e/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.1
2+
3+
* Adds support for Android E2E tests that utilize other @Rule's, like GrantPermissionRule.
4+
15
## 0.4.0
26

37
* **Breaking change** Driver request_data call's response has changed to

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44

55
package dev.flutter.plugins.e2e;
66

7-
import android.app.Activity;
87
import android.util.Log;
98
import androidx.test.rule.ActivityTestRule;
109
import java.lang.reflect.Field;
1110
import java.util.Map;
1211
import java.util.concurrent.ExecutionException;
1312
import org.junit.Rule;
13+
import org.junit.rules.TestRule;
1414
import org.junit.runner.Description;
1515
import org.junit.runner.Runner;
1616
import org.junit.runner.notification.Failure;
1717
import org.junit.runner.notification.RunNotifier;
1818

1919
public class FlutterTestRunner extends Runner {
20+
2021
private static final String TAG = "FlutterTestRunner";
2122

2223
final Class testClass;
23-
ActivityTestRule<Activity> rule = null;
24+
TestRule rule = null;
2425

2526
public FlutterTestRunner(Class<?> testClass) {
2627
super();
@@ -32,7 +33,10 @@ public FlutterTestRunner(Class<?> testClass) {
3233
if (field.isAnnotationPresent(Rule.class)) {
3334
try {
3435
Object instance = testClass.newInstance();
35-
rule = (ActivityTestRule<Activity>) field.get(instance);
36+
if (field.get(instance) instanceof ActivityTestRule) {
37+
rule = (TestRule) field.get(instance);
38+
break;
39+
}
3640
} catch (InstantiationException | IllegalAccessException e) {
3741
// This might occur if the developer did not make the rule public.
3842
// We could call field.setAccessible(true) but it seems better to throw.
@@ -53,7 +57,9 @@ public void run(RunNotifier notifier) {
5357
throw new RuntimeException("Unable to run tests due to missing activity rule");
5458
}
5559
try {
56-
rule.launchActivity(null);
60+
if (rule instanceof ActivityTestRule) {
61+
((ActivityTestRule) rule).launchActivity(null);
62+
}
5763
} catch (RuntimeException e) {
5864
Log.v(TAG, "launchActivity failed, possibly because the activity was already running. " + e);
5965
Log.v(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.e2e_example;
2+
3+
import android.Manifest.permission;
4+
import androidx.test.rule.ActivityTestRule;
5+
import androidx.test.rule.GrantPermissionRule;
6+
import dev.flutter.plugins.e2e.FlutterTestRunner;
7+
import org.junit.Rule;
8+
import org.junit.runner.RunWith;
9+
10+
/**
11+
* Demonstrates how a E2E test on Android can be run with permissions already granted. This is
12+
* helpful if developers want to test native App behavior that depends on certain system service
13+
* results which are guarded with permissions.
14+
*/
15+
@RunWith(FlutterTestRunner.class)
16+
public class MainActivityWithPermissionTest {
17+
18+
@Rule
19+
public GrantPermissionRule permissionRule =
20+
GrantPermissionRule.grant(permission.ACCESS_COARSE_LOCATION);
21+
22+
@Rule
23+
public ActivityTestRule<MainActivity> rule =
24+
new ActivityTestRule<>(MainActivity.class, true, false);
25+
}

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.4.0
3+
version: 0.4.1
44
homepage: https://github.com/flutter/plugins/tree/master/packages/e2e
55

66
environment:

0 commit comments

Comments
 (0)