11package com.arkivanov.decompose
22
3- import com.arkivanov.decompose.backhandler.TestBackDispatcher
43import com.arkivanov.decompose.router.TestInstance
5- import com.arkivanov.decompose.statekeeper.TestStateKeeperDispatcher
4+ import com.arkivanov.decompose.testutils.TestComponentContext
65import com.arkivanov.decompose.testutils.consume
6+ import com.arkivanov.decompose.testutils.recreate
77import com.arkivanov.decompose.testutils.register
88import com.arkivanov.essenty.backhandler.BackCallback
9- import com.arkivanov.essenty.instancekeeper.InstanceKeeperDispatcher
109import com.arkivanov.essenty.instancekeeper.getOrCreate
1110import com.arkivanov.essenty.lifecycle.Lifecycle
1211import com.arkivanov.essenty.lifecycle.Lifecycle.State.CREATED
@@ -16,7 +15,6 @@ import com.arkivanov.essenty.lifecycle.destroy
1615import com.arkivanov.essenty.lifecycle.resume
1716import com.arkivanov.essenty.lifecycle.start
1817import com.arkivanov.essenty.lifecycle.stop
19- import com.arkivanov.essenty.statekeeper.SerializableContainer
2018import kotlin.test.Test
2119import kotlin.test.assertEquals
2220import kotlin.test.assertFailsWith
@@ -30,7 +28,7 @@ class ChildContextWithLifecycleTest {
3028
3129 @Test
3230 fun GIVEN_created_WHEN_parent_lifecycle_resumed_and_child_lifecycle_initialized_THEN_child_lifecycle_initialized () {
33- val context = TestContext ( )
31+ val context = TestComponentContext (lifecycle = LifecycleRegistry () )
3432 val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
3533
3634 context.lifecycle.resume()
@@ -40,7 +38,7 @@ class ChildContextWithLifecycleTest {
4038
4139 @Test
4240 fun GIVEN_created_WHEN_parent_lifecycle_initialized_and_child_lifecycle_resumed_THEN_child_lifecycle_initialized () {
43- val context = TestContext ( )
41+ val context = TestComponentContext (lifecycle = LifecycleRegistry () )
4442 val childLifecycle = LifecycleRegistry ()
4543 val childContext = context.childContext(key = " key" , lifecycle = childLifecycle)
4644
@@ -51,7 +49,7 @@ class ChildContextWithLifecycleTest {
5149
5250 @Test
5351 fun GIVEN_created_WHEN_parent_lifecycle_resumed_and_child_lifecycle_resumed_THEN_child_lifecycle_resumed () {
54- val context = TestContext ( )
52+ val context = TestComponentContext (lifecycle = LifecycleRegistry () )
5553 val childLifecycle = LifecycleRegistry ()
5654 val childContext = context.childContext(key = " key" , lifecycle = childLifecycle)
5755
@@ -63,7 +61,7 @@ class ChildContextWithLifecycleTest {
6361
6462 @Test
6563 fun GIVEN_created_WHEN_parent_lifecycle_destroyed_and_child_lifecycle_resumed_THEN_child_lifecycle_destroyed () {
66- val context = TestContext ( )
64+ val context = TestComponentContext (lifecycle = LifecycleRegistry () )
6765 val childLifecycle = LifecycleRegistry ()
6866 val childContext = context.childContext(key = " key" , lifecycle = childLifecycle)
6967
@@ -76,7 +74,7 @@ class ChildContextWithLifecycleTest {
7674
7775 @Test
7876 fun GIVEN_parent_and_child_lifecycles_resumed_WHEN_child_lifecycle_destroyed_THEN_error_thrown () {
79- val context = TestContext ( )
77+ val context = TestComponentContext (lifecycle = LifecycleRegistry () )
8078 val childLifecycle = LifecycleRegistry ()
8179 context.childContext(key = " key" , lifecycle = childLifecycle)
8280 context.lifecycle.resume()
@@ -89,14 +87,12 @@ class ChildContextWithLifecycleTest {
8987
9088 @Test
9189 fun WHEN_recreated_THEN_child_state_restored () {
92- val context = TestContext ( )
90+ val context = TestComponentContext (lifecycle = LifecycleRegistry () )
9391 val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
9492 childContext.stateKeeper.register(" child_key" ) { " savedChildState" }
9593 context.lifecycle.resume()
9694
97- val savedParentState = context.stateKeeper.save()
98- context.lifecycle.destroy()
99- val newContext = TestContext (savedParentState)
95+ val newContext = context.recreate()
10096 val newChild = newContext.childContext(key = " key" , lifecycle = LifecycleRegistry ())
10197 val restoredChildState = newChild.stateKeeper.consume<String >(" child_key" )
10298
@@ -105,21 +101,20 @@ class ChildContextWithLifecycleTest {
105101
106102 @Test
107103 fun WHEN_created_THEN_child_registered_in_StateKeeper () {
108- val context = TestContext ()
104+ val context = TestComponentContext ()
109105
110106 context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
111107
112- context.stateKeeper.assertSupplierRegistered(key = " key" )
108+ assertTrue( context.stateKeeper.isRegistered( " key" ) )
113109 }
114110
115111 @Test
116112 fun WHEN_recreated_THEN_child_instance_retained () {
117- val instanceKeeperDispatcher = InstanceKeeperDispatcher ()
118- val context = TestContext (instanceKeeper = instanceKeeperDispatcher)
113+ val context = TestComponentContext ()
119114 val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
120115 val instance = childContext.instanceKeeper.getOrCreate(key = " child" , factory = ::TestInstance )
121116
122- val newContext = TestContext (instanceKeeper = instanceKeeperDispatcher )
117+ val newContext = context.recreate(isConfigurationChange = true )
123118 val newChildContext = newContext.childContext(key = " key" , lifecycle = LifecycleRegistry ())
124119 val newInstance = newChildContext.instanceKeeper.getOrCreate(key = " child" , factory = ::TestInstance )
125120
@@ -128,8 +123,7 @@ class ChildContextWithLifecycleTest {
128123
129124 @Test
130125 fun WHEN_destroyed_THEN_child_instance_not_destroyed () {
131- val instanceKeeperDispatcher = InstanceKeeperDispatcher ()
132- val context = TestContext (instanceKeeper = instanceKeeperDispatcher)
126+ val context = TestComponentContext (lifecycle = LifecycleRegistry ())
133127 val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
134128 val instance = childContext.instanceKeeper.getOrCreate(key = " child" , factory = ::TestInstance )
135129 context.lifecycle.resume()
@@ -140,19 +134,18 @@ class ChildContextWithLifecycleTest {
140134
141135 @Test
142136 fun WHEN_parent_InstanceKeeper_destroyed_THEN_child_instance_destroyed () {
143- val instanceKeeperDispatcher = InstanceKeeperDispatcher ()
144- val context = TestContext (instanceKeeper = instanceKeeperDispatcher)
137+ val context = TestComponentContext ()
145138 val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
146139 val instance = childContext.instanceKeeper.getOrCreate(key = " child" , factory = ::TestInstance )
147140
148- instanceKeeperDispatcher .destroy()
141+ context.instanceKeeper .destroy()
149142
150143 assertTrue(instance.isDestroyed)
151144 }
152145
153146 @Test
154147 fun WHEN_created_THEN_child_registered_in_InstanceKeeper () {
155- val context = TestContext ()
148+ val context = TestComponentContext ()
156149
157150 context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
158151
@@ -161,9 +154,8 @@ class ChildContextWithLifecycleTest {
161154
162155 @Test
163156 fun GIVEN_child_lifecycle_stopped_and_enabled_child_BackHandler_registered_WHEN_backHandler_back_THEN_returns_false () {
164- val context = TestContext ()
165- val childContext =
166- context.childContext(key = " key" , lifecycle = LifecycleRegistry (initialState = CREATED ))
157+ val context = TestComponentContext ()
158+ val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry (initialState = CREATED ))
167159 childContext.backHandler.register(BackCallback (isEnabled = true , onBack = {}))
168160
169161 val result = context.backHandler.back()
@@ -173,7 +165,7 @@ class ChildContextWithLifecycleTest {
173165
174166 @Test
175167 fun GIVEN_child_lifecycle_stopped_and_disabled_child_BackHandler_registered_WHEN_child_lifecycle_started_and_backHandler_back_THEN_returns_false () {
176- val context = TestContext ()
168+ val context = TestComponentContext ()
177169 val childLifecycle = LifecycleRegistry (initialState = CREATED )
178170 val childContext = context.childContext(key = " key" , lifecycle = childLifecycle)
179171 childContext.backHandler.register(BackCallback (isEnabled = false , onBack = {}))
@@ -186,7 +178,7 @@ class ChildContextWithLifecycleTest {
186178
187179 @Test
188180 fun GIVEN_child_lifecycle_started_and_enabled_child_BackHandler_registered_WHEN_backHandler_back_THEN_returns_true () {
189- val context = TestContext ()
181+ val context = TestComponentContext ()
190182 val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry (initialState = STARTED ))
191183 childContext.backHandler.register(BackCallback (isEnabled = true , onBack = {}))
192184
@@ -197,7 +189,7 @@ class ChildContextWithLifecycleTest {
197189
198190 @Test
199191 fun GIVEN_child_lifecycle_started_and_disabled_child_BackHandler_registered_WHEN_backHandler_back_THEN_returns_false () {
200- val context = TestContext ()
192+ val context = TestComponentContext ()
201193 val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry (initialState = STARTED ))
202194 childContext.backHandler.register(BackCallback (isEnabled = false , onBack = {}))
203195
@@ -208,7 +200,7 @@ class ChildContextWithLifecycleTest {
208200
209201 @Test
210202 fun GIVEN_child_lifecycle_started_and_enabled_child_BackHandler_registered_WHEN_child_lifecycle_stopped_and_backHandler_back_THEN_returns_false () {
211- val context = TestContext ()
203+ val context = TestComponentContext ()
212204 val childLifecycle = LifecycleRegistry (initialState = STARTED )
213205 val childContext = context.childContext(key = " key" , lifecycle = childLifecycle)
214206 childContext.backHandler.register(BackCallback (isEnabled = true , onBack = {}))
@@ -221,7 +213,7 @@ class ChildContextWithLifecycleTest {
221213
222214 @Test
223215 fun GIVEN_disabled_child_BackHandler_registered_WHEN_backHandler_back_THEN_returns_false () {
224- val context = TestContext ()
216+ val context = TestComponentContext ()
225217 val childContext = context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
226218 childContext.backHandler.register(BackCallback (isEnabled = false , onBack = {}))
227219
@@ -232,21 +224,11 @@ class ChildContextWithLifecycleTest {
232224
233225 @Test
234226 fun GIVEN_child_BackHandler_not_registered_WHEN_backHandler_back_THEN_returns_false () {
235- val context = TestContext ()
227+ val context = TestComponentContext ()
236228 context.childContext(key = " key" , lifecycle = LifecycleRegistry ())
237229
238230 val result = context.backHandler.back()
239231
240232 assertFalse(result)
241233 }
242-
243- private class TestContext (
244- savedState : SerializableContainer ? = null ,
245- override val instanceKeeper : InstanceKeeperDispatcher = InstanceKeeperDispatcher (),
246- ) : ComponentContext {
247- override val lifecycle: LifecycleRegistry = LifecycleRegistry ()
248- override val stateKeeper: TestStateKeeperDispatcher = TestStateKeeperDispatcher (savedState)
249- override val backHandler: TestBackDispatcher = TestBackDispatcher ()
250- override val componentContextFactory: ComponentContextFactory <ComponentContext > = ComponentContextFactory (::DefaultComponentContext )
251- }
252234}
0 commit comments