Home | History | Annotate | Download | only in addressinput
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      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.i18n.addressinput;
     18 
     19 import com.android.i18n.addressinput.LookupKey.KeyType;
     20 
     21 import java.util.HashMap;
     22 import java.util.Map;
     23 
     24 import junit.framework.TestCase;
     25 
     26 /**
     27  * Unit tests for the LookupKey class.
     28  */
     29 public class LookupKeyTest extends TestCase {
     30     private static final String ROOT_KEY = "data";
     31     private static final String ROOT_EXAMPLE_KEY = "examples";
     32     private static final String US_KEY = "data/US";
     33     private static final String CALIFORNIA_KEY = "data/US/CA";
     34     private static final String EXAMPLE_LOCAL_US_KEY = "examples/US/local/_default";
     35 
     36     // Data key for Da-an District, Taipei Taiwan
     37     private static final String TW_KEY = "data/TW/\u53F0\u5317\u5E02/\u5927\u5B89\u5340";
     38 
     39     // Example key for TW's address (local script)
     40     private static final String TW_EXAMPLE_LOCAL_KEY = "examples/TW/local/_default";
     41 
     42     // Example key for TW's address (latin script)
     43     private static final String TW_EXAMPLE_LATIN_KEY = "examples/TW/latin/_default";
     44 
     45     private static final String RANDOM_KEY = "sdfIisooIFOOBAR";
     46     private static final String RANDOM_COUNTRY_KEY = "data/asIOSDxcowW";
     47 
     48     public void testRootKey() {
     49         LookupKey key = new LookupKey.Builder(KeyType.DATA).build();
     50         assertEquals(ROOT_KEY, key.toString());
     51 
     52         LookupKey key2 = new LookupKey.Builder(key.toString()).build();
     53         assertEquals(ROOT_KEY, key2.toString());
     54     }
     55 
     56     public void testDataKeys() {
     57         LookupKey key = new LookupKey.Builder(US_KEY).build();
     58         assertEquals(US_KEY, key.toString());
     59 
     60         LookupKey key2 = new LookupKey.Builder(CALIFORNIA_KEY).build();
     61         assertEquals(CALIFORNIA_KEY, key2.toString());
     62     }
     63 
     64     public void testExampleRootKeys() {
     65         LookupKey key = new LookupKey.Builder(KeyType.EXAMPLES).build();
     66         assertEquals(ROOT_EXAMPLE_KEY, key.toString());
     67     }
     68 
     69     public void testExampleKeys() {
     70         AddressData address = new AddressData.Builder().setCountry("US")
     71                                                        .setLanguageCode("en")
     72                                                        .build();
     73 
     74         LookupKey key = new LookupKey.Builder(KeyType.EXAMPLES).setAddressData(address).build();
     75         assertEquals(EXAMPLE_LOCAL_US_KEY, key.toString());
     76 
     77         key = new LookupKey.Builder(EXAMPLE_LOCAL_US_KEY).build();
     78         assertEquals(EXAMPLE_LOCAL_US_KEY, key.toString());
     79     }
     80 
     81     public void testKeyWithWrongScriptType() {
     82         String wrongScript = "examples/US/asdfasdfasdf/_default";
     83         try {
     84             new LookupKey.Builder(wrongScript).build();
     85             fail("should fail since the script type is wrong");
     86         } catch (RuntimeException e) {
     87             // Expected.
     88         }
     89     }
     90 
     91     public void testFallbackToCountry() {
     92         // Admin Area is missing.
     93         AddressData address = new AddressData.Builder().setCountry("US")
     94                                                        .setLocality("Mt View")
     95                                                        .build();
     96 
     97         LookupKey key = new LookupKey.Builder(KeyType.DATA).setAddressData(address).build();
     98 
     99         assertEquals("locality should be omitted since admin area is not specified", US_KEY,
    100                      key.toString());
    101 
    102         // Tries key string with the same problem (missing Admin Area).
    103         key = new LookupKey.Builder("data/US//Mt View").build();
    104 
    105         assertEquals("locality should be omitted since admin area is not specified", US_KEY,
    106                      key.toString());
    107     }
    108 
    109     public void testNonUsAddress() {
    110         AddressData address = new AddressData.Builder().setCountry("TW")
    111                                                        // Taipei City
    112                                                        .setAdminArea("\u53F0\u5317\u5E02")
    113                                                        // Da-an District
    114                                                        .setLocality("\u5927\u5B89\u5340")
    115                                                        .build();
    116 
    117         LookupKey key = new LookupKey.Builder(KeyType.DATA).setAddressData(address).build();
    118         assertEquals(TW_KEY, key.toString());
    119 
    120         key = new LookupKey.Builder(KeyType.EXAMPLES).setAddressData(address).build();
    121         assertEquals(TW_EXAMPLE_LOCAL_KEY, key.toString());
    122 
    123         address = new AddressData.Builder(address).setLanguageCode("zh-latn").build();
    124         key = new LookupKey.Builder(KeyType.EXAMPLES).setAddressData(address).build();
    125         assertEquals(TW_EXAMPLE_LATIN_KEY, key.toString());
    126     }
    127 
    128     public void testGetKeyForUpperLevelFieldWithDataKey() {
    129         AddressData address = new AddressData.Builder().setCountry("US")
    130                                                        .setAdminArea("CA")
    131                                                        .setLocality("Mt View")
    132                                                        .build();
    133 
    134         LookupKey key = new LookupKey.Builder(KeyType.DATA).setAddressData(address).build();
    135         LookupKey newKey = key.getKeyForUpperLevelField(AddressField.COUNTRY);
    136         assertNotNull("failed to get key for " + AddressField.COUNTRY, newKey);
    137         assertEquals("data/US", newKey.toString());
    138 
    139         newKey = key.getKeyForUpperLevelField(AddressField.ADMIN_AREA);
    140         assertNotNull("failed to get key for " + AddressField.ADMIN_AREA, newKey);
    141         assertEquals("data/US/CA", newKey.toString());
    142         assertEquals("original key should not be changed", "data/US/CA/Mt View", key.toString());
    143 
    144         newKey = key.getKeyForUpperLevelField(AddressField.LOCALITY);
    145         assertNotNull("failed to get key for " + AddressField.LOCALITY, newKey);
    146         assertEquals("data/US/CA/Mt View", newKey.toString());
    147 
    148         newKey = key.getKeyForUpperLevelField(AddressField.DEPENDENT_LOCALITY);
    149         assertNull("should return null for field not contained in current key", newKey);
    150 
    151         newKey = key.getKeyForUpperLevelField(AddressField.RECIPIENT);
    152         assertNull("should return null since field '" + AddressField.RECIPIENT +
    153                    "' is not in address hierarchy", newKey);
    154     }
    155 
    156     public void testGetKeyForUpperLevelFieldWithExampleKey() {
    157         LookupKey key = new LookupKey.Builder("examples/US/latin/_default").build();
    158 
    159         try {
    160             key.getKeyForUpperLevelField(AddressField.COUNTRY);
    161             fail("should fail if you try to get parent key for an example key.");
    162         } catch (RuntimeException e) {
    163             // Expected.
    164         }
    165     }
    166 
    167     public void testGetParentKey() {
    168         AddressData address = new AddressData.Builder().setCountry("US")
    169                                                        .setAdminArea("CA")
    170                                                        .setLocality("Mt View")
    171                                                        .setDependentLocality("El Camino")
    172                                                        .build();
    173 
    174         LookupKey key = new LookupKey.Builder(KeyType.DATA).setAddressData(address).build();
    175         assertEquals("data/US/CA/Mt View/El Camino", key.toString());
    176 
    177         key = key.getParentKey();
    178         assertEquals("data/US/CA/Mt View", key.toString());
    179 
    180         key = key.getParentKey();
    181         assertEquals("data/US/CA", key.toString());
    182 
    183         key = key.getParentKey();
    184         assertEquals("data/US", key.toString());
    185 
    186         key = key.getParentKey();
    187         assertEquals("data", key.toString());
    188 
    189         key = key.getParentKey();
    190         assertNull("root key's parent should be null", key);
    191     }
    192 
    193     public void testInvalidKeyTypeWillFail() {
    194         try {
    195             new LookupKey.Builder(RANDOM_KEY).build();
    196             fail("Should fail if key string does not start with a valid key type");
    197         } catch (RuntimeException e) {
    198             // Expected.
    199         }
    200     }
    201 
    202     /**
    203      * Ensures that even when the input key string is random, we still create a key. (We do not
    204      * verify if the key maps to an real world entity like a city or country).
    205      */
    206     public void testWeDontVerifyKeyName() {
    207         LookupKey key = new LookupKey.Builder(RANDOM_COUNTRY_KEY).build();
    208         assertEquals(RANDOM_COUNTRY_KEY, key.toString());
    209     }
    210 
    211     public void testHash() {
    212         String keys[] = { ROOT_KEY, ROOT_EXAMPLE_KEY, US_KEY, CALIFORNIA_KEY };
    213         Map<LookupKey, String> map = new HashMap<LookupKey, String>();
    214 
    215         for (String key : keys) {
    216             map.put(new LookupKey.Builder(key).build(), key);
    217         }
    218 
    219         for (String key : keys) {
    220             assertTrue(map.containsKey(new LookupKey.Builder(key).build()));
    221             assertEquals(key, map.get(new LookupKey.Builder(key).build()));
    222         }
    223         assertFalse(map.containsKey(new LookupKey.Builder(RANDOM_COUNTRY_KEY).build()));
    224     }
    225 
    226     public void testGetValueForUpperLevelField() {
    227         LookupKey key = new LookupKey.Builder("data/US/CA").build();
    228         assertEquals("US", key.getValueForUpperLevelField(AddressField.COUNTRY));
    229     }
    230 
    231     public void testGetValueForUpperLevelFieldInvalid() {
    232         LookupKey key = new LookupKey.Builder("data").build();
    233         assertEquals("", key.getValueForUpperLevelField(AddressField.COUNTRY));
    234         LookupKey key2 = new LookupKey.Builder("data/").build();
    235         assertEquals("", key2.getValueForUpperLevelField(AddressField.COUNTRY));
    236     }
    237 }
    238