Fix false alarm zero GPS measurement CTS failure

Android was updated to allow GnssMeasurementEvent's with
zero valid measurements (and just a clock.)
This CTS Verifier update now allows this.

Improved flakiness of GnssMeasurementRegistration status
check.

Also improved some test timeouts (restoring per test times),
output text, and variable names.

Bug: 30391885
Change-Id: I82661f06070faacadf069e1857be160405b6f55a
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index ef77c84..4ef177e 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -419,7 +419,7 @@
 
     <!-- Strings for Location GNSS tests -->
     <string name="location_gnss_constellation_type_test">GNSS Measurement Constellation Test</string>
-    <string name="location_gnss_measure_no_location_test">GNSS Measurement WhenNoLocation Test</string>
+    <string name="location_gnss_measure_no_location_test">GNSS Measurement Before Location Test</string>
     <string name="location_gnss_reg_test">GNSS Measurement Registration Test</string>
     <string name="location_gnss_value_test">GNSS Measurement Values Test</string>
     <string name="location_gnss_nav_msg_test">GNSS Navigation Message Test</string>
diff --git a/tests/tests/location/src/android/location/cts/GnssMeasurementRegistrationTest.java b/tests/tests/location/src/android/location/cts/GnssMeasurementRegistrationTest.java
index b46e906..2457c76 100644
--- a/tests/tests/location/src/android/location/cts/GnssMeasurementRegistrationTest.java
+++ b/tests/tests/location/src/android/location/cts/GnssMeasurementRegistrationTest.java
@@ -54,7 +54,6 @@
     private static final int GPS_EVENTS_COUNT = 1;
     private TestLocationListener mLocationListener;
     private TestGnssMeasurementListener mMeasurementListener;
-    private TestGpsStatusListener mGpsStatusListener;
 
     @Override
     protected void setUp() throws Exception {
@@ -72,9 +71,6 @@
         if (mMeasurementListener != null) {
             mTestLocationManager.unregisterGnssMeasurementCallback(mMeasurementListener);
         }
-        if (mGpsStatusListener != null) {
-            mTestLocationManager.removeGpsStatusListener(mGpsStatusListener);
-        }
         super.tearDown();
     }
 
@@ -94,8 +90,8 @@
         mTestLocationManager.registerGnssMeasurementCallback(mMeasurementListener);
 
         mMeasurementListener.await();
-        if (!mMeasurementListener.verifyState(isMeasurementTestStrict())) {
-            // If test is strict verifyState will assert conditions are good for further testing.
+        if (!mMeasurementListener.verifyStatus(isMeasurementTestStrict())) {
+            // If test is strict verifyStatus will assert conditions are good for further testing.
             // Else this returns false and, we arrive here, and then return from here (pass.)
             return;
         }
@@ -111,7 +107,7 @@
 
         SoftAssert.failAsWarning(
                 TAG,
-                "GPS measurements were not received without registering for location updates."
+                "GPS measurements were not received without registering for location updates. "
                 + "Trying again with Location request.");
 
         // Register for location updates.
diff --git a/tests/tests/location/src/android/location/cts/GnssMeasurementValuesTest.java b/tests/tests/location/src/android/location/cts/GnssMeasurementValuesTest.java
index e68464e..023b616 100644
--- a/tests/tests/location/src/android/location/cts/GnssMeasurementValuesTest.java
+++ b/tests/tests/location/src/android/location/cts/GnssMeasurementValuesTest.java
@@ -93,7 +93,7 @@
 
         Log.i(TAG, "Location status received = " + mLocationListener.isLocationReceived());
 
-        if (!mMeasurementListener.verifyState(isMeasurementTestStrict())) {
+        if (!mMeasurementListener.verifyStatus(isMeasurementTestStrict())) {
             // If test is strict and veriifyState reutrns false, an assert exception happens and
             // test fails.   If test is not strict, we arrive here, and:
             return; // exit (with pass)
diff --git a/tests/tests/location/src/android/location/cts/GnssMeasurementWhenNoLocationTest.java b/tests/tests/location/src/android/location/cts/GnssMeasurementWhenNoLocationTest.java
index 18d9552..0f4775e 100644
--- a/tests/tests/location/src/android/location/cts/GnssMeasurementWhenNoLocationTest.java
+++ b/tests/tests/location/src/android/location/cts/GnssMeasurementWhenNoLocationTest.java
@@ -55,7 +55,7 @@
  */
 public class GnssMeasurementWhenNoLocationTest extends GnssTestCase {
 
-    private static final String TAG = "GnssMeasWhenNoFixTest";
+    private static final String TAG = "GnssMeasBeforeLocTest";
     private TestGnssMeasurementListener mMeasurementListener;
     private TestGpsStatusListener mGpsStatusListener;
     private TestLocationListener mLocationListener;
@@ -88,7 +88,7 @@
     }
 
     /**
-     * Test GPS measurements without a location fix.
+     * Test for GPS measurements before a location fix.
      */
     public void testGnssMeasurementWhenNoLocation() throws Exception {
         // Checks if GPS hardware feature is present, skips test (pass) if not,
@@ -116,12 +116,13 @@
         mLocationListener = new TestLocationListener(LOCATIONS_COUNT);
         mTestLocationManager.requestLocationUpdates(mLocationListener);
 
-        if (!mMeasurementListener.verifyState(isMeasurementTestStrict())) {
-            return; // exit peacefully (if not already asserted out inside verifyState)
+        mMeasurementListener.awaitStatus();
+        if (!mMeasurementListener.verifyStatus(isMeasurementTestStrict())) {
+            return; // exit peacefully (if not already asserted out inside verifyStatus)
         }
 
-        // Wait for a few measurements (so as to check later that we've satisified this wait
-        // before getting _any_ locations.)
+        // Wait for two measurement events - this is better than waiting for a location calculation
+        // because the test generally completes much faster.
         mMeasurementListener.await();
 
         Log.i(TAG, "mLocationListener.isLocationReceived(): "
@@ -134,22 +135,23 @@
         List<GnssMeasurementsEvent> events = mMeasurementListener.getEvents();
         Log.i(TAG, "Number of GPS measurement events received = " + events.size());
 
-        SoftAssert.failOrWarning(isMeasurementTestStrict(),
-                "No GnssMeasurementEvent's found. Device may be indoors.  Retry outdoors?",
-                !events.isEmpty());
-        if (events.isEmpty() && !isMeasurementTestStrict()) {
-            return; // allow a (passing) return, if not strict, otherwise continue
-        }
-
         // Ensure that after getting a few (at least 2) measurements, that we still don't have
         // location (i.e. that we got measurements before location.)  Fail, if strict, warn, if not.
         SoftAssert.failOrWarning(isMeasurementTestStrict(),
-                "Insufficient GnssMeasurementEvent's found before location event.",
+                "Location was received before " + events.size() +
+                        " GnssMeasurementEvents with measurements were reported. " +
+                        "Test expects at least " + EVENTS_COUNT +
+                        " GnssMeasurementEvents before a location, given the cold start start. " +
+                        "Ensure no other active GPS apps (so the cold start command works) " +
+                        "and retry?",
                 !mLocationListener.isLocationReceived());
+        if (mLocationListener.isLocationReceived() && !isMeasurementTestStrict()) {
+            return; // allow a (passing) return, if not strict, otherwise continue
+        }
 
-        // If device is not indoor, verify that we receive GPS measurements before being able to
-        // calculate the position solution and verify that mandatory fields of GnssMeasurement are
-        // in expected ranges.
+        // If device is not indoors, verify
+        // 1) that we receive GPS measurements before being able to calculate the position solution
+        // 2) that mandatory fields of GnssMeasurement are in expected ranges.
         GnssMeasurementsEvent firstEvent = events.get(0);
         Collection<GnssMeasurement> gpsMeasurements = firstEvent.getMeasurements();
         int satelliteCount = gpsMeasurements.size();
@@ -159,8 +161,8 @@
             gpsPrns[i] = measurement.getSvid();
             ++i;
         }
+        Log.i(TAG, "First GnssMeasurementsEvent with PRNs=" + Arrays.toString(gpsPrns));
 
-        Log.i(TAG, "Gps Measurements 1st Event PRNs=" + Arrays.toString(gpsPrns));
         SoftAssert softAssert = new SoftAssert(TAG);
         long timeInNs = firstEvent.getClock().getTimeNanos();
         softAssert.assertTrue("GPS measurement satellite count check: ",
diff --git a/tests/tests/location/src/android/location/cts/GnssMeasurementsConstellationTest.java b/tests/tests/location/src/android/location/cts/GnssMeasurementsConstellationTest.java
index 201c692..4f125fc 100644
--- a/tests/tests/location/src/android/location/cts/GnssMeasurementsConstellationTest.java
+++ b/tests/tests/location/src/android/location/cts/GnssMeasurementsConstellationTest.java
@@ -22,7 +22,6 @@
 import android.util.Log;
 
 import java.util.List;
-import java.util.concurrent.TimeUnit;
 
 import junit.framework.Assert;
 
@@ -90,7 +89,7 @@
         mTestLocationManager.requestLocationUpdates(mLocationListener);
 
         mMeasurementListener.await();
-        if (!mMeasurementListener.verifyState(isMeasurementTestStrict())) {
+        if (!mMeasurementListener.verifyStatus(isMeasurementTestStrict())) {
             return;
         }
 
diff --git a/tests/tests/location/src/android/location/cts/TestGnssMeasurementListener.java b/tests/tests/location/src/android/location/cts/TestGnssMeasurementListener.java
index e7e2958..1809bfb 100644
--- a/tests/tests/location/src/android/location/cts/TestGnssMeasurementListener.java
+++ b/tests/tests/location/src/android/location/cts/TestGnssMeasurementListener.java
@@ -28,17 +28,19 @@
 /**
  * Used for receiving GPS satellite measurements from the GPS engine.
  * Each measurement contains raw and computed data identifying a satellite.
+ * Only counts measurement events with more than one actual Measurement in them (not just clock)
  */
 class TestGnssMeasurementListener extends GnssMeasurementsEvent.Callback {
     // Timeout in sec for count down latch wait
-    private static final int TIMEOUT_IN_SEC = 90;
+    private static final int STATUS_TIMEOUT_IN_SEC = 10;
+    private static final int MEAS_TIMEOUT_IN_SEC = 60;
 
     private volatile int mStatus = -1;
 
     private final String mTag;
-    private final int mEventsToCollect;
     private final List<GnssMeasurementsEvent> mMeasurementsEvents;
     private final CountDownLatch mCountDownLatch;
+    private final CountDownLatch mCountDownLatchStatus;
 
     TestGnssMeasurementListener(String tag) {
         this(tag, 0);
@@ -47,14 +49,16 @@
     TestGnssMeasurementListener(String tag, int eventsToCollect) {
         mTag = tag;
         mCountDownLatch = new CountDownLatch(eventsToCollect);
-        mEventsToCollect = eventsToCollect;
+        mCountDownLatchStatus = new CountDownLatch(1);
         mMeasurementsEvents = new ArrayList<>(eventsToCollect);
     }
 
     @Override
     public void onGnssMeasurementsReceived(GnssMeasurementsEvent event) {
-        mMeasurementsEvents.add(event);
-        if (mMeasurementsEvents.size() >= mEventsToCollect) {
+        // Only count measurement events with more than one actual Measurement in them
+        // (not just clock)
+        if (event.getMeasurements().size() > 0) {
+            mMeasurementsEvents.add(event);
             mCountDownLatch.countDown();
         }
     }
@@ -62,20 +66,23 @@
     @Override
     public void onStatusChanged(int status) {
         mStatus = status;
-        if (mStatus != GnssMeasurementsEvent.Callback.STATUS_READY) {
-            mCountDownLatch.countDown();
-        }
+        mCountDownLatchStatus.countDown();
+    }
+
+    public boolean awaitStatus() throws InterruptedException {
+        return TestUtils.waitFor(mCountDownLatchStatus, STATUS_TIMEOUT_IN_SEC);
     }
 
     public boolean await() throws InterruptedException {
-        return TestUtils.waitFor(mCountDownLatch);
+        return TestUtils.waitFor(mCountDownLatch, MEAS_TIMEOUT_IN_SEC);
     }
 
+
     /**
      * @return {@code true} if the state of the test ensures that data is expected to be collected,
      *         {@code false} otherwise.
      */
-    public boolean verifyState(boolean testIsStrict) {
+    public boolean verifyStatus(boolean testIsStrict) {
         switch (getStatus()) {
             case GnssMeasurementsEvent.Callback.STATUS_NOT_SUPPORTED:
                 String message = "GnssMeasurements is not supported in the device:"
diff --git a/tests/tests/location/src/android/location/cts/TestGnssNavigationMessageListener.java b/tests/tests/location/src/android/location/cts/TestGnssNavigationMessageListener.java
index 3765f3c..068c4cb 100644
--- a/tests/tests/location/src/android/location/cts/TestGnssNavigationMessageListener.java
+++ b/tests/tests/location/src/android/location/cts/TestGnssNavigationMessageListener.java
@@ -32,7 +32,7 @@
 class TestGnssNavigationMessageListener extends GnssNavigationMessage.Callback {
 
     // Timeout in sec for count down latch wait
-    private static final long TIMEOUT_IN_SEC = 90L;
+    private static final int TIMEOUT_IN_SEC = 90;
 
     private volatile int mStatus = -1;
 
@@ -66,7 +66,7 @@
 
     public boolean await() throws InterruptedException {
         Log.i(mTag, "Number of GPS Navigation Message received = " + getEvents().size());
-        return TestUtils.waitFor(mCountDownLatch);
+        return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC);
     }
 
     /**
diff --git a/tests/tests/location/src/android/location/cts/TestGnssStatusCallback.java b/tests/tests/location/src/android/location/cts/TestGnssStatusCallback.java
index b5e102e..cc4c930 100644
--- a/tests/tests/location/src/android/location/cts/TestGnssStatusCallback.java
+++ b/tests/tests/location/src/android/location/cts/TestGnssStatusCallback.java
@@ -87,6 +87,6 @@
     }
 
     public boolean await() throws InterruptedException {
-        return TestUtils.waitFor(mCountDownLatch);
+        return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC);
     }
 }
diff --git a/tests/tests/location/src/android/location/cts/TestGpsStatusListener.java b/tests/tests/location/src/android/location/cts/TestGpsStatusListener.java
index 7f2b6d3..bd114c1 100644
--- a/tests/tests/location/src/android/location/cts/TestGpsStatusListener.java
+++ b/tests/tests/location/src/android/location/cts/TestGpsStatusListener.java
@@ -91,6 +91,6 @@
     }
 
     public boolean await() throws InterruptedException {
-        return TestUtils.waitFor(mCountDownLatch);
+        return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC);
     }
 }
diff --git a/tests/tests/location/src/android/location/cts/TestLocationListener.java b/tests/tests/location/src/android/location/cts/TestLocationListener.java
index 7681ec4..f266d5a 100644
--- a/tests/tests/location/src/android/location/cts/TestLocationListener.java
+++ b/tests/tests/location/src/android/location/cts/TestLocationListener.java
@@ -30,7 +30,7 @@
     private volatile boolean mProviderEnabled;
     private volatile boolean mLocationReceived;
     // Timeout in sec for count down latch wait
-    private static final int TIMEOUT_IN_SEC = 300;
+    private static final int TIMEOUT_IN_SEC = 120;
     private final CountDownLatch mCountDownLatch;
 
     TestLocationListener(int locationToCollect) {
@@ -62,7 +62,7 @@
     }
 
     public boolean await() throws InterruptedException {
-        return TestUtils.waitFor(mCountDownLatch);
+        return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC);
     }
 
     /**
diff --git a/tests/tests/location/src/android/location/cts/TestUtils.java b/tests/tests/location/src/android/location/cts/TestUtils.java
index 82d644e..a038457 100644
--- a/tests/tests/location/src/android/location/cts/TestUtils.java
+++ b/tests/tests/location/src/android/location/cts/TestUtils.java
@@ -22,9 +22,8 @@
 public class TestUtils {
     private static final long STANDARD_WAIT_TIME_MS = 50;
     private static final long STANDARD_SLEEP_TIME_MS = 50;
-    private static final long STANDARD_WAIT_TIME_ROUNDS = 1200;
 
-    public static boolean waitFor(CountDownLatch latch) throws InterruptedException {
+    public static boolean waitFor(CountDownLatch latch, int timeInSec) throws InterruptedException {
         // Since late 2014, if the main thread has been occupied for long enough, Android will
         // increase its priority. Such new behavior can causes starvation to the background thread -
         // even if the main thread has called await() to yield its execution, the background thread
@@ -37,7 +36,9 @@
         // hack isn't ideal, but at least it can work.
         //
         // See also: b/17423027
-        for (int i = 0; i < STANDARD_WAIT_TIME_ROUNDS; ++i) {
+        long waitTimeRounds = (TimeUnit.SECONDS.toMillis(timeInSec)) /
+                (STANDARD_WAIT_TIME_MS + STANDARD_SLEEP_TIME_MS);
+        for (int i = 0; i < waitTimeRounds; ++i) {
             Thread.sleep(STANDARD_SLEEP_TIME_MS);
             if (latch.await(STANDARD_WAIT_TIME_MS, TimeUnit.MILLISECONDS)) {
                 return true;