blob: f6300ee209859479ce4e035a558f564c943ced3c [file] [log] [blame]
Seigo Nonakaf1644f72017-11-27 22:09:49 -08001/*
2 * Copyright (C) 2017 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 android.text;
18
19import static org.junit.Assert.assertEquals;
Seigo Nonaka6f1868b2017-12-01 16:24:19 -080020import static org.junit.Assert.assertNotEquals;
Seigo Nonakaf1644f72017-11-27 22:09:49 -080021import static org.junit.Assert.assertNotNull;
22
23import android.content.Context;
24import android.graphics.Typeface;
25import android.support.test.InstrumentationRegistry;
26import android.support.test.filters.SmallTest;
27import android.support.test.runner.AndroidJUnit4;
28
29import org.junit.Test;
30import org.junit.runner.RunWith;
31
32@SmallTest
33@RunWith(AndroidJUnit4.class)
Seigo Nonaka9d3bd082018-01-11 10:02:12 -080034public class MeasuredParagraphTest {
Seigo Nonakaf1644f72017-11-27 22:09:49 -080035 private static final TextDirectionHeuristic LTR = TextDirectionHeuristics.LTR;
36 private static final TextDirectionHeuristic RTL = TextDirectionHeuristics.RTL;
37
38 private static final TextPaint PAINT = new TextPaint();
39 static {
40 // The test font has following coverage and width.
41 // U+0020: 10em
42 // U+002E (.): 10em
43 // U+0043 (C): 100em
44 // U+0049 (I): 1em
45 // U+004C (L): 50em
46 // U+0056 (V): 5em
47 // U+0058 (X): 10em
48 // U+005F (_): 0em
49 // U+FFFD (invalid surrogate will be replaced to this): 7em
50 // U+10331 (\uD800\uDF31): 10em
51 Context context = InstrumentationRegistry.getTargetContext();
52 PAINT.setTypeface(Typeface.createFromAsset(context.getAssets(),
53 "fonts/StaticLayoutLineBreakingTestFont.ttf"));
54 PAINT.setTextSize(1.0f); // Make 1em == 1px.
55 }
56
57 private String charsToString(char[] chars) {
58 return (new StringBuilder()).append(chars).toString();
59 }
60
61 @Test
62 public void buildForBidi() {
Seigo Nonaka9d3bd082018-01-11 10:02:12 -080063 MeasuredParagraph mt = null;
Seigo Nonakaf1644f72017-11-27 22:09:49 -080064
Seigo Nonaka9d3bd082018-01-11 10:02:12 -080065 mt = MeasuredParagraph.buildForBidi("XXX", 0, 3, LTR, null);
Seigo Nonakaf1644f72017-11-27 22:09:49 -080066 assertNotNull(mt);
67 assertNotNull(mt.getChars());
68 assertEquals("XXX", charsToString(mt.getChars()));
69 assertEquals(Layout.DIR_LEFT_TO_RIGHT, mt.getParagraphDir());
70 assertNotNull(mt.getDirections(0, 3));
71 assertEquals(0, mt.getWholeWidth(), 0);
72 assertEquals(0, mt.getWidths().size());
73 assertEquals(0, mt.getSpanEndCache().size());
74 assertEquals(0, mt.getFontMetrics().size());
Seigo Nonaka6f1868b2017-12-01 16:24:19 -080075 assertEquals(0, mt.getNativePtr());
Seigo Nonakaf1644f72017-11-27 22:09:49 -080076
77 // Recycle it
Seigo Nonaka9d3bd082018-01-11 10:02:12 -080078 MeasuredParagraph mt2 = MeasuredParagraph.buildForBidi("_VVV_", 1, 4, RTL, mt);
Seigo Nonakaf1644f72017-11-27 22:09:49 -080079 assertEquals(mt2, mt);
80 assertNotNull(mt2.getChars());
81 assertEquals("VVV", charsToString(mt.getChars()));
82 assertNotNull(mt2.getDirections(0, 3));
83 assertEquals(0, mt2.getWholeWidth(), 0);
84 assertEquals(0, mt2.getWidths().size());
85 assertEquals(0, mt2.getSpanEndCache().size());
86 assertEquals(0, mt2.getFontMetrics().size());
Seigo Nonaka6f1868b2017-12-01 16:24:19 -080087 assertEquals(0, mt2.getNativePtr());
Seigo Nonakaf1644f72017-11-27 22:09:49 -080088
89 mt2.recycle();
90 }
91
92 @Test
93 public void buildForMeasurement() {
Seigo Nonaka9d3bd082018-01-11 10:02:12 -080094 MeasuredParagraph mt = null;
Seigo Nonakaf1644f72017-11-27 22:09:49 -080095
Seigo Nonaka9d3bd082018-01-11 10:02:12 -080096 mt = MeasuredParagraph.buildForMeasurement(PAINT, "XXX", 0, 3, LTR, null);
Seigo Nonakaf1644f72017-11-27 22:09:49 -080097 assertNotNull(mt);
98 assertNotNull(mt.getChars());
99 assertEquals("XXX", charsToString(mt.getChars()));
100 assertEquals(Layout.DIR_LEFT_TO_RIGHT, mt.getParagraphDir());
101 assertNotNull(mt.getDirections(0, 3));
102 assertEquals(30, mt.getWholeWidth(), 0);
103 assertEquals(3, mt.getWidths().size());
104 assertEquals(10, mt.getWidths().get(0), 0);
105 assertEquals(10, mt.getWidths().get(1), 0);
106 assertEquals(10, mt.getWidths().get(2), 0);
107 assertEquals(0, mt.getSpanEndCache().size());
108 assertEquals(0, mt.getFontMetrics().size());
Seigo Nonaka6f1868b2017-12-01 16:24:19 -0800109 assertEquals(0, mt.getNativePtr());
Seigo Nonakaf1644f72017-11-27 22:09:49 -0800110
111 // Recycle it
Seigo Nonaka9d3bd082018-01-11 10:02:12 -0800112 MeasuredParagraph mt2 =
113 MeasuredParagraph.buildForMeasurement(PAINT, "_VVV_", 1, 4, RTL, mt);
Seigo Nonakaf1644f72017-11-27 22:09:49 -0800114 assertEquals(mt2, mt);
115 assertNotNull(mt2.getChars());
116 assertEquals("VVV", charsToString(mt.getChars()));
117 assertEquals(Layout.DIR_RIGHT_TO_LEFT, mt2.getParagraphDir());
118 assertNotNull(mt2.getDirections(0, 3));
119 assertEquals(15, mt2.getWholeWidth(), 0);
120 assertEquals(3, mt2.getWidths().size());
121 assertEquals(5, mt2.getWidths().get(0), 0);
122 assertEquals(5, mt2.getWidths().get(1), 0);
123 assertEquals(5, mt2.getWidths().get(2), 0);
124 assertEquals(0, mt2.getSpanEndCache().size());
125 assertEquals(0, mt2.getFontMetrics().size());
Seigo Nonaka6f1868b2017-12-01 16:24:19 -0800126 assertEquals(0, mt2.getNativePtr());
127
128 mt2.recycle();
129 }
130
131 @Test
132 public void buildForStaticLayout() {
Seigo Nonaka9d3bd082018-01-11 10:02:12 -0800133 MeasuredParagraph mt = null;
Seigo Nonaka6f1868b2017-12-01 16:24:19 -0800134
Seigo Nonaka87b15472018-01-12 14:06:29 -0800135 mt = MeasuredParagraph.buildForStaticLayout(PAINT, "XXX", 0, 3, LTR, false, null);
Seigo Nonaka6f1868b2017-12-01 16:24:19 -0800136 assertNotNull(mt);
137 assertNotNull(mt.getChars());
138 assertEquals("XXX", charsToString(mt.getChars()));
139 assertEquals(Layout.DIR_LEFT_TO_RIGHT, mt.getParagraphDir());
140 assertNotNull(mt.getDirections(0, 3));
141 assertEquals(0, mt.getWholeWidth(), 0);
142 assertEquals(0, mt.getWidths().size());
143 assertEquals(1, mt.getSpanEndCache().size());
144 assertEquals(3, mt.getSpanEndCache().get(0));
145 assertNotEquals(0, mt.getFontMetrics().size());
146 assertNotEquals(0, mt.getNativePtr());
147
148 // Recycle it
Seigo Nonaka9d3bd082018-01-11 10:02:12 -0800149 MeasuredParagraph mt2 =
Seigo Nonaka87b15472018-01-12 14:06:29 -0800150 MeasuredParagraph.buildForStaticLayout(PAINT, "_VVV_", 1, 4, RTL, false, mt);
Seigo Nonaka6f1868b2017-12-01 16:24:19 -0800151 assertEquals(mt2, mt);
152 assertNotNull(mt2.getChars());
153 assertEquals("VVV", charsToString(mt.getChars()));
154 assertEquals(Layout.DIR_RIGHT_TO_LEFT, mt2.getParagraphDir());
155 assertNotNull(mt2.getDirections(0, 3));
156 assertEquals(0, mt2.getWholeWidth(), 0);
157 assertEquals(0, mt2.getWidths().size());
158 assertEquals(1, mt2.getSpanEndCache().size());
159 assertEquals(4, mt2.getSpanEndCache().get(0));
160 assertNotEquals(0, mt2.getFontMetrics().size());
161 assertNotEquals(0, mt2.getNativePtr());
Seigo Nonakaf1644f72017-11-27 22:09:49 -0800162
163 mt2.recycle();
164 }
165
166 @Test
167 public void testFor70146381() {
Seigo Nonaka9d3bd082018-01-11 10:02:12 -0800168 MeasuredParagraph.buildForMeasurement(PAINT, "X…", 0, 2, RTL, null);
Seigo Nonakaf1644f72017-11-27 22:09:49 -0800169 }
170}