Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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 
     20 import android.location.Criteria;
     21 import android.os.Parcel;
     22 import android.test.AndroidTestCase;
     23 
     24 public class CriteriaTest extends AndroidTestCase {
     25     public void testConstructor() {
     26         new Criteria();
     27 
     28         Criteria c = new Criteria();
     29         c.setAccuracy(Criteria.ACCURACY_FINE);
     30         c.setAltitudeRequired(true);
     31         c.setBearingRequired(true);
     32         c.setCostAllowed(true);
     33         c.setPowerRequirement(Criteria.POWER_HIGH);
     34         c.setSpeedRequired(true);
     35         Criteria criteria = new Criteria(c);
     36         assertEquals(Criteria.ACCURACY_FINE, criteria.getAccuracy());
     37         assertTrue(criteria.isAltitudeRequired());
     38         assertTrue(criteria.isBearingRequired());
     39         assertTrue(criteria.isCostAllowed());
     40         assertTrue(criteria.isSpeedRequired());
     41         assertEquals(Criteria.POWER_HIGH, criteria.getPowerRequirement());
     42 
     43         try {
     44             new Criteria(null);
     45             fail("should throw NullPointerException.");
     46         } catch (NullPointerException e) {
     47             // expected.
     48         }
     49     }
     50 
     51     public void testDescribeContents() {
     52         Criteria criteria = new Criteria();
     53         criteria.describeContents();
     54     }
     55 
     56     public void testAccessAccuracy() {
     57         Criteria criteria = new Criteria();
     58 
     59         criteria.setAccuracy(Criteria.ACCURACY_FINE);
     60         assertEquals(Criteria.ACCURACY_FINE, criteria.getAccuracy());
     61 
     62         criteria.setAccuracy(Criteria.ACCURACY_COARSE);
     63         assertEquals(Criteria.ACCURACY_COARSE, criteria.getAccuracy());
     64 
     65         try {
     66             // It should throw IllegalArgumentException
     67             criteria.setAccuracy(-1);
     68             // issue 1728526
     69         } catch (IllegalArgumentException e) {
     70             // expected.
     71         }
     72 
     73         try {
     74             // It should throw IllegalArgumentException
     75             criteria.setAccuracy(Criteria.ACCURACY_COARSE + 1);
     76             // issue 1728526
     77         } catch (IllegalArgumentException e) {
     78             // expected.
     79         }
     80     }
     81 
     82     public void testAccessPowerRequirement() {
     83         Criteria criteria = new Criteria();
     84 
     85         criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
     86         assertEquals(Criteria.NO_REQUIREMENT, criteria.getPowerRequirement());
     87 
     88         criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
     89         assertEquals(Criteria.POWER_MEDIUM, criteria.getPowerRequirement());
     90 
     91         try {
     92             criteria.setPowerRequirement(-1);
     93             fail("should throw IllegalArgumentException");
     94         } catch (IllegalArgumentException e) {
     95             // expected.
     96         }
     97 
     98         try {
     99             criteria.setPowerRequirement(Criteria.POWER_HIGH + 1);
    100             fail("should throw IllegalArgumentException");
    101         } catch (IllegalArgumentException e) {
    102             // expected.
    103         }
    104     }
    105 
    106     public void testAccessAltitudeRequired() {
    107         Criteria criteria = new Criteria();
    108 
    109         criteria.setAltitudeRequired(false);
    110         assertFalse(criteria.isAltitudeRequired());
    111 
    112         criteria.setAltitudeRequired(true);
    113         assertTrue(criteria.isAltitudeRequired());
    114     }
    115 
    116     public void testAccessBearingAccuracy() {
    117         Criteria criteria = new Criteria();
    118 
    119         criteria.setBearingAccuracy(Criteria.ACCURACY_LOW);
    120         assertEquals(Criteria.ACCURACY_LOW, criteria.getBearingAccuracy());
    121 
    122         criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
    123         assertEquals(Criteria.ACCURACY_HIGH, criteria.getBearingAccuracy());
    124 
    125         criteria.setBearingAccuracy(Criteria.NO_REQUIREMENT);
    126         assertEquals(Criteria.NO_REQUIREMENT, criteria.getBearingAccuracy());
    127       }
    128 
    129     public void testAccessBearingRequired() {
    130         Criteria criteria = new Criteria();
    131 
    132         criteria.setBearingRequired(false);
    133         assertFalse(criteria.isBearingRequired());
    134 
    135         criteria.setBearingRequired(true);
    136         assertTrue(criteria.isBearingRequired());
    137     }
    138 
    139     public void testAccessCostAllowed() {
    140         Criteria criteria = new Criteria();
    141 
    142         criteria.setCostAllowed(false);
    143         assertFalse(criteria.isCostAllowed());
    144 
    145         criteria.setCostAllowed(true);
    146         assertTrue(criteria.isCostAllowed());
    147     }
    148 
    149     public void testAccessHorizontalAccuracy() {
    150         Criteria criteria = new Criteria();
    151 
    152         criteria.setHorizontalAccuracy(Criteria.ACCURACY_LOW);
    153         assertEquals(Criteria.ACCURACY_LOW, criteria.getHorizontalAccuracy());
    154 
    155         criteria.setHorizontalAccuracy(Criteria.ACCURACY_MEDIUM);
    156         assertEquals(Criteria.ACCURACY_MEDIUM, criteria.getHorizontalAccuracy());
    157 
    158         criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
    159         assertEquals(Criteria.ACCURACY_HIGH, criteria.getHorizontalAccuracy());
    160 
    161         criteria.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
    162         assertEquals(Criteria.NO_REQUIREMENT, criteria.getHorizontalAccuracy());
    163     }
    164 
    165     public void testAccessSpeedAccuracy() {
    166         Criteria criteria = new Criteria();
    167 
    168         criteria.setSpeedAccuracy(Criteria.ACCURACY_LOW);
    169         assertEquals(Criteria.ACCURACY_LOW, criteria.getSpeedAccuracy());
    170 
    171         criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
    172         assertEquals(Criteria.ACCURACY_HIGH, criteria.getSpeedAccuracy());
    173 
    174         criteria.setSpeedAccuracy(Criteria.NO_REQUIREMENT);
    175         assertEquals(Criteria.NO_REQUIREMENT, criteria.getSpeedAccuracy());
    176     }
    177 
    178     public void testAccessSpeedRequired() {
    179         Criteria criteria = new Criteria();
    180 
    181         criteria.setSpeedRequired(false);
    182         assertFalse(criteria.isSpeedRequired());
    183 
    184         criteria.setSpeedRequired(true);
    185         assertTrue(criteria.isSpeedRequired());
    186     }
    187 
    188     public void testAccessVerticalAccuracy() {
    189         Criteria criteria = new Criteria();
    190 
    191         criteria.setVerticalAccuracy(Criteria.ACCURACY_LOW);
    192         assertEquals(Criteria.ACCURACY_LOW, criteria.getVerticalAccuracy());
    193 
    194        criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
    195         assertEquals(Criteria.ACCURACY_HIGH, criteria.getVerticalAccuracy());
    196 
    197         criteria.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
    198         assertEquals(Criteria.NO_REQUIREMENT, criteria.getVerticalAccuracy());
    199     }
    200 
    201     public void testWriteToParcel() {
    202         Criteria criteria = new Criteria();
    203         criteria.setAltitudeRequired(true);
    204         criteria.setBearingRequired(false);
    205         criteria.setCostAllowed(true);
    206         criteria.setSpeedRequired(true);
    207 
    208         Parcel parcel = Parcel.obtain();
    209         criteria.writeToParcel(parcel, 0);
    210         parcel.setDataPosition(0);
    211 
    212         Criteria newCriteria = Criteria.CREATOR.createFromParcel(parcel);
    213 
    214         assertEquals(criteria.getAccuracy(), newCriteria.getAccuracy());
    215         assertEquals(criteria.getPowerRequirement(), newCriteria.getPowerRequirement());
    216         assertEquals(criteria.isAltitudeRequired(), newCriteria.isAltitudeRequired());
    217         assertEquals(criteria.isBearingRequired(), newCriteria.isBearingRequired());
    218         assertEquals(criteria.isSpeedRequired(), newCriteria.isSpeedRequired());
    219         assertEquals(criteria.isCostAllowed(), newCriteria.isCostAllowed());
    220 
    221         parcel.recycle();
    222     }
    223 }
    224