1 /* 2 * Copyright (C) 2010 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 android.test.suitebuilder.annotation.SmallTest; 20 import android.util.Log; 21 22 import junit.framework.TestCase; 23 24 /** 25 * Tests StaticLayout bidi implementation. 26 */ 27 public class StaticLayoutBidiTest extends TestCase { 28 29 public static final int REQ_DL = 2; // Layout.DIR_REQUEST_DEFAULT_LTR; 30 public static final int REQ_DR = -2; // Layout.DIR_REQUEST_DEFAULT_RTL; 31 public static final int REQ_L = 1; // Layout.DIR_REQUEST_LTR; 32 public static final int REQ_R = -1; // Layout.DIR_REQUEST_RTL; 33 public static final int L = Layout.DIR_LEFT_TO_RIGHT; 34 public static final int R = Layout.DIR_RIGHT_TO_LEFT; 35 36 public static final String SP = " "; 37 public static final String ALEF = "\u05d0"; 38 public static final String BET = "\u05d1"; 39 public static final String GIMEL = "\u05d2"; 40 public static final String DALET = "\u05d3"; 41 42 //@SmallTest 43 public void testAllLtr() { 44 expectBidi(REQ_DL, "a test", "000000", L); 45 } 46 47 //@SmallTest 48 public void testLtrRtl() { 49 expectBidi(REQ_DL, "abc " + ALEF + BET + GIMEL, "0000111", L); 50 } 51 52 //@SmallTest 53 public void testAllRtl() { 54 expectBidi(REQ_DL, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", R); 55 } 56 57 //@SmallTest 58 public void testRtlLtr() { 59 expectBidi(REQ_DL, ALEF + BET + GIMEL + " abc", "1111000", R); 60 } 61 62 //@SmallTest 63 public void testRAllLtr() { 64 expectBidi(REQ_R, "a test", "000000", R); 65 } 66 67 //@SmallTest 68 public void testRLtrRtl() { 69 expectBidi(REQ_R, "abc " + ALEF + BET + GIMEL, "0001111", R); 70 } 71 72 //@SmallTest 73 public void testLAllRtl() { 74 expectBidi(REQ_L, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", L); 75 } 76 77 //@SmallTest 78 public void testLRtlLtr() { 79 expectBidi(REQ_L, ALEF + BET + GIMEL + " abc", "1110000", L); 80 } 81 82 private void expectBidi(int dir, String text, 83 String expectedLevels, int expectedDir) { 84 char[] chs = text.toCharArray(); 85 int n = chs.length; 86 byte[] chInfo = new byte[n]; 87 88 int resultDir = StaticLayout.bidi(dir, chs, chInfo, n, false); 89 90 { 91 StringBuilder sb = new StringBuilder("info:"); 92 for (int i = 0; i < n; ++i) { 93 sb.append(" ").append(String.valueOf(chInfo[i])); 94 } 95 Log.i("BIDI", sb.toString()); 96 } 97 98 char[] resultLevelChars = new char[n]; 99 for (int i = 0; i < n; ++i) { 100 resultLevelChars[i] = (char)('0' + chInfo[i]); 101 } 102 String resultLevels = new String(resultLevelChars); 103 assertEquals("direction", expectedDir, resultDir); 104 assertEquals("levels", expectedLevels, resultLevels); 105 } 106 107 //@SmallTest 108 public void testNativeBidi() { 109 // native bidi returns levels, not simply directions 110 expectNativeBidi(REQ_DL, ALEF + BET + GIMEL + " abc", "1111222", R); 111 } 112 113 private void expectNativeBidi(int dir, String text, 114 String expectedLevels, int expectedDir) { 115 char[] chs = text.toCharArray(); 116 int n = chs.length; 117 byte[] chInfo = new byte[n]; 118 119 int resultDir = AndroidBidi.bidi(dir, chs, chInfo, n, false); 120 121 { 122 StringBuilder sb = new StringBuilder("info:"); 123 for (int i = 0; i < n; ++i) { 124 sb.append(" ").append(String.valueOf(chInfo[i])); 125 } 126 Log.i("BIDI", sb.toString()); 127 } 128 129 char[] resultLevelChars = new char[n]; 130 for (int i = 0; i < n; ++i) { 131 resultLevelChars[i] = (char)('0' + chInfo[i]); 132 } 133 String resultLevels = new String(resultLevelChars); 134 assertEquals("direction", expectedDir, resultDir); 135 assertEquals("levels", expectedLevels, resultLevels); 136 } 137 } 138