diff --git a/detox/android/detox/src/full/java/com/wix/detox/reactnative/ReactNativeExtension.kt b/detox/android/detox/src/full/java/com/wix/detox/reactnative/ReactNativeExtension.kt index 7b7281a365..f3fbf37358 100644 --- a/detox/android/detox/src/full/java/com/wix/detox/reactnative/ReactNativeExtension.kt +++ b/detox/android/detox/src/full/java/com/wix/detox/reactnative/ReactNativeExtension.kt @@ -57,7 +57,6 @@ object ReactNativeExtension { Log.i(LOG_TAG, "Reloading React Native") (applicationContext as ReactApplication).let { - val networkSyncEnabled = rnIdlingResources?.networkSyncEnabled ?: true clearIdlingResources() val previousReactContext = getCurrentReactContextSafe(it) @@ -65,7 +64,7 @@ object ReactNativeExtension { reloadReactNativeInBackground(it) val reactContext = awaitNewReactNativeContext(it, previousReactContext) - enableOrDisableSynchronization(reactContext, networkSyncEnabled) + enableOrDisableSynchronization(reactContext) } } @@ -94,11 +93,6 @@ object ReactNativeExtension { return null } - @JvmStatic - fun setNetworkSynchronization(enable: Boolean) { - rnIdlingResources?.setNetworkSynchronization(enable) - } - @JvmStatic fun toggleNetworkSynchronization(enable: Boolean) { rnIdlingResources?.let { @@ -130,11 +124,11 @@ object ReactNativeExtension { return rnLoadingMonitor.getNewContext()!! } - private fun enableOrDisableSynchronization(reactContext: ReactContext, networkSyncEnabled: Boolean = true) { + private fun enableOrDisableSynchronization(reactContext: ReactContext) { if (shouldDisableSynchronization()) { clearAllSynchronization() } else { - setupIdlingResources(reactContext, networkSyncEnabled) + setupIdlingResources(reactContext) } } @@ -143,10 +137,10 @@ object ReactNativeExtension { return launchArgs.hasEnableSynchronization() && launchArgs.enableSynchronization.equals("0") } - private fun setupIdlingResources(reactContext: ReactContext, networkSyncEnabled: Boolean = true) { + private fun setupIdlingResources(reactContext: ReactContext) { val launchArgs = LaunchArgs() - rnIdlingResources = ReactNativeIdlingResources(reactContext, launchArgs, networkSyncEnabled).apply { + rnIdlingResources = ReactNativeIdlingResources(reactContext, launchArgs).apply { registerAll() } } diff --git a/detox/android/detox/src/full/java/com/wix/detox/reactnative/idlingresources/ReactNativeIdlingResources.kt b/detox/android/detox/src/full/java/com/wix/detox/reactnative/idlingresources/ReactNativeIdlingResources.kt index cfd3e63a2b..83713de1b0 100644 --- a/detox/android/detox/src/full/java/com/wix/detox/reactnative/idlingresources/ReactNativeIdlingResources.kt +++ b/detox/android/detox/src/full/java/com/wix/detox/reactnative/idlingresources/ReactNativeIdlingResources.kt @@ -19,7 +19,6 @@ import org.joor.Reflect class ReactNativeIdlingResources( private val reactContext: ReactContext, private var launchArgs: LaunchArgs, - internal var networkSyncEnabled: Boolean = true, private val idlingResourcesFactory: DetoxIdlingResourceFactory = DetoxIdlingResourceFactory(reactContext) ) { companion object { @@ -47,26 +46,9 @@ class ReactNativeIdlingResources( unregisterIdlingResources() } - fun setNetworkSynchronization(enable: Boolean) { - runBlocking { - if (networkSyncEnabled == enable) { - return@runBlocking - } - - if (enable) { - setupIdlingResource(IdlingResourcesName.Network) - } else { - removeIdlingResource(IdlingResourcesName.Network) - } - networkSyncEnabled = enable - } - } - fun pauseNetworkSynchronization() = pauseIdlingResource(IdlingResourcesName.Network) fun resumeNetworkSynchronization() { - if (networkSyncEnabled) { - resumeIdlingResource(IdlingResourcesName.Network) - } + resumeIdlingResource(IdlingResourcesName.Network) } fun pauseRNTimersIdlingResource() = pauseIdlingResource(IdlingResourcesName.Timers) @@ -109,14 +91,10 @@ class ReactNativeIdlingResources( } private suspend fun setupIdlingResources() { - setupIdlingResource(IdlingResourcesName.RNBridge) - setupIdlingResource(IdlingResourcesName.Timers) - setupIdlingResource(IdlingResourcesName.UIModule) - setupIdlingResource(IdlingResourcesName.Animations) - if (networkSyncEnabled) { - setupIdlingResource(IdlingResourcesName.Network) + idlingResources.putAll(idlingResourcesFactory.create()) + idlingResources.forEach { (_, idlingResource) -> + IdlingRegistry.getInstance().register(idlingResource) } - setupIdlingResource(IdlingResourcesName.AsyncStorage) } private fun syncIdlingResources() { @@ -150,15 +128,6 @@ class ReactNativeIdlingResources( idlingResource?.resume() } - private suspend fun setupIdlingResource(idlingResourcesName: IdlingResourcesName) { - val idlingResource = idlingResourcesFactory.create(idlingResourcesName) - - idlingResource?.let { - IdlingRegistry.getInstance().register(it) - idlingResources[idlingResourcesName] = it - } - } - private fun removeIdlingResource(idlingResourcesName: IdlingResourcesName) { val idlingResource = idlingResources[idlingResourcesName] idlingResource?.let { diff --git a/detox/android/detox/src/full/java/com/wix/detox/reactnative/idlingresources/factory/DetoxIdlingResourceFactory.kt b/detox/android/detox/src/full/java/com/wix/detox/reactnative/idlingresources/factory/DetoxIdlingResourceFactory.kt index 438484dc9d..3fa08e2f13 100644 --- a/detox/android/detox/src/full/java/com/wix/detox/reactnative/idlingresources/factory/DetoxIdlingResourceFactory.kt +++ b/detox/android/detox/src/full/java/com/wix/detox/reactnative/idlingresources/factory/DetoxIdlingResourceFactory.kt @@ -12,14 +12,21 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext class DetoxIdlingResourceFactory(private val reactContext: ReactContext) { - suspend fun create(name: IdlingResourcesName): DetoxIdlingResource? = withContext(Dispatchers.Main) { - return@withContext when (name) { - IdlingResourcesName.Timers -> TimersIdlingResource(reactContext) - IdlingResourcesName.AsyncStorage -> AsyncStorageIdlingResource.createIfNeeded(reactContext) - IdlingResourcesName.RNBridge -> BridgeIdlingResource(reactContext) - IdlingResourcesName.UIModule -> UIModuleIdlingResource(reactContext) - IdlingResourcesName.Animations -> AnimatedModuleIdlingResource(reactContext) - IdlingResourcesName.Network -> NetworkIdlingResource(reactContext) + suspend fun create(): Map = withContext(Dispatchers.Main) { + val result = mutableMapOf( + IdlingResourcesName.Timers to TimersIdlingResource(reactContext), + IdlingResourcesName.RNBridge to BridgeIdlingResource(reactContext), + IdlingResourcesName.UIModule to UIModuleIdlingResource(reactContext), + IdlingResourcesName.Animations to AnimatedModuleIdlingResource(reactContext), + IdlingResourcesName.Network to NetworkIdlingResource(reactContext) + ) + + val asyncStorageIdlingResource = AsyncStorageIdlingResource.createIfNeeded(reactContext) + if (asyncStorageIdlingResource != null) { + result[IdlingResourcesName.AsyncStorage] = asyncStorageIdlingResource } + + return@withContext result } } +