Skip to content

Commit d304c9f

Browse files
authored
Merge pull request #1025 from arkivanov/update-v4
[V4] Update v4 branch
2 parents cbfbf1c + e775512 commit d304c9f

28 files changed

Lines changed: 2417 additions & 614 deletions

File tree

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.arkivanov.decompose
22

3-
import com.arkivanov.decompose.statekeeper.TestStateKeeperDispatcher
3+
import com.arkivanov.decompose.testutils.TestComponentContext
44
import com.arkivanov.essenty.backhandler.BackCallback
5-
import com.arkivanov.essenty.backhandler.BackDispatcher
6-
import com.arkivanov.essenty.instancekeeper.InstanceKeeperDispatcher
7-
import com.arkivanov.essenty.lifecycle.LifecycleRegistry
85
import kotlin.test.Test
96
import kotlin.test.assertEquals
107

@@ -13,8 +10,7 @@ class ChildContextWithBackHandlerPriorityTest {
1310

1411
@Test
1512
fun GIVEN_backHandlerPriority_specified_WHEN_back_THEN_order_according_to_priority() {
16-
val backDispatcher = BackDispatcher()
17-
val context = TestContext(backDispatcher)
13+
val context = TestComponentContext()
1814
val childContext1 = context.childContext(key = "context1", backHandlerPriority = 3)
1915
val childContext2 = context.childContext(key = "context2", backHandlerPriority = 4)
2016
val childContext3 = context.childContext(key = "context3", backHandlerPriority = 2)
@@ -30,23 +26,22 @@ class ChildContextWithBackHandlerPriorityTest {
3026
childContext2.backHandler.register(backCallback2)
3127
childContext3.backHandler.register(backCallback3)
3228

33-
backDispatcher.back()
29+
context.backHandler.back()
3430
backCallback02.isEnabled = false
35-
backDispatcher.back()
31+
context.backHandler.back()
3632
backCallback2.isEnabled = false
37-
backDispatcher.back()
33+
context.backHandler.back()
3834
backCallback1.isEnabled = false
39-
backDispatcher.back()
35+
context.backHandler.back()
4036
backCallback3.isEnabled = false
41-
backDispatcher.back()
37+
context.backHandler.back()
4238

4339
assertEquals(listOf("02", "2", "1", "3", "01"), events)
4440
}
4541

4642
@Test
4743
fun GIVEN_backHandlerPriority_not_specified_WHEN_back_THEN_reverse_order() {
48-
val backDispatcher = BackDispatcher()
49-
val context = TestContext(backDispatcher)
44+
val context = TestComponentContext()
5045
val childContext1 = context.childContext(key = "context1")
5146
val childContext2 = context.childContext(key = "context2")
5247
val childContext3 = context.childContext(key = "context3")
@@ -62,27 +57,18 @@ class ChildContextWithBackHandlerPriorityTest {
6257
childContext2.backHandler.register(backCallback2)
6358
childContext3.backHandler.register(backCallback3)
6459

65-
backDispatcher.back()
60+
context.backHandler.back()
6661
backCallback02.isEnabled = false
67-
backDispatcher.back()
62+
context.backHandler.back()
6863
backCallback01.isEnabled = false
69-
backDispatcher.back()
64+
context.backHandler.back()
7065
backCallback3.isEnabled = false
71-
backDispatcher.back()
66+
context.backHandler.back()
7267
backCallback2.isEnabled = false
73-
backDispatcher.back()
68+
context.backHandler.back()
7469
backCallback1.isEnabled = false
75-
backDispatcher.back()
70+
context.backHandler.back()
7671

7772
assertEquals(listOf("02", "01", "3", "2", "1"), events)
7873
}
79-
80-
private class TestContext(
81-
override val backHandler: BackDispatcher = BackDispatcher()
82-
) : ComponentContext {
83-
override val lifecycle: LifecycleRegistry = LifecycleRegistry()
84-
override val stateKeeper: TestStateKeeperDispatcher = TestStateKeeperDispatcher()
85-
override val instanceKeeper: InstanceKeeperDispatcher = InstanceKeeperDispatcher()
86-
override val componentContextFactory: ComponentContextFactory<ComponentContext> = ComponentContextFactory(::DefaultComponentContext)
87-
}
8874
}

‎decompose/src/commonTest/kotlin/com/arkivanov/decompose/ChildContextWithLifecycleTest.kt‎

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.arkivanov.decompose
22

3-
import com.arkivanov.decompose.backhandler.TestBackDispatcher
43
import com.arkivanov.decompose.router.TestInstance
5-
import com.arkivanov.decompose.statekeeper.TestStateKeeperDispatcher
4+
import com.arkivanov.decompose.testutils.TestComponentContext
65
import com.arkivanov.decompose.testutils.consume
6+
import com.arkivanov.decompose.testutils.recreate
77
import com.arkivanov.decompose.testutils.register
88
import com.arkivanov.essenty.backhandler.BackCallback
9-
import com.arkivanov.essenty.instancekeeper.InstanceKeeperDispatcher
109
import com.arkivanov.essenty.instancekeeper.getOrCreate
1110
import com.arkivanov.essenty.lifecycle.Lifecycle
1211
import com.arkivanov.essenty.lifecycle.Lifecycle.State.CREATED
@@ -16,7 +15,6 @@ import com.arkivanov.essenty.lifecycle.destroy
1615
import com.arkivanov.essenty.lifecycle.resume
1716
import com.arkivanov.essenty.lifecycle.start
1817
import com.arkivanov.essenty.lifecycle.stop
19-
import com.arkivanov.essenty.statekeeper.SerializableContainer
2018
import kotlin.test.Test
2119
import kotlin.test.assertEquals
2220
import 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

Comments
 (0)