HanziToPinyinTest.java revision 71340347b4862d4b1368a5d69d1667e2245952e4
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
17package com.android.providers.contacts;
18
19import com.android.providers.contacts.HanziToPinyin.Token;
20
21import android.test.suitebuilder.annotation.SmallTest;
22
23import java.text.Collator;
24import java.util.ArrayList;
25import java.util.Arrays;
26import java.util.Locale;
27
28import junit.framework.TestCase;
29
30@SmallTest
31public class HanziToPinyinTest extends TestCase {
32    private final static String ONE_HANZI = "\u675C";
33    private final static String TWO_HANZI = "\u675C\u9D51";
34    private final static String ASSIC = "test";
35    private final static String ONE_UNKNOWN = "\uFF71";
36    private final static String MISC = "test\u675C   Test with space\uFF71\uFF71\u675C";
37
38    @SmallTest
39    public void testGetToken() throws Exception {
40        if (!Arrays.asList(Collator.getAvailableLocales()).contains(Locale.CHINA)) {
41            return;
42        }
43        ArrayList<Token> tokens = HanziToPinyin.getInstance().get(ONE_HANZI);
44        assertEquals(tokens.size(), 1);
45        assertEquals(tokens.get(0).type, Token.PINYIN);
46        assertTrue(tokens.get(0).target.equalsIgnoreCase("DU"));
47
48        tokens = HanziToPinyin.getInstance().get(TWO_HANZI);
49        assertEquals(tokens.size(), 2);
50        assertEquals(tokens.get(0).type, Token.PINYIN);
51        assertEquals(tokens.get(1).type, Token.PINYIN);
52        assertTrue(tokens.get(0).target.equalsIgnoreCase("DU"));
53        assertTrue(tokens.get(1).target.equalsIgnoreCase("JUAN"));
54
55        tokens = HanziToPinyin.getInstance().get(ASSIC);
56        assertEquals(tokens.size(), 1);
57        assertEquals(tokens.get(0).type, Token.LATIN);
58
59        tokens = HanziToPinyin.getInstance().get(ONE_UNKNOWN);
60        assertEquals(tokens.size(), 1);
61        assertEquals(tokens.get(0).type, Token.UNKNOWN);
62
63        tokens = HanziToPinyin.getInstance().get(MISC);
64        assertEquals(tokens.size(), 7);
65        assertEquals(tokens.get(0).type, Token.LATIN);
66        assertEquals(tokens.get(1).type, Token.PINYIN);
67        assertEquals(tokens.get(2).type, Token.LATIN);
68        assertEquals(tokens.get(3).type, Token.LATIN);
69        assertEquals(tokens.get(4).type, Token.LATIN);
70        assertEquals(tokens.get(5).type, Token.UNKNOWN);
71        assertEquals(tokens.get(6).type, Token.PINYIN);
72    }
73}
74