aboutsummaryrefslogtreecommitdiff
path: root/java/test/com/android/i18n/addressinput/LookupKeyTest.java
blob: fa07618751e9679ec118c8396a41c932bfdea0ca (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.i18n.addressinput;

import com.android.i18n.addressinput.LookupKey.KeyType;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

/**
 * Unit tests for the LookupKey class.
 */
public class LookupKeyTest extends TestCase {
  private static final String ROOT_KEY = "data";
  private static final String ROOT_EXAMPLE_KEY = "examples";
  private static final String US_KEY = "data/US";
  private static final String CALIFORNIA_KEY = "data/US/CA";
  private static final String EXAMPLE_LOCAL_US_KEY = "examples/US/local/_default";

  // Data key for Da-an District, Taipei Taiwan
  private static final String TW_KEY = "data/TW/\u53F0\u5317\u5E02/\u5927\u5B89\u5340";

  // Example key for TW's address (local script)
  private static final String TW_EXAMPLE_LOCAL_KEY = "examples/TW/local/_default";

  // Example key for TW's address (latin script)
  private static final String TW_EXAMPLE_LATIN_KEY = "examples/TW/latin/_default";

  private static final String RANDOM_KEY = "sdfIisooIFOOBAR";
  private static final String RANDOM_COUNTRY_KEY = "data/asIOSDxcowW";

  public void testRootKey() {
    LookupKey key = new LookupKey.Builder(KeyType.DATA).build();
    assertEquals(ROOT_KEY, key.toString());

    LookupKey key2 = new LookupKey.Builder(key.toString()).build();
    assertEquals(ROOT_KEY, key2.toString());
  }

  public void testDataKeys() {
    LookupKey key = new LookupKey.Builder(US_KEY).build();
    assertEquals(US_KEY, key.toString());

    LookupKey key2 = new LookupKey.Builder(CALIFORNIA_KEY).build();
    assertEquals(CALIFORNIA_KEY, key2.toString());
  }

  public void testExampleRootKeys() {
    LookupKey key = new LookupKey.Builder(KeyType.EXAMPLES).build();
    assertEquals(ROOT_EXAMPLE_KEY, key.toString());
  }

  public void testExampleKeys() {
    AddressData address = new AddressData.Builder().setCountry("US")
        .setLanguageCode("en")
        .build();

    LookupKey key = new LookupKey.Builder(KeyType.EXAMPLES).setAddressData(address).build();
    assertEquals(EXAMPLE_LOCAL_US_KEY, key.toString());

    key = new LookupKey.Builder(EXAMPLE_LOCAL_US_KEY).build();
    assertEquals(EXAMPLE_LOCAL_US_KEY, key.toString());
  }

  public void testKeyWithWrongScriptType() {
    String wrongScript = "examples/US/asdfasdfasdf/_default";
    try {
      new LookupKey.Builder(wrongScript).build();
      fail("should fail since the script type is wrong");
    } catch (RuntimeException e) {
      // Expected.
    }
  }

  public void testFallbackToCountry() {
    // Admin Area is missing.
    AddressData address = new AddressData.Builder().setCountry("US")
        .setLocality("Mt View")
        .build();

    LookupKey key = new LookupKey.Builder(KeyType.DATA).setAddressData(address).build();

    assertEquals("locality should be omitted since admin area is not specified", US_KEY,
        key.toString());

    // Tries key string with the same problem (missing Admin Area).
    key = new LookupKey.Builder("data/US//Mt View").build();

    assertEquals("locality should be omitted since admin area is not specified", US_KEY,
        key.toString());
  }

  public void testNonUsAddress() {
    AddressData address = new AddressData.Builder().setCountry("TW")
        // Taipei City
        .setAdminArea("\u53F0\u5317\u5E02")
        // Da-an District
        .setLocality("\u5927\u5B89\u5340")
        .build();

    LookupKey key = new LookupKey.Builder(KeyType.DATA).setAddressData(address).build();
    assertEquals(TW_KEY, key.toString());

    key = new LookupKey.Builder(KeyType.EXAMPLES).setAddressData(address).build();
    assertEquals(TW_EXAMPLE_LOCAL_KEY, key.toString());

    address = new AddressData.Builder(address).setLanguageCode("zh-latn").build();
    key = new LookupKey.Builder(KeyType.EXAMPLES).setAddressData(address).build();
    assertEquals(TW_EXAMPLE_LATIN_KEY, key.toString());
  }

  public void testGetKeyForUpperLevelFieldWithDataKey() {
    AddressData address = new AddressData.Builder().setCountry("US")
        .setAdminArea("CA")
        .setLocality("Mt View")
        .build();

    LookupKey key = new LookupKey.Builder(KeyType.DATA).setAddressData(address).build();
    LookupKey newKey = key.getKeyForUpperLevelField(AddressField.COUNTRY);
    assertNotNull("failed to get key for " + AddressField.COUNTRY, newKey);
    assertEquals("data/US", newKey.toString());

    newKey = key.getKeyForUpperLevelField(AddressField.ADMIN_AREA);
    assertNotNull("failed to get key for " + AddressField.ADMIN_AREA, newKey);
    assertEquals("data/US/CA", newKey.toString());
    assertEquals("original key should not be changed", "data/US/CA/Mt View", key.toString());

    newKey = key.getKeyForUpperLevelField(AddressField.LOCALITY);
    assertNotNull("failed to get key for " + AddressField.LOCALITY, newKey);
    assertEquals("data/US/CA/Mt View", newKey.toString());

    newKey = key.getKeyForUpperLevelField(AddressField.DEPENDENT_LOCALITY);
    assertNull("should return null for field not contained in current key", newKey);

    newKey = key.getKeyForUpperLevelField(AddressField.RECIPIENT);
    assertNull("should return null since field '" + AddressField.RECIPIENT +
        "' is not in address hierarchy", newKey);
  }

  public void testGetKeyForUpperLevelFieldWithExampleKey() {
    LookupKey key = new LookupKey.Builder("examples/US/latin/_default").build();

    try {
      key.getKeyForUpperLevelField(AddressField.COUNTRY);
      fail("should fail if you try to get parent key for an example key.");
    } catch (RuntimeException e) {
      // Expected.
    }
  }

  public void testGetParentKey() {
    AddressData address = new AddressData.Builder().setCountry("US")
        .setAdminArea("CA")
        .setLocality("Mt View")
        .setDependentLocality("El Camino")
        .build();

    LookupKey key = new LookupKey.Builder(KeyType.DATA).setAddressData(address).build();
    assertEquals("data/US/CA/Mt View/El Camino", key.toString());

    key = key.getParentKey();
    assertEquals("data/US/CA/Mt View", key.toString());

    key = key.getParentKey();
    assertEquals("data/US/CA", key.toString());

    key = key.getParentKey();
    assertEquals("data/US", key.toString());

    key = key.getParentKey();
    assertEquals("data", key.toString());

    key = key.getParentKey();
    assertNull("root key's parent should be null", key);
  }

  public void testInvalidKeyTypeWillFail() {
    try {
      new LookupKey.Builder(RANDOM_KEY).build();
      fail("Should fail if key string does not start with a valid key type");
    } catch (RuntimeException e) {
      // Expected.
    }
  }

  /**
   * Ensures that even when the input key string is random, we still create a key. (We do not
   * verify if the key maps to an real world entity like a city or country).
   */
  public void testWeDontVerifyKeyName() {
    LookupKey key = new LookupKey.Builder(RANDOM_COUNTRY_KEY).build();
    assertEquals(RANDOM_COUNTRY_KEY, key.toString());
  }

  public void testHash() {
    String keys[] = { ROOT_KEY, ROOT_EXAMPLE_KEY, US_KEY, CALIFORNIA_KEY };
    Map<LookupKey, String> map = new HashMap<LookupKey, String>();

    for (String key : keys) {
      map.put(new LookupKey.Builder(key).build(), key);
    }

    for (String key : keys) {
      assertTrue(map.containsKey(new LookupKey.Builder(key).build()));
      assertEquals(key, map.get(new LookupKey.Builder(key).build()));
    }
    assertFalse(map.containsKey(new LookupKey.Builder(RANDOM_COUNTRY_KEY).build()));
  }

  public void testGetValueForUpperLevelField() {
    LookupKey key = new LookupKey.Builder("data/US/CA").build();
    assertEquals("US", key.getValueForUpperLevelField(AddressField.COUNTRY));
  }

  public void testGetValueForUpperLevelFieldInvalid() {
    LookupKey key = new LookupKey.Builder("data").build();
    assertEquals("", key.getValueForUpperLevelField(AddressField.COUNTRY));
    LookupKey key2 = new LookupKey.Builder("data/").build();
    assertEquals("", key2.getValueForUpperLevelField(AddressField.COUNTRY));
  }
}