Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.text; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 20 | import static org.junit.Assert.assertNotEquals; |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 21 | import static org.junit.Assert.assertNotNull; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.graphics.Typeface; |
| 25 | import android.support.test.InstrumentationRegistry; |
| 26 | import android.support.test.filters.SmallTest; |
| 27 | import android.support.test.runner.AndroidJUnit4; |
| 28 | |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | |
| 32 | @SmallTest |
| 33 | @RunWith(AndroidJUnit4.class) |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 34 | public class MeasuredParagraphTest { |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 35 | 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 Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 63 | MeasuredParagraph mt = null; |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 64 | |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 65 | mt = MeasuredParagraph.buildForBidi("XXX", 0, 3, LTR, null); |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 66 | 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 Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 75 | assertEquals(0, mt.getNativePtr()); |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 76 | |
| 77 | // Recycle it |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 78 | MeasuredParagraph mt2 = MeasuredParagraph.buildForBidi("_VVV_", 1, 4, RTL, mt); |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 79 | 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 Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 87 | assertEquals(0, mt2.getNativePtr()); |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 88 | |
| 89 | mt2.recycle(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void buildForMeasurement() { |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 94 | MeasuredParagraph mt = null; |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 95 | |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 96 | mt = MeasuredParagraph.buildForMeasurement(PAINT, "XXX", 0, 3, LTR, null); |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 97 | 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 Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 109 | assertEquals(0, mt.getNativePtr()); |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 110 | |
| 111 | // Recycle it |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 112 | MeasuredParagraph mt2 = |
| 113 | MeasuredParagraph.buildForMeasurement(PAINT, "_VVV_", 1, 4, RTL, mt); |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 114 | 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 Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 126 | assertEquals(0, mt2.getNativePtr()); |
| 127 | |
| 128 | mt2.recycle(); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void buildForStaticLayout() { |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 133 | MeasuredParagraph mt = null; |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 134 | |
Seigo Nonaka | 87b1547 | 2018-01-12 14:06:29 -0800 | [diff] [blame] | 135 | mt = MeasuredParagraph.buildForStaticLayout(PAINT, "XXX", 0, 3, LTR, false, null); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 136 | 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 Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 149 | MeasuredParagraph mt2 = |
Seigo Nonaka | 87b1547 | 2018-01-12 14:06:29 -0800 | [diff] [blame] | 150 | MeasuredParagraph.buildForStaticLayout(PAINT, "_VVV_", 1, 4, RTL, false, mt); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 151 | 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 Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 162 | |
| 163 | mt2.recycle(); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void testFor70146381() { |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 168 | MeasuredParagraph.buildForMeasurement(PAINT, "X…", 0, 2, RTL, null); |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 169 | } |
| 170 | } |