[go: nahoru, domu]

blob: 501a6a9656d9755897481b3a3de216eb17698b7d [file] [log] [blame]
Seigo Nonaka70066752019-12-09 19:43:52 -08001/*
2 * Copyright 2019 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
Louis Pullen-Freilichab194752020-07-21 22:21:22 +010017package androidx.compose.ui.text
Seigo Nonaka70066752019-12-09 19:43:52 -080018
19import androidx.ui.core.Constraints
Seigo Nonaka70066752019-12-09 19:43:52 -080020import androidx.ui.core.LayoutDirection
Louis Pullen-Freilichab194752020-07-21 22:21:22 +010021import androidx.compose.ui.text.font.Font
22import androidx.compose.ui.text.style.TextOverflow
George Mount842c8c12020-01-08 16:03:42 -080023import androidx.ui.unit.Density
George Mount8f237572020-04-30 12:08:30 -070024import androidx.ui.unit.IntSize
George Mount842c8c12020-01-08 16:03:42 -080025import androidx.ui.unit.em
Seigo Nonaka70066752019-12-09 19:43:52 -080026import com.google.common.truth.Truth.assertThat
27import com.nhaarman.mockitokotlin2.mock
28import org.junit.Before
29import org.junit.Test
30import org.junit.runner.RunWith
31import org.junit.runners.JUnit4
32
33@RunWith(JUnit4::class)
34class TextLayoutHelperTest {
35
36 lateinit var resourceLoader: Font.ResourceLoader
37
38 lateinit var referenceResult: TextLayoutResult
39
40 @Before
41 fun setUp() {
42 resourceLoader = mock()
43
44 referenceResult = TextLayoutResult(
45 TextLayoutInput(
46 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
47 style = TextStyle(),
haoyuec586472020-03-31 18:04:39 -070048 placeholders = listOf(),
Seigo Nonaka70066752019-12-09 19:43:52 -080049 maxLines = 1,
50 softWrap = true,
51 overflow = TextOverflow.Ellipsis,
52 density = Density(1.0f),
53 layoutDirection = LayoutDirection.Ltr,
54 resourceLoader = resourceLoader,
George Mount8f237572020-04-30 12:08:30 -070055 constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -080056 ),
57 multiParagraph = mock(),
George Mount8f237572020-04-30 12:08:30 -070058 size = IntSize(50, 50)
Seigo Nonaka70066752019-12-09 19:43:52 -080059 )
60 }
61
62 @Test
63 fun testCanResue_same() {
George Mount8f237572020-04-30 12:08:30 -070064 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -080065 assertThat(referenceResult.canReuse(
66 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
67 style = TextStyle(),
68 maxLines = 1,
69 softWrap = true,
70 overflow = TextOverflow.Ellipsis,
71 density = Density(1.0f),
72 layoutDirection = LayoutDirection.Ltr,
73 resourceLoader = resourceLoader,
74 constraints = constraints
75 )).isTrue()
76 }
77
78 @Test
79 fun testCanResue_different_text() {
George Mount8f237572020-04-30 12:08:30 -070080 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -080081 assertThat(referenceResult.canReuse(
82 text = AnnotatedString.Builder("Hello, Android").toAnnotatedString(),
83 style = TextStyle(),
84 maxLines = 1,
85 softWrap = true,
86 overflow = TextOverflow.Ellipsis,
87 density = Density(1.0f),
88 layoutDirection = LayoutDirection.Ltr,
89 resourceLoader = resourceLoader,
90 constraints = constraints
91 )).isFalse()
92 }
93
94 @Test
95 fun testCanResue_different_style() {
George Mount8f237572020-04-30 12:08:30 -070096 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -080097 assertThat(referenceResult.canReuse(
98 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
99 style = TextStyle(fontSize = 1.5.em),
100 maxLines = 1,
101 softWrap = true,
102 overflow = TextOverflow.Ellipsis,
103 density = Density(1.0f),
104 layoutDirection = LayoutDirection.Ltr,
105 resourceLoader = resourceLoader,
106 constraints = constraints
107 )).isFalse()
108 }
109
110 @Test
111 fun testCanResue_different_maxLines() {
George Mount8f237572020-04-30 12:08:30 -0700112 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -0800113 assertThat(referenceResult.canReuse(
114 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
115 style = TextStyle(),
116 maxLines = 2,
117 softWrap = true,
118 overflow = TextOverflow.Ellipsis,
119 density = Density(1.0f),
120 layoutDirection = LayoutDirection.Ltr,
121 resourceLoader = resourceLoader,
122 constraints = constraints
123 )).isFalse()
124 }
125
126 @Test
127 fun testCanResue_different_softWrap() {
George Mount8f237572020-04-30 12:08:30 -0700128 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -0800129 assertThat(referenceResult.canReuse(
130 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
131 style = TextStyle(),
132 maxLines = 1,
133 softWrap = false,
134 overflow = TextOverflow.Ellipsis,
135 density = Density(1.0f),
136 layoutDirection = LayoutDirection.Ltr,
137 resourceLoader = resourceLoader,
138 constraints = constraints
139 )).isFalse()
140 }
141
142 @Test
143 fun testCanResue_different_overflow() {
George Mount8f237572020-04-30 12:08:30 -0700144 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -0800145 assertThat(referenceResult.canReuse(
146 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
147 style = TextStyle(),
148 maxLines = 1,
149 softWrap = true,
150 overflow = TextOverflow.Clip,
151 density = Density(1.0f),
152 layoutDirection = LayoutDirection.Ltr,
153 resourceLoader = resourceLoader,
154 constraints = constraints
155 )).isFalse()
156 }
157
158 @Test
159 fun testCanResue_different_density() {
George Mount8f237572020-04-30 12:08:30 -0700160 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -0800161 assertThat(referenceResult.canReuse(
162 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
163 style = TextStyle(),
164 maxLines = 1,
165 softWrap = true,
166 overflow = TextOverflow.Ellipsis,
167 density = Density(2.0f),
168 layoutDirection = LayoutDirection.Ltr,
169 resourceLoader = resourceLoader,
170 constraints = constraints
171 )).isFalse()
172 }
173
174 @Test
175 fun testCanResue_different_layoutDirection() {
George Mount8f237572020-04-30 12:08:30 -0700176 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -0800177 assertThat(referenceResult.canReuse(
178 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
179 style = TextStyle(),
180 maxLines = 1,
181 softWrap = true,
182 overflow = TextOverflow.Ellipsis,
183 density = Density(1.0f),
184 layoutDirection = LayoutDirection.Rtl,
185 resourceLoader = resourceLoader,
186 constraints = constraints
187 )).isFalse()
188 }
189
190 @Test
191 fun testCanResue_different_resourceLoader() {
George Mount8f237572020-04-30 12:08:30 -0700192 val constraints = Constraints.fixedWidth(100)
Seigo Nonaka70066752019-12-09 19:43:52 -0800193 assertThat(referenceResult.canReuse(
194 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
195 style = TextStyle(),
196 maxLines = 1,
197 softWrap = true,
198 overflow = TextOverflow.Ellipsis,
199 density = Density(1.0f),
200 layoutDirection = LayoutDirection.Ltr,
201 resourceLoader = mock(),
202 constraints = constraints
203 )).isFalse()
204 }
205
206 @Test
207 fun testCanResue_different_constraints() {
208 assertThat(referenceResult.canReuse(
209 text = AnnotatedString.Builder("Hello, World").toAnnotatedString(),
210 style = TextStyle(),
211 maxLines = 1,
212 softWrap = true,
213 overflow = TextOverflow.Ellipsis,
214 density = Density(1.0f),
215 layoutDirection = LayoutDirection.Ltr,
216 resourceLoader = resourceLoader,
George Mount8f237572020-04-30 12:08:30 -0700217 constraints = Constraints.fixedWidth(200)
Seigo Nonaka70066752019-12-09 19:43:52 -0800218 )).isFalse()
219 }
220}