blob: a63dd6976cac29d92233adcabfcce1c871b96d0d [file] [log] [blame]
Neda Topoljanaccbc331f2023-03-13 17:20:52 +00001/*
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
17package androidx.wear.protolayout.material;
18
19import static androidx.wear.protolayout.ColorBuilders.argb;
20import static androidx.wear.protolayout.LayoutElementBuilders.FONT_WEIGHT_BOLD;
21import static androidx.wear.protolayout.LayoutElementBuilders.FONT_WEIGHT_MEDIUM;
22import static androidx.wear.protolayout.LayoutElementBuilders.FONT_WEIGHT_NORMAL;
23import static androidx.wear.protolayout.LayoutElementBuilders.TEXT_ALIGN_END;
24import static androidx.wear.protolayout.LayoutElementBuilders.TEXT_OVERFLOW_ELLIPSIZE_END;
25import static androidx.wear.protolayout.material.Typography.TYPOGRAPHY_BODY1;
26import static androidx.wear.protolayout.material.Typography.TYPOGRAPHY_CAPTION2;
27import static androidx.wear.protolayout.material.Typography.TYPOGRAPHY_TITLE1;
28import static androidx.wear.protolayout.material.Typography.getFontStyleBuilder;
29import static androidx.wear.protolayout.material.Typography.getLineHeightForTypography;
30
31import static com.google.common.truth.Truth.assertThat;
32
33import static org.junit.Assert.assertThrows;
34
35import static java.nio.charset.StandardCharsets.UTF_8;
36
37import android.content.Context;
38import android.graphics.Color;
39
40import androidx.test.core.app.ApplicationProvider;
41import androidx.test.ext.junit.runners.AndroidJUnit4;
42import androidx.wear.protolayout.LayoutElementBuilders.Box;
43import androidx.wear.protolayout.LayoutElementBuilders.Column;
44import androidx.wear.protolayout.LayoutElementBuilders.FontStyle;
45import androidx.wear.protolayout.ModifiersBuilders.Background;
46import androidx.wear.protolayout.ModifiersBuilders.ElementMetadata;
47import androidx.wear.protolayout.ModifiersBuilders.Modifiers;
Neda Topoljanac54f04692023-03-23 17:40:59 +000048import androidx.wear.protolayout.expression.ProtoLayoutExperimental;
Neda Topoljanaccbc331f2023-03-13 17:20:52 +000049
50import org.junit.Test;
51import org.junit.runner.RunWith;
52import org.robolectric.annotation.internal.DoNotInstrument;
53
54@RunWith(AndroidJUnit4.class)
55@DoNotInstrument
56public 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 Topoljanac54f04692023-03-23 17:40:59 +0000115 .setModifiers(
116 new Modifiers.Builder()
117 .setMetadata(
118 new ElementMetadata.Builder()
119 .setTagData("test".getBytes(UTF_8))
120 .build())
121 .build())
122 .build();
Neda Topoljanaccbc331f2023-03-13 17:20:52 +0000123
124 assertThat(Text.fromLayoutElement(box)).isNull();
125 }
126
127 @Test
Neda Topoljanac54f04692023-03-23 17:40:59 +0000128 @ProtoLayoutExperimental
Neda Topoljanaccbc331f2023-03-13 17:20:52 +0000129 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 Topoljanac54f04692023-03-23 17:40:59 +0000147 .setExcludeFontPadding(true)
Neda Topoljanaccbc331f2023-03-13 17:20:52 +0000148 .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 Topoljanac54f04692023-03-23 17:40:59 +0000168 @ProtoLayoutExperimental
Neda Topoljanaccbc331f2023-03-13 17:20:52 +0000169 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 Topoljanac54f04692023-03-23 17:40:59 +0000183 assertThat(actualText.getExcludeFontPadding()).isTrue();
Neda Topoljanaccbc331f2023-03-13 17:20:52 +0000184 }
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}