diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [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.tooling |
| 18 | |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 19 | import androidx.compose.InternalComposeApi |
| 20 | import androidx.compose.getValue |
| 21 | import androidx.compose.key |
| 22 | import androidx.compose.mutableStateOf |
| 23 | import androidx.compose.resetSourceInfo |
| 24 | import androidx.compose.setValue |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 25 | import androidx.test.filters.SmallTest |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 26 | import androidx.ui.core.Modifier |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 27 | import androidx.ui.core.WithConstraints |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 28 | import androidx.compose.foundation.Box |
| 29 | import androidx.compose.foundation.Text |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame^] | 30 | import androidx.compose.foundation.layout.Column |
| 31 | import androidx.compose.foundation.layout.padding |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 32 | import androidx.ui.unit.Density |
| 33 | import androidx.ui.unit.dp |
| 34 | import org.junit.Assert |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 35 | import org.junit.Before |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 36 | import org.junit.Test |
| 37 | import org.junit.runner.RunWith |
| 38 | import org.junit.runners.JUnit4 |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 39 | import java.util.concurrent.CountDownLatch |
| 40 | import java.util.concurrent.TimeUnit |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 41 | |
| 42 | @SmallTest |
| 43 | @RunWith(JUnit4::class) |
| 44 | class BoundsTest : ToolingTest() { |
| 45 | fun Group.all(): Collection<Group> = |
| 46 | listOf(this) + this.children.flatMap { it.all() } |
| 47 | |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 48 | @Before |
| 49 | fun reset() { |
| 50 | @OptIn(InternalComposeApi::class) |
| 51 | resetSourceInfo() |
| 52 | } |
| 53 | |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 54 | @Test |
| 55 | fun testBounds() { |
Diego Perez | 6db70a3 | 2020-05-06 13:53:30 +0100 | [diff] [blame] | 56 | val slotTableRecord = SlotTableRecord.create() |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 57 | show { |
Diego Perez | 6db70a3 | 2020-05-06 13:53:30 +0100 | [diff] [blame] | 58 | Inspectable(slotTableRecord) { |
Anastasia Soboleva | 875d957 | 2020-02-11 14:53:47 +0000 | [diff] [blame] | 59 | Box { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 60 | Column(Modifier.padding(10.dp)) { |
Louis Pullen-Freilich | 99dc99a | 2020-04-28 00:05:59 +0100 | [diff] [blame] | 61 | Text("Hello", Modifier.padding(5.dp)) |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | activityTestRule.runOnUiThread { |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 68 | val tree = slotTableRecord.store.first().asTree() |
| 69 | val boundingBoxes = tree.firstOrNull { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 70 | it.position?.contains("BoundsTest.kt") == true && it.box.right > 0 |
Leland Richardson | 797bd0cd | 2020-05-07 15:16:51 -0700 | [diff] [blame] | 71 | }!! |
| 72 | .all() |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 73 | .filter { |
| 74 | val name = it.position |
| 75 | name != null && name.contains("BoundsTest.kt") |
| 76 | } |
| 77 | .map { |
| 78 | it.box.left |
| 79 | } |
| 80 | .distinct() |
| 81 | .sorted() |
| 82 | .toTypedArray() |
| 83 | |
| 84 | with(Density(activityTestRule.activity)) { |
| 85 | println(boundingBoxes.contentDeepToString()) |
| 86 | Assert.assertArrayEquals( |
| 87 | arrayOf( |
| 88 | 0.dp.toIntPx(), // Root |
Matvei Malkov | 2b055a7 | 2020-06-12 17:26:17 +0100 | [diff] [blame] | 89 | 10.dp.toIntPx(), // Column |
| 90 | 15.dp.toIntPx()), // Text |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 91 | boundingBoxes |
| 92 | ) |
| 93 | } |
| 94 | } |
| 95 | } |
Chuck Jazdzewski | 4fab4b9 | 2020-06-15 09:51:37 -0700 | [diff] [blame] | 96 | |
| 97 | @Test |
| 98 | fun testBoundWithConstraints() { |
| 99 | val slotTableRecord = SlotTableRecord.create() |
| 100 | show { |
| 101 | Inspectable(slotTableRecord) { |
| 102 | WithConstraints { |
| 103 | Column { |
| 104 | Box { |
| 105 | Text("Hello") |
| 106 | } |
| 107 | Box { |
| 108 | Text("Hello") |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | activityTestRule.runOnUiThread { |
| 116 | val store = slotTableRecord.store |
| 117 | Assert.assertTrue(store.size > 1) |
| 118 | val trees = slotTableRecord.store.map { it.asTree() } |
| 119 | val boundingBoxes = trees.map { |
| 120 | it.all().filter { |
| 121 | it.box.right > 0 && it.key.let { |
| 122 | it is String && it.contains("BoundsTest.kt") |
| 123 | } |
| 124 | } |
| 125 | }.flatten().groupBy { it.key } |
| 126 | |
| 127 | Assert.assertTrue(boundingBoxes.size >= 6) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | fun testDisposeWithComposeTables() { |
| 133 | val slotTableRecord = SlotTableRecord.create() |
| 134 | var value by mutableStateOf(0) |
| 135 | var latch = CountDownLatch(1) |
| 136 | show { |
| 137 | Inspectable(slotTableRecord) { |
| 138 | key(value) { |
| 139 | WithConstraints { |
| 140 | Text("Hello") |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | latch.countDown() |
| 145 | } |
| 146 | |
| 147 | activityTestRule.runOnUiThread { |
| 148 | latch = CountDownLatch(1) |
| 149 | value = 1 |
| 150 | } |
| 151 | latch.await(1, TimeUnit.SECONDS) |
| 152 | |
| 153 | activityTestRule.runOnUiThread { |
| 154 | latch = CountDownLatch(1) |
| 155 | value = 2 |
| 156 | } |
| 157 | latch.await(1, TimeUnit.SECONDS) |
| 158 | |
| 159 | Assert.assertTrue(slotTableRecord.store.size < 3) |
| 160 | } |
diegoperez | 1d18640 | 2020-01-27 14:21:05 +0000 | [diff] [blame] | 161 | } |