Neda Topoljanac | cbc331f | 2023-03-13 17:20:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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.wear.protolayout.material; |
| 18 | |
| 19 | import static androidx.wear.protolayout.ColorBuilders.argb; |
| 20 | import static androidx.wear.protolayout.LayoutElementBuilders.FONT_WEIGHT_BOLD; |
| 21 | import static androidx.wear.protolayout.LayoutElementBuilders.FONT_WEIGHT_MEDIUM; |
| 22 | import static androidx.wear.protolayout.LayoutElementBuilders.FONT_WEIGHT_NORMAL; |
| 23 | import static androidx.wear.protolayout.LayoutElementBuilders.TEXT_ALIGN_END; |
| 24 | import static androidx.wear.protolayout.LayoutElementBuilders.TEXT_OVERFLOW_ELLIPSIZE_END; |
| 25 | import static androidx.wear.protolayout.material.Typography.TYPOGRAPHY_BODY1; |
| 26 | import static androidx.wear.protolayout.material.Typography.TYPOGRAPHY_CAPTION2; |
| 27 | import static androidx.wear.protolayout.material.Typography.TYPOGRAPHY_TITLE1; |
| 28 | import static androidx.wear.protolayout.material.Typography.getFontStyleBuilder; |
| 29 | import static androidx.wear.protolayout.material.Typography.getLineHeightForTypography; |
| 30 | |
| 31 | import static com.google.common.truth.Truth.assertThat; |
| 32 | |
| 33 | import static org.junit.Assert.assertThrows; |
| 34 | |
| 35 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 36 | |
| 37 | import android.content.Context; |
| 38 | import android.graphics.Color; |
| 39 | |
| 40 | import androidx.test.core.app.ApplicationProvider; |
| 41 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 42 | import androidx.wear.protolayout.LayoutElementBuilders.Box; |
| 43 | import androidx.wear.protolayout.LayoutElementBuilders.Column; |
| 44 | import androidx.wear.protolayout.LayoutElementBuilders.FontStyle; |
| 45 | import androidx.wear.protolayout.ModifiersBuilders.Background; |
| 46 | import androidx.wear.protolayout.ModifiersBuilders.ElementMetadata; |
| 47 | import androidx.wear.protolayout.ModifiersBuilders.Modifiers; |
Neda Topoljanac | 54f0469 | 2023-03-23 17:40:59 +0000 | [diff] [blame] | 48 | import androidx.wear.protolayout.expression.ProtoLayoutExperimental; |
Neda Topoljanac | cbc331f | 2023-03-13 17:20:52 +0000 | [diff] [blame] | 49 | |
| 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
| 52 | import org.robolectric.annotation.internal.DoNotInstrument; |
| 53 | |
| 54 | @RunWith(AndroidJUnit4.class) |
| 55 | @DoNotInstrument |
| 56 | public class TextTest { |
| 57 | |
| 58 | public static final int NUM_OF_FONT_STYLE_CONST = 12; |
| 59 | private static final Context CONTEXT = ApplicationProvider.getApplicationContext(); |
| 60 | |
| 61 | @Test |
| 62 | public void testTypography_incorrectTypography_negativeValue() { |
| 63 | assertThrows(IllegalArgumentException.class, () -> getFontStyleBuilder(-1, CONTEXT)); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testTypography_incorrectTypography_positiveValue() { |
| 68 | assertThrows( |
| 69 | IllegalArgumentException.class, |
| 70 | () -> getFontStyleBuilder(NUM_OF_FONT_STYLE_CONST + 1, CONTEXT)); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testLineHeight_incorrectTypography_negativeValue() { |
| 75 | assertThrows(IllegalArgumentException.class, () -> getLineHeightForTypography(-1)); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testLineHeight_incorrectTypography_positiveValue() { |
| 80 | assertThrows( |
| 81 | IllegalArgumentException.class, |
| 82 | () -> getLineHeightForTypography(NUM_OF_FONT_STYLE_CONST + 1)); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testTypography_body1() { |
| 87 | FontStyle fontStyle = getFontStyleBuilder(TYPOGRAPHY_BODY1, CONTEXT).build(); |
| 88 | assertFontStyle(fontStyle, 16, FONT_WEIGHT_NORMAL, 0.01f, 20, TYPOGRAPHY_BODY1); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void testTypography_caption2() { |
| 93 | FontStyle fontStyle = getFontStyleBuilder(TYPOGRAPHY_CAPTION2, CONTEXT).build(); |
| 94 | assertFontStyle(fontStyle, 12, FONT_WEIGHT_MEDIUM, 0.01f, 16, TYPOGRAPHY_CAPTION2); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testWrongElement() { |
| 99 | Column box = new Column.Builder().build(); |
| 100 | |
| 101 | assertThat(Text.fromLayoutElement(box)).isNull(); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testWrongBox() { |
| 106 | Box box = new Box.Builder().build(); |
| 107 | |
| 108 | assertThat(Text.fromLayoutElement(box)).isNull(); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testWrongTag() { |
| 113 | Box box = |
| 114 | new Box.Builder() |
Neda Topoljanac | 54f0469 | 2023-03-23 17:40:59 +0000 | [diff] [blame] | 115 | .setModifiers( |
| 116 | new Modifiers.Builder() |
| 117 | .setMetadata( |
| 118 | new ElementMetadata.Builder() |
| 119 | .setTagData("test".getBytes(UTF_8)) |
| 120 | .build()) |
| 121 | .build()) |
| 122 | .build(); |
Neda Topoljanac | cbc331f | 2023-03-13 17:20:52 +0000 | [diff] [blame] | 123 | |
| 124 | assertThat(Text.fromLayoutElement(box)).isNull(); |
| 125 | } |
| 126 | |
| 127 | @Test |
Neda Topoljanac | 54f0469 | 2023-03-23 17:40:59 +0000 | [diff] [blame] | 128 | @ProtoLayoutExperimental |
Neda Topoljanac | cbc331f | 2023-03-13 17:20:52 +0000 | [diff] [blame] | 129 | public void testText() { |
| 130 | String textContent = "Testing text."; |
| 131 | Modifiers modifiers = |
| 132 | new Modifiers.Builder() |
| 133 | .setBackground(new Background.Builder().setColor(argb(Color.BLUE)).build()) |
| 134 | .build(); |
| 135 | int color = Color.YELLOW; |
| 136 | Text text = |
| 137 | new Text.Builder(CONTEXT, textContent) |
| 138 | .setItalic(true) |
| 139 | .setColor(argb(color)) |
| 140 | .setTypography(TYPOGRAPHY_TITLE1) |
| 141 | .setUnderline(true) |
| 142 | .setMaxLines(2) |
| 143 | .setModifiers(modifiers) |
| 144 | .setOverflow(TEXT_OVERFLOW_ELLIPSIZE_END) |
| 145 | .setMultilineAlignment(TEXT_ALIGN_END) |
| 146 | .setWeight(FONT_WEIGHT_BOLD) |
Neda Topoljanac | 54f0469 | 2023-03-23 17:40:59 +0000 | [diff] [blame] | 147 | .setExcludeFontPadding(true) |
Neda Topoljanac | cbc331f | 2023-03-13 17:20:52 +0000 | [diff] [blame] | 148 | .build(); |
| 149 | |
| 150 | FontStyle expectedFontStyle = |
| 151 | getFontStyleBuilder(TYPOGRAPHY_TITLE1, CONTEXT) |
| 152 | .setItalic(true) |
| 153 | .setUnderline(true) |
| 154 | .setColor(argb(color)) |
| 155 | .setWeight(FONT_WEIGHT_BOLD) |
| 156 | .build(); |
| 157 | |
| 158 | assertTextIsEqual(text, textContent, modifiers, color, expectedFontStyle); |
| 159 | |
| 160 | Box box = new Box.Builder().addContent(text).build(); |
| 161 | Text newText = Text.fromLayoutElement(box.getContents().get(0)); |
| 162 | assertThat(newText).isNotNull(); |
| 163 | assertTextIsEqual(newText, textContent, modifiers, color, expectedFontStyle); |
| 164 | |
| 165 | assertThat(Text.fromLayoutElement(text)).isEqualTo(text); |
| 166 | } |
| 167 | |
Neda Topoljanac | 54f0469 | 2023-03-23 17:40:59 +0000 | [diff] [blame] | 168 | @ProtoLayoutExperimental |
Neda Topoljanac | cbc331f | 2023-03-13 17:20:52 +0000 | [diff] [blame] | 169 | private void assertTextIsEqual( |
| 170 | Text actualText, |
| 171 | String expectedTextContent, |
| 172 | Modifiers expectedModifiers, |
| 173 | int expectedColor, |
| 174 | FontStyle expectedFontStyle) { |
| 175 | assertThat(actualText.getFontStyle().toProto()).isEqualTo(expectedFontStyle.toProto()); |
| 176 | assertThat(actualText.getText()).isEqualTo(expectedTextContent); |
| 177 | assertThat(actualText.getColor().getArgb()).isEqualTo(expectedColor); |
| 178 | assertThat(actualText.getOverflow()).isEqualTo(TEXT_OVERFLOW_ELLIPSIZE_END); |
| 179 | assertThat(actualText.getMultilineAlignment()).isEqualTo(TEXT_ALIGN_END); |
| 180 | assertThat(actualText.getMaxLines()).isEqualTo(2); |
| 181 | assertThat(actualText.getLineHeight()) |
| 182 | .isEqualTo(getLineHeightForTypography(TYPOGRAPHY_TITLE1).getValue()); |
Neda Topoljanac | 54f0469 | 2023-03-23 17:40:59 +0000 | [diff] [blame] | 183 | assertThat(actualText.getExcludeFontPadding()).isTrue(); |
Neda Topoljanac | cbc331f | 2023-03-13 17:20:52 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | private void assertFontStyle( |
| 187 | FontStyle actualFontStyle, |
| 188 | int expectedSize, |
| 189 | int expectedWeight, |
| 190 | float expectedLetterSpacing, |
| 191 | float expectedLineHeight, |
| 192 | int typographyCode) { |
| 193 | assertThat(actualFontStyle.getSize()).isNotNull(); |
| 194 | assertThat(actualFontStyle.getWeight()).isNotNull(); |
| 195 | assertThat(actualFontStyle.getLetterSpacing()).isNotNull(); |
| 196 | assertThat(actualFontStyle.getSize().getValue()).isEqualTo(expectedSize); |
| 197 | assertThat(actualFontStyle.getWeight().getValue()).isEqualTo(expectedWeight); |
| 198 | assertThat(actualFontStyle.getLetterSpacing().getValue()).isEqualTo(expectedLetterSpacing); |
| 199 | assertThat(getLineHeightForTypography(typographyCode).getValue()) |
| 200 | .isEqualTo(expectedLineHeight); |
| 201 | } |
| 202 | } |