1 /* 2 * Copyright (C) 2020 The Android Open Source Project 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 android.location.cts.none; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNull; 22 import static org.junit.Assert.assertSame; 23 import static org.junit.Assert.assertTrue; 24 import static org.junit.Assert.fail; 25 26 import java.util.Locale; 27 28 import android.location.Address; 29 import android.os.Bundle; 30 import android.os.Parcel; 31 32 import androidx.test.ext.junit.runners.AndroidJUnit4; 33 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 @RunWith(AndroidJUnit4.class) 38 public class AddressTest { 39 40 private static final double DELTA = 0.001; 41 42 @Test testConstructor()43 public void testConstructor() { 44 new Address(Locale.ENGLISH); 45 46 new Address(Locale.FRANCE); 47 48 new Address(null); 49 } 50 51 @Test testAccessAdminArea()52 public void testAccessAdminArea() { 53 Address address = new Address(Locale.ITALY); 54 55 String adminArea = "CA"; 56 address.setAdminArea(adminArea); 57 assertEquals(adminArea, address.getAdminArea()); 58 59 address.setAdminArea(null); 60 assertNull(address.getAdminArea()); 61 } 62 63 @Test testAccessCountryCode()64 public void testAccessCountryCode() { 65 Address address = new Address(Locale.JAPAN); 66 67 String countryCode = "US"; 68 address.setCountryCode(countryCode); 69 assertEquals(countryCode, address.getCountryCode()); 70 71 address.setCountryCode(null); 72 assertNull(address.getCountryCode()); 73 } 74 75 @Test testAccessCountryName()76 public void testAccessCountryName() { 77 Address address = new Address(Locale.KOREA); 78 79 String countryName = "China"; 80 address.setCountryName(countryName); 81 assertEquals(countryName, address.getCountryName()); 82 83 address.setCountryName(null); 84 assertNull(address.getCountryName()); 85 } 86 87 @Test testAccessExtras()88 public void testAccessExtras() { 89 Address address = new Address(Locale.TAIWAN); 90 91 Bundle extras = new Bundle(); 92 extras.putBoolean("key1", false); 93 byte b = 10; 94 extras.putByte("key2", b); 95 96 address.setExtras(extras); 97 Bundle actual = address.getExtras(); 98 assertFalse(actual.getBoolean("key1")); 99 assertEquals(b, actual.getByte("key2")); 100 101 address.setExtras(null); 102 assertNull(address.getExtras()); 103 } 104 105 @Test testAccessFeatureName()106 public void testAccessFeatureName() { 107 Address address = new Address(Locale.SIMPLIFIED_CHINESE); 108 109 String featureName = "Golden Gate Bridge"; 110 address.setFeatureName(featureName); 111 assertEquals(featureName, address.getFeatureName()); 112 113 address.setFeatureName(null); 114 assertNull(address.getFeatureName()); 115 } 116 117 @Test testAccessLatitude()118 public void testAccessLatitude() { 119 Address address = new Address(Locale.CHINA); 120 assertFalse(address.hasLatitude()); 121 122 double latitude = 1.23456789; 123 address.setLatitude(latitude); 124 assertTrue(address.hasLatitude()); 125 assertEquals(latitude, address.getLatitude(), DELTA); 126 127 address.clearLatitude(); 128 assertFalse(address.hasLatitude()); 129 try { 130 address.getLatitude(); 131 fail("should throw IllegalStateException."); 132 } catch (IllegalStateException e) { 133 // pass 134 } 135 } 136 137 @Test testAccessLongitude()138 public void testAccessLongitude() { 139 Address address = new Address(Locale.CHINA); 140 assertFalse(address.hasLongitude()); 141 142 double longitude = 1.23456789; 143 address.setLongitude(longitude); 144 assertTrue(address.hasLongitude()); 145 assertEquals(longitude, address.getLongitude(), DELTA); 146 147 address.clearLongitude(); 148 assertFalse(address.hasLongitude()); 149 try { 150 address.getLongitude(); 151 fail("should throw IllegalStateException."); 152 } catch (IllegalStateException e) { 153 // pass 154 } 155 } 156 157 @Test testAccessPhone()158 public void testAccessPhone() { 159 Address address = new Address(Locale.CHINA); 160 161 String phone = "+86-13512345678"; 162 address.setPhone(phone); 163 assertEquals(phone, address.getPhone()); 164 165 address.setPhone(null); 166 assertNull(address.getPhone()); 167 } 168 169 @Test testAccessPostalCode()170 public void testAccessPostalCode() { 171 Address address = new Address(Locale.CHINA); 172 173 String postalCode = "93110"; 174 address.setPostalCode(postalCode); 175 assertEquals(postalCode, address.getPostalCode()); 176 177 address.setPostalCode(null); 178 assertNull(address.getPostalCode()); 179 } 180 181 @Test testAccessThoroughfare()182 public void testAccessThoroughfare() { 183 Address address = new Address(Locale.CHINA); 184 185 String thoroughfare = "1600 Ampitheater Parkway"; 186 address.setThoroughfare(thoroughfare); 187 assertEquals(thoroughfare, address.getThoroughfare()); 188 189 address.setThoroughfare(null); 190 assertNull(address.getThoroughfare()); 191 } 192 193 @Test testAccessUrl()194 public void testAccessUrl() { 195 Address address = new Address(Locale.CHINA); 196 197 String Url = "Url"; 198 address.setUrl(Url); 199 assertEquals(Url, address.getUrl()); 200 201 address.setUrl(null); 202 assertNull(address.getUrl()); 203 } 204 205 @Test testAccessSubAdminArea()206 public void testAccessSubAdminArea() { 207 Address address = new Address(Locale.CHINA); 208 209 String subAdminArea = "Santa Clara County"; 210 address.setSubAdminArea(subAdminArea); 211 assertEquals(subAdminArea, address.getSubAdminArea()); 212 213 address.setSubAdminArea(null); 214 assertNull(address.getSubAdminArea()); 215 } 216 217 @Test testToString()218 public void testToString() { 219 Address address = new Address(Locale.CHINA); 220 221 address.setUrl("www.google.com"); 222 address.setPostalCode("95120"); 223 String expected = "Address[addressLines=[],feature=null,admin=null,sub-admin=null," + 224 "locality=null,thoroughfare=null,postalCode=95120,countryCode=null," + 225 "countryName=null,hasLatitude=false,latitude=0.0,hasLongitude=false," + 226 "longitude=0.0,phone=null,url=www.google.com,extras=null]"; 227 assertEquals(expected, address.toString()); 228 } 229 230 @Test testAddressLine()231 public void testAddressLine() { 232 Address address = new Address(Locale.CHINA); 233 234 try { 235 address.setAddressLine(-1, null); 236 fail("should throw IllegalArgumentException"); 237 } catch (IllegalArgumentException e) { 238 // pass 239 } 240 241 try { 242 address.getAddressLine(-1); 243 fail("should throw IllegalArgumentException"); 244 } catch (IllegalArgumentException e) { 245 // pass 246 } 247 248 address.setAddressLine(0, null); 249 assertNull(address.getAddressLine(0)); 250 assertEquals(0, address.getMaxAddressLineIndex()); 251 252 final String line1 = "1"; 253 address.setAddressLine(0, line1); 254 assertEquals(line1, address.getAddressLine(0)); 255 assertEquals(0, address.getMaxAddressLineIndex()); 256 257 final String line2 = "2"; 258 address.setAddressLine(5, line2); 259 assertEquals(line2, address.getAddressLine(5)); 260 assertEquals(5, address.getMaxAddressLineIndex()); 261 262 address.setAddressLine(2, null); 263 assertNull(address.getAddressLine(2)); 264 assertEquals(5, address.getMaxAddressLineIndex()); 265 } 266 267 @Test testGetLocale()268 public void testGetLocale() { 269 Locale locale = Locale.US; 270 Address address = new Address(locale); 271 assertSame(locale, address.getLocale()); 272 273 locale = Locale.UK; 274 address = new Address(locale); 275 assertSame(locale, address.getLocale()); 276 277 address = new Address(null); 278 assertNull(address.getLocale()); 279 } 280 281 @Test testAccessLocality()282 public void testAccessLocality() { 283 Address address = new Address(Locale.PRC); 284 285 String locality = "Hollywood"; 286 address.setLocality(locality); 287 assertEquals(locality, address.getLocality()); 288 289 address.setLocality(null); 290 assertNull(address.getLocality()); 291 } 292 293 @Test testAccessPremises()294 public void testAccessPremises() { 295 Address address = new Address(Locale.PRC); 296 297 String premises = "Appartment"; 298 address.setPremises(premises); 299 assertEquals(premises, address.getPremises()); 300 301 address.setPremises(null); 302 assertNull(address.getPremises()); 303 } 304 305 @Test testAccessSubLocality()306 public void testAccessSubLocality() { 307 Address address = new Address(Locale.PRC); 308 309 String subLocality = "Sarchnar"; 310 address.setSubLocality(subLocality); 311 assertEquals(subLocality, address.getSubLocality()); 312 313 address.setSubLocality(null); 314 assertNull(address.getSubLocality()); 315 } 316 317 @Test testAccessSubThoroughfare()318 public void testAccessSubThoroughfare() { 319 Address address = new Address(Locale.PRC); 320 321 String subThoroughfare = "1600"; 322 address.setSubThoroughfare(subThoroughfare); 323 assertEquals(subThoroughfare, address.getSubThoroughfare()); 324 325 address.setSubThoroughfare(null); 326 assertNull(address.getSubThoroughfare()); 327 } 328 329 @Test testWriteToParcel()330 public void testWriteToParcel() { 331 Locale locale = Locale.KOREA; 332 Address address = new Address(locale); 333 334 Parcel parcel = Parcel.obtain(); 335 address.writeToParcel(parcel, 0); 336 parcel.setDataPosition(0); 337 assertEquals(locale.getLanguage(), parcel.readString()); 338 assertEquals(locale.getCountry(), parcel.readString()); 339 assertEquals(0, parcel.readInt()); 340 assertEquals(address.getFeatureName(), parcel.readString()); 341 assertEquals(address.getAdminArea(), parcel.readString()); 342 assertEquals(address.getSubAdminArea(), parcel.readString()); 343 assertEquals(address.getLocality(), parcel.readString()); 344 assertEquals(address.getSubLocality(), parcel.readString()); 345 assertEquals(address.getThoroughfare(), parcel.readString()); 346 assertEquals(address.getSubThoroughfare(), parcel.readString()); 347 assertEquals(address.getPremises(), parcel.readString()); 348 assertEquals(address.getPostalCode(), parcel.readString()); 349 assertEquals(address.getCountryCode(), parcel.readString()); 350 assertEquals(address.getCountryName(), parcel.readString()); 351 assertEquals(0, parcel.readInt()); 352 assertEquals(0, parcel.readInt()); 353 assertEquals(address.getPhone(), parcel.readString()); 354 assertEquals(address.getUrl(), parcel.readString()); 355 assertEquals(address.getExtras(), parcel.readBundle()); 356 357 parcel.recycle(); 358 } 359 } 360