[go: nahoru, domu]

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

Commit

Permalink
[instrumentation_adapter] update boilerplate to use @rule instead of …
Browse files Browse the repository at this point in the history
…FlutterTest (#2023)

* Updates the boilerplate test to use `@Rule` instead of `FlutterTest`, bringing it more in line with how native Android tests work.
  • Loading branch information
collinjackson committed Aug 28, 2019
1 parent c5ade10 commit d7ad50b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
4 changes: 4 additions & 0 deletions packages/instrumentation_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.0

* Update boilerplate test to use `@Rule` instead of `FlutterTest`.

## 0.0.2

* Document current usage instructions, which require adding a Java test file.
Expand Down
15 changes: 6 additions & 9 deletions packages/instrumentation_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ package com.example.myapp;
import androidx.test.rule.ActivityTestRule;
import dev.flutter.plugins.instrumentationadapter.FlutterRunner;
import dev.flutter.plugins.instrumentationadapter.FlutterTest;
import java.lang.Override;
import org.junit.Rule;
import org.junit.runner.RunWith;
@RunWith(FlutterRunner.class)
public class MainActivityTest extends FlutterTest {
@Override
public void launchActivity() {
ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
rule.launchActivity(null);
}
}```
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
}
```

Use gradle commands to build an instrumentation test for Android.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

package dev.flutter.plugins.instrumentationadapter;

import android.app.Activity;
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.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
Expand All @@ -15,13 +19,24 @@ public class FlutterRunner extends Runner {

final Class testClass;

public FlutterRunner(Class<FlutterTest> testClass) {
public FlutterRunner(Class<?> testClass) {
super();
this.testClass = testClass;
try {
testClass.newInstance().launchActivity();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalThreadStateException("Unable to launch test");

// 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();
ActivityTestRule<Activity> rule = (ActivityTestRule<Activity>) field.get(instance);
rule.launchActivity(null);
} 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);
}
}
}
}

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion packages/instrumentation_adapter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: instrumentation_adapter
description: Runs tests that use the flutter_test API as platform native instrumentation tests.
version: 0.0.2
version: 0.1.0
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/instrumentation_adapter

Expand Down

0 comments on commit d7ad50b

Please sign in to comment.