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 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertNotNull; |
| 23 | import static org.junit.Assert.assertNull; |
| 24 | import static org.junit.Assert.assertTrue; |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 25 | |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 26 | import android.graphics.Canvas; |
| 27 | import android.graphics.Paint; |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 28 | import android.support.test.filters.SmallTest; |
| 29 | import android.support.test.runner.AndroidJUnit4; |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 30 | import android.text.style.ReplacementSpan; |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 31 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | |
| 35 | @SmallTest |
| 36 | @RunWith(AndroidJUnit4.class) |
| 37 | public class DynamicLayoutTest { |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 38 | private static final int WIDTH = 10000; |
| 39 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 40 | @Test |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 41 | public void testGetBlocksAlwaysNeedToBeRedrawn_en() { |
| 42 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 43 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 44 | ALIGN_NORMAL, 0, 0, false); |
| 45 | |
| 46 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 47 | |
| 48 | builder.append("abcd efg\n"); |
| 49 | builder.append("hijk lmn\n"); |
| 50 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 51 | |
| 52 | builder.delete(0, builder.length()); |
| 53 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 54 | } |
| 55 | |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 56 | private class MockReplacementSpan extends ReplacementSpan { |
| 57 | public int getSize(Paint paint, CharSequence text, int start, int end, |
| 58 | Paint.FontMetricsInt fm) { |
| 59 | return 10; |
| 60 | } |
| 61 | |
| 62 | public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, |
| 63 | int y, int bottom, Paint paint) { } |
| 64 | } |
| 65 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 66 | @Test |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 67 | public void testGetBlocksAlwaysNeedToBeRedrawn_replacementSpan() { |
| 68 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 69 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 70 | ALIGN_NORMAL, 0, 0, false); |
| 71 | |
| 72 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 73 | |
| 74 | builder.append("abcd efg\n"); |
| 75 | builder.append("hijk lmn\n"); |
| 76 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 77 | |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 78 | builder.setSpan(new MockReplacementSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 79 | assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 80 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 81 | |
Roozbeh Pournader | de0353d | 2017-06-06 15:44:03 -0700 | [diff] [blame] | 82 | builder.setSpan(new MockReplacementSpan(), 9, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 83 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 84 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1)); |
| 85 | |
| 86 | builder.delete(9, 13); |
| 87 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 88 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1)); |
| 89 | |
| 90 | builder.delete(0, 4); |
| 91 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 92 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().isEmpty()); |
| 93 | } |
| 94 | |
Andrei Stingaceanu | e1a7d0d | 2017-04-24 16:53:17 +0100 | [diff] [blame] | 95 | @Test |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 96 | public void testGetBlocksAlwaysNeedToBeRedrawn_thai() { |
| 97 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 98 | final DynamicLayout layout = new DynamicLayout(builder, new TextPaint(), WIDTH, |
| 99 | ALIGN_NORMAL, 0, 0, false); |
| 100 | |
| 101 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 102 | |
| 103 | builder.append("\u0E22\u0E34\u0E19\u0E14\u0E35\u0E15\u0E49\u0E2D\u0E19\u0E23\u0E31\u0E1A"); |
| 104 | builder.append("\u0E2A\u0E39\u0E48"); |
| 105 | assertNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 106 | |
| 107 | builder.append("\u0E48\u0E48\u0E48\u0E48\u0E48"); |
| 108 | assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn()); |
| 109 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 110 | |
| 111 | builder.delete(builder.length() -5, builder.length()); |
| 112 | assertFalse(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0)); |
| 113 | assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().isEmpty()); |
| 114 | } |
| 115 | } |