[go: nahoru, domu]

blob: f488a788d161ca3d443e2d2f34dc9c8edc293cc7 [file] [log] [blame]
diegoperez1d186402020-01-27 14:21:05 +00001/*
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
17package androidx.ui.tooling
18
Chuck Jazdzewski4fab4b92020-06-15 09:51:37 -070019import androidx.compose.InternalComposeApi
20import androidx.compose.getValue
21import androidx.compose.key
22import androidx.compose.mutableStateOf
23import androidx.compose.resetSourceInfo
24import androidx.compose.setValue
diegoperez1d186402020-01-27 14:21:05 +000025import androidx.test.filters.SmallTest
Adam Powell999a89b2020-03-11 09:08:07 -070026import androidx.ui.core.Modifier
Chuck Jazdzewski4fab4b92020-06-15 09:51:37 -070027import androidx.ui.core.WithConstraints
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010028import androidx.compose.foundation.Box
29import androidx.compose.foundation.Text
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010030import androidx.compose.foundation.layout.Column
31import androidx.compose.foundation.layout.padding
diegoperez1d186402020-01-27 14:21:05 +000032import androidx.ui.unit.Density
33import androidx.ui.unit.dp
34import org.junit.Assert
Chuck Jazdzewski4fab4b92020-06-15 09:51:37 -070035import org.junit.Before
diegoperez1d186402020-01-27 14:21:05 +000036import org.junit.Test
37import org.junit.runner.RunWith
38import org.junit.runners.JUnit4
Chuck Jazdzewski4fab4b92020-06-15 09:51:37 -070039import java.util.concurrent.CountDownLatch
40import java.util.concurrent.TimeUnit
diegoperez1d186402020-01-27 14:21:05 +000041
42@SmallTest
43@RunWith(JUnit4::class)
44class BoundsTest : ToolingTest() {
45 fun Group.all(): Collection<Group> =
46 listOf(this) + this.children.flatMap { it.all() }
47
Chuck Jazdzewski4fab4b92020-06-15 09:51:37 -070048 @Before
49 fun reset() {
50 @OptIn(InternalComposeApi::class)
51 resetSourceInfo()
52 }
53
diegoperez1d186402020-01-27 14:21:05 +000054 @Test
55 fun testBounds() {
Diego Perez6db70a32020-05-06 13:53:30 +010056 val slotTableRecord = SlotTableRecord.create()
diegoperez1d186402020-01-27 14:21:05 +000057 show {
Diego Perez6db70a32020-05-06 13:53:30 +010058 Inspectable(slotTableRecord) {
Anastasia Soboleva875d9572020-02-11 14:53:47 +000059 Box {
Adam Powell999a89b2020-03-11 09:08:07 -070060 Column(Modifier.padding(10.dp)) {
Louis Pullen-Freilich99dc99a2020-04-28 00:05:59 +010061 Text("Hello", Modifier.padding(5.dp))
diegoperez1d186402020-01-27 14:21:05 +000062 }
63 }
64 }
65 }
66
67 activityTestRule.runOnUiThread {
Leland Richardson797bd0cd2020-05-07 15:16:51 -070068 val tree = slotTableRecord.store.first().asTree()
69 val boundingBoxes = tree.firstOrNull {
George Mount8f237572020-04-30 12:08:30 -070070 it.position?.contains("BoundsTest.kt") == true && it.box.right > 0
Leland Richardson797bd0cd2020-05-07 15:16:51 -070071 }!!
72 .all()
diegoperez1d186402020-01-27 14:21:05 +000073 .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 Malkov2b055a72020-06-12 17:26:17 +010089 10.dp.toIntPx(), // Column
90 15.dp.toIntPx()), // Text
diegoperez1d186402020-01-27 14:21:05 +000091 boundingBoxes
92 )
93 }
94 }
95 }
Chuck Jazdzewski4fab4b92020-06-15 09:51:37 -070096
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 }
diegoperez1d186402020-01-27 14:21:05 +0000161}