@@ -63,78 +63,6 @@ import org.junit.Test
6363import org.junit.rules.Timeout
6464import java.util.concurrent.TimeUnit
6565
66- /* *
67- * IdlingResource that waits for a condition to become true.
68- * Used to synchronize ViewModel state changes with test assertions.
69- */
70- class ViewModelIdlingResource (
71- private val resourceName : String ,
72- private val checkCondition : () -> Boolean
73- ) : IdlingResource {
74- @Volatile
75- private var callback: IdlingResource .ResourceCallback ? = null
76-
77- override fun getName (): String = resourceName
78-
79- override fun isIdleNow (): Boolean {
80- val idle = checkCondition()
81- if (idle && callback != null ) {
82- callback?.onTransitionToIdle()
83- }
84- return idle
85- }
86-
87- override fun registerIdleTransitionCallback (callback : IdlingResource .ResourceCallback ? ) {
88- this .callback = callback
89- }
90- }
91-
92- /* *
93- * Helper function to wait for a condition using IdlingResource.
94- * Automatically registers and unregisters the idling resource.
95- * Uses Espresso's built-in synchronization mechanism instead of manual polling.
96- *
97- * @param resourceName Name for the idling resource (for debugging)
98- * @param condition Lambda that returns true when the condition is met
99- * @param timeoutSeconds Maximum time to wait for the condition (default: 10 seconds)
100- * @param block Code to execute once the condition is met
101- */
102- inline fun <T > waitForCondition (
103- resourceName : String ,
104- noinline condition : () -> Boolean ,
105- timeoutSeconds : Long = 10,
106- block : () -> T
107- ): T {
108- val idlingResource = ViewModelIdlingResource (resourceName, condition)
109-
110- // Set custom timeout for this wait operation
111- val previousTimeout = IdlingPolicies .getMasterIdlingPolicy()
112- IdlingPolicies .setMasterPolicyTimeout(timeoutSeconds, TimeUnit .SECONDS )
113-
114- IdlingRegistry .getInstance().register(idlingResource)
115- try {
116- // Let Espresso handle the synchronization - it will poll the IdlingResource
117- // and wait until isIdleNow() returns true, respecting the timeout policy
118- Espresso .onIdle()
119-
120- return block()
121- } catch (e: Exception ) {
122- // Provide better error message if timeout occurs
123- if (e.message?.contains(" IdlingResource" ) == true ||
124- e.message?.contains(" timeout" ) == true ) {
125- throw AssertionError (" Timeout waiting for condition '$resourceName ' after ${timeoutSeconds} s" , e)
126- }
127- throw e
128- } finally {
129- IdlingRegistry .getInstance().unregister(idlingResource)
130- // Restore previous timeout policy
131- IdlingPolicies .setMasterPolicyTimeout(
132- previousTimeout.idleTimeout,
133- previousTimeout.idleTimeoutUnit
134- )
135- }
136- }
137-
13866class TokenMigrationViewActivityTest {
13967
14068 @get:Rule
0 commit comments