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