Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package androidx.ui.core |
| 18 | |
| 19 | import androidx.compose.Applier |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 20 | import androidx.compose.Composable |
| 21 | import androidx.compose.Composer |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 22 | import androidx.compose.ExperimentalComposeApi |
| 23 | import androidx.compose.InternalComposeApi |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 24 | import androidx.compose.Recomposer |
| 25 | import androidx.compose.SlotTable |
| 26 | import androidx.compose.currentComposer |
Louis Pullen-Freilich | 4b2e7e0 | 2020-07-21 17:26:40 +0100 | [diff] [blame] | 27 | import androidx.compose.runtime.dispatch.MonotonicFrameClock |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 28 | import androidx.compose.invalidate |
| 29 | import androidx.compose.remember |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 30 | import androidx.compose.withRunningRecomposer |
| 31 | import kotlinx.coroutines.channels.Channel |
| 32 | import kotlinx.coroutines.runBlocking |
Adam Powell | b0c1b40 | 2020-06-24 20:26:09 -0700 | [diff] [blame] | 33 | import kotlinx.coroutines.withContext |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 34 | import org.junit.Assert.assertEquals |
| 35 | import org.junit.Assert.assertNotEquals |
| 36 | import org.junit.Assert.assertNotNull |
| 37 | import org.junit.Assert.assertTrue |
| 38 | import org.junit.Test |
| 39 | |
| 40 | private class TestTagModifier<T>(val name: String, val value: T) : Modifier.Element |
| 41 | |
| 42 | fun <T> Modifier.testTag(name: String, value: T) = this + TestTagModifier(name, value) |
| 43 | |
| 44 | fun <T> Modifier.getTestTag(name: String, default: T): T = foldIn(default) { acc, element -> |
| 45 | @Suppress("UNCHECKED_CAST") |
| 46 | if (element is TestTagModifier<*> && element.name == name) element.value as T else acc |
| 47 | } |
| 48 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 49 | @OptIn(InternalComposeApi::class) |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 50 | class ComposedModifierTest { |
| 51 | |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Confirm that a [composed] modifier correctly constructs separate instances when materialized |
| 54 | */ |
| 55 | @Test |
Adam Powell | b0c1b40 | 2020-06-24 20:26:09 -0700 | [diff] [blame] | 56 | fun materializeComposedModifier() = runBlocking(TestFrameClock()) { |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 57 | // Note: assumes single-threaded composition |
| 58 | var counter = 0 |
| 59 | val sourceMod = Modifier.testTag("static", 0) |
| 60 | .composed { testTag("dynamic", ++counter) } |
| 61 | |
Adam Powell | b0c1b40 | 2020-06-24 20:26:09 -0700 | [diff] [blame] | 62 | withRunningRecomposer { recomposer -> |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 63 | lateinit var firstMaterialized: Modifier |
| 64 | lateinit var secondMaterialized: Modifier |
| 65 | compose(recomposer) { |
| 66 | firstMaterialized = currentComposer.materialize(sourceMod) |
| 67 | secondMaterialized = currentComposer.materialize(sourceMod) |
| 68 | } |
| 69 | |
| 70 | assertNotEquals("I recomposed some modifiers", 0, counter) |
| 71 | |
| 72 | assertEquals( |
| 73 | "first static value equal to source", |
| 74 | sourceMod.getTestTag("static", Int.MIN_VALUE), |
| 75 | firstMaterialized.getTestTag("static", Int.MAX_VALUE) |
| 76 | ) |
| 77 | assertEquals( |
| 78 | "second static value equal to source", |
| 79 | sourceMod.getTestTag("static", Int.MIN_VALUE), |
| 80 | secondMaterialized.getTestTag("static", Int.MAX_VALUE) |
| 81 | ) |
| 82 | assertEquals( |
| 83 | "dynamic value not present in source", |
| 84 | Int.MIN_VALUE, |
| 85 | sourceMod.getTestTag("dynamic", Int.MIN_VALUE) |
| 86 | ) |
| 87 | assertNotEquals( |
| 88 | "dynamic value present in first materialized", |
| 89 | Int.MIN_VALUE, |
| 90 | firstMaterialized.getTestTag("dynamic", Int.MIN_VALUE) |
| 91 | ) |
| 92 | assertNotEquals( |
| 93 | "dynamic value present in second materialized", |
| 94 | Int.MIN_VALUE, |
| 95 | firstMaterialized.getTestTag("dynamic", Int.MIN_VALUE) |
| 96 | ) |
| 97 | assertNotEquals( |
| 98 | "first and second dynamic values must be unequal", |
| 99 | firstMaterialized.getTestTag("dynamic", Int.MIN_VALUE), |
| 100 | secondMaterialized.getTestTag("dynamic", Int.MIN_VALUE) |
| 101 | ) |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 102 | } |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Confirm that recomposition occurs on invalidation |
| 107 | */ |
| 108 | @Test |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 109 | fun recomposeComposedModifier() = runBlocking { |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 110 | // Manually invalidate the composition of the modifier instead of using mutableStateOf |
| 111 | // Frame-based recomposition requires the FrameManager be up and running. |
| 112 | var value = 0 |
| 113 | lateinit var invalidator: () -> Unit |
| 114 | |
| 115 | val sourceMod = Modifier.composed { |
| 116 | invalidator = invalidate |
| 117 | testTag("changing", value) |
| 118 | } |
| 119 | |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 120 | val frameClock = TestFrameClock() |
Adam Powell | b0c1b40 | 2020-06-24 20:26:09 -0700 | [diff] [blame] | 121 | withContext(frameClock) { |
| 122 | withRunningRecomposer { recomposer -> |
| 123 | lateinit var materialized: Modifier |
| 124 | compose(recomposer) { |
| 125 | materialized = currentComposer.materialize(sourceMod) |
| 126 | } |
| 127 | |
| 128 | assertEquals( |
| 129 | "initial composition value", |
| 130 | 0, |
| 131 | materialized.getTestTag("changing", Int.MIN_VALUE) |
| 132 | ) |
| 133 | |
| 134 | value = 5 |
| 135 | invalidator() |
| 136 | frameClock.frame(0L) |
| 137 | |
| 138 | assertEquals( |
| 139 | "recomposed composition value", |
| 140 | 5, |
| 141 | materialized.getTestTag("changing", Int.MIN_VALUE) |
| 142 | ) |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 143 | } |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 144 | } |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Test |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 148 | fun rememberComposedModifier() = runBlocking { |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 149 | lateinit var invalidator: () -> Unit |
| 150 | val sourceMod = Modifier.composed { |
| 151 | invalidator = invalidate |
| 152 | val state = remember { Any() } |
| 153 | testTag("remembered", state) |
| 154 | } |
| 155 | |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 156 | val frameClock = TestFrameClock() |
| 157 | |
Adam Powell | b0c1b40 | 2020-06-24 20:26:09 -0700 | [diff] [blame] | 158 | withContext(frameClock) { |
| 159 | withRunningRecomposer { recomposer -> |
| 160 | val results = mutableListOf<Any?>() |
| 161 | val notFound = Any() |
| 162 | compose(recomposer) { |
| 163 | results.add( |
| 164 | currentComposer.materialize(sourceMod).getTestTag("remembered", notFound) |
| 165 | ) |
| 166 | } |
| 167 | |
| 168 | assertTrue("one item added for initial composition", results.size == 1) |
| 169 | assertNotNull("remembered object not null", results[0]) |
| 170 | |
| 171 | invalidator() |
| 172 | frameClock.frame(0) |
| 173 | |
| 174 | assertEquals("two items added after recomposition", 2, results.size) |
| 175 | assertTrue("no null items", results.none { it === notFound }) |
| 176 | assertEquals("remembered references are equal", results[0], results[1]) |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 177 | } |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 178 | } |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Test |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 182 | fun nestedComposedModifiers() = runBlocking { |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 183 | val mod = Modifier.composed { |
| 184 | composed { |
| 185 | testTag("nested", 10) |
| 186 | } |
| 187 | } |
| 188 | |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 189 | val frameClock = TestFrameClock() |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 190 | |
Adam Powell | b0c1b40 | 2020-06-24 20:26:09 -0700 | [diff] [blame] | 191 | withContext(frameClock) { |
| 192 | withRunningRecomposer { recomposer -> |
| 193 | lateinit var materialized: Modifier |
| 194 | compose(recomposer) { |
| 195 | materialized = currentComposer.materialize(mod) |
| 196 | } |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 197 | |
Adam Powell | b0c1b40 | 2020-06-24 20:26:09 -0700 | [diff] [blame] | 198 | assertEquals( |
| 199 | "fully unwrapped composed modifier value", |
| 200 | 10, |
| 201 | materialized.getTestTag("nested", 0) |
| 202 | ) |
| 203 | } |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 204 | } |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 208 | @OptIn(InternalComposeApi::class, ExperimentalComposeApi::class) |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 209 | private fun compose( |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 210 | recomposer: Recomposer, |
Louis Pullen-Freilich | 3a54b94 | 2020-05-07 13:23:03 +0100 | [diff] [blame] | 211 | block: @Composable () -> Unit |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 212 | ): Composer<Unit> { |
| 213 | return Composer( |
| 214 | SlotTable(), |
| 215 | EmptyApplier(), |
| 216 | recomposer |
| 217 | ).apply { |
| 218 | composeRoot { |
| 219 | @Suppress("UNCHECKED_CAST") |
| 220 | val fn = block as (Composer<*>, Int, Int) -> Unit |
| 221 | fn(this, 0, 0) |
| 222 | } |
| 223 | applyChanges() |
| 224 | slotTable.verifyWellFormed() |
| 225 | } |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Adam Powell | 28b24c3 | 2020-06-15 17:08:49 -0700 | [diff] [blame] | 228 | private class TestFrameClock : MonotonicFrameClock { |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 229 | |
| 230 | private val frameCh = Channel<Long>() |
| 231 | |
| 232 | suspend fun frame(frameTimeNanos: Long) { |
| 233 | frameCh.send(frameTimeNanos) |
Adam Powell | 85b14d0 | 2020-05-19 11:39:03 -0700 | [diff] [blame] | 234 | } |
Adam Powell | 11d00c7 | 2020-04-15 09:28:13 -0700 | [diff] [blame] | 235 | |
Adam Powell | e6bbf20 | 2020-06-05 16:19:08 -0700 | [diff] [blame] | 236 | override suspend fun <R> withFrameNanos(onFrame: (Long) -> R): R = onFrame(frameCh.receive()) |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Leland Richardson | 656c113 | 2020-06-03 09:12:42 -0700 | [diff] [blame] | 239 | @OptIn(ExperimentalComposeApi::class) |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 240 | class EmptyApplier : Applier<Unit> { |
| 241 | override val current: Unit = Unit |
| 242 | override fun down(node: Unit) {} |
| 243 | override fun up() {} |
| 244 | override fun insert(index: Int, instance: Unit) { |
| 245 | error("Unexpected") |
Adam Powell | a1f55b9 | 2020-04-20 16:32:38 -0700 | [diff] [blame] | 246 | } |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 247 | override fun remove(index: Int, count: Int) { |
| 248 | error("Unexpected") |
| 249 | } |
| 250 | override fun move(from: Int, to: Int, count: Int) { |
| 251 | error("Unexpected") |
| 252 | } |
Adam Powell | 8dec9b7 | 2020-06-19 14:24:14 -0700 | [diff] [blame] | 253 | override fun clear() {} |
Leland Richardson | 19285b1 | 2020-06-18 13:37:21 -0700 | [diff] [blame] | 254 | } |