Skip to content

Commit

Permalink
Update EnroTest to correctly uninstall navigation controllers for Jvm…
Browse files Browse the repository at this point in the history
… based AndroidTest test
  • Loading branch information
isaac-udy committed Oct 16, 2023
1 parent 5c17754 commit 9abf684
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions enro-test/src/main/java/dev/enro/test/EnroTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Application>()
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()
}
}
}

Expand All @@ -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
}

Expand All @@ -55,13 +64,5 @@ object EnroTest {
fun enableAnimations(controller: NavigationController) {
controller.isAnimationsDisabled = false
}

private fun isInstrumented(): Boolean {
runCatching {
InstrumentationRegistry.getInstrumentation()
return true
}
return false
}
}

0 comments on commit 9abf684

Please sign in to comment.