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.GnssNavigationMessage;
     20 import android.util.Log;
     21 
     22 import java.util.List;
     23 
     24 /**
     25  * Test the {@link GnssNavigationMessage} without location registration.
     26  *
     27  * Test steps:
     28  * 1. Register for {@link GnssNavigationMessage}s.
     29  * 2. Wait for {@link #EVENTS_COUNT} events to arrive.
     30  * 3. Check {@link GnssNavigationMessage} status: if the status is not
     31  *    {@link GnssNavigationMessage#Callback#STATUS_READY}, the test will be skipped because one of the
     32  *    following reasons:
     33  *          3.1 the device does not support the feature,
     34  *          3.2 GPS is disabled in the device,
     35  *          3.3 Location is disabled in the device.
     36  * 4. If at least one {@link GnssNavigationMessage} is received, the test will pass.
     37  * 5. If no {@link GnssNavigationMessage}s are received, then check whether the device is
     38  *    deep indoor. This is done by performing the following steps:
     39  *          2.1 Register for location updates, and {@link GpsStatus} events.
     40  *          2.2 Wait for {@link TestGpsStatusListener#TIMEOUT_IN_SEC}.
     41  *          2.3 If no {@link GpsStatus} is received this will mean that the device is located
     42  *              indoor. Test will be skipped.
     43  *          2.4 If we receive a {@link GpsStatus}, it mean that {@link GnssNavigationMessage}s
     44  *              are provided only if the application registers for location updates as well:
     45  *                  2.4.1 The test will pass with a warning for the M release.
     46  *                  2.4.2 The test might fail in a future Android release, when this requirement
     47  *                        becomes mandatory.
     48  */
     49 public class GnssNavigationMessageRegistrationTest extends GnssTestCase {
     50 
     51     private static final String TAG = "GpsNavMsgRegTest";
     52     private static final int EVENTS_COUNT = 5;
     53     private TestGnssNavigationMessageListener mTestGnssNavigationMessageListener;
     54     private TestLocationListener mLocationListener;
     55     private TestGnssStatusCallback mGnssStatusCallback;
     56 
     57     @Override
     58     protected void setUp() throws Exception {
     59         super.setUp();
     60         mTestLocationManager = new TestLocationManager(getContext());
     61     }
     62 
     63     @Override
     64     protected void tearDown() throws Exception {
     65         // Unregister GnssNavigationMessageListener
     66         if (mTestGnssNavigationMessageListener != null) {
     67             mTestLocationManager
     68                     .unregisterGnssNavigationMessageCallback(mTestGnssNavigationMessageListener);
     69             mTestGnssNavigationMessageListener = null;
     70         }
     71         if (mLocationListener != null) {
     72             mTestLocationManager.removeLocationUpdates(mLocationListener);
     73         }
     74         if (mGnssStatusCallback != null) {
     75             mTestLocationManager.unregisterGnssStatusCallback(mGnssStatusCallback);
     76         }
     77         super.tearDown();
     78     }
     79 
     80     /**
     81      * Tests that one can listen for {@link GnssNavigationMessage}s for collection purposes.
     82      * It only performs sanity checks for the Navigation messages received.
     83      */
     84     public void testGnssNavigationMessageRegistration() throws Exception {
     85         // Checks if GPS hardware feature is present, skips test (pass) if not,
     86         // and hard asserts that Location/GPS (Provider) is turned on if is Cts Verifier.
     87         if (!TestMeasurementUtil
     88                 .canTestRunOnCurrentDevice(mTestLocationManager, isCtsVerifierTest())) {
     89             return;
     90         }
     91 
     92         // Register Gps Navigation Message Listener.
     93         mTestGnssNavigationMessageListener =
     94                 new TestGnssNavigationMessageListener(TAG, EVENTS_COUNT);
     95         mTestLocationManager.registerGnssNavigationMessageCallback(mTestGnssNavigationMessageListener);
     96 
     97         mTestGnssNavigationMessageListener.await();
     98         if (!mTestGnssNavigationMessageListener.verifyState()) {
     99             return;
    100         }
    101 
    102         List<GnssNavigationMessage> events = mTestGnssNavigationMessageListener.getEvents();
    103         if (!events.isEmpty()) {
    104             // Verify mandatory GnssNavigationMessage field values.
    105             TestMeasurementUtil.verifyGnssNavMessageMandatoryField(mTestLocationManager, events);
    106             // Test passes if we get at least 1 GPS Navigation Message event.
    107             Log.i(TAG, "Received GPS Navigation Message. Test Pass.");
    108             return;
    109         }
    110 
    111         // If no {@link GnssNavigationMessage}s are received, then check whether the device is
    112         // deep indoor.
    113         Log.i(TAG, "Did not receive any GPS Navigation Message. Test if device is deep indoor.");
    114 
    115         // Register for location updates.
    116         mLocationListener = new TestLocationListener(EVENTS_COUNT);
    117         mTestLocationManager.requestLocationUpdates(mLocationListener);
    118 
    119         // Wait for location updates
    120         mLocationListener.await();
    121         Log.i(TAG, "Location received = " + mLocationListener.isLocationReceived());
    122 
    123         // Register for Gps Status updates
    124         mGnssStatusCallback = new TestGnssStatusCallback(TAG, EVENTS_COUNT);
    125         mTestLocationManager.registerGnssStatusCallback(mGnssStatusCallback);
    126 
    127         // Wait for Gps Status updates
    128         mGnssStatusCallback.awaitStatus();
    129         if (mGnssStatusCallback.getGnssStatus() == null) {
    130             // Skip the Test. No Satellites are visible. Device may be Indoor
    131             Log.i(TAG, "No Satellites are visible. Device may be Indoor. Skipping Test.");
    132             return;
    133         }
    134 
    135         SoftAssert.failAsWarning(
    136                 TAG,
    137                 "GPS Navigation Messages were not received without registering for location" +
    138                         " updates.");
    139     }
    140 }
    141