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