Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 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.location.GnssMeasurement;
     20 import android.location.GnssMeasurementsEvent;
     21 import android.util.Log;
     22 
     23 import java.util.List;
     24 
     25 /**
     26  * Test the {@link GnssMeasurement} values.
     27  *
     28  * Test steps:
     29  * 1. Register for location updates.
     30  * 2. Register a listener for {@link GnssMeasurementsEvent}s.
     31  * 3. Wait for {@link #LOCATION_TO_COLLECT_COUNT} locations.
     32  *          3.1 Confirm locations have been found.
     33  * 4. Check {@link GnssMeasurementsEvent} status: if the status is not
     34  *    {@link GnssMeasurementsEvent#STATUS_READY}, the test will be skipped because one of the
     35  *    following reasons:
     36  *          4.1 the device does not support the GPS feature,
     37  *          4.2 GPS Location is disabled in the device and this is CTS (non-verifier)
     38  *  5. Verify {@link GnssMeasurement}s (all mandatory fields), the test will fail if any of the
     39  *     mandatory fields is not populated or in the expected range.
     40  */
     41 public class GnssMeasurementValuesTest extends GnssTestCase {
     42 
     43     private static final String TAG = "GnssMeasValuesTest";
     44     private static final int LOCATION_TO_COLLECT_COUNT = 5;
     45 
     46     private TestGnssMeasurementListener mMeasurementListener;
     47     private TestLocationListener mLocationListener;
     48 
     49     @Override
     50     protected void setUp() throws Exception {
     51         super.setUp();
     52 
     53         mTestLocationManager = new TestLocationManager(getContext());
     54     }
     55 
     56     @Override
     57     protected void tearDown() throws Exception {
     58         // Unregister listeners
     59         if (mLocationListener != null) {
     60             mTestLocationManager.removeLocationUpdates(mLocationListener);
     61         }
     62         if (mMeasurementListener != null) {
     63             mTestLocationManager.unregisterGnssMeasurementCallback(mMeasurementListener);
     64         }
     65         super.tearDown();
     66     }
     67 
     68     /**
     69      * Tests that one can listen for {@link GnssMeasurementsEvent} for collection purposes.
     70      * It only performs sanity checks for the measurements received.
     71      * This tests uses actual data retrieved from GPS HAL.
     72      */
     73     public void testListenForGnssMeasurements() throws Exception {
     74         // Checks if GPS hardware feature is present, skips test (pass) if not,
     75         // and hard asserts that Location/GPS (Provider) is turned on if is Cts Verifier.
     76        if (!TestMeasurementUtil.canTestRunOnCurrentDevice(mTestLocationManager,
     77                 TAG, MIN_HARDWARE_YEAR_MEASUREMENTS_REQUIRED, isCtsVerifierTest())) {
     78             return;
     79         }
     80 
     81         mLocationListener = new TestLocationListener(LOCATION_TO_COLLECT_COUNT);
     82         mTestLocationManager.requestLocationUpdates(mLocationListener);
     83 
     84         mMeasurementListener = new TestGnssMeasurementListener(TAG);
     85         mTestLocationManager.registerGnssMeasurementCallback(mMeasurementListener);
     86 
     87         boolean success = mLocationListener.await();
     88         SoftAssert.failOrWarning(isMeasurementTestStrict(),
     89                 "Time elapsed without getting enough location fixes."
     90                         + " Possibly, the test has been run deep indoors."
     91                         + " Consider retrying test outdoors.",
     92                 success);
     93 
     94         Log.i(TAG, "Location status received = " + mLocationListener.isLocationReceived());
     95 
     96         if (!mMeasurementListener.verifyStatus(isMeasurementTestStrict())) {
     97             // If test is strict and verifyStatus returns false, an assert exception happens and
     98             // test fails.   If test is not strict, we arrive here, and:
     99             return; // exit (with pass)
    100         }
    101 
    102         List<GnssMeasurementsEvent> events = mMeasurementListener.getEvents();
    103         int eventCount = events.size();
    104         Log.i(TAG, "Number of Gps Event received = " + eventCount);
    105 
    106         SoftAssert softAssert = new SoftAssert(TAG);
    107 
    108         softAssert.assertTrue("GnssMeasurementEvent count", "X > 0",
    109                 String.valueOf(eventCount), eventCount > 0);
    110 
    111         boolean carrierPhaseQualityPrrFound = false;
    112         // we received events, so perform a quick sanity check on mandatory fields
    113         for (GnssMeasurementsEvent event : events) {
    114             // Verify Gps Event mandatory fields are in required ranges
    115             assertNotNull("GnssMeasurementEvent cannot be null.", event);
    116 
    117             // TODO(sumitk): To validate the timestamp if we receive GPS clock.
    118             long timeInNs = event.getClock().getTimeNanos();
    119             TestMeasurementUtil.assertGnssClockFields(event.getClock(), softAssert, timeInNs);
    120 
    121             for (GnssMeasurement measurement : event.getMeasurements()) {
    122                 TestMeasurementUtil.assertAllGnssMeasurementMandatoryFields(mTestLocationManager,
    123                         measurement, softAssert, timeInNs);
    124                 carrierPhaseQualityPrrFound |=
    125                         TestMeasurementUtil.gnssMeasurementHasCarrierPhasePrr(measurement);
    126             }
    127         }
    128         softAssert.assertOrWarnTrue(isMeasurementTestStrict(),
    129                 "GNSS Measurements PRRs with Carrier Phase "
    130                         + "level uncertainties.  If failed, retry near window or outdoors?",
    131                 carrierPhaseQualityPrrFound);
    132         softAssert.assertAll();
    133     }
    134 }
    135