Skip to content

Commit

Permalink
[e2e] Replaces the check for ActivityTestRule (flutter#2633)
Browse files Browse the repository at this point in the history
Android: Replaces the check for ActivityTestRule and adds a second test for GrantPermissionRule enabled tests
  • Loading branch information
ened authored and jorgefspereira committed Oct 10, 2020
1 parent 9f7fbfb commit f53a247
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/e2e/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.1

* Adds support for Android E2E tests that utilize other @Rule's, like GrantPermissionRule.

## 0.4.0

* **Breaking change** Driver request_data call's response has changed to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

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;
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;
ActivityTestRule<Activity> rule = null;
TestRule rule = null;

public FlutterTestRunner(Class<?> testClass) {
super();
Expand All @@ -32,7 +33,10 @@ public FlutterTestRunner(Class<?> testClass) {
if (field.isAnnotationPresent(Rule.class)) {
try {
Object instance = testClass.newInstance();
rule = (ActivityTestRule<Activity>) field.get(instance);
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.
Expand All @@ -53,7 +57,9 @@ public void run(RunNotifier notifier) {
throw new RuntimeException("Unable to run tests due to missing activity rule");
}
try {
rule.launchActivity(null);
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.e2e_example;

import android.Manifest.permission;
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;
import dev.flutter.plugins.e2e.FlutterTestRunner;
import org.junit.Rule;
import org.junit.runner.RunWith;

/**
* Demonstrates how a E2E test on Android can be run with permissions already granted. This is
* helpful if developers want to test native App behavior that depends on certain system service
* results which are guarded with permissions.
*/
@RunWith(FlutterTestRunner.class)
public class MainActivityWithPermissionTest {

@Rule
public GrantPermissionRule permissionRule =
GrantPermissionRule.grant(permission.ACCESS_COARSE_LOCATION);

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

environment:
Expand Down

0 comments on commit f53a247

Please sign in to comment.