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; |
| 20 | |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Paint; |
| 23 | import android.graphics.Paint.FontMetricsInt; |
| 24 | import android.text.style.ReplacementSpan; |
| 25 | import junit.framework.TestCase; |
| 26 | |
| 27 | public class DynamicLayoutTest extends TestCase { |
| 28 | private static final int WIDTH = 10000; |
| 29 | |
| 30 | public void testGetBlocksAlwaysNeedToBeRedrawn_en() { |
| 31 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 32 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 33 | ALIGN_NORMAL, 0, 0, false); |
| 34 | |
| 35 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 36 | |
| 37 | builder.append("abcd efg\n"); |
| 38 | builder.append("hijk lmn\n"); |
| 39 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 40 | |
| 41 | builder.delete(0, builder.length()); |
| 42 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | private static class MockReplacementSpan extends ReplacementSpan { |
| 47 | @Override |
| 48 | public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) { |
| 49 | return 10; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, |
| 54 | int y, int bottom, Paint paint) { |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public void testGetBlocksAlwaysNeedToBeRedrawn_replacementSpan() { |
| 59 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 60 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 61 | ALIGN_NORMAL, 0, 0, false); |
| 62 | |
| 63 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 64 | |
| 65 | builder.append("abcd efg\n"); |
| 66 | builder.append("hijk lmn\n"); |
| 67 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 68 | |
| 69 | builder.setSpan(new MockReplacementSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 70 | assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 71 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 72 | |
| 73 | builder.setSpan(new MockReplacementSpan(), 9, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 74 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 75 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1)); |
| 76 | |
| 77 | builder.delete(9, 13); |
| 78 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 79 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1)); |
| 80 | |
| 81 | builder.delete(0, 4); |
| 82 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 83 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().isEmpty()); |
| 84 | } |
| 85 | |
| 86 | public void testGetBlocksAlwaysNeedToBeRedrawn_thai() { |
| 87 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 88 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 89 | ALIGN_NORMAL, 0, 0, false); |
| 90 | |
| 91 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 92 | |
| 93 | builder.append("\u0E22\u0E34\u0E19\u0E14\u0E35\u0E15\u0E49\u0E2D\u0E19\u0E23\u0E31\u0E1A"); |
| 94 | builder.append("\u0E2A\u0E39\u0E48"); |
| 95 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 96 | |
| 97 | builder.append("\u0E48\u0E48\u0E48\u0E48\u0E48"); |
| 98 | assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 99 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 100 | |
| 101 | builder.delete(builder.length() -5, builder.length()); |
| 102 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 103 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().isEmpty()); |
| 104 | } |
| 105 | } |