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.KeyType; 20 import com.android.i18n.addressinput.testing.AsyncTestCase; 21 22 import java.util.List; 23 24 /** 25 * Basic Tests for {@link FormController}. 26 */ 27 public class FormControllerTest extends AsyncTestCase { 28 29 private static final AddressData US_CA_ADDRESS; 30 private static final AddressData US_ADDRESS; 31 private ClientData clientData; 32 33 static { 34 US_CA_ADDRESS = new AddressData.Builder().setCountry("US") 35 .setAdminArea("CA") 36 .setLocality("Mt View") 37 .setAddressLine1("1098 Alta Ave") 38 .setPostalCode("94043") 39 .build(); 40 US_ADDRESS = new AddressData.Builder().setCountry("US").build(); 41 } 42 43 @Override 44 public void setUp() { 45 clientData = new ClientData(new CacheData()); 46 } 47 48 public void testRequestDataForAddress() { 49 final FormController controller = new FormController(clientData, "en", "US"); 50 51 delayTestFinish(15000); 52 53 controller.requestDataForAddress(US_CA_ADDRESS, new DataLoadListener() { 54 boolean beginCalled = false; 55 @Override 56 public void dataLoadingBegin() { 57 beginCalled = true; 58 } 59 60 @Override 61 public void dataLoadingEnd() { 62 assertTrue("dataLoadingBegin should be called before dataLoadingEnd", 63 beginCalled); 64 LookupKey usCaMtvKey = new LookupKey.Builder(KeyType.DATA) 65 .setAddressData(US_CA_ADDRESS).build(); 66 LookupKey usKey = usCaMtvKey.getKeyForUpperLevelField( 67 AddressField.COUNTRY); 68 LookupKey usCaKey = usCaMtvKey.getKeyForUpperLevelField( 69 AddressField.ADMIN_AREA); 70 assertNotNull("key should be data/US/CA", usCaKey); 71 assertNotNull("key should be data/US/CA/Mt View", usCaMtvKey); 72 assertNotNull(clientData.get(usKey.toString())); 73 assertNotNull(clientData.get(usCaKey.toString())); 74 assertNull(clientData.get(usCaMtvKey.toString())); 75 finishTest(); 76 } 77 }); 78 } 79 80 public void testRequestDataForBadAddress() { 81 final AddressData address = new AddressData.Builder(US_CA_ADDRESS) 82 .setAdminArea("FOOBAR") 83 .setLocality("KarKar") 84 .build(); 85 86 final FormController controller = new FormController(clientData, "en", "US"); 87 88 delayTestFinish(15000); 89 90 controller.requestDataForAddress(address, new DataLoadListener() { 91 boolean beginCalled = false; 92 @Override 93 public void dataLoadingBegin() { 94 beginCalled = true; 95 } 96 97 @Override 98 public void dataLoadingEnd() { 99 assertTrue("dataLoadingBegin should be called before dataLoadingEnd", 100 beginCalled); 101 LookupKey badKey = new LookupKey.Builder(KeyType.DATA) 102 .setAddressData(address).build(); 103 LookupKey usKey = badKey.getKeyForUpperLevelField(AddressField.COUNTRY); 104 105 List<RegionData> rdata = controller.getRegionData(usKey); 106 assertTrue(rdata.size() > 0); 107 String subkey = rdata.get(0).getKey(); 108 assertNotNull("Should be the first US state", subkey); 109 LookupKey usFirstStateKey = 110 new LookupKey.Builder(usKey.toString() + "/" + subkey).build(); 111 112 assertNotNull(clientData.get(usKey.toString())); 113 assertNotNull(clientData.get(usFirstStateKey.toString())); 114 assertNull(clientData.get(badKey.toString())); 115 finishTest(); 116 } 117 }); 118 } 119 120 public void testRequestDataForCountry() { 121 final FormController controller = new FormController(clientData, "en", "US"); 122 123 delayTestFinish(15000); 124 125 controller.requestDataForAddress(US_ADDRESS, new DataLoadListener() { 126 boolean beginCalled = false; 127 @Override 128 public void dataLoadingBegin() { 129 beginCalled = true; 130 } 131 132 @Override 133 public void dataLoadingEnd() { 134 assertTrue("dataLoadingBegin should be called before dataLoadingEnd", 135 beginCalled); 136 LookupKey usKey = new LookupKey.Builder(KeyType.DATA) 137 .setAddressData(US_ADDRESS).build(); 138 assertNotNull("key should be data/US", usKey); 139 List<RegionData> rdata = controller.getRegionData(usKey); 140 assertTrue(rdata.size() > 0); 141 String subkey = rdata.get(0).getKey(); 142 assertNotNull("Should be the first US state", subkey); 143 LookupKey usFirstStateKey = 144 new LookupKey.Builder(usKey.toString() + "/" + subkey).build(); 145 assertNotNull(clientData.get(usKey.toString())); 146 assertNotNull(clientData.get(usFirstStateKey.toString())); 147 finishTest(); 148 } 149 }); 150 } 151 } 152