aboutsummaryrefslogtreecommitdiff
path: root/java/test/com/android/i18n/addressinput/CacheDataTest.java
blob: 7b72e47898acba04b012671ac67a063d1691c290 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * 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.testing.AddressDataMapLoader;
import com.android.i18n.addressinput.testing.AsyncTestCase;

import org.json.JSONException;
import org.json.JSONObject;

public class CacheDataTest extends AsyncTestCase {
  private CacheData cache;

  private static final String DELIM = "~";

  private static final String CANADA_KEY = "data/CA";

  private static final String US_KEY = "data/US";

  private static final String CALIFORNIA_KEY = "data/US/CA";

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

  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";

  private static final String FRANCE_KEY = "data/FR";

  private static Integer listenerInvokeCount = 0;

  private static boolean reachedMaxCount = false;

  @Override
  public void setUp() {
    cache = new CacheData();
  }

  public void testJsonConstructor() {
    // Creating cache with content.
    String id = "data/CA";
    JSONObject jsonObject = null;
    try {
      jsonObject = new JSONObject(AddressDataMapLoader.DATA.get(id));
    } catch (JSONException jsonException) {
      // If this throws an exception the test fails.
      fail("Can't parse json object");
    }
    cache.addToJsoMap(id, jsonObject);
    String toBackup = cache.getJsonString();

    // Creating cache from saved data.
    cache = new CacheData(toBackup);
    assertTrue(cache.containsKey(id));
  }

  public void testJsonConstructorTruncatedProperString() {
    // Creating cache with content.
    String id = "data/CA";
    try {
      JSONObject jsonObject = new JSONObject(AddressDataMapLoader.DATA.get(id));
      String jsonString = jsonObject.toString();
      jsonString = jsonString.substring(0, jsonString.length() / 2);

      cache = new CacheData(jsonString);
      assertTrue(cache.toString(), cache.isEmpty());
    } catch (JSONException jsonException) {
      // If this throws an exception the test fails.
      fail("Can't parse json object");
    }
  }

  public void testSimpleFetching() {
    final LookupKey key = new LookupKey.Builder(CANADA_KEY).build();

    delayTestFinish(10000);

    cache.fetchDynamicData(key, null, new DataLoadListener() {
      boolean beginCalled = false;

      @Override
      public void dataLoadingBegin() {
        beginCalled = true;
      }

      @Override
      public void dataLoadingEnd() {
        assertTrue("dataLoadingBegin should be called", beginCalled);
        JsoMap map = cache.getObj(CANADA_KEY);

        assertTrue(map.containsKey(AddressDataKey.ID.name().toLowerCase()));
        assertTrue(map.containsKey(AddressDataKey.LANG.name().toLowerCase()));
        assertTrue(map.containsKey(AddressDataKey.ZIP.name().toLowerCase()));
        assertTrue(map.containsKey(AddressDataKey.FMT.name().toLowerCase()));
        assertTrue(map.containsKey(AddressDataKey.SUB_KEYS.name().toLowerCase()));
        assertTrue(map.containsKey(AddressDataKey.SUB_NAMES.name().toLowerCase()));
        assertFalse(map.containsKey(AddressDataKey.SUB_LNAMES.name().toLowerCase()));

        int namesSize =
            map.get(AddressDataKey.SUB_NAMES.name().toLowerCase()).split(DELIM).length;
        int keysSize =
            map.get(AddressDataKey.SUB_KEYS.name().toLowerCase()).split(DELIM).length;

        assertEquals("Expect 13 states in Canada.", 13, namesSize);
        assertEquals(namesSize, keysSize);
        finishTest();
      }
    });
  }

  public void testFetchingTaiwanData() {
    final LookupKey key = new LookupKey.Builder(TW_KEY).build();

    delayTestFinish(10000);

    cache.fetchDynamicData(key, null, new DataLoadListener() {
      boolean beginCalled = false;

      @Override
      public void dataLoadingBegin() {
        beginCalled = true;
      }

      @Override
      public void dataLoadingEnd() {
        assertTrue("dataLoadingBegin should be called", beginCalled);

        JsoMap map = cache.getObj(TW_KEY);

        assertTrue(map.containsKey(AddressDataKey.ID.name().toLowerCase()));
        assertTrue(map.containsKey(AddressDataKey.KEY.name().toLowerCase()));
        assertTrue(map.containsKey(AddressDataKey.LANG.name().toLowerCase()));
        assertTrue(map.containsKey(AddressDataKey.ZIP.name().toLowerCase()));
        assertFalse(map.containsKey(AddressDataKey.FMT.name().toLowerCase()));
        assertFalse(map.containsKey(AddressDataKey.SUB_KEYS.name().toLowerCase()));
        assertFalse(map.containsKey(AddressDataKey.SUB_NAMES.name().toLowerCase()));
        assertFalse(map.containsKey(AddressDataKey.SUB_LNAMES.name().toLowerCase()));

        // Da-an district.
        assertEquals("\u5927\u5B89\u5340",
            map.get(AddressDataKey.KEY.name().toLowerCase()));

        assertEquals("zh-hant", map.get(AddressDataKey.LANG.name().toLowerCase()));

        finishTest();
      }
    });
  }

  public void testFetchingExamples() {
    final LookupKey key = new LookupKey.Builder(EXAMPLE_LOCAL_US_KEY).build();

    delayTestFinish(10000);

    cache.fetchDynamicData(key, null, new DataLoadListener() {
      boolean beginCalled = false;

      @Override
      public void dataLoadingBegin() {
        beginCalled = true;
      }

      @Override
      public void dataLoadingEnd() {
        assertTrue("dataLoadingBegin should be called", beginCalled);

        JsoMap map = cache.getObj(EXAMPLE_LOCAL_US_KEY);
        assertTrue(map.containsKey("name"));
        finishTest();
      }
    });
  }

  public void testFetchingOneKeyManyTimes() {
    final LookupKey key = new LookupKey.Builder(CALIFORNIA_KEY).build();
    final int maxCount = 10;

    class CounterListener implements DataLoadListener {
      @Override
      public void dataLoadingBegin() {
        listenerInvokeCount++;
        if (listenerInvokeCount == maxCount) {
          reachedMaxCount = true;
        }
        assertTrue("CounterListener's dataLoadingBegin should not be invoked for more " +
            "than " + maxCount + " times",
            listenerInvokeCount <= maxCount);
      }

      @Override
      public void dataLoadingEnd() {
        listenerInvokeCount--;
        assertTrue(listenerInvokeCount >= 0);
        if (listenerInvokeCount == 0) {
          assertTrue("Expect to see key " + key + " cached when CounterListener's " +
              " dataLoadingEnd is invoked",
              cache.containsKey(key.toString()));
          assertTrue("Expect CounterListener's dataLoadingEnd to be triggered " +
              maxCount + " times in total", reachedMaxCount);
          finishTest();
        }
      }
    }

    delayTestFinish(10000);

    for (int i = 0; i < maxCount; ++i) {
      cache.fetchDynamicData(key, null, new CounterListener());
    }

    // Null listeners should not affect results.
    cache.fetchDynamicData(key, null, null);
    cache.fetchDynamicData(key, null, null);
    cache.fetchDynamicData(key, null, null);
  }

  public void testFetchAgainRightAfterOneFetchStart() {
    final LookupKey key = new LookupKey.Builder(US_KEY).build();

    delayTestFinish(10000);

    cache.fetchDynamicData(key, null, null);

    cache.fetchDynamicData(key, null, new DataLoadListener() {
      boolean beginCalled = false;

      @Override
      public void dataLoadingBegin() {
        assertFalse("data for key " + key + " should not be fetched yet",
            cache.containsKey(key.toString()));
        beginCalled = true;
      }

      @Override
      public void dataLoadingEnd() {
        assertTrue("dataLoadingBegin should be called", beginCalled);

        assertTrue(cache.containsKey(key.toString()));

        cache.fetchDynamicData(key, null, new DataLoadListener() {
          boolean beginCalled2 = false;

          @Override
          public void dataLoadingBegin() {
            beginCalled2 = true;
          }

          @Override
          public void dataLoadingEnd() {
            assertTrue("dataLoadingBegin should be called", beginCalled2);

            assertTrue(cache.containsKey(key.toString()));
            finishTest();
          }
        });
      }
    });
  }

  public void testInvalidKey() {
    final LookupKey key = new LookupKey.Builder(RANDOM_COUNTRY_KEY).build();

    delayTestFinish(15000);

    cache.fetchDynamicData(key, null, new DataLoadListener() {
      boolean beginCalled = false;

      @Override
      public void dataLoadingBegin() {
        beginCalled = true;
      }

      @Override
      public void dataLoadingEnd() {
        assertTrue("dataLoadingBegin should be called", beginCalled);
        assertFalse(cache.containsKey(key.toString()));

        finishTest();
      }
    });
  }

  public void testSetUrl() {
    final LookupKey key = new LookupKey.Builder(FRANCE_KEY).build();
    final String originalUrl = cache.getUrl();

    assertFalse(FRANCE_KEY + " should not be in the cache. Do you request it before this test?",
        cache.containsKey(key.toString()));

    delayTestFinish(10000);
    // Something that is not an URL.
    cache.setUrl("FDSSfdfdsfasdfadsf");

    cache.fetchDynamicData(key, null, new DataLoadListener() {
      boolean beginCalled = false;

      @Override
      public void dataLoadingBegin() {
        beginCalled = true;
      }

      @Override
      public void dataLoadingEnd() {
        assertTrue("dataLoadingBegin should be called", beginCalled);
        assertFalse(cache.containsKey(key.toString()));
        cache.setUrl(originalUrl);
        finishTest();
      }
    });
  }
}