Home | History | Annotate | Download | only in icu
      1 /*
      2  * Copyright (C) 2017 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 libcore.libcore.icu;
     17 
     18 import org.junit.Test;
     19 import android.icu.text.AlphabeticIndex;
     20 import android.icu.text.UnicodeSet;
     21 import android.icu.util.LocaleData;
     22 import android.icu.util.ULocale;
     23 import java.util.Locale;
     24 
     25 import static org.junit.Assert.assertTrue;
     26 
     27 /**
     28  * Test for {@link android.icu.text.AlphabeticIndex}
     29  */
     30 public class AlphabeticIndexTest {
     31 
     32     @Test
     33     public void test_english() {
     34         verifyIndex(Locale.ENGLISH);
     35     }
     36 
     37     // http://b/64953401
     38     @Test
     39     public void test_amharic() {
     40         Locale amharic = Locale.forLanguageTag("am");
     41         UnicodeSet exemplarSet = LocaleData
     42                 .getExemplarSet(ULocale.forLocale(amharic), 0, LocaleData.ES_INDEX);
     43         // If this assert fails it means that the am locale has gained an exemplar characters set
     44         // for index (see key ExemplarCharactersIndex in locale/am.txt). If that's the case, please
     45         // find another locale that's missing that key where the logic in
     46         // AlphabeticIndex.addIndexExemplars will generate buckets from alternate data.
     47         assertTrue(exemplarSet == null || exemplarSet.isEmpty());
     48         verifyIndex(amharic);
     49     }
     50 
     51     private void verifyIndex(Locale locale) {
     52         ULocale uLocale = ULocale.forLocale(locale);
     53         AlphabeticIndex index = new AlphabeticIndex(uLocale);
     54         LocaleData localeData = LocaleData.getInstance(uLocale);
     55 
     56         // 0 = "default options", there is no constant for this.
     57         UnicodeSet exemplarSet = localeData.getExemplarSet(0, LocaleData.ES_STANDARD);
     58         for (String s : exemplarSet) {
     59             index.addRecord(s, s);
     60         }
     61         assertTrue("Not enough buckets: " + index.getBucketLabels(), index.getBucketCount() > 1);
     62     }
     63 }
     64