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
17package android.text;
18
19import android.text.Layout.Alignment;
20import junit.framework.TestCase;
21
22/**
23 * Tests for text measuring methods of StaticLayout.
24 */
25public class StaticLayoutTextMeasuringTest extends TestCase {
26    private static final float SPACE_MULTI = 1.0f;
27    private static final float SPACE_ADD = 0.0f;
28    private static final int DEFAULT_OUTER_WIDTH = 150;
29    private static final Alignment DEFAULT_ALIGN = Alignment.ALIGN_LEFT;
30
31    private TextPaint mDefaultPaint;
32
33    @Override
34    protected void setUp() throws Exception {
35        super.setUp();
36        if (mDefaultPaint == null) {
37            mDefaultPaint = new TextPaint();
38        }
39    }
40
41    public void testGetPrimaryHorizontal_zwnbsp() {
42        // a, ZERO WIDTH NO-BREAK SPACE
43        String testString = "a\uFEFF";
44        StaticLayout layout = new StaticLayout(testString, mDefaultPaint,
45                DEFAULT_OUTER_WIDTH, DEFAULT_ALIGN, SPACE_MULTI, SPACE_ADD, true);
46
47        assertEquals(0.0f, layout.getPrimaryHorizontal(0));
48        assertEquals(layout.getPrimaryHorizontal(2), layout.getPrimaryHorizontal(1));
49    }
50
51    public void testGetPrimaryHorizontal_devanagari() {
52        // DEVANAGARI LETTER KA, DEVANAGARI VOWEL SIGN AA
53        String testString = "\u0915\u093E";
54        StaticLayout layout = new StaticLayout(testString, mDefaultPaint,
55                DEFAULT_OUTER_WIDTH, DEFAULT_ALIGN, SPACE_MULTI, SPACE_ADD, true);
56
57        assertEquals(0.0f, layout.getPrimaryHorizontal(0));
58        assertEquals(layout.getPrimaryHorizontal(2), layout.getPrimaryHorizontal(1));
59    }
60
61    public void testGetPrimaryHorizontal_flagEmoji() {
62        // REGIONAL INDICATOR SYMBOL LETTER U, REGIONAL INDICATOR SYMBOL LETTER S, REGIONAL
63        // INDICATOR SYMBOL LETTER Z
64        // First two code points (U and S) forms a US flag.
65        String testString = "\uD83C\uDDFA\uD83C\uDDF8\uD83C\uDDFF";
66        StaticLayout layout = new StaticLayout(testString, mDefaultPaint,
67                DEFAULT_OUTER_WIDTH, DEFAULT_ALIGN, SPACE_MULTI, SPACE_ADD, true);
68
69        assertEquals(0.0f, layout.getPrimaryHorizontal(0));
70        assertEquals(layout.getPrimaryHorizontal(4), layout.getPrimaryHorizontal(1));
71        assertEquals(layout.getPrimaryHorizontal(4), layout.getPrimaryHorizontal(2));
72        assertEquals(layout.getPrimaryHorizontal(4), layout.getPrimaryHorizontal(3));
73
74        assertTrue(layout.getPrimaryHorizontal(6) > layout.getPrimaryHorizontal(4));
75        assertEquals(layout.getPrimaryHorizontal(6), layout.getPrimaryHorizontal(5));
76    }
77}
78