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
17package com.android.i18n.addressinput;
18
19import com.android.i18n.addressinput.testing.AsyncTestCase;
20
21/**
22 * Tests for the ClientData class, to ensure it uses the cache data to correctly fetch data from the
23 * server and recovers if no data is present.
24 */
25public class ClientDataTest extends AsyncTestCase {
26    private ClientData client;
27
28    @Override
29    public void setUp() {
30        client = new ClientData(new CacheData());
31    }
32
33    public void testGet() {
34        AddressVerificationNodeData data = client.get("data");
35        assertNotNull(data);
36    }
37
38    public void testGet2() {
39        AddressVerificationNodeData data;
40
41        data = client.get("data");
42        assertNotNull(data);
43
44        data = client.get("data");
45        assertNotNull(data);
46    }
47
48    public void testPrefetchCountry() {
49        delayTestFinish(60000);
50
51        client.prefetchCountry("TW", new DataLoadListener() {
52            boolean beginCalled = false;
53
54            @Override
55            public void dataLoadingBegin() {
56                beginCalled = true;
57            }
58
59            @Override
60            public void dataLoadingEnd() {
61                assertTrue("dataLoadingBegin should be called", beginCalled);
62                // Currently this test only tests that the execution doesn't crash and eventually
63                // terminates. TODO: Write test cases to verify that correct data is loaded.
64                finishTest();
65            }
66        });
67    }
68
69    public void testFetchDataWithBadServer() {
70        CacheData badCache = new CacheData();
71        badCache.setUrl("http://www.google.com");
72        ClientData badServerClient = new ClientData(badCache);
73
74        AddressVerificationNodeData data = badServerClient.get("data/US");
75
76        // No data was available on the server or in the cache - it should check
77        // that there is nothing in region data constants, and should return the
78        // data from there.
79        assertNotNull(data);
80        String unitedStatesFormatInfo = data.get(AddressDataKey.FMT);
81        assertEquals("%N%n%O%n%A%n%C, %S %Z", unitedStatesFormatInfo);
82    }
83}
84