-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Closed
flutter/plugins
#2633Labels
f: integration_testThe flutter/packages/integration_test pluginThe flutter/packages/integration_test pluginpackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyAndroid applications specifically
Description
Android instrumentation tests can automatically grant permissions to the activity/service under test. This is done using https://developer.android.com/reference/androidx/test/rule/GrantPermissionRule.
The process currently fails due to the FlutterTestRunner implementation.
Steps to Reproduce
Write a e2e enabled AndroidTest like this:
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;
import dev.flutter.plugins.e2e.FlutterTestRunner;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
@RunWith(FlutterTestRunner.class)
public class MainActivityTest {
@Rule
public TestRule mRuntimePermissionRule = GrantPermissionRule
.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
@Rule
public ActivityTestRule<io.flutter.embedding.android.FlutterActivity> rule = new ActivityTestRule<>(
io.flutter.embedding.android.FlutterActivity.class);
}Expected results:
The application should run with the permissions applied.
Actual results:
The test will fail with this error message:
java.lang.ClassCastException: androidx.test.rule.GrantPermissionRule cannot be cast to androidx.test.rule.ActivityTestRule
at dev.flutter.plugins.e2e.FlutterTestRunner.<init>(FlutterTestRunner.java:35)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at androidx.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:63)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at androidx.test.internal.runner.AndroidRunnerBuilder.runnerForClass(AndroidRunnerBuilder.java:153)
at androidx.test.internal.runner.TestLoader$ScanningRunnerBuilder.runnerForClass(TestLoader.java:143)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at androidx.test.internal.runner.TestLoader.doCreateRunner(TestLoader.java:73)
at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:104)
at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:793)
at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:547)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:390)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2196)
This is observed using e2e 0.4.0.
The fix should be fairly straightforward, FlutterTestRunner needs to be modified here:
// 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();
rule = (ActivityTestRule<Activity>) field.get(instance);
} 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);
}
}
}
Metadata
Metadata
Assignees
Labels
f: integration_testThe flutter/packages/integration_test pluginThe flutter/packages/integration_test pluginpackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyAndroid applications specifically