Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Return early from NavigatorProvider#addNavigator if previous & naviga…
…tor are equal

We want to return early from `addNavigator` if the previous and to-be-added navigator are equal. This allows calling `addNavigator` as a side effect of every composition without having to worry about the Navigator being replaced every time.

Test: Added addExistingNavigatorDoesntReplace and addWithSameNameButUnequalNavigatorDoesReplace in NavigatorProviderTest.kt

Fixes: b/187443146
Change-Id: Id09b7e3306c49f261b80c724d36c614671f5a7ac
  • Loading branch information
jossiwolf committed May 7, 2021
commit ba934c563f6e50f20d147be86bba254285c524da
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public open class NavigatorProvider {
): Navigator<out NavDestination>? {
require(validateName(name)) { "navigator name cannot be an empty string" }
val previousNavigator = _navigators[name]
if (previousNavigator == navigator) {
return navigator
}
check(previousNavigator?.isAttached != true) {
"Navigator $navigator is replacing an already attached $previousNavigator"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package androidx.navigation

import android.os.Bundle
import androidx.navigation.testing.TestNavigatorState
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import org.junit.Assert.fail
Expand Down Expand Up @@ -100,6 +101,50 @@ class NavigatorProviderTest {
.isEqualTo(navigator)
}

@Test
fun addExistingNavigatorDoesntReplace() {
val navigatorState = TestNavigatorState()
val provider = NavigatorProvider()
val navigator = EmptyNavigator()

provider.addNavigator(navigator)
assertThat(provider.getNavigator<EmptyNavigator>(EmptyNavigator.NAME))
.isEqualTo(navigator)

navigator.onAttach(navigatorState)
assertWithMessage("Navigator should be attached")
.that(provider.getNavigator<EmptyNavigator>(EmptyNavigator.NAME).isAttached)
.isTrue()

// addNavigator should throw when trying to replace an existing, attached navigator, but
// we should have returned before that
try {
provider.addNavigator(navigator)
} catch (navigatorAlreadyAttached: IllegalStateException) {
fail(
"addNavigator with an existing navigator should return early and not " +
"attempt to replace"
)
}
}

@Test
fun addWithSameNameButUnequalNavigatorDoesReplace() {
val provider = NavigatorProvider()
val navigatorA = EmptyNavigator()
val navigatorB = EmptyNavigator()

assertThat(navigatorA).isNotEqualTo(navigatorB)

provider.addNavigator(navigatorA)
assertThat(provider.getNavigator<EmptyNavigator>(EmptyNavigator.NAME))
.isEqualTo(navigatorA)

provider.addNavigator(navigatorB)
assertThat(provider.getNavigator<EmptyNavigator>(EmptyNavigator.NAME))
.isEqualTo(navigatorB)
}

private val provider = NavigatorProvider()

@Test
Expand Down