Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 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 android.location.cts;
     18 
     19 import android.platform.test.annotations.AppModeFull;
     20 
     21 /**
     22  * Test the "gps" location output works through various rate changes
     23  *
     24  * Tests:
     25  * 1. Toggle through various rates.
     26  * 2. Mix toggling through various rates with start & stop.
     27  *
     28  * Inspired by bugs 65246279, 65425110
     29  */
     30 
     31 public class GnssLocationRateChangeTest extends GnssTestCase {
     32 
     33     private static final String TAG = "GnssLocationRateChangeTest";
     34     private static final int LOCATION_TO_COLLECT_COUNT = 1;
     35 
     36     private TestLocationListener mLocationListenerMain;
     37     private TestLocationListener mLocationListenerAfterRateChanges;
     38     // Various rates, where underlying GNSS hardware states may enter different modes
     39     private static final int[] TBF_MSEC = {0, 4_000, 250_000, 6_000_000, 10, 1_000, 16_000, 64_000};
     40     private static final int LOOPS_FOR_STRESS_TEST = 20;
     41 
     42     @Override
     43     protected void setUp() throws Exception {
     44         super.setUp();
     45         mTestLocationManager = new TestLocationManager(getContext());
     46         // Using separate listeners, so the await trigger for the after-rate-changes listener is
     47         // independent of any possible locations that flow during setup, and rate change stress
     48         // testing
     49         mLocationListenerMain = new TestLocationListener(LOCATION_TO_COLLECT_COUNT);
     50         mLocationListenerAfterRateChanges = new TestLocationListener(LOCATION_TO_COLLECT_COUNT);
     51     }
     52 
     53     @Override
     54     protected void tearDown() throws Exception {
     55         // Unregister listeners
     56         if (mLocationListenerMain != null) {
     57             mTestLocationManager.removeLocationUpdates(mLocationListenerMain);
     58         }
     59         if (mLocationListenerAfterRateChanges != null) {
     60             mTestLocationManager.removeLocationUpdates(mLocationListenerAfterRateChanges);
     61         }
     62         super.tearDown();
     63     }
     64 
     65     /**
     66      * Requests (GPS) Locations at various rates that may stress the underlying GNSS software
     67      * and firmware state machine layers, ensuring Location output
     68      * remains responsive after all is done.
     69      */
     70     @AppModeFull(reason = "Flaky in instant mode.")
     71     public void testVariedRates() throws Exception {
     72         if (!TestUtils.deviceHasGpsFeature(getContext())) {
     73             return;
     74         }
     75 
     76         SoftAssert softAssert = new SoftAssert(TAG);
     77         mTestLocationManager.requestLocationUpdates(mLocationListenerMain);
     78         softAssert.assertTrue("Location should be received at test start",
     79                 mLocationListenerMain.await());
     80 
     81         for (int timeBetweenLocationsMsec : TBF_MSEC) {
     82             // Rapidly change rates requested, to ensure GNSS provider state changes can handle this
     83             mTestLocationManager.requestLocationUpdates(mLocationListenerMain,
     84                     timeBetweenLocationsMsec);
     85         }
     86         mTestLocationManager.removeLocationUpdates(mLocationListenerMain);
     87 
     88         mTestLocationManager.requestLocationUpdates(mLocationListenerAfterRateChanges);
     89         softAssert.assertTrue("Location should be received at test end",
     90                 mLocationListenerAfterRateChanges.await());
     91 
     92         softAssert.assertAll();
     93     }
     94 
     95     /**
     96      * Requests (GPS) Locations at various rates, and toggles between requests and removals,
     97      * that may stress the underlying GNSS software
     98      * and firmware state machine layers, ensuring Location output remains responsive
     99      * after all is done.
    100      */
    101     public void testVariedRatesOnOff() throws Exception {
    102         if (!TestUtils.deviceHasGpsFeature(getContext())) {
    103             return;
    104         }
    105 
    106         SoftAssert softAssert = new SoftAssert(TAG);
    107         mTestLocationManager.requestLocationUpdates(mLocationListenerMain);
    108         softAssert.assertTrue("Location should be received at test start",
    109                 mLocationListenerMain.await());
    110 
    111         for (int timeBetweenLocationsMsec : TBF_MSEC) {
    112             // Rapidly change rates requested, to ensure GNSS provider state changes can handle this
    113             mTestLocationManager.requestLocationUpdates(mLocationListenerMain,
    114                     timeBetweenLocationsMsec);
    115             // Also flip the requests on and off quickly
    116             mTestLocationManager.removeLocationUpdates(mLocationListenerMain);
    117         }
    118 
    119         mTestLocationManager.requestLocationUpdates(mLocationListenerAfterRateChanges);
    120         softAssert.assertTrue("Location should be received at test end",
    121                 mLocationListenerAfterRateChanges.await());
    122 
    123         softAssert.assertAll();
    124     }
    125 
    126     /**
    127      * Requests (GPS) Locations at various rates, and toggles between requests and removals,
    128      * in multiple loops to provide additional stress to the underlying GNSS software
    129      * and firmware state machine layers, ensuring Location output remains responsive
    130      * after all is done.
    131      */
    132     public void testVariedRatesRepetitive() throws Exception {
    133         if (!TestUtils.deviceHasGpsFeature(getContext())) {
    134             return;
    135         }
    136 
    137         SoftAssert softAssert = new SoftAssert(TAG);
    138         mTestLocationManager.requestLocationUpdates(mLocationListenerMain);
    139         softAssert.assertTrue("Location should be received at test start",
    140                 mLocationListenerMain.await());
    141 
    142         // Two loops, first without removes, then with removes
    143         for (int i = 0; i < LOOPS_FOR_STRESS_TEST; i++) {
    144             for (int timeBetweenLocationsMsec : TBF_MSEC) {
    145                mTestLocationManager.requestLocationUpdates(mLocationListenerMain,
    146                         timeBetweenLocationsMsec);
    147             }
    148         }
    149         for (int i = 0; i < LOOPS_FOR_STRESS_TEST; i++) {
    150             for (int timeBetweenLocationsMsec : TBF_MSEC) {
    151                 mTestLocationManager.requestLocationUpdates(mLocationListenerMain,
    152                         timeBetweenLocationsMsec);
    153                 // Also flip the requests on and off quickly
    154                 mTestLocationManager.removeLocationUpdates(mLocationListenerMain);
    155             }
    156         }
    157 
    158         mTestLocationManager.requestLocationUpdates(mLocationListenerAfterRateChanges);
    159         softAssert.assertTrue("Location should be received at test end",
    160                 mLocationListenerAfterRateChanges.await());
    161 
    162         softAssert.assertAll();
    163     }
    164 }
    165