Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 1 | /* |
| 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-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 17 | package androidx.compose.ui.text |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 18 | |
| 19 | import androidx.ui.core.Constraints |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 20 | import androidx.ui.core.LayoutDirection |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 21 | import androidx.compose.ui.text.font.Font |
| 22 | import androidx.compose.ui.text.style.TextOverflow |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 23 | import androidx.ui.unit.Density |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 24 | import androidx.ui.unit.IntSize |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 25 | import androidx.ui.unit.em |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 26 | import com.google.common.truth.Truth.assertThat |
| 27 | import com.nhaarman.mockitokotlin2.mock |
| 28 | import org.junit.Before |
| 29 | import org.junit.Test |
| 30 | import org.junit.runner.RunWith |
| 31 | import org.junit.runners.JUnit4 |
| 32 | |
| 33 | @RunWith(JUnit4::class) |
| 34 | class 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(), |
haoyu | ec58647 | 2020-03-31 18:04:39 -0700 | [diff] [blame] | 48 | placeholders = listOf(), |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 49 | maxLines = 1, |
| 50 | softWrap = true, |
| 51 | overflow = TextOverflow.Ellipsis, |
| 52 | density = Density(1.0f), |
| 53 | layoutDirection = LayoutDirection.Ltr, |
| 54 | resourceLoader = resourceLoader, |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 55 | constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 56 | ), |
| 57 | multiParagraph = mock(), |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 58 | size = IntSize(50, 50) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 59 | ) |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | fun testCanResue_same() { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 64 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 65 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 80 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 81 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 96 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 97 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 112 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 113 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 128 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 129 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 144 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 145 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 160 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 161 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 176 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 177 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 192 | val constraints = Constraints.fixedWidth(100) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 193 | 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 Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 217 | constraints = Constraints.fixedWidth(200) |
Seigo Nonaka | 7006675 | 2019-12-09 19:43:52 -0800 | [diff] [blame] | 218 | )).isFalse() |
| 219 | } |
| 220 | } |