blob: 811bf2c43320f61301e8bd0b5179944f400f89b7 [file] [log] [blame]
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +09001/*
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
17package android.text;
18
19import static android.text.Layout.Alignment.ALIGN_NORMAL;
Roozbeh Pournaderde0353d2017-06-06 15:44:03 -070020
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010021import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertNotNull;
23import static org.junit.Assert.assertNull;
24import static org.junit.Assert.assertTrue;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090025
Roozbeh Pournaderde0353d2017-06-06 15:44:03 -070026import android.graphics.Canvas;
27import android.graphics.Paint;
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010028import android.support.test.filters.SmallTest;
29import android.support.test.runner.AndroidJUnit4;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090030import android.text.style.ReplacementSpan;
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090031
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010032import org.junit.Test;
33import org.junit.runner.RunWith;
34
35@SmallTest
36@RunWith(AndroidJUnit4.class)
37public class DynamicLayoutTest {
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090038 private static final int WIDTH = 10000;
39
Andrei Stingaceanue1a7d0d2017-04-24 16:53:17 +010040 @Test
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090041 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 Pournaderde0353d2017-06-06 15:44:03 -070056 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 Stingaceanue1a7d0d2017-04-24 16:53:17 +010066 @Test
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090067 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 Pournaderde0353d2017-06-06 15:44:03 -070078 builder.setSpan(new MockReplacementSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090079 assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn());
80 assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
81
Roozbeh Pournaderde0353d2017-06-06 15:44:03 -070082 builder.setSpan(new MockReplacementSpan(), 9, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090083 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 Stingaceanue1a7d0d2017-04-24 16:53:17 +010095 @Test
Keisuke Kuroyanagif5af4a32016-08-31 21:40:53 +090096 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}