Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/instrumentation_adapter

Expand Down