android-test icon indicating copy to clipboard operation
android-test copied to clipboard

Throw `onActivity` exception from the test thread

Open copybara-service[bot] opened this issue 2 years ago • 0 comments

Throw onActivity exception from the test thread

Previously if the ActivityAction or the assertions (e.g. checking whether the activity is destroyed or not) threw then the application would die unnaturally due to the main looper raising an uncaught exception, this would not provide the line that caused the exception in the case of a null activity, e.g.:

java.lang.NullPointerException: Cannot run onActivity since Activity has been destroyed already
	at androidx.test.internal.util.Checks.checkNotNull(Checks.java:50)
	at androidx.test.core.app.ActivityScenario.lambda$onActivity$2$androidx-test-core-app-ActivityScenario(ActivityScenario.java:787)
	at androidx.test.core.app.ActivityScenario$$ExternalSyntheticLambda2.run(Unknown Source:4)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:463)
	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:2379)
        ...

If the runnable has been posted (i.e. onActivity was called not on the main thread) catch the exception and throw it wrapped in a RuntimeException from the thread that called onActivity to allow the full stack trace to be captured from the callsite, it should also generally allow the test to finish naturally and teardown.

copybara-service[bot] avatar May 04 '23 00:05 copybara-service[bot]