aboutsummaryrefslogtreecommitdiff
path: root/java/test/com/android/i18n/addressinput/AddressVerificationDataTest.java
blob: b57955a167621142672b498377dd771f214f1152 (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
/*
 * 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 junit.framework.TestCase;

/**
 * Tests to ensure that {@code AddressVerificationData} can parse all the default data.
 */
public class AddressVerificationDataTest extends TestCase {

    private static final AddressVerificationData ADDRESS_DATA =
            new AddressVerificationData(AddressDataMapLoader.DATA);

    public void testParseAllData() {
        for (String key : ADDRESS_DATA.keys()) {
            AddressVerificationNodeData nodeData = ADDRESS_DATA.get(key);
            assertNotNull(key + " maps to null value.", nodeData);
            assertNotNull("Id is required", nodeData.get(AddressDataKey.ID));
        }
    }

    public void testLoadingCountries() {
        AddressVerificationNodeData nodeData = ADDRESS_DATA.get("data");
        String[] countries = nodeData.get(AddressDataKey.COUNTRIES).split("~");
        assertTrue(countries.length > 0);
    }

    public void testUsData() {
        AddressVerificationNodeData nodeData = ADDRESS_DATA.get("data/US");
        assertEquals("data/US", nodeData.get(AddressDataKey.ID));
        assertNotNull(nodeData.get(AddressDataKey.SUB_KEYS));
        assertNotNull(nodeData.get(AddressDataKey.SUB_NAMES));
        assertEquals("en", nodeData.get(AddressDataKey.LANG));
    }

    public void testCaData() {
        AddressVerificationNodeData nodeData = ADDRESS_DATA.get("data/CA");
        String names = nodeData.get(AddressDataKey.SUB_NAMES);
        String keys = nodeData.get(AddressDataKey.SUB_KEYS);

        assertEquals("data/CA", nodeData.get(AddressDataKey.ID));
        assertEquals("en", nodeData.get(AddressDataKey.LANG));

        assertEquals("AB~BC~MB~NB~NL~NT~NS~NU~ON~PE~QC~SK~YT", keys);
        assertEquals("Alberta~British Columbia~Manitoba~New Brunswick" +
                     "~Newfoundland and Labrador~Northwest Territories~Nova Scotia~Nunavut" +
                     "~Ontario~Prince Edward Island~Quebec~Saskatchewan~Yukon",
                     names);
    }

    public void testCaFrenchData() {
        AddressVerificationNodeData nodeData = ADDRESS_DATA.get("data/CA--fr");
        String names = nodeData.get(AddressDataKey.SUB_NAMES);
        String keys = nodeData.get(AddressDataKey.SUB_KEYS);

        assertEquals("data/CA--fr", nodeData.get(AddressDataKey.ID));
        assertEquals("fr", nodeData.get(AddressDataKey.LANG));
        assertEquals("AB~BC~PE~MB~NB~NS~NU~ON~QC~SK~NL~NT~YT", keys);
        assertTrue(names.contains("Colombie"));
    }

    public void testBackSlashUnEscaped() {
        for (String lookupKey : ADDRESS_DATA.keys()) {
            AddressVerificationNodeData nodeData = ADDRESS_DATA.get(lookupKey);
            for (AddressDataKey dataKey : AddressDataKey.values()) {
                String val = nodeData.get(dataKey);
                if (val != null) {
                    assertFalse("Backslashes need to be unescaped: " + val, val.contains("\\\\"));
                }
            }
        }

        // Spot check.
        assertEquals("Kazhakstan's postal code pattern mismatched", "\\d{6}",
                     ADDRESS_DATA.get("data/KZ").get(AddressDataKey.ZIP));
    }

    public void testExampleData() {
        assertNotNull("Expects example data.", AddressDataMapLoader.DATA.get("examples"));
        assertNotNull("Expects example US address.",
                      AddressDataMapLoader.DATA.get("examples/US/local/en"));
        assertEquals("'examples/TW/local/zh_Hant' and 'examples/TW/local/_default' should " +
                     "return same value.",
                     AddressDataMapLoader.DATA.get("examples/TW/local/zh_Hant"),
                     AddressDataMapLoader.DATA.get("examples/TW/local/_default"));
    }
}