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 android.test.suitebuilder.annotation.SmallTest;
     20 import android.util.Log;
     21 
     22 import junit.framework.TestCase;
     23 
     24 /**
     25  * Quick check of native 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         expectNativeBidi(REQ_DL, "a test", "000000", L);
     45     }
     46 
     47     //@SmallTest
     48     public void testLtrRtl() {
     49         expectNativeBidi(REQ_DL, "abc " + ALEF + BET + GIMEL, "0000111", L);
     50     }
     51 
     52     //@SmallTest
     53     public void testAllRtl() {
     54         expectNativeBidi(REQ_DL, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", R);
     55     }
     56 
     57     //@SmallTest
     58     public void testRtlLtr() {
     59         expectNativeBidi(REQ_DL,  ALEF + BET + GIMEL + " abc", "1111222", R);
     60     }
     61 
     62     //@SmallTest
     63     public void testRAllLtr() {
     64         expectNativeBidi(REQ_R, "a test", "222222", R);
     65     }
     66 
     67     //@SmallTest
     68     public void testRLtrRtl() {
     69         expectNativeBidi(REQ_R, "abc " + ALEF + BET + GIMEL, "2221111", R);
     70     }
     71 
     72     //@SmallTest
     73     public void testLAllRtl() {
     74         expectNativeBidi(REQ_L, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", L);
     75     }
     76 
     77     //@SmallTest
     78     public void testLRtlLtr() {
     79         expectNativeBidi(REQ_DL,  ALEF + BET + GIMEL + " abc", "1111222", R);
     80     }
     81 
     82     //@SmallTest
     83     public void testNativeBidi() {
     84         expectNativeBidi(REQ_L,  ALEF + BET + GIMEL + " abc", "1110000", L);
     85     }
     86 
     87     private void expectNativeBidi(int dir, String text,
     88             String expectedLevels, int expectedDir) {
     89         char[] chs = text.toCharArray();
     90         int n = chs.length;
     91         byte[] chInfo = new byte[n];
     92 
     93         int resultDir = AndroidBidi.bidi(dir, chs, chInfo, n, false);
     94 
     95         {
     96             StringBuilder sb = new StringBuilder("info:");
     97             for (int i = 0; i < n; ++i) {
     98                 sb.append(" ").append(String.valueOf(chInfo[i]));
     99             }
    100             Log.i("BIDI", sb.toString());
    101         }
    102 
    103         char[] resultLevelChars = new char[n];
    104         for (int i = 0; i < n; ++i) {
    105             resultLevelChars[i] = (char)('0' + chInfo[i]);
    106         }
    107         String resultLevels = new String(resultLevelChars);
    108         assertEquals("direction", expectedDir, resultDir);
    109         assertEquals("levels", expectedLevels, resultLevels);
    110     }
    111 }
    112