1 /* 2 * Copyright (C) 2016 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 package com.android.providers.contacts; 17 18 import android.icu.text.AlphabeticIndex; 19 import android.icu.text.AlphabeticIndex.ImmutableIndex; 20 import android.test.suitebuilder.annotation.Suppress; 21 import android.util.Log; 22 23 import java.util.Arrays; 24 import java.util.Locale; 25 26 public class ContactsIcuTest extends FixedAndroidTestCase { 27 private static final String TAG = "ContactsIcuTest"; 28 buildIndex(String... localeTags)29 private static ImmutableIndex buildIndex(String... localeTags) { 30 final AlphabeticIndex ai = new AlphabeticIndex(Locale.forLanguageTag(localeTags[0])); 31 32 // Add secondary locales, if any. 33 for (int i = 1; i < localeTags.length; i++) { 34 ai.addLabels(Locale.forLanguageTag(localeTags[i])); 35 } 36 37 final ImmutableIndex index = ai.buildImmutableIndex(); 38 39 Log.d(TAG, "Locales=" + Arrays.asList(localeTags)); 40 ContactLocaleUtils.dumpIndex(index); 41 42 return index; 43 } 44 getBucket(ImmutableIndex index, String name)45 private static String getBucket(ImmutableIndex index, String name) { 46 return index.getBucket(index.getBucketIndex(name)).getLabel(); 47 } 48 checkBucket(ImmutableIndex index, String expectedBucket, String str)49 private static boolean checkBucket(ImmutableIndex index, String expectedBucket, String str) { 50 51 boolean okay = true; 52 53 // Test each unicode character in the given string. 54 final int length = str.length(); 55 int offset = 0; 56 while (offset < length) { 57 final int codePoint = Character.codePointAt(str, offset); 58 59 final String ch = new String(new int[]{codePoint}, 0, 1); 60 61 final String actual = getBucket(index, ch); 62 63 if (!expectedBucket.equals(actual)) { 64 Log.e(TAG, "Bucket for '" + ch + "' expected to be '" 65 + expectedBucket + "', but was '" + actual + "'"); 66 okay = false; 67 } 68 69 offset += Character.charCount(codePoint); 70 } 71 72 return okay; 73 } 74 75 @Suppress // This test doesn't pass since android's ICU data doesn't cover rarely used chars. testTraditionalChineseStrokeCounts()76 public void testTraditionalChineseStrokeCounts() { 77 final ImmutableIndex index = buildIndex("zh-Hant-TW"); 78 79 boolean okay = true; 80 // Data generated from: https://en.wiktionary.org/wiki/Index:Chinese_total_strokes 81 okay &= checkBucket(index, "1劃", "一丨丶丿乀乁乙乚乛亅"); 82 okay &= checkBucket(index, "2劃", "㐅丁丂七丄丅丆丩丷乂乃乄乜九了二亠人亻儿入八冂冖冫几"); 83 okay &= checkBucket(index, "3劃", "㐃㐄㐇㐈㐉㔾㔿万丈三上下丌个丫丸久乆乇么义乊乞也习乡"); 84 okay &= checkBucket(index, "4劃", "㐊㐋㐧㓀㓁㓅㔫㔹㕕㕚㕛㝉㞢㠪㢧㲸㸦不"); 85 okay &= checkBucket(index, "5劃", "㐀㐌㐍㐎㐏㐰㐱㐲㐳㐴㐵㐶㐷㒰㒱㓚㓛㓜"); 86 okay &= checkBucket(index, "10劃", "㑣㑥㑦㑧㑨㑩㑪㑫㑬㑭㒭㓐㓑㓒"); 87 okay &= checkBucket(index, "20劃", "㒤㒥㒦㒹㔒㘓㘔㘥㚀㜶㜷㜸㠤"); 88 okay &= checkBucket(index, "39劃", "靐"); 89 okay &= checkBucket(index, "48劃", "龘"); 90 91 assertTrue("Some tests failed. See logcat for details", okay); 92 93 /* 94 D ContactsIcuTest: Locales=[zh-Hant-TW] 95 D ContactLocale: Labels=[…,1劃,2劃,3劃,4劃,5劃,6劃,7劃,8劃,9劃,10劃,11劃,12劃,13劃,14劃,15劃,16劃,17劃,18劃,19劃,20劃,21劃,22劃,23劃,24劃,25劃,26劃,27劃,28劃,29劃,30劃,31劃,32劃,33劃,35劃,36劃,39劃,48劃,…] 96 E ContactsIcuTest: Bucket for '㐅' expected to be '2劃', but was '48劃' 97 E ContactsIcuTest: Bucket for '㐃' expected to be '3劃', but was '48劃' 98 E ContactsIcuTest: Bucket for '㐇' expected to be '3劃', but was '48劃' 99 E ContactsIcuTest: Bucket for '㐈' expected to be '3劃', but was '48劃' 100 E ContactsIcuTest: Bucket for '㐉' expected to be '3劃', but was '48劃' 101 E ContactsIcuTest: Bucket for '㔿' expected to be '3劃', but was '48劃' 102 E ContactsIcuTest: Bucket for '㐊' expected to be '4劃', but was '48劃' 103 E ContactsIcuTest: Bucket for '㐋' expected to be '4劃', but was '48劃' 104 E ContactsIcuTest: Bucket for '㐧' expected to be '4劃', but was '48劃' 105 E ContactsIcuTest: Bucket for '㓀' expected to be '4劃', but was '48劃' 106 E ContactsIcuTest: Bucket for '㓅' expected to be '4劃', but was '48劃' 107 E ContactsIcuTest: Bucket for '㕕' expected to be '4劃', but was '48劃' 108 E ContactsIcuTest: Bucket for '㕚' expected to be '4劃', but was '48劃' 109 E ContactsIcuTest: Bucket for '㕛' expected to be '4劃', but was '48劃' 110 E ContactsIcuTest: Bucket for '㝉' expected to be '4劃', but was '48劃' 111 E ContactsIcuTest: Bucket for '㞢' expected to be '4劃', but was '48劃' 112 E ContactsIcuTest: Bucket for '㠪' expected to be '4劃', but was '48劃' 113 E ContactsIcuTest: Bucket for '㢧' expected to be '4劃', but was '48劃' 114 E ContactsIcuTest: Bucket for '㲸' expected to be '4劃', but was '48劃' 115 E ContactsIcuTest: Bucket for '㐌' expected to be '5劃', but was '48劃' 116 E ContactsIcuTest: Bucket for '㐍' expected to be '5劃', but was '48劃' 117 E ContactsIcuTest: Bucket for '㐎' expected to be '5劃', but was '48劃' 118 E ContactsIcuTest: Bucket for '㐏' expected to be '5劃', but was '48劃' 119 E ContactsIcuTest: Bucket for '㒱' expected to be '5劃', but was '48劃' 120 E ContactsIcuTest: Bucket for '㓚' expected to be '5劃', but was '48劃' 121 E ContactsIcuTest: Bucket for '㓛' expected to be '5劃', but was '48劃' 122 E ContactsIcuTest: Bucket for '㓜' expected to be '5劃', but was '48劃' 123 E ContactsIcuTest: Bucket for '㑣' expected to be '10劃', but was '48劃' 124 E ContactsIcuTest: Bucket for '㑧' expected to be '10劃', but was '48劃' 125 E ContactsIcuTest: Bucket for '㑨' expected to be '10劃', but was '48劃' 126 E ContactsIcuTest: Bucket for '㑩' expected to be '10劃', but was '48劃' 127 E ContactsIcuTest: Bucket for '㑪' expected to be '10劃', but was '48劃' 128 E ContactsIcuTest: Bucket for '㑫' expected to be '10劃', but was '48劃' 129 E ContactsIcuTest: Bucket for '㑬' expected to be '10劃', but was '48劃' 130 E ContactsIcuTest: Bucket for '㑭' expected to be '10劃', but was '48劃' 131 E ContactsIcuTest: Bucket for '㒭' expected to be '10劃', but was '48劃' 132 E ContactsIcuTest: Bucket for '㓐' expected to be '10劃', but was '48劃' 133 E ContactsIcuTest: Bucket for '㓑' expected to be '10劃', but was '48劃' 134 E ContactsIcuTest: Bucket for '㓒' expected to be '10劃', but was '48劃' 135 E ContactsIcuTest: Bucket for '㒤' expected to be '20劃', but was '48劃' 136 E ContactsIcuTest: Bucket for '㒦' expected to be '20劃', but was '48劃' 137 E ContactsIcuTest: Bucket for '㒹' expected to be '20劃', but was '48劃' 138 E ContactsIcuTest: Bucket for '㔒' expected to be '20劃', but was '48劃' 139 E ContactsIcuTest: Bucket for '㘓' expected to be '20劃', but was '48劃' 140 E ContactsIcuTest: Bucket for '㘔' expected to be '20劃', but was '48劃' 141 E ContactsIcuTest: Bucket for '㚀' expected to be '20劃', but was '48劃' 142 E ContactsIcuTest: Bucket for '㠤' expected to be '20劃', but was '48劃' 143 */ 144 } 145 } 146