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 org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 22 import android.support.test.filters.SmallTest; 23 import android.support.test.runner.AndroidJUnit4; 24 import android.text.Layout.Alignment; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 /** 31 * Tests for text measuring methods of StaticLayout. 32 */ 33 @SmallTest 34 @RunWith(AndroidJUnit4.class) 35 public class StaticLayoutTextMeasuringTest { 36 private static final float SPACE_MULTI = 1.0f; 37 private static final float SPACE_ADD = 0.0f; 38 private static final int DEFAULT_OUTER_WIDTH = 150; 39 private static final Alignment DEFAULT_ALIGN = Alignment.ALIGN_LEFT; 40 41 private TextPaint mDefaultPaint; 42 43 @Before 44 public void setup() { 45 if (mDefaultPaint == null) { 46 mDefaultPaint = new TextPaint(); 47 } 48 } 49 50 @Test 51 public void testGetPrimaryHorizontal_zwnbsp() { 52 // a, ZERO WIDTH NO-BREAK SPACE 53 String testString = "a\uFEFF"; 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), 0f); 58 assertEquals(layout.getPrimaryHorizontal(2), layout.getPrimaryHorizontal(1), 0f); 59 } 60 61 @Test 62 public void testGetPrimaryHorizontal_devanagari() { 63 // DEVANAGARI LETTER KA, DEVANAGARI VOWEL SIGN AA 64 String testString = "\u0915\u093E"; 65 StaticLayout layout = new StaticLayout(testString, mDefaultPaint, 66 DEFAULT_OUTER_WIDTH, DEFAULT_ALIGN, SPACE_MULTI, SPACE_ADD, true); 67 68 assertEquals(0.0f, layout.getPrimaryHorizontal(0), 0f); 69 assertEquals(layout.getPrimaryHorizontal(2), layout.getPrimaryHorizontal(1), 0f); 70 } 71 72 @Test 73 public void testGetPrimaryHorizontal_flagEmoji() { 74 // REGIONAL INDICATOR SYMBOL LETTER U, REGIONAL INDICATOR SYMBOL LETTER S, REGIONAL 75 // INDICATOR SYMBOL LETTER Z 76 // First two code points (U and S) forms a US flag. 77 String testString = "\uD83C\uDDFA\uD83C\uDDF8\uD83C\uDDFF"; 78 StaticLayout layout = new StaticLayout(testString, mDefaultPaint, 79 DEFAULT_OUTER_WIDTH, DEFAULT_ALIGN, SPACE_MULTI, SPACE_ADD, true); 80 81 assertEquals(0.0f, layout.getPrimaryHorizontal(0), 0f); 82 assertEquals(layout.getPrimaryHorizontal(4), layout.getPrimaryHorizontal(1), 0f); 83 assertEquals(layout.getPrimaryHorizontal(4), layout.getPrimaryHorizontal(2), 0f); 84 assertEquals(layout.getPrimaryHorizontal(4), layout.getPrimaryHorizontal(3), 0f); 85 86 assertTrue(layout.getPrimaryHorizontal(6) > layout.getPrimaryHorizontal(4)); 87 assertEquals(layout.getPrimaryHorizontal(6), layout.getPrimaryHorizontal(5), 0f); 88 } 89 } 90