diff --git a/enro-test/src/main/java/dev/enro/test/EnroTest.kt b/enro-test/src/main/java/dev/enro/test/EnroTest.kt index 632e751e..7d82c0c8 100644 --- a/enro-test/src/main/java/dev/enro/test/EnroTest.kt +++ b/enro-test/src/main/java/dev/enro/test/EnroTest.kt @@ -1,9 +1,9 @@ @file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") + package dev.enro.test import android.app.Application import androidx.test.core.app.ApplicationProvider -import androidx.test.platform.app.InstrumentationRegistry import dev.enro.core.controller.NavigationApplication import dev.enro.core.controller.NavigationController import dev.enro.core.controller.createUnattachedNavigationController @@ -13,26 +13,29 @@ object EnroTest { private var navigationController: NavigationController? = null + private val application: Application? + get() { + runCatching { + return ApplicationProvider.getApplicationContext() + } + return null + } + fun installNavigationController() { if (navigationController != null) { uninstallNavigationController() } - if (isInstrumented()) { - val application = ApplicationProvider.getApplicationContext() - if (application is NavigationApplication) { - navigationController = application.navigationController.apply { - isInTest = true - } - return + navigationController = when (val application = application) { + is NavigationApplication -> application.navigationController + else -> createUnattachedNavigationController() + }.apply { + isInTest = true + when (val application = application) { + is NavigationApplication -> return@apply + null -> installForJvmTests() + else -> install(application) } - navigationController?.apply { install(application) } - } else { - navigationController = createUnattachedNavigationController() - .apply { - isInTest = true - installForJvmTests() - } } } @@ -41,6 +44,12 @@ object EnroTest { navigationController?.apply { isInTest = false } + + navigationController?.apply { + isInTest = false + if (application is NavigationApplication) return@apply + uninstall(application ?: return@apply) + } navigationController = null } @@ -55,13 +64,5 @@ object EnroTest { fun enableAnimations(controller: NavigationController) { controller.isAnimationsDisabled = false } - - private fun isInstrumented(): Boolean { - runCatching { - InstrumentationRegistry.getInstrumentation() - return true - } - return false - } }