Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / java / test / com / android / i18n / addressinput / CacheDataTest.java
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.testing.AddressDataMapLoader;
20 import com.android.i18n.addressinput.testing.AsyncTestCase;
21
22 import org.json.JSONException;
23 import org.json.JSONObject;
24
25 public class CacheDataTest extends AsyncTestCase {
26   private CacheData cache;
27
28   private static final String DELIM = "~";
29
30   private static final String CANADA_KEY = "data/CA";
31
32   private static final String US_KEY = "data/US";
33
34   private static final String CALIFORNIA_KEY = "data/US/CA";
35
36   private static final String RANDOM_COUNTRY_KEY = "data/asIOSDxcowW";
37
38   private static final String EXAMPLE_LOCAL_US_KEY = "examples/US/local/_default";
39
40   // Data key for Da-an District, Taipei Taiwan
41   private static final String TW_KEY = "data/TW/\u53F0\u5317\u5E02/\u5927\u5B89\u5340";
42
43   private static final String FRANCE_KEY = "data/FR";
44
45   private static Integer listenerInvokeCount = 0;
46
47   private static boolean reachedMaxCount = false;
48
49   @Override
50   public void setUp() {
51     cache = new CacheData();
52   }
53
54   public void testJsonConstructor() {
55     // Creating cache with content.
56     String id = "data/CA";
57     JSONObject jsonObject = null;
58     try {
59       jsonObject = new JSONObject(AddressDataMapLoader.DATA.get(id));
60     } catch (JSONException jsonException) {
61       // If this throws an exception the test fails.
62       fail("Can't parse json object");
63     }
64     cache.addToJsoMap(id, jsonObject);
65     String toBackup = cache.getJsonString();
66
67     // Creating cache from saved data.
68     cache = new CacheData(toBackup);
69     assertTrue(cache.containsKey(id));
70   }
71
72   public void testJsonConstructorTruncatedProperString() {
73     // Creating cache with content.
74     String id = "data/CA";
75     try {
76       JSONObject jsonObject = new JSONObject(AddressDataMapLoader.DATA.get(id));
77       String jsonString = jsonObject.toString();
78       jsonString = jsonString.substring(0, jsonString.length() / 2);
79
80       cache = new CacheData(jsonString);
81       assertTrue(cache.toString(), cache.isEmpty());
82     } catch (JSONException jsonException) {
83       // If this throws an exception the test fails.
84       fail("Can't parse json object");
85     }
86   }
87
88   public void testSimpleFetching() {
89     final LookupKey key = new LookupKey.Builder(CANADA_KEY).build();
90
91     delayTestFinish(10000);
92
93     cache.fetchDynamicData(key, null, new DataLoadListener() {
94       boolean beginCalled = false;
95
96       @Override
97       public void dataLoadingBegin() {
98         beginCalled = true;
99       }
100
101       @Override
102       public void dataLoadingEnd() {
103         assertTrue("dataLoadingBegin should be called", beginCalled);
104         JsoMap map = cache.getObj(CANADA_KEY);
105
106         assertTrue(map.containsKey(AddressDataKey.ID.name().toLowerCase()));
107         assertTrue(map.containsKey(AddressDataKey.LANG.name().toLowerCase()));
108         assertTrue(map.containsKey(AddressDataKey.ZIP.name().toLowerCase()));
109         assertTrue(map.containsKey(AddressDataKey.FMT.name().toLowerCase()));
110         assertTrue(map.containsKey(AddressDataKey.SUB_KEYS.name().toLowerCase()));
111         assertTrue(map.containsKey(AddressDataKey.SUB_NAMES.name().toLowerCase()));
112         assertFalse(map.containsKey(AddressDataKey.SUB_LNAMES.name().toLowerCase()));
113
114         int namesSize =
115             map.get(AddressDataKey.SUB_NAMES.name().toLowerCase()).split(DELIM).length;
116         int keysSize =
117             map.get(AddressDataKey.SUB_KEYS.name().toLowerCase()).split(DELIM).length;
118
119         assertEquals("Expect 13 states in Canada.", 13, namesSize);
120         assertEquals(namesSize, keysSize);
121         finishTest();
122       }
123     });
124   }
125
126   public void testFetchingTaiwanData() {
127     final LookupKey key = new LookupKey.Builder(TW_KEY).build();
128
129     delayTestFinish(10000);
130
131     cache.fetchDynamicData(key, null, new DataLoadListener() {
132       boolean beginCalled = false;
133
134       @Override
135       public void dataLoadingBegin() {
136         beginCalled = true;
137       }
138
139       @Override
140       public void dataLoadingEnd() {
141         assertTrue("dataLoadingBegin should be called", beginCalled);
142
143         JsoMap map = cache.getObj(TW_KEY);
144
145         assertTrue(map.containsKey(AddressDataKey.ID.name().toLowerCase()));
146         assertTrue(map.containsKey(AddressDataKey.KEY.name().toLowerCase()));
147         assertTrue(map.containsKey(AddressDataKey.LANG.name().toLowerCase()));
148         assertTrue(map.containsKey(AddressDataKey.ZIP.name().toLowerCase()));
149         assertFalse(map.containsKey(AddressDataKey.FMT.name().toLowerCase()));
150         assertFalse(map.containsKey(AddressDataKey.SUB_KEYS.name().toLowerCase()));
151         assertFalse(map.containsKey(AddressDataKey.SUB_NAMES.name().toLowerCase()));
152         assertFalse(map.containsKey(AddressDataKey.SUB_LNAMES.name().toLowerCase()));
153
154         // Da-an district.
155         assertEquals("\u5927\u5B89\u5340",
156             map.get(AddressDataKey.KEY.name().toLowerCase()));
157
158         assertEquals("zh-hant", map.get(AddressDataKey.LANG.name().toLowerCase()));
159
160         finishTest();
161       }
162     });
163   }
164
165   public void testFetchingExamples() {
166     final LookupKey key = new LookupKey.Builder(EXAMPLE_LOCAL_US_KEY).build();
167
168     delayTestFinish(10000);
169
170     cache.fetchDynamicData(key, null, new DataLoadListener() {
171       boolean beginCalled = false;
172
173       @Override
174       public void dataLoadingBegin() {
175         beginCalled = true;
176       }
177
178       @Override
179       public void dataLoadingEnd() {
180         assertTrue("dataLoadingBegin should be called", beginCalled);
181
182         JsoMap map = cache.getObj(EXAMPLE_LOCAL_US_KEY);
183         assertTrue(map.containsKey("name"));
184         finishTest();
185       }
186     });
187   }
188
189   public void testFetchingOneKeyManyTimes() {
190     final LookupKey key = new LookupKey.Builder(CALIFORNIA_KEY).build();
191     final int maxCount = 10;
192
193     class CounterListener implements DataLoadListener {
194       @Override
195       public void dataLoadingBegin() {
196         listenerInvokeCount++;
197         if (listenerInvokeCount == maxCount) {
198           reachedMaxCount = true;
199         }
200         assertTrue("CounterListener's dataLoadingBegin should not be invoked for more " +
201             "than " + maxCount + " times",
202             listenerInvokeCount <= maxCount);
203       }
204
205       @Override
206       public void dataLoadingEnd() {
207         listenerInvokeCount--;
208         assertTrue(listenerInvokeCount >= 0);
209         if (listenerInvokeCount == 0) {
210           assertTrue("Expect to see key " + key + " cached when CounterListener's " +
211               " dataLoadingEnd is invoked",
212               cache.containsKey(key.toString()));
213           assertTrue("Expect CounterListener's dataLoadingEnd to be triggered " +
214               maxCount + " times in total", reachedMaxCount);
215           finishTest();
216         }
217       }
218     }
219
220     delayTestFinish(10000);
221
222     for (int i = 0; i < maxCount; ++i) {
223       cache.fetchDynamicData(key, null, new CounterListener());
224     }
225
226     // Null listeners should not affect results.
227     cache.fetchDynamicData(key, null, null);
228     cache.fetchDynamicData(key, null, null);
229     cache.fetchDynamicData(key, null, null);
230   }
231
232   public void testFetchAgainRightAfterOneFetchStart() {
233     final LookupKey key = new LookupKey.Builder(US_KEY).build();
234
235     delayTestFinish(10000);
236
237     cache.fetchDynamicData(key, null, null);
238
239     cache.fetchDynamicData(key, null, new DataLoadListener() {
240       boolean beginCalled = false;
241
242       @Override
243       public void dataLoadingBegin() {
244         assertFalse("data for key " + key + " should not be fetched yet",
245             cache.containsKey(key.toString()));
246         beginCalled = true;
247       }
248
249       @Override
250       public void dataLoadingEnd() {
251         assertTrue("dataLoadingBegin should be called", beginCalled);
252
253         assertTrue(cache.containsKey(key.toString()));
254
255         cache.fetchDynamicData(key, null, new DataLoadListener() {
256           boolean beginCalled2 = false;
257
258           @Override
259           public void dataLoadingBegin() {
260             beginCalled2 = true;
261           }
262
263           @Override
264           public void dataLoadingEnd() {
265             assertTrue("dataLoadingBegin should be called", beginCalled2);
266
267             assertTrue(cache.containsKey(key.toString()));
268             finishTest();
269           }
270         });
271       }
272     });
273   }
274
275   public void testInvalidKey() {
276     final LookupKey key = new LookupKey.Builder(RANDOM_COUNTRY_KEY).build();
277
278     delayTestFinish(15000);
279
280     cache.fetchDynamicData(key, null, new DataLoadListener() {
281       boolean beginCalled = false;
282
283       @Override
284       public void dataLoadingBegin() {
285         beginCalled = true;
286       }
287
288       @Override
289       public void dataLoadingEnd() {
290         assertTrue("dataLoadingBegin should be called", beginCalled);
291         assertFalse(cache.containsKey(key.toString()));
292
293         finishTest();
294       }
295     });
296   }
297
298   public void testSetUrl() {
299     final LookupKey key = new LookupKey.Builder(FRANCE_KEY).build();
300     final String originalUrl = cache.getUrl();
301
302     assertFalse(FRANCE_KEY + " should not be in the cache. Do you request it before this test?",
303         cache.containsKey(key.toString()));
304
305     delayTestFinish(10000);
306     // Something that is not an URL.
307     cache.setUrl("FDSSfdfdsfasdfadsf");
308
309     cache.fetchDynamicData(key, null, new DataLoadListener() {
310       boolean beginCalled = false;
311
312       @Override
313       public void dataLoadingBegin() {
314         beginCalled = true;
315       }
316
317       @Override
318       public void dataLoadingEnd() {
319         assertTrue("dataLoadingBegin should be called", beginCalled);
320         assertFalse(cache.containsKey(key.toString()));
321         cache.setUrl(originalUrl);
322         finishTest();
323       }
324     });
325   }
326 }