android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.4k stars 8.29k forks source link

[GithubBrowserSample] ActivityTestRule is deprecated in AutoClearedValueTest #972

Open CoenQian opened 3 years ago

CoenQian commented 3 years ago

image

As it mentioned above, ActivityTestRule is deprecated.

Would you please provide a new AutoClearedValueTest by ActivityScenarioRule instead of ActivityTestRule?

I had tried codes like these, but failed.

@get:Rule
val activityScenarioRule = activityScenarioRule<SingleFragmentActivity>()

 @Before
fun setUp() {
    testFragment = TestFragment()
    activityScenarioRule.scenario.onActivity {
        it.setFragment(testFragment)
    }
    InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}

stacktrace error:

java.lang.IllegalStateException: Method addObserver must be called on the main thread
    at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317)
    at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172)