Issue
I'm using Espresso to do UI test for my Android app.
Following the official guide, I used ActivityTestRule to launch the Activity under test. But when test finishes, a crash occurred.
stack trace is
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6247)
at android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:5222)
at android.view.View.playSoundEffect(View.java:17876)
at android.view.View.performClick(View.java:4755)
at com.example.android.activityinstrumentation.ReadExternalFileActivity.finish(ReadExternalFileActivity.java:196)
at android.support.test.rule.ActivityTestRule.finishActivity(ActivityTestRule.java:234)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:259)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Throw the call stack, I found AndroidJunitRunner tried to call finish() of the Activity from non-UI thread, which caused the crash.
My app Overrides method finish() of the Activity, popping up a dialog to ask user confirm exit or not, which should be executed on UI thread.
Does it mean, I can not do UI work in finish() method of Activity when using Espresso? Or is this a bug of Espresso?
Solution
Do not do UI work in finish().
finish() is NOT for this purpose. finish() is what happens after the user says yes to that dialog.
The Activity should be gone from the screen by the time it reaches it. I'm actually a little surprised it took a test-case to see that fail.
Answered By - JoeHz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.