Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 1 | /* |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 2 | * Copyright 2020 The Android Open Source Project |
Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 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 | |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 17 | package androidx.compose.test |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 18 | |
| 19 | import android.view.View |
| 20 | import android.view.ViewGroup |
| 21 | import android.widget.LinearLayout |
| 22 | import android.widget.TextView |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 23 | import androidx.compose.Composable |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 24 | import androidx.compose.ExperimentalComposeApi |
Andrey Kulikov | d13aefe | 2020-03-16 13:52:22 +0000 | [diff] [blame] | 25 | import androidx.compose.clearRoots |
Chuck Jazdzewski | b15857c | 2020-05-26 17:14:02 -0700 | [diff] [blame] | 26 | import androidx.compose.getValue |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 27 | import androidx.compose.invalidate |
| 28 | import androidx.compose.key |
Chuck Jazdzewski | b15857c | 2020-05-26 17:14:02 -0700 | [diff] [blame] | 29 | import androidx.compose.mutableStateOf |
| 30 | import androidx.compose.remember |
| 31 | import androidx.compose.setValue |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 32 | import androidx.compose.snapshots.currentSnapshot |
Cătălin Tudor | a2ed24e | 2019-07-04 13:50:58 +0100 | [diff] [blame] | 33 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
Alexandre Elias | 558d1c9 | 2020-02-20 18:04:25 -0800 | [diff] [blame] | 34 | import androidx.test.filters.MediumTest |
Cătălin Tudor | a2ed24e | 2019-07-04 13:50:58 +0100 | [diff] [blame] | 35 | import junit.framework.TestCase.assertEquals |
| 36 | import junit.framework.TestCase.assertFalse |
| 37 | import junit.framework.TestCase.assertNotSame |
| 38 | import junit.framework.TestCase.assertTrue |
Leland Richardson | 7f848ab | 2019-12-12 13:43:41 -0800 | [diff] [blame] | 39 | import org.junit.After |
Cătălin Tudor | a2ed24e | 2019-07-04 13:50:58 +0100 | [diff] [blame] | 40 | import org.junit.Rule |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 41 | import org.junit.Test |
| 42 | import org.junit.runner.RunWith |
Chuck Jazdzewski | d023504592 | 2020-07-22 11:14:20 -0700 | [diff] [blame^] | 43 | import kotlin.test.assertNotNull |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 44 | |
Alexandre Elias | 558d1c9 | 2020-02-20 18:04:25 -0800 | [diff] [blame] | 45 | @MediumTest |
Cătălin Tudor | a2ed24e | 2019-07-04 13:50:58 +0100 | [diff] [blame] | 46 | @RunWith(AndroidJUnit4::class) |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 47 | class RecomposerTests : BaseComposeTest() { |
Leland Richardson | 7f848ab | 2019-12-12 13:43:41 -0800 | [diff] [blame] | 48 | @After |
| 49 | fun teardown() { |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 50 | clearRoots() |
Leland Richardson | 7f848ab | 2019-12-12 13:43:41 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Cătălin Tudor | a2ed24e | 2019-07-04 13:50:58 +0100 | [diff] [blame] | 53 | @get:Rule |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 54 | override val activityRule = makeTestActivityRule() |
Cătălin Tudor | a2ed24e | 2019-07-04 13:50:58 +0100 | [diff] [blame] | 55 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 56 | @Test |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 57 | fun testNativeViewWithAttributes() { |
| 58 | compose { |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 59 | TextView(id = 456, text = "some text") |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 60 | }.then { activity -> |
| 61 | assertEquals(1, activity.root.childCount) |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 62 | |
| 63 | val tv = activity.findViewById(456) as TextView |
| 64 | assertEquals("some text", tv.text) |
| 65 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 66 | assertEquals(tv, activity.root.getChildAt(0)) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 67 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Test |
| 71 | fun testSlotKeyChangeCausesRecreate() { |
| 72 | var i = 1 |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 73 | var tv1: TextView? = null |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 74 | val trigger = Trigger() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 75 | compose { |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 76 | trigger.subscribe() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 77 | // this should cause the textview to get recreated on every compose |
| 78 | i++ |
| 79 | |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 80 | key(i) { |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 81 | TextView(id = 456, text = "some text") |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 82 | } |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 83 | }.then { activity -> |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 84 | tv1 = activity.findViewById(456) as TextView |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 85 | trigger.recompose() |
| 86 | }.then { activity -> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 87 | assertEquals("Compose got called twice", 3, i) |
| 88 | |
| 89 | val tv2 = activity.findViewById(456) as TextView |
| 90 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 91 | assertFalse( |
| 92 | "The text views should be different instances", |
| 93 | tv1 === tv2 |
| 94 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 95 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 96 | assertEquals( |
| 97 | "The unused child got removed from the view hierarchy", |
| 98 | 1, |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 99 | activity.root.childCount |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 100 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | fun testViewWithViewChildren() { |
| 106 | compose { |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 107 | LinearLayout(id = 345) { |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 108 | TextView(id = 456, text = "some text") |
| 109 | TextView(id = 567, text = "some text") |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 110 | } |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 111 | }.then { activity -> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 112 | val ll = activity.findViewById(345) as LinearLayout |
| 113 | val tv1 = activity.findViewById(456) as TextView |
| 114 | val tv2 = activity.findViewById(567) as TextView |
| 115 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 116 | assertEquals("The linear layout should be the only child of root", 1, |
| 117 | activity.root.childCount) |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 118 | assertEquals("Both children should have been added", 2, ll.childCount) |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 119 | assertTrue( |
| 120 | "Should be the expected TextView (1)", |
| 121 | ll.getChildAt(0) === tv1 |
| 122 | ) |
| 123 | assertTrue( |
| 124 | "Should be the expected TextView (2)", |
| 125 | ll.getChildAt(1) === tv2 |
| 126 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | fun testForLoop() { |
| 132 | val items = listOf(1, 2, 3, 4, 5, 6) |
| 133 | compose { |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 134 | LinearLayout(id = 345) { |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 135 | for (i in items) { |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 136 | TextView(id = 456, text = "some text $i") |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 139 | }.then { activity -> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 140 | val ll = activity.findViewById(345) as LinearLayout |
| 141 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 142 | assertEquals("The linear layout should be the only child of root", 1, |
| 143 | activity.root.childCount) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 144 | assertEquals("Each item in the for loop should be a child", items.size, ll.childCount) |
| 145 | items.forEachIndexed { index, i -> |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 146 | assertEquals( |
| 147 | "Should be the correct child", "some text $i", |
| 148 | (ll.getChildAt(index) as TextView).text |
| 149 | ) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | fun testRecompose() { |
| 156 | val counter = Counter() |
| 157 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 158 | compose { |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 159 | RecomposeTestComponentsA( |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 160 | counter, |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 161 | ClickAction.Recompose |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 162 | ) |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 163 | }.then { activity -> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 164 | // everything got rendered once |
| 165 | assertEquals(1, counter["A"]) |
| 166 | assertEquals(1, counter["100"]) |
| 167 | assertEquals(1, counter["101"]) |
| 168 | assertEquals(1, counter["102"]) |
| 169 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 170 | (activity.findViewById(100) as TextView).performClick() |
| 171 | (activity.findViewById(102) as TextView).performClick() |
| 172 | |
| 173 | // nothing should happen synchronously |
| 174 | assertEquals(1, counter["A"]) |
| 175 | assertEquals(1, counter["100"]) |
| 176 | assertEquals(1, counter["101"]) |
| 177 | assertEquals(1, counter["102"]) |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 178 | }.then { activity -> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 179 | // only the clicked view got rerendered |
| 180 | assertEquals(1, counter["A"]) |
| 181 | assertEquals(2, counter["100"]) |
| 182 | assertEquals(1, counter["101"]) |
| 183 | assertEquals(2, counter["102"]) |
| 184 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 185 | // recompose() both the parent and the child... and show that the child only |
| 186 | // recomposes once as a result |
| 187 | (activity.findViewById(99) as LinearLayout).performClick() |
| 188 | (activity.findViewById(102) as TextView).performClick() |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 189 | }.then { |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 190 | |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 191 | assertEquals(2, counter["A"]) |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 192 | assertEquals(3, counter["100"]) |
| 193 | assertEquals(2, counter["101"]) |
Leland Richardson | f87e0e9 | 2019-01-08 19:09:03 -0800 | [diff] [blame] | 194 | assertEquals(3, counter["102"]) |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | @Test |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 199 | fun testRootRecompose() { |
| 200 | val counter = Counter() |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 201 | val trigger = Trigger() |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 202 | |
Leland Richardson | 6cab6e3 | 2019-05-02 11:52:13 -0700 | [diff] [blame] | 203 | val listener = |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 204 | ClickAction.PerformOnView { |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 205 | trigger.recompose() |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 206 | } |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 207 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 208 | compose { |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 209 | trigger.subscribe() |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 210 | RecomposeTestComponentsA( |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 211 | counter, |
| 212 | listener |
| 213 | ) |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 214 | }.then { activity -> |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 215 | // everything got rendered once |
| 216 | assertEquals(1, counter["A"]) |
| 217 | assertEquals(1, counter["100"]) |
| 218 | assertEquals(1, counter["101"]) |
| 219 | assertEquals(1, counter["102"]) |
| 220 | |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 221 | (activity.findViewById(100) as TextView).performClick() |
| 222 | (activity.findViewById(102) as TextView).performClick() |
| 223 | |
| 224 | // nothing should happen synchronously |
| 225 | assertEquals(1, counter["A"]) |
| 226 | assertEquals(1, counter["100"]) |
| 227 | assertEquals(1, counter["101"]) |
| 228 | assertEquals(1, counter["102"]) |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 229 | }.then { activity -> |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 230 | // as we recompose ROOT on every tap, everything should be increased once, because two |
| 231 | // clicks layed to one frame. None of these components are skippable, so each increments |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 232 | assertEquals(2, counter["A"]) |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 233 | assertEquals(2, counter["100"]) |
| 234 | assertEquals(2, counter["101"]) |
| 235 | assertEquals(2, counter["102"]) |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 236 | |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 237 | (activity.findViewById(99) as LinearLayout).performClick() |
| 238 | (activity.findViewById(102) as TextView).performClick() |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 239 | }.then { |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 240 | // again, no matter what we tapped, we want to recompose root, so all counts increased |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 241 | assertEquals(3, counter["A"]) |
Leland Richardson | 75f3ba9 | 2020-02-20 19:54:44 -0800 | [diff] [blame] | 242 | assertEquals(3, counter["100"]) |
| 243 | assertEquals(3, counter["101"]) |
| 244 | assertEquals(3, counter["102"]) |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 247 | |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 248 | // components for testing recompose behavior above |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 249 | sealed class ClickAction { |
| 250 | object Recompose : ClickAction() |
| 251 | class PerformOnView(val action: (View) -> Unit) : ClickAction() |
| 252 | } |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 253 | |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 254 | @Composable fun RecomposeTestComponentsB(counter: Counter, listener: ClickAction, id: Int = 0) { |
| 255 | counter.inc("$id") |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 256 | |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 257 | val recompose = invalidate |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 258 | |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 259 | TextView(id = id, onClickListener = View.OnClickListener { |
| 260 | @Suppress("DEPRECATION") |
| 261 | when (listener) { |
| 262 | is ClickAction.Recompose -> recompose() |
| 263 | is ClickAction.PerformOnView -> listener.action.invoke(it) |
| 264 | } |
| 265 | }) |
| 266 | } |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 267 | |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 268 | @Composable fun RecomposeTestComponentsA(counter: Counter, listener: ClickAction) { |
| 269 | counter.inc("A") |
| 270 | val recompose = invalidate |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 271 | LinearLayout(id = 99, onClickListener = View.OnClickListener { |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 272 | @Suppress("DEPRECATION") |
| 273 | when (listener) { |
| 274 | is ClickAction.Recompose -> recompose() |
| 275 | is ClickAction.PerformOnView -> listener.action.invoke(it) |
| 276 | } |
| 277 | }) { |
| 278 | for (id in 100..102) { |
| 279 | key(id) { |
Leland Richardson | 0085028 | 2020-03-04 14:40:37 -0800 | [diff] [blame] | 280 | RecomposeTestComponentsB( |
| 281 | counter, |
| 282 | listener, |
| 283 | id |
| 284 | ) |
Matvei Malkov | 3bb20c3 | 2019-01-21 16:59:16 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | @Test |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 291 | fun testCorrectViewTree() { |
| 292 | compose { |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 293 | LinearLayout { |
| 294 | LinearLayout { } |
| 295 | LinearLayout { } |
| 296 | } |
| 297 | LinearLayout { } |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 298 | }.then { activity -> |
| 299 | assertChildHierarchy(activity.root) { |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 300 | """ |
| 301 | <LinearLayout> |
| 302 | <LinearLayout /> |
| 303 | <LinearLayout /> |
| 304 | </LinearLayout> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 305 | <LinearLayout /> |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 306 | """ |
| 307 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | @Test |
| 312 | fun testCorrectViewTreeWithComponents() { |
| 313 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 314 | @Composable fun B() { |
Leland Richardson | 534a346 | 2020-02-05 23:26:02 -0800 | [diff] [blame] | 315 | TextView() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | compose { |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 319 | LinearLayout { |
| 320 | LinearLayout { |
| 321 | B() |
| 322 | } |
| 323 | LinearLayout { |
| 324 | B() |
| 325 | } |
| 326 | } |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 327 | }.then { activity -> |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 328 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 329 | assertChildHierarchy(activity.root) { |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 330 | """ |
| 331 | <LinearLayout> |
| 332 | <LinearLayout> |
| 333 | <TextView /> |
| 334 | </LinearLayout> |
| 335 | <LinearLayout> |
| 336 | <TextView /> |
| 337 | </LinearLayout> |
| 338 | </LinearLayout> |
| 339 | """ |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | @Test |
| 345 | fun testCorrectViewTreeWithComponentWithMultipleRoots() { |
| 346 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 347 | @Composable fun B() { |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 348 | TextView() |
| 349 | TextView() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | compose { |
Alexandre Elias | c69fc4a | 2020-02-20 17:50:33 -0800 | [diff] [blame] | 353 | LinearLayout { |
| 354 | LinearLayout { |
| 355 | B() |
| 356 | } |
| 357 | LinearLayout { |
| 358 | B() |
| 359 | } |
| 360 | } |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 361 | }.then { |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 362 | |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 363 | assertChildHierarchy(activity.root) { |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 364 | """ |
| 365 | <LinearLayout> |
| 366 | <LinearLayout> |
| 367 | <TextView /> |
| 368 | <TextView /> |
| 369 | </LinearLayout> |
| 370 | <LinearLayout> |
| 371 | <TextView /> |
| 372 | <TextView /> |
| 373 | </LinearLayout> |
| 374 | </LinearLayout> |
| 375 | """ |
| 376 | } |
| 377 | } |
| 378 | } |
Chuck Jazdzewski | 7e19aa5 | 2019-04-19 12:36:48 -0700 | [diff] [blame] | 379 | |
Chuck Jazdzewski | b15857c | 2020-05-26 17:14:02 -0700 | [diff] [blame] | 380 | @Test // regression b/157111271 |
| 381 | fun testInsertDuringRecomposition() { |
| 382 | var includeA by mutableStateOf(false) |
| 383 | var someState by mutableStateOf(0) |
| 384 | var someOtherState by mutableStateOf(1) |
| 385 | |
| 386 | @Composable fun B(@Suppress("UNUSED_PARAMETER") value: Int) { |
| 387 | // empty |
| 388 | } |
| 389 | |
| 390 | @Composable fun A() { |
| 391 | B(someState) |
| 392 | someState++ |
| 393 | } |
| 394 | |
| 395 | @Composable fun T() { |
| 396 | subCompose { |
| 397 | // Take up some slot space |
| 398 | // This makes it more likely to reproduce bug 157111271. |
| 399 | remember(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) { |
| 400 | 1 |
| 401 | } |
| 402 | if (includeA) { |
| 403 | Wrapper { |
| 404 | B(0) |
| 405 | B(someOtherState) |
| 406 | B(2) |
| 407 | B(3) |
| 408 | B(4) |
| 409 | A() |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | compose { |
| 416 | T() |
| 417 | }.then { |
| 418 | includeA = true |
| 419 | }.then { |
| 420 | someOtherState = 10 |
| 421 | }.then { |
| 422 | // force recompose |
| 423 | } |
| 424 | } |
| 425 | |
Chuck Jazdzewski | d023504592 | 2020-07-22 11:14:20 -0700 | [diff] [blame^] | 426 | @Test // regression test for b/161892016 |
| 427 | fun testMultipleRecompose() { |
| 428 | class A |
| 429 | |
| 430 | var state1 by mutableStateOf(1) |
| 431 | var state2 by mutableStateOf(1) |
| 432 | |
| 433 | @Composable fun validate(a: A?) { |
| 434 | assertNotNull(a) |
| 435 | } |
| 436 | |
| 437 | @Composable fun use(@Suppress("UNUSED_PARAMETER") i: Int) { } |
| 438 | |
| 439 | @Composable fun useA(a: A = A()) { |
| 440 | validate(a) |
| 441 | use(state2) |
| 442 | } |
| 443 | |
| 444 | @Composable fun test() { |
| 445 | use(state1) |
| 446 | useA() |
| 447 | } |
| 448 | |
| 449 | compose { |
| 450 | test() |
| 451 | }.then { |
| 452 | // Recompose test() skipping useA() |
| 453 | state1 = 2 |
| 454 | }.then { |
| 455 | // Recompose useA(). In the bug this causes validate() to be passed a null because |
| 456 | // the recompose scope is updated with a lambda that captures the parameters when the |
| 457 | // default parameter expressions are skipped. |
| 458 | state2 = 2 |
| 459 | }.then { |
| 460 | // force recompose to validate a. |
| 461 | } |
| 462 | } |
| 463 | |
Chuck Jazdzewski | 7e19aa5 | 2019-04-19 12:36:48 -0700 | [diff] [blame] | 464 | @Test |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 465 | @OptIn(ExperimentalComposeApi::class) |
Chuck Jazdzewski | 7e19aa5 | 2019-04-19 12:36:48 -0700 | [diff] [blame] | 466 | fun testFrameTransition() { |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 467 | var snapshotId: Int? = null |
Chuck Jazdzewski | 7e19aa5 | 2019-04-19 12:36:48 -0700 | [diff] [blame] | 468 | compose { |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 469 | snapshotId = currentSnapshot().id |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 470 | }.then { |
Chuck Jazdzewski | 0a90de9 | 2020-05-21 10:03:47 -0700 | [diff] [blame] | 471 | assertNotSame(snapshotId, currentSnapshot().id) |
Chuck Jazdzewski | 7e19aa5 | 2019-04-19 12:36:48 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
Chuck Jazdzewski | b15857c | 2020-05-26 17:14:02 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | @Composable |
| 477 | fun Wrapper(children: @Composable () -> Unit) { |
| 478 | children() |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | fun assertChildHierarchy(root: ViewGroup, getHierarchy: () -> String) { |
| 482 | val realHierarchy = printChildHierarchy(root) |
| 483 | |
Chuck Jazdzewski | fff2da2 | 2019-08-05 10:14:43 -0700 | [diff] [blame] | 484 | assertEquals( |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 485 | normalizeString(getHierarchy()), |
| 486 | realHierarchy.trim() |
| 487 | ) |
| 488 | } |
| 489 | |
| 490 | fun normalizeString(str: String): String { |
Chuck Jazdzewski | 9d160f3 | 2019-04-08 09:47:44 -0700 | [diff] [blame] | 491 | val lines = str.split('\n').dropWhile { it.isBlank() }.dropLastWhile { |
| 492 | it.isBlank() |
| 493 | } |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 494 | if (lines.isEmpty()) return "" |
| 495 | val toRemove = lines.first().takeWhile { it == ' ' }.length |
| 496 | return lines.joinToString("\n") { it.substring(Math.min(toRemove, it.length)) } |
| 497 | } |
| 498 | |
| 499 | fun printChildHierarchy(root: ViewGroup): String { |
| 500 | val sb = StringBuilder() |
| 501 | for (i in 0 until root.childCount) { |
| 502 | printView(root.getChildAt(i), 0, sb) |
| 503 | } |
| 504 | return sb.toString() |
| 505 | } |
| 506 | |
| 507 | fun printView(view: View, indent: Int, sb: StringBuilder) { |
| 508 | val whitespace = " ".repeat(indent) |
| 509 | val name = view.javaClass.simpleName |
| 510 | val attributes = printAttributes(view) |
| 511 | if (view is ViewGroup && view.childCount > 0) { |
Jim Sproch | a88c07a | 2020-06-25 13:00:03 -0700 | [diff] [blame] | 512 | sb.appendLine("$whitespace<$name$attributes>") |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 513 | for (i in 0 until view.childCount) { |
| 514 | printView(view.getChildAt(i), indent + 4, sb) |
| 515 | } |
Jim Sproch | a88c07a | 2020-06-25 13:00:03 -0700 | [diff] [blame] | 516 | sb.appendLine("$whitespace</$name>") |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 517 | } else { |
Jim Sproch | a88c07a | 2020-06-25 13:00:03 -0700 | [diff] [blame] | 518 | sb.appendLine("$whitespace<$name$attributes />") |
Chuck Jazdzewski | 724fffc | 2018-08-10 13:42:56 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | fun printAttributes(view: View): String { |
| 523 | val attrs = mutableListOf<String>() |
| 524 | |
| 525 | // NOTE: right now we only look for id and text as attributes to print out... but we are |
| 526 | // free to add more if it makes sense |
| 527 | if (view.id != -1) { |
| 528 | attrs.add("id=${view.id}") |
| 529 | } |
| 530 | |
| 531 | if (view is TextView && view.text.length > 0) { |
| 532 | attrs.add("text='${view.text}'") |
| 533 | } |
| 534 | |
| 535 | val result = attrs.joinToString(" ", prefix = " ") |
| 536 | if (result.length == 1) { |
| 537 | return "" |
| 538 | } |
| 539 | return result |
| 540 | } |
Leland Richardson | 76600e3 | 2020-01-15 17:22:29 -0800 | [diff] [blame] | 541 | |
| 542 | class Counter { |
| 543 | private var counts = mutableMapOf<String, Int>() |
| 544 | fun inc(key: String) = counts.getOrPut(key, { 0 }).let { counts[key] = it + 1 } |
| 545 | fun reset() { |
| 546 | counts = mutableMapOf() |
| 547 | } |
| 548 | |
| 549 | operator fun get(key: String) = counts[key] ?: 0 |
| 550 | } |