Home | History | Annotate | Download | only in addressinput
      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.LookupKey.ScriptType;
     20 
     21 import java.util.ArrayList;
     22 import java.util.List;
     23 
     24 import junit.framework.TestCase;
     25 
     26 /**
     27  * Tests for the FormatInterpreter class.
     28  */
     29 public class FormatInterpreterTest extends TestCase {
     30     private static final AddressData US_CA_ADDRESS;
     31     private static final AddressData TW_ADDRESS;
     32 
     33     private FormatInterpreter formatInterpreter;
     34 
     35     static {
     36         US_CA_ADDRESS = new AddressData.Builder().setCountry("US")
     37                                                  .setAdminArea("CA")
     38                                                  .setLocality("Mt View")
     39                                                  .setAddressLine1("1098 Alta Ave")
     40                                                  .setPostalCode("94043")
     41                                                  .build();
     42 
     43         TW_ADDRESS = new AddressData.Builder().setCountry("TW")
     44                                               .setAdminArea("\u53F0\u5317\u5E02")  // Taipei city
     45                                               .setLocality("\u5927\u5B89\u5340")  // Da-an district
     46                                               .setAddressLine1("Sec. 3 Hsin-yi Rd.")
     47                                               .setPostalCode("106")
     48                                               .setOrganization("Giant Bike Store")
     49                                               .setRecipient("Mr. Liu")
     50                                               .build();
     51     }
     52 
     53     @Override
     54     public void setUp() {
     55         formatInterpreter = new FormatInterpreter(new FormOptions.Builder().build());
     56     }
     57 
     58     public void testIterateUsAddressFields() {
     59         AddressField[] format = {
     60             AddressField.RECIPIENT,
     61             AddressField.ORGANIZATION,
     62             AddressField.ADDRESS_LINE_1,
     63             AddressField.ADDRESS_LINE_2,
     64             AddressField.LOCALITY,
     65             AddressField.ADMIN_AREA,
     66             AddressField.POSTAL_CODE
     67         };
     68 
     69         int currIndex = 0;
     70         for (AddressField field : formatInterpreter.getAddressFieldOrder(ScriptType.LOCAL, "US")) {
     71             assertEquals("index " + currIndex + " should have matched",
     72                          format[currIndex].getChar(), field.getChar());
     73             currIndex++;
     74         }
     75     }
     76 
     77     /**
     78      * Tests that this works for the case of Vanuatu, since we have no country-specific formatting
     79      * data for that country. Should fall back to the default region.
     80      */
     81     public void testIterateVuAddressFields() {
     82         AddressField[] format = {
     83             AddressField.RECIPIENT,
     84             AddressField.ORGANIZATION,
     85             AddressField.ADDRESS_LINE_1,
     86             AddressField.ADDRESS_LINE_2,
     87             AddressField.LOCALITY,
     88         };
     89 
     90         int currIndex = 0;
     91         for (AddressField field : formatInterpreter.getAddressFieldOrder(ScriptType.LOCAL, "VU")) {
     92             assertEquals("index " + currIndex + " should have matched",
     93                          format[currIndex].getChar(), field.getChar());
     94             currIndex++;
     95         }
     96     }
     97 
     98     public void testOverrideFieldOrder() {
     99         AddressField[] expectedOrder = {
    100             AddressField.ADMIN_AREA,
    101             AddressField.ORGANIZATION,
    102             AddressField.ADDRESS_LINE_1,
    103             AddressField.ADDRESS_LINE_2,
    104             AddressField.LOCALITY,
    105             AddressField.RECIPIENT,
    106             AddressField.POSTAL_CODE
    107         };
    108 
    109         FormatInterpreter myInterpreter = new FormatInterpreter(
    110                 new FormOptions.Builder().customizeFieldOrder("US",
    111                                                               AddressField.ADMIN_AREA,
    112                                                               AddressField.RECIPIENT,
    113                                                               AddressField.SORTING_CODE,
    114                                                               AddressField.POSTAL_CODE).build());
    115 
    116         int currIndex = 0;
    117         for (AddressField field : myInterpreter.getAddressFieldOrder(ScriptType.LOCAL, "US")) {
    118             assertEquals("Wrong field order for US on index " + currIndex + " of address fields.",
    119                          expectedOrder[currIndex], field);
    120 
    121             // Sorting code (CEDEX) is not in US address format and should be
    122             // neglected even if it is specified in customizeFieldOrder().
    123             assertNotSame(AddressField.SORTING_CODE, field);
    124             currIndex++;
    125         }
    126     }
    127 
    128     public void testIterateTwLatinAddressFields() {
    129         AddressField[] format = {
    130             AddressField.RECIPIENT,
    131             AddressField.ORGANIZATION,
    132             AddressField.ADDRESS_LINE_1,
    133             AddressField.ADDRESS_LINE_2,
    134             AddressField.LOCALITY,
    135             AddressField.ADMIN_AREA,
    136             AddressField.POSTAL_CODE
    137         };
    138 
    139         int currIndex = 0;
    140         for (AddressField field : formatInterpreter.getAddressFieldOrder(ScriptType.LATIN, "TW")) {
    141             assertEquals("Unexpected field order -- mismatched on index " + currIndex,
    142                          format[currIndex].getChar(), field.getChar());
    143             currIndex++;
    144         }
    145     }
    146 
    147     public void testUsEnvelopeAddress() {
    148         List<String> expected = new ArrayList<String>();
    149         expected.add("1098 Alta Ave");
    150         expected.add("Mt View CA 94043");
    151 
    152         List<String> real = formatInterpreter.getEnvelopeAddress(US_CA_ADDRESS);
    153 
    154         assertEquals(expected, real);
    155     }
    156 
    157     public void testTwEnvelopeAddress() {
    158         // To be in this order, the whole address should really be in Traditional Chinese - for
    159         // readability, only the neighbourhood and city are.
    160         List<String> expected = new ArrayList<String>();
    161         expected.add("106");
    162         expected.add("\u53F0\u5317\u5E02\u5927\u5B89\u5340");  // Taipei city, Da-an district
    163         expected.add("Sec. 3 Hsin-yi Rd.");
    164         expected.add("Giant Bike Store");
    165         expected.add("Mr. Liu");
    166 
    167         List<String> real = formatInterpreter.getEnvelopeAddress(TW_ADDRESS);
    168 
    169         assertEquals(expected, real);
    170     }
    171 
    172     public void testEnvelopeAddressIncompleteAddress() {
    173         List<String> expected = new ArrayList<String>();
    174         expected.add("1098 Alta Ave");
    175         expected.add("CA 94043");
    176 
    177         AddressData address = new AddressData.Builder().set(US_CA_ADDRESS)
    178                                                        .set(AddressField.LOCALITY, null).build();
    179 
    180         List<String> real = formatInterpreter.getEnvelopeAddress(address);
    181 
    182         assertEquals(expected, real);
    183     }
    184 
    185     public void testEnvelopeAddressEmptyAddress() {
    186         List<String> expected = new ArrayList<String>();
    187         AddressData address = new AddressData.Builder().setCountry("US").build();
    188 
    189         List<String> real = formatInterpreter.getEnvelopeAddress(address);
    190         assertEquals(expected, real);
    191     }
    192 }
    193