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