Home | History | Annotate | Download | only in cts
      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 import dalvik.annotation.TestTargets;
     27 import dalvik.annotation.TestLevel;
     28 import dalvik.annotation.TestTargetNew;
     29 import dalvik.annotation.TestTargetClass;
     30 import dalvik.annotation.ToBeFixed;
     31 
     32 /**
     33  * Test the main functionalities of the AddressTest.
     34  */
     35 @TestTargetClass(Address.class)
     36 public class AddressTest extends TestCase {
     37     @TestTargetNew(
     38         level = TestLevel.COMPLETE,
     39         notes = "Test constructor",
     40         method = "Address",
     41         args = {java.util.Locale.class}
     42     )
     43     public void testConstructor() {
     44         new Address(Locale.ENGLISH);
     45 
     46         new Address(Locale.FRANCE);
     47 
     48         new Address(null);
     49     }
     50 
     51     @TestTargetNew(
     52         level = TestLevel.COMPLETE,
     53         notes = "Test describeContents()",
     54         method = "describeContents",
     55         args = {}
     56     )
     57     public void testDescribeContents() {
     58         Address address = new Address(Locale.GERMAN);
     59 
     60         assertEquals(0, address.describeContents());
     61 
     62         Bundle extras = new Bundle();
     63         extras.putParcelable("key1", new MockParcelable());
     64         address.setExtras(extras);
     65 
     66         assertEquals(extras.describeContents(), address.describeContents());
     67     }
     68 
     69     @TestTargets({
     70         @TestTargetNew(
     71             level = TestLevel.COMPLETE,
     72             notes = "Test setAdminArea(String) and getAdminArea()",
     73             method = "setAdminArea",
     74             args = {java.lang.String.class}
     75         ),
     76         @TestTargetNew(
     77             level = TestLevel.COMPLETE,
     78             notes = "Test setAdminArea(String) and getAdminArea()",
     79             method = "getAdminArea",
     80             args = {}
     81         )
     82     })
     83     public void testAccessAdminArea() {
     84         Address address = new Address(Locale.ITALY);
     85 
     86         String adminArea = "CA";
     87         address.setAdminArea(adminArea);
     88         assertEquals(adminArea, address.getAdminArea());
     89 
     90         address.setAdminArea(null);
     91         assertNull(address.getAdminArea());
     92     }
     93 
     94     @TestTargets({
     95         @TestTargetNew(
     96             level = TestLevel.COMPLETE,
     97             notes = "Test setCountryCode(String) and getCountryCode()",
     98             method = "setCountryCode",
     99             args = {java.lang.String.class}
    100         ),
    101         @TestTargetNew(
    102             level = TestLevel.COMPLETE,
    103             notes = "Test setCountryCode(String) and getCountryCode()",
    104             method = "getCountryCode",
    105             args = {}
    106         )
    107     })
    108     public void testAccessCountryCode() {
    109         Address address = new Address(Locale.JAPAN);
    110 
    111         String countryCode = "US";
    112         address.setCountryCode(countryCode);
    113         assertEquals(countryCode, address.getCountryCode());
    114 
    115         address.setCountryCode(null);
    116         assertNull(address.getCountryCode());
    117     }
    118 
    119     @TestTargets({
    120         @TestTargetNew(
    121             level = TestLevel.COMPLETE,
    122             notes = "Test setCountryName(String) and getCountryName()",
    123             method = "setCountryName",
    124             args = {java.lang.String.class}
    125         ),
    126         @TestTargetNew(
    127             level = TestLevel.COMPLETE,
    128             notes = "Test setCountryName(String) and getCountryName()",
    129             method = "getCountryName",
    130             args = {}
    131         )
    132     })
    133     public void testAccessCountryName() {
    134         Address address = new Address(Locale.KOREA);
    135 
    136         String countryName = "China";
    137         address.setCountryName(countryName);
    138         assertEquals(countryName, address.getCountryName());
    139 
    140         address.setCountryName(null);
    141         assertNull(address.getCountryName());
    142     }
    143 
    144     @TestTargets({
    145         @TestTargetNew(
    146             level = TestLevel.COMPLETE,
    147             notes = "Test setExtras(Bundle) and getExtras()",
    148             method = "setExtras",
    149             args = {android.os.Bundle.class}
    150         ),
    151         @TestTargetNew(
    152             level = TestLevel.COMPLETE,
    153             notes = "Test setExtras(Bundle) and getExtras()",
    154             method = "getExtras",
    155             args = {}
    156         )
    157     })
    158     public void testAccessExtras() {
    159         Address address = new Address(Locale.TAIWAN);
    160 
    161         Bundle extras = new Bundle();
    162         extras.putBoolean("key1", false);
    163         byte b = 10;
    164         extras.putByte("key2", b);
    165 
    166         address.setExtras(extras);
    167         Bundle actual = address.getExtras();
    168         assertFalse(actual.getBoolean("key1"));
    169         assertEquals(b, actual.getByte("key2"));
    170 
    171         address.setExtras(null);
    172         assertNull(address.getExtras());
    173     }
    174 
    175     @TestTargets({
    176         @TestTargetNew(
    177             level = TestLevel.COMPLETE,
    178             notes = "Test setFeatureName(String) and getFeatureName()",
    179             method = "setFeatureName",
    180             args = {java.lang.String.class}
    181         ),
    182         @TestTargetNew(
    183             level = TestLevel.COMPLETE,
    184             notes = "Test setFeatureName(String) and getFeatureName()",
    185             method = "getFeatureName",
    186             args = {}
    187         )
    188     })
    189     public void testAccessFeatureName() {
    190         Address address = new Address(Locale.SIMPLIFIED_CHINESE);
    191 
    192         String featureName = "Golden Gate Bridge";
    193         address.setFeatureName(featureName);
    194         assertEquals(featureName, address.getFeatureName());
    195 
    196         address.setFeatureName(null);
    197         assertNull(address.getFeatureName());
    198     }
    199 
    200     @TestTargets({
    201         @TestTargetNew(
    202             level = TestLevel.COMPLETE,
    203             notes = "Test setLatitude(double) and getLatitude(), clearLatitude(), hasLatitude()",
    204             method = "setLatitude",
    205             args = {double.class}
    206         ),
    207         @TestTargetNew(
    208             level = TestLevel.COMPLETE,
    209             notes = "Test setLatitude(double) and getLatitude(), clearLatitude(), hasLatitude()",
    210             method = "getLatitude",
    211             args = {}
    212         ),
    213         @TestTargetNew(
    214             level = TestLevel.COMPLETE,
    215             notes = "Test setLatitude(double) and getLatitude(), clearLatitude(), hasLatitude()",
    216             method = "clearLatitude",
    217             args = {}
    218         ),
    219         @TestTargetNew(
    220             level = TestLevel.COMPLETE,
    221             notes = "Test setLatitude(double) and getLatitude(), clearLatitude(), hasLatitude()",
    222             method = "hasLatitude",
    223             args = {}
    224         )
    225     })
    226     public void testAccessLatitude() {
    227         Address address = new Address(Locale.CHINA);
    228         assertFalse(address.hasLatitude());
    229 
    230         double latitude = 1.23456789;
    231         address.setLatitude(latitude);
    232         assertTrue(address.hasLatitude());
    233         assertEquals(latitude, address.getLatitude());
    234 
    235         address.clearLatitude();
    236         assertFalse(address.hasLatitude());
    237         try {
    238             address.getLatitude();
    239             fail("should throw IllegalStateException.");
    240         } catch (IllegalStateException e) {
    241         }
    242     }
    243 
    244     @TestTargets({
    245         @TestTargetNew(
    246             level = TestLevel.COMPLETE,
    247             notes = "",
    248             method = "setLongitude",
    249             args = {double.class}
    250         ),
    251         @TestTargetNew(
    252             level = TestLevel.COMPLETE,
    253             notes = "",
    254             method = "getLongitude",
    255             args = {}
    256         ),
    257         @TestTargetNew(
    258             level = TestLevel.COMPLETE,
    259             notes = "",
    260             method = "clearLongitude",
    261             args = {}
    262         ),
    263         @TestTargetNew(
    264             level = TestLevel.COMPLETE,
    265             notes = "",
    266             method = "hasLongitude",
    267             args = {}
    268         )
    269     })
    270     public void testAccessLongitude() {
    271         Address address = new Address(Locale.CHINA);
    272         assertFalse(address.hasLongitude());
    273 
    274         double longitude = 1.23456789;
    275         address.setLongitude(longitude);
    276         assertTrue(address.hasLongitude());
    277         assertEquals(longitude, address.getLongitude());
    278 
    279         address.clearLongitude();
    280         assertFalse(address.hasLongitude());
    281         try {
    282             address.getLongitude();
    283             fail("should throw IllegalStateException.");
    284         } catch (IllegalStateException e) {
    285         }
    286     }
    287 
    288     @TestTargets({
    289         @TestTargetNew(
    290             level = TestLevel.COMPLETE,
    291             notes = "Test setPhone(String) and getPhone()",
    292             method = "setPhone",
    293             args = {java.lang.String.class}
    294         ),
    295         @TestTargetNew(
    296             level = TestLevel.COMPLETE,
    297             notes = "Test setPhone(String) and getPhone()",
    298             method = "getPhone",
    299             args = {}
    300         )
    301     })
    302     @ToBeFixed(bug = "", explanation = "getPhone() should never throw IllegalStateException. " +
    303             "Should remove @throws clause from its javadoc")
    304     public void testAccessPhone() {
    305         Address address = new Address(Locale.CHINA);
    306 
    307         String phone = "+86-13512345678";
    308         address.setPhone(phone);
    309         assertEquals(phone, address.getPhone());
    310 
    311         address.setPhone(null);
    312         assertNull(address.getPhone());
    313     }
    314 
    315     @TestTargets({
    316         @TestTargetNew(
    317             level = TestLevel.COMPLETE,
    318             notes = "Test setPostalCode(String) and getPostalCode()",
    319             method = "setPostalCode",
    320             args = {java.lang.String.class}
    321         ),
    322         @TestTargetNew(
    323             level = TestLevel.COMPLETE,
    324             notes = "Test setPostalCode(String) and getPostalCode()",
    325             method = "getPostalCode",
    326             args = {}
    327         )
    328     })
    329     public void testAccessPostalCode() {
    330         Address address = new Address(Locale.CHINA);
    331 
    332         String postalCode = "93110";
    333         address.setPostalCode(postalCode);
    334         assertEquals(postalCode, address.getPostalCode());
    335 
    336         address.setPostalCode(null);
    337         assertNull(address.getPostalCode());
    338     }
    339 
    340     @TestTargets({
    341         @TestTargetNew(
    342             level = TestLevel.COMPLETE,
    343             notes = "Test setThoroughfare(String) and getThoroughfare()",
    344             method = "setThoroughfare",
    345             args = {java.lang.String.class}
    346         ),
    347         @TestTargetNew(
    348             level = TestLevel.COMPLETE,
    349             notes = "Test setThoroughfare(String) and getThoroughfare()",
    350             method = "getThoroughfare",
    351             args = {}
    352         )
    353     })
    354     public void testAccessThoroughfare() {
    355         Address address = new Address(Locale.CHINA);
    356 
    357         String thoroughfare = "1600 Ampitheater Parkway";
    358         address.setThoroughfare(thoroughfare);
    359         assertEquals(thoroughfare, address.getThoroughfare());
    360 
    361         address.setThoroughfare(null);
    362         assertNull(address.getThoroughfare());
    363     }
    364 
    365     @TestTargets({
    366         @TestTargetNew(
    367             level = TestLevel.COMPLETE,
    368             notes = "Test setUrl(String) and getUrl()",
    369             method = "setUrl",
    370             args = {java.lang.String.class}
    371         ),
    372         @TestTargetNew(
    373             level = TestLevel.COMPLETE,
    374             notes = "Test setUrl(String) and getUrl()",
    375             method = "getUrl",
    376             args = {}
    377         )
    378     })
    379     public void testAccessUrl() {
    380         Address address = new Address(Locale.CHINA);
    381 
    382         String Url = "Url";
    383         address.setUrl(Url);
    384         assertEquals(Url, address.getUrl());
    385 
    386         address.setUrl(null);
    387         assertNull(address.getUrl());
    388     }
    389 
    390     @TestTargets({
    391         @TestTargetNew(
    392             level = TestLevel.COMPLETE,
    393             notes = "Test setSubAdminArea(String) and getSubAdminArea()",
    394             method = "setSubAdminArea",
    395             args = {java.lang.String.class}
    396         ),
    397         @TestTargetNew(
    398             level = TestLevel.COMPLETE,
    399             notes = "Test setSubAdminArea(String) and getSubAdminArea()",
    400             method = "getSubAdminArea",
    401             args = {}
    402         )
    403     })
    404     public void testAccessSubAdminArea() {
    405         Address address = new Address(Locale.CHINA);
    406 
    407         String subAdminArea = "Santa Clara County";
    408         address.setSubAdminArea(subAdminArea);
    409         assertEquals(subAdminArea, address.getSubAdminArea());
    410 
    411         address.setSubAdminArea(null);
    412         assertNull(address.getSubAdminArea());
    413     }
    414 
    415     @TestTargetNew(
    416         level = TestLevel.COMPLETE,
    417         notes = "Test toString()",
    418         method = "toString",
    419         args = {}
    420     )
    421     public void testToString() {
    422         Address address = new Address(Locale.CHINA);
    423 
    424         address.setUrl("www.google.com");
    425         address.setPostalCode("95120");
    426         String expected = "Address[addressLines=[],feature=null,admin=null,sub-admin=null," +
    427                 "locality=null,thoroughfare=null,postalCode=95120,countryCode=null," +
    428                 "countryName=null,hasLatitude=false,latitude=0.0,hasLongitude=false," +
    429                 "longitude=0.0,phone=null,url=www.google.com,extras=null]";
    430         assertEquals(expected, address.toString());
    431     }
    432 
    433     @TestTargets({
    434         @TestTargetNew(
    435             level = TestLevel.COMPLETE,
    436             notes = "",
    437             method = "setAddressLine",
    438             args = {int.class, java.lang.String.class}
    439         ),
    440         @TestTargetNew(
    441             level = TestLevel.COMPLETE,
    442             notes = "",
    443             method = "getMaxAddressLineIndex",
    444             args = {}
    445         ),
    446         @TestTargetNew(
    447             level = TestLevel.COMPLETE,
    448             notes = "",
    449             method = "getAddressLine",
    450             args = {int.class}
    451         )
    452     })
    453     public void testAddressLine() {
    454         Address address = new Address(Locale.CHINA);
    455 
    456         try {
    457             address.setAddressLine(-1, null);
    458             fail("should throw IllegalArgumentException");
    459         } catch (IllegalArgumentException e) {
    460         }
    461 
    462         try {
    463             address.getAddressLine(-1);
    464             fail("should throw IllegalArgumentException");
    465         } catch (IllegalArgumentException e) {
    466         }
    467 
    468         address.setAddressLine(0, null);
    469         assertNull(address.getAddressLine(0));
    470         assertEquals(0, address.getMaxAddressLineIndex());
    471 
    472         final String line1 = "1";
    473         address.setAddressLine(0, line1);
    474         assertEquals(line1, address.getAddressLine(0));
    475         assertEquals(0, address.getMaxAddressLineIndex());
    476 
    477         final String line2 = "2";
    478         address.setAddressLine(5, line2);
    479         assertEquals(line2, address.getAddressLine(5));
    480         assertEquals(5, address.getMaxAddressLineIndex());
    481 
    482         address.setAddressLine(2, null);
    483         assertNull(address.getAddressLine(2));
    484         assertEquals(5, address.getMaxAddressLineIndex());
    485     }
    486 
    487     @TestTargetNew(
    488         level = TestLevel.COMPLETE,
    489         notes = "Test getLocale()",
    490         method = "getLocale",
    491         args = {}
    492     )
    493     public void testGetLocale() {
    494         Locale locale = Locale.US;
    495         Address address = new Address(locale);
    496         assertSame(locale, address.getLocale());
    497 
    498         locale = Locale.UK;
    499         address = new Address(locale);
    500         assertSame(locale, address.getLocale());
    501 
    502         address = new Address(null);
    503         assertNull(address.getLocale());
    504     }
    505 
    506     @TestTargets({
    507         @TestTargetNew(
    508             level = TestLevel.COMPLETE,
    509             notes = "Test setLocality(String) and getLocality()",
    510             method = "setLocality",
    511             args = {java.lang.String.class}
    512         ),
    513         @TestTargetNew(
    514             level = TestLevel.COMPLETE,
    515             notes = "Test setLocality(String) and getLocality()",
    516             method = "getLocality",
    517             args = {}
    518         )
    519     })
    520     public void testAccessLocality() {
    521         Address address = new Address(Locale.PRC);
    522 
    523         String locality = "Hollywood";
    524         address.setLocality(locality);
    525         assertEquals(locality, address.getLocality());
    526 
    527         address.setLocality(null);
    528         assertNull(address.getLocality());
    529     }
    530 
    531     @TestTargetNew(
    532         level = TestLevel.COMPLETE,
    533         notes = "Test writeToParcel(Parcel, int), this function ignores the parameter 'flag'.",
    534         method = "writeToParcel",
    535         args = {android.os.Parcel.class, int.class}
    536     )
    537     public void testWriteToParcel() {
    538         Locale locale = Locale.KOREA;
    539         Address address = new Address(locale);
    540 
    541         Parcel parcel = Parcel.obtain();
    542         address.writeToParcel(parcel, 0);
    543         parcel.setDataPosition(0);
    544         assertEquals(locale.getLanguage(), parcel.readString());
    545         assertEquals(locale.getCountry(), parcel.readString());
    546         assertEquals(0, parcel.readInt());
    547         assertEquals(address.getFeatureName(), parcel.readString());
    548         assertEquals(address.getAdminArea(), parcel.readString());
    549         assertEquals(address.getSubAdminArea(), parcel.readString());
    550         assertEquals(address.getLocality(), parcel.readString());
    551         assertEquals(address.getSubLocality(), parcel.readString());
    552         assertEquals(address.getThoroughfare(), parcel.readString());
    553         assertEquals(address.getSubThoroughfare(), parcel.readString());
    554         assertEquals(address.getPremises(), parcel.readString());
    555         assertEquals(address.getPostalCode(), parcel.readString());
    556         assertEquals(address.getCountryCode(), parcel.readString());
    557         assertEquals(address.getCountryName(), parcel.readString());
    558         assertEquals(0, parcel.readInt());
    559         assertEquals(0, parcel.readInt());
    560         assertEquals(address.getPhone(), parcel.readString());
    561         assertEquals(address.getUrl(), parcel.readString());
    562         assertEquals(address.getExtras(), parcel.readBundle());
    563     }
    564 
    565     private class MockParcelable implements Parcelable {
    566         public int describeContents() {
    567             return Parcelable.CONTENTS_FILE_DESCRIPTOR;
    568         }
    569 
    570         public void writeToParcel(Parcel dest, int flags) {
    571         }
    572     }
    573 }
    574