2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
19 import android.test.suitebuilder.annotation.SmallTest;
20 import android.util.Log;
22 import junit.framework.TestCase;
25 * Tests StaticLayout bidi implementation.
27 public class StaticLayoutBidiTest extends TestCase {
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;
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";
43 public void testAllLtr() {
44 expectBidi(REQ_DL, "a test", "000000", L);
48 public void testLtrRtl() {
49 expectBidi(REQ_DL, "abc " + ALEF + BET + GIMEL, "0000111", L);
53 public void testAllRtl() {
54 expectBidi(REQ_DL, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", R);
58 public void testRtlLtr() {
59 expectBidi(REQ_DL, ALEF + BET + GIMEL + " abc", "1111000", R);
63 public void testRAllLtr() {
64 expectBidi(REQ_R, "a test", "000000", R);
68 public void testRLtrRtl() {
69 expectBidi(REQ_R, "abc " + ALEF + BET + GIMEL, "0001111", R);
73 public void testLAllRtl() {
74 expectBidi(REQ_L, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", L);
78 public void testLRtlLtr() {
79 expectBidi(REQ_L, ALEF + BET + GIMEL + " abc", "1110000", L);
82 private void expectBidi(int dir, String text,
83 String expectedLevels, int expectedDir) {
84 char[] chs = text.toCharArray();
86 byte[] chInfo = new byte[n];
88 int resultDir = StaticLayout.bidi(dir, chs, chInfo, n, false);
91 StringBuilder sb = new StringBuilder("info:");
92 for (int i = 0; i < n; ++i) {
93 sb.append(" ").append(String.valueOf(chInfo[i]));
95 Log.i("BIDI", sb.toString());
98 char[] resultLevelChars = new char[n];
99 for (int i = 0; i < n; ++i) {
100 resultLevelChars[i] = (char)('0' + chInfo[i]);
102 String resultLevels = new String(resultLevelChars);
103 assertEquals("direction", expectedDir, resultDir);
104 assertEquals("levels", expectedLevels, resultLevels);
108 public void testNativeBidi() {
109 // native bidi returns levels, not simply directions
110 expectNativeBidi(REQ_DL, ALEF + BET + GIMEL + " abc", "1111222", R);
113 private void expectNativeBidi(int dir, String text,
114 String expectedLevels, int expectedDir) {
115 char[] chs = text.toCharArray();
117 byte[] chInfo = new byte[n];
119 int resultDir = AndroidBidi.bidi(dir, chs, chInfo, n, false);
122 StringBuilder sb = new StringBuilder("info:");
123 for (int i = 0; i < n; ++i) {
124 sb.append(" ").append(String.valueOf(chInfo[i]));
126 Log.i("BIDI", sb.toString());
129 char[] resultLevelChars = new char[n];
130 for (int i = 0; i < n; ++i) {
131 resultLevelChars[i] = (char)('0' + chInfo[i]);
133 String resultLevels = new String(resultLevelChars);
134 assertEquals("direction", expectedDir, resultDir);
135 assertEquals("levels", expectedLevels, resultLevels);