Skip to content

Commit

Permalink
Merge pull request #403 from LachlanMcKee/fix-backstack-byte-code
Browse files Browse the repository at this point in the history
Fix backstack JDK 17 byte code
  • Loading branch information
LachlanMcKee authored Nov 18, 2024
2 parents c4abb51 + 2367fc1 commit 62dc428
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 105 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
org.gradle.caching=true
org.gradle.parallel=true

VERSION_NAME=0.40.3
VERSION_NAME=0.40.4

android.useAndroidX=true
android.defaults.buildfeatures.buildconfig=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.os.Parcelable
import com.badoo.ribs.core.modality.BuildParams
import com.badoo.ribs.minimal.reactive.Cancellable
import com.badoo.ribs.minimal.reactive.Source
import com.badoo.ribs.minimal.reactive.map
import com.badoo.ribs.minimal.state.Store
import com.badoo.ribs.minimal.state.TimeCapsule
import com.badoo.ribs.minimal.reactive.map
import com.badoo.ribs.routing.Routing
import com.badoo.ribs.routing.history.RoutingHistory
import com.badoo.ribs.routing.history.RoutingHistoryElement
Expand Down Expand Up @@ -72,7 +72,9 @@ class BackStack<C : Parcelable> internal constructor(
override fun baseLineState(fromRestored: Boolean): RoutingHistory<C> =
timeCapsule.initialState()

private val store = object : Store<State<C>>(timeCapsule.initialState()) {
private val store = BackStackStore()

private inner class BackStackStore : Store<State<C>>(timeCapsule.initialState()) {
init {
timeCapsule.register(timeCapsuleKey) { state }
initializeBackstack()
Expand Down Expand Up @@ -114,15 +116,21 @@ class BackStack<C : Parcelable> internal constructor(
)
}

private fun routingWithCorrectId(element: RoutingHistoryElement<C>, index: Int): Routing<C> =
private fun routingWithCorrectId(
element: RoutingHistoryElement<C>,
index: Int
): Routing<C> =
element.routing.copy(
identifier = state.contentIdForPosition(
index,
element.routing.configuration
)
)

private fun overlaysWithCorrectId(element: RoutingHistoryElement<C>, index: Int): List<Routing<C>> =
private fun overlaysWithCorrectId(
element: RoutingHistoryElement<C>,
index: Int
): List<Routing<C>> =
element.overlays.mapIndexed { overlayIndex, overlay ->
overlay.copy(
identifier = state.overlayIdForPosition(
Expand All @@ -144,7 +152,7 @@ class BackStack<C : Parcelable> internal constructor(
}

val activeConfiguration: C
get()= state.elements
get() = state.elements
.last()
.routing
.configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,67 @@
package com.badoo.ribs.example.root

// TODO: Uncomment once Mockito issues with BackStack are fixed.
// This blocked the update to AGP 8.2.2
//@ExtendWith(RxSchedulerExtension::class)
//class RootInteractorAuthStateTest {
//
// private val backStack: BackStack<Configuration> = mock()
//
// private val stateRelay = PublishRelay.create<AuthState>()
// private val authDataSource = mock<AuthDataSource>().apply {
// whenever(authUpdates).thenReturn(stateRelay)
// }
// private lateinit var interactor: RootInteractor
// private lateinit var interactorTestHelper: InteractorTestHelper<Nothing>
//
// @BeforeEach
// fun setup() {
// interactor = RootInteractor(
// buildParams = emptyBuildParams(),
// backStack = backStack,
// authDataSource = authDataSource,
// networkErrors = PublishRelay.create()
// )
// interactorTestHelper = InteractorTestHelper(interactor)
// }
//
// @ParameterizedTest
// @MethodSource("data")
// fun `an example test with some conditions should pass`(
// authState: AuthState,
// expectedConfiguration: Configuration
// ) {
// interactorTestHelper.moveToStateAndCheck(Lifecycle.State.CREATED) {
// stateRelay.accept(authState)
//
// verify(backStack).replace(expectedConfiguration)
// }
// }
//
// companion object {
// @JvmStatic
// fun data(): Stream<Arguments> = Stream.of(
// Arguments.of(AuthState.Unauthenticated, Configuration.Content.LoggedOut),
// Arguments.of(AuthState.Anonymous, Configuration.Content.LoggedIn),
// Arguments.of(AuthState.Authenticated(""), Configuration.Content.LoggedIn)
// )
// }
//}
import androidx.lifecycle.Lifecycle
import com.badoo.ribs.example.RxSchedulerExtension
import com.badoo.ribs.example.auth.AuthDataSource
import com.badoo.ribs.example.auth.AuthState
import com.badoo.ribs.example.root.routing.RootRouter.Configuration
import com.badoo.ribs.routing.source.backstack.BackStack
import com.badoo.ribs.routing.source.backstack.operation.replace
import com.badoo.ribs.test.InteractorTestHelper
import com.badoo.ribs.test.emptyBuildParams
import com.jakewharton.rxrelay2.PublishRelay
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import java.util.stream.Stream

@ExtendWith(RxSchedulerExtension::class)
class RootInteractorAuthStateTest {

private val backStack: BackStack<Configuration> = mock()

private val stateRelay = PublishRelay.create<AuthState>()
private val authDataSource = mock<AuthDataSource>().apply {
whenever(authUpdates).thenReturn(stateRelay)
}
private lateinit var interactor: RootInteractor
private lateinit var interactorTestHelper: InteractorTestHelper<Nothing>

@BeforeEach
fun setup() {
interactor = RootInteractor(
buildParams = emptyBuildParams(),
backStack = backStack,
authDataSource = authDataSource,
networkErrors = PublishRelay.create()
)
interactorTestHelper = InteractorTestHelper(interactor)
}

@ParameterizedTest
@MethodSource("data")
fun `an example test with some conditions should pass`(
authState: AuthState,
expectedConfiguration: Configuration
) {
interactorTestHelper.moveToStateAndCheck(Lifecycle.State.CREATED) {
stateRelay.accept(authState)

verify(backStack).replace(expectedConfiguration)
}
}

companion object {
@JvmStatic
fun data(): Stream<Arguments> = Stream.of(
Arguments.of(AuthState.Unauthenticated, Configuration.Content.LoggedOut),
Arguments.of(AuthState.Anonymous, Configuration.Content.LoggedIn),
Arguments.of(AuthState.Authenticated(""), Configuration.Content.LoggedIn)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,76 @@
package com.badoo.ribs.sandbox.rib.switcher

// TODO: Uncomment once Mockito issues with BackStack are fixed.
// This blocked the update to AGP 8.2.2
//@RunWith(RobolectricTestRunner::class)
//class SwitcherInteractorTest {
//
// private val backStack: BackStack<Configuration> = mock()
// private val dialogToTestOverlay: DialogToTestOverlay = mock()
//
// private val viewEventRelay = PublishRelay.create<Event>()
// private lateinit var interactor: SwitcherInteractor
// private lateinit var interactorTestHelper: InteractorTestHelper<SwitcherView>
//
// @Before
// fun setup() {
// interactor = SwitcherInteractor(
// buildParams = emptyBuildParams(),
// backStack = backStack,
// dialogToTestOverlay = dialogToTestOverlay,
// transitions = Observable.just(SETTLED),
// transitionSettled = { true }
// )
//
// interactorTestHelper = createInteractorTestHelper(interactor, viewEventRelay)
// }
//
// @Test
// fun `router open overlay dialog when show overlay dialog clicked`(){
// interactorTestHelper.moveToStateAndCheck(STARTED) {
// viewEventRelay.accept(Event.ShowOverlayDialogClicked)
//
// verify(backStack).pushOverlay(Overlay.Dialog)
// }
// }
//
// @Test
// fun `router open blocker when show blocker clicked`(){
// interactorTestHelper.moveToStateAndCheck(STARTED) {
// viewEventRelay.accept(Event.ShowBlockerClicked)
//
// verify(backStack).push(Content.Blocker)
// }
// }
//
// @Test
// fun `skip view event when view not resumed`(){
// interactorTestHelper.moveToStateAndCheck(CREATED) {
// viewEventRelay.accept(Event.ShowBlockerClicked)
//
// verify(backStack, never()).push(Content.Blocker)
// }
// }
//}
import androidx.lifecycle.Lifecycle.State.CREATED
import androidx.lifecycle.Lifecycle.State.STARTED
import com.badoo.common.ribs.rx2.createInteractorTestHelper
import com.badoo.ribs.routing.router.Router.TransitionState.SETTLED
import com.badoo.ribs.routing.source.backstack.BackStack
import com.badoo.ribs.routing.source.backstack.operation.push
import com.badoo.ribs.routing.source.backstack.operation.pushOverlay
import com.badoo.ribs.sandbox.rib.switcher.SwitcherView.Event
import com.badoo.ribs.sandbox.rib.switcher.dialog.DialogToTestOverlay
import com.badoo.ribs.sandbox.rib.switcher.routing.SwitcherRouter.Configuration
import com.badoo.ribs.sandbox.rib.switcher.routing.SwitcherRouter.Configuration.Content
import com.badoo.ribs.sandbox.rib.switcher.routing.SwitcherRouter.Configuration.Overlay
import com.badoo.ribs.test.InteractorTestHelper
import com.badoo.ribs.test.emptyBuildParams
import com.jakewharton.rxrelay2.PublishRelay
import io.reactivex.Observable
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class SwitcherInteractorTest {

private val backStack: BackStack<Configuration> = mock()
private val dialogToTestOverlay: DialogToTestOverlay = mock()

private val viewEventRelay = PublishRelay.create<Event>()
private lateinit var interactor: SwitcherInteractor
private lateinit var interactorTestHelper: InteractorTestHelper<SwitcherView>

@Before
fun setup() {
interactor = SwitcherInteractor(
buildParams = emptyBuildParams(),
backStack = backStack,
dialogToTestOverlay = dialogToTestOverlay,
transitions = Observable.just(SETTLED),
transitionSettled = { true }
)

interactorTestHelper = createInteractorTestHelper(interactor, viewEventRelay)
}

@Test
fun `router open overlay dialog when show overlay dialog clicked`() {
interactorTestHelper.moveToStateAndCheck(STARTED) {
viewEventRelay.accept(Event.ShowOverlayDialogClicked)

verify(backStack).pushOverlay(Overlay.Dialog)
}
}

@Test
fun `router open blocker when show blocker clicked`() {
interactorTestHelper.moveToStateAndCheck(STARTED) {
viewEventRelay.accept(Event.ShowBlockerClicked)

verify(backStack).push(Content.Blocker)
}
}

@Test
fun `skip view event when view not resumed`() {
interactorTestHelper.moveToStateAndCheck(CREATED) {
viewEventRelay.accept(Event.ShowBlockerClicked)

verify(backStack, never()).push(Content.Blocker)
}
}
}

0 comments on commit 62dc428

Please sign in to comment.