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 1 commit
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
Next Next commit
Switch to using @rule
  • Loading branch information
collinjackson committed Aug 27, 2019
commit 837a87145c11d48186f0e16165125cb75c82e8f2
13 changes: 5 additions & 8 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);
}
}```
@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.