• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.providers.contacts;
18 
19 import android.test.suitebuilder.annotation.SmallTest;
20 import android.text.TextUtils;
21 import android.util.Log;
22 
23 import com.android.providers.contacts.HanziToPinyin.Token;
24 
25 import junit.framework.TestCase;
26 
27 import java.util.ArrayList;
28 
29 @SmallTest
30 public class HanziToPinyinTest extends TestCase {
31     private final static String ONE_HANZI = "\u675C";
32     private final static String TWO_HANZI = "\u675C\u9D51";
33     private final static String ASSIC = "test";
34     private final static String ONE_UNKNOWN = "\uFF71";
35     private final static String MISC = "test\u675C   Test with space\uFF71\uFF71\u675C";
36 
hasChineseTransliterator()37     private boolean hasChineseTransliterator() {
38         return HanziToPinyin.getInstance().hasChineseTransliterator();
39     }
40 
test(final char hanzi, final String expectedPinyin)41     private void test(final char hanzi, final String expectedPinyin) throws Exception {
42         final String hanziString = Character.toString(hanzi);
43         ArrayList<Token> tokens = HanziToPinyin.getInstance().getTokens(hanziString);
44         assertEquals(tokens.size(), 1);
45         final String newString = tokens.get(0).target;
46         if (TextUtils.isEmpty(expectedPinyin)) {
47             assertEquals("Expected no transliteration for '" + hanziString
48                          + "' but got '" + newString + "'",
49                          tokens.get(0).type, Token.UNKNOWN);
50             assertTrue("Expected to get back original string for '"
51                        + hanziString  + "' but got '" + newString + "'",
52                        newString.equals(hanziString));
53         } else {
54             assertEquals("Expected transliteration for '" + hanziString
55                          + "' of '" + expectedPinyin + "' but got none",
56                          tokens.get(0).type, Token.PINYIN);
57             assertTrue("Expected transliteration for '" + hanziString + "' of '"
58                        + expectedPinyin + "' but got '" + newString + "'",
59                        newString.equalsIgnoreCase(expectedPinyin));
60         }
61     }
62 
63     @SmallTest
testGetToken()64     public void testGetToken() throws Exception {
65         if (!hasChineseTransliterator()) {
66             return;
67         }
68         ArrayList<Token> tokens = HanziToPinyin.getInstance().getTokens(ONE_HANZI);
69         assertEquals(tokens.size(), 1);
70         assertEquals(tokens.get(0).type, Token.PINYIN);
71         assertTrue(tokens.get(0).target.equalsIgnoreCase("DU"));
72 
73         tokens = HanziToPinyin.getInstance().getTokens(TWO_HANZI);
74         assertEquals(tokens.size(), 2);
75         assertEquals(tokens.get(0).type, Token.PINYIN);
76         assertEquals(tokens.get(1).type, Token.PINYIN);
77         assertTrue(tokens.get(0).target.equalsIgnoreCase("DU"));
78         assertTrue(tokens.get(1).target.equalsIgnoreCase("JUAN"));
79 
80         tokens = HanziToPinyin.getInstance().getTokens(ASSIC);
81         assertEquals(tokens.size(), 1);
82         assertEquals(tokens.get(0).type, Token.LATIN);
83 
84         tokens = HanziToPinyin.getInstance().getTokens(ONE_UNKNOWN);
85         assertEquals(tokens.size(), 1);
86         assertEquals(tokens.get(0).type, Token.UNKNOWN);
87 
88         tokens = HanziToPinyin.getInstance().getTokens(MISC);
89         assertEquals(tokens.size(), 7);
90         assertEquals(tokens.get(0).type, Token.LATIN);
91         assertEquals(tokens.get(1).type, Token.PINYIN);
92         assertEquals(tokens.get(2).type, Token.LATIN);
93         assertEquals(tokens.get(3).type, Token.LATIN);
94         assertEquals(tokens.get(4).type, Token.LATIN);
95         assertEquals(tokens.get(5).type, Token.UNKNOWN);
96         assertEquals(tokens.get(6).type, Token.PINYIN);
97     }
98 
99     /**
100      * Test each supported han against expected pinyin from transliterator.
101      */
102     @SmallTest
testTransliterator()103     public void testTransliterator() throws Exception {
104         if (!hasChineseTransliterator()) {
105             return;
106         }
107         test('\u4e00', "YI");
108         test('\u5201', "DIAO");
109         test('\u5602', "JIAO");
110         test('\u5a03', "WA");
111         test('\u5e04', "DING");
112         test('\u6205', "GANG");
113         test('\u6606', "KUN");
114         test('\u6a07', "XIU");
115         test('\u6e08', "JI");
116         test('\u7209', "LA");
117         test('\u79ff', "FU");
118         test('\u7a00', "XI");
119         test('\u7e01', "YUAN");
120         test('\u8202', "CHONG");
121         test('\u8603', "RUI");
122         test('\u8a04', "QIU");
123         test('\u8e05', "XUE");
124         test('\u9206', "QIAN");
125         test('\u9607', "DU");
126         test('\u9a08', "PIAN");
127         test('\u9e09', "YANG");
128     }
129 }
130