Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package android.text; |
| 18 | |
| 19 | import static android.text.Layout.Alignment.ALIGN_NORMAL; |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 20 | |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 22 | import static org.junit.Assert.assertFalse; |
| 23 | import static org.junit.Assert.assertNotNull; |
| 24 | import static org.junit.Assert.assertNull; |
| 25 | import static org.junit.Assert.assertTrue; |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 26 | |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas; |
| 28 | import android.graphics.Paint; |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 29 | import android.support.test.filters.SmallTest; |
| 30 | import android.support.test.runner.AndroidJUnit4; |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 31 | import android.text.style.ReplacementSpan; |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 32 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | |
| 36 | @SmallTest |
| 37 | @RunWith(AndroidJUnit4.class) |
| 38 | public class DynamicLayoutTest { |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 39 | private static final int WIDTH = 10000; |
| 40 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 41 | @Test |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 42 | public void testGetBlocksAlwaysNeedToBeRedrawn_en() { |
| 43 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 44 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 45 | ALIGN_NORMAL, 0, 0, false); |
| 46 | |
| 47 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 48 | |
| 49 | builder.append("abcd efg\n"); |
| 50 | builder.append("hijk lmn\n"); |
| 51 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 52 | |
| 53 | builder.delete(0, builder.length()); |
| 54 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 55 | } |
| 56 | |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 57 | private class MockReplacementSpan extends ReplacementSpan { |
| 58 | public int getSize(Paint paint, CharSequence text, int start, int end, |
| 59 | Paint.FontMetricsInt fm) { |
| 60 | return 10; |
| 61 | } |
| 62 | |
| 63 | public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, |
| 64 | int y, int bottom, Paint paint) { } |
| 65 | } |
| 66 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 67 | @Test |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 68 | public void testGetBlocksAlwaysNeedToBeRedrawn_replacementSpan() { |
| 69 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 70 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 71 | ALIGN_NORMAL, 0, 0, false); |
| 72 | |
| 73 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 74 | |
| 75 | builder.append("abcd efg\n"); |
| 76 | builder.append("hijk lmn\n"); |
| 77 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 78 | |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 79 | builder.setSpan(new MockReplacementSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 80 | assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 81 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 82 | |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 83 | builder.setSpan(new MockReplacementSpan(), 9, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 84 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 85 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1)); |
| 86 | |
| 87 | builder.delete(9, 13); |
| 88 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 89 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1)); |
| 90 | |
| 91 | builder.delete(0, 4); |
| 92 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 93 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().isEmpty()); |
| 94 | } |
| 95 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 96 | @Test |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 97 | public void testGetBlocksAlwaysNeedToBeRedrawn_thai() { |
| 98 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 99 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 100 | ALIGN_NORMAL, 0, 0, false); |
| 101 | |
| 102 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 103 | |
| 104 | builder.append("\u0E22\u0E34\u0E19\u0E14\u0E35\u0E15\u0E49\u0E2D\u0E19\u0E23\u0E31\u0E1A"); |
| 105 | builder.append("\u0E2A\u0E39\u0E48"); |
| 106 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 107 | |
| 108 | builder.append("\u0E48\u0E48\u0E48\u0E48\u0E48"); |
| 109 | assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 110 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 111 | |
| 112 | builder.delete(builder.length() -5, builder.length()); |
| 113 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 114 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().isEmpty()); |
| 115 | } |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 116 | |
| 117 | @Test |
| 118 | public void testGetLineExtra_withoutLinespacing() { |
| 119 | final SpannableStringBuilder text = new SpannableStringBuilder("a\nb\nc"); |
| 120 | final TextPaint textPaint = new TextPaint(); |
| 121 | |
| 122 | // create a StaticLayout to check against |
| 123 | final StaticLayout staticLayout = StaticLayout.Builder.obtain(text, 0, |
| 124 | text.length(), textPaint, WIDTH) |
| 125 | .setAlignment(ALIGN_NORMAL) |
| 126 | .setIncludePad(false) |
| 127 | .build(); |
| 128 | |
| 129 | // create the DynamicLayout |
| 130 | final DynamicLayout dynamicLayout = new DynamicLayout(text, |
| 131 | textPaint, |
| 132 | WIDTH, |
| 133 | ALIGN_NORMAL, |
| 134 | 1f /*spacingMultiplier*/, |
| 135 | 0 /*spacingAdd*/, |
| 136 | false /*includepad*/); |
| 137 | |
| 138 | final int lineCount = staticLayout.getLineCount(); |
| 139 | assertEquals(lineCount, dynamicLayout.getLineCount()); |
| 140 | for (int i = 0; i < lineCount; i++) { |
| 141 | assertEquals(staticLayout.getLineExtra(i), dynamicLayout.getLineExtra(i)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testGetLineExtra_withLinespacing() { |
| 147 | final SpannableStringBuilder text = new SpannableStringBuilder("a\nb\nc"); |
| 148 | final TextPaint textPaint = new TextPaint(); |
| 149 | final float spacingMultiplier = 2f; |
| 150 | final float spacingAdd = 4; |
| 151 | |
| 152 | // create a StaticLayout to check against |
| 153 | final StaticLayout staticLayout = StaticLayout.Builder.obtain(text, 0, |
| 154 | text.length(), textPaint, WIDTH) |
| 155 | .setAlignment(ALIGN_NORMAL) |
| 156 | .setIncludePad(false) |
| 157 | .setLineSpacing(spacingAdd, spacingMultiplier) |
| 158 | .build(); |
| 159 | |
| 160 | // create the DynamicLayout |
| 161 | final DynamicLayout dynamicLayout = new DynamicLayout(text, |
| 162 | textPaint, |
| 163 | WIDTH, |
| 164 | ALIGN_NORMAL, |
| 165 | spacingMultiplier, |
| 166 | spacingAdd, |
| 167 | false /*includepad*/); |
| 168 | |
| 169 | final int lineCount = staticLayout.getLineCount(); |
| 170 | assertEquals(lineCount, dynamicLayout.getLineCount()); |
| 171 | for (int i = 0; i < lineCount - 1; i++) { |
| 172 | assertEquals(staticLayout.getLineExtra(i), dynamicLayout.getLineExtra(i)); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | @Test(expected = IndexOutOfBoundsException.class) |
| 177 | public void testGetLineExtra_withNegativeValue() { |
| 178 | final DynamicLayout layout = new DynamicLayout("", new TextPaint(), 10 /*width*/, |
| 179 | ALIGN_NORMAL, 1.0f /*spacingMultiplier*/, 0f /*spacingAdd*/, false /*includepad*/); |
| 180 | layout.getLineExtra(-1); |
| 181 | } |
| 182 | |
| 183 | @Test(expected = IndexOutOfBoundsException.class) |
| 184 | public void testGetLineExtra_withParamGreaterThanLineCount() { |
| 185 | final DynamicLayout layout = new DynamicLayout("", new TextPaint(), 10 /*width*/, |
| 186 | ALIGN_NORMAL, 1.0f /*spacingMultiplier*/, 0f /*spacingAdd*/, false /*includepad*/); |
| 187 | layout.getLineExtra(100); |
| 188 | } |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 189 | } |