Add CTS Verifier test for GPS.

Currently this tests:
1) GPS works (a location is eventually provided)
2) GPS update frequency is never faster than the requested minTime paramter

Bug: 6424983
Change-Id: Ia87092e82bbdd18ab1173729f28bc363ca17a6da
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index a3a81a2..4b47f65 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -23,12 +23,13 @@
     <!-- Using 10 for more complete NFC support... -->
     <uses-sdk android:minSdkVersion="10"></uses-sdk>
 
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
-    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
-    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
     <uses-permission android:name="android.permission.BLUETOOTH" />
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.CAMERA" />
+    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.NFC" />
     <uses-feature android:name="android.hardware.camera.front"
@@ -243,6 +244,17 @@
             <meta-data android:name="test_category" android:value="@string/test_category_features" />
         </activity>
 
+        <activity android:name=".location.GpsTestActivity"
+                android:label="@string/location_gps_test"
+                android:configChanges="keyboardHidden|orientation">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.cts.intent.category.MANUAL_TEST" />
+            </intent-filter>
+            <meta-data android:name="test_category" android:value="@string/test_category_hardware" />
+            <meta-data android:name="test_required_features" android:value="android.hardware.location.gps" />
+        </activity>
+
         <activity android:name=".nfc.NfcTestActivity"
                 android:label="@string/nfc_test"
                 android:configChanges="keyboardHidden|orientation">
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index de99738..6cf8d7d 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -191,6 +191,14 @@
 
     <string name="empty"></string>
 
+    <!-- Strings for Location tests -->
+    <string name="location_gps_test">GPS Test</string>
+    <string name="location_gps_test_info">This test verifies basic GPS behavior
+        and callback scheduling.
+        Make sure the device has line of sight to GPS satellites
+        (for example, outside, or near a window)
+        and then press OK to run the automated tests.</string>
+
     <!-- Strings for NfcTestActivity -->
     <string name="nfc_test">NFC Test</string>
     <string name="nfc_test_info">The Peer-to-Peer Data Exchange tests require two devices with
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/location/GpsTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/location/GpsTestActivity.java
new file mode 100644
index 0000000..93cdfc5
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/location/GpsTestActivity.java
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.verifier.location;
+
+import com.android.cts.verifier.PassFailButtons;
+import com.android.cts.verifier.R;
+
+import android.content.Context;
+import android.graphics.Color;
+import android.graphics.Typeface;
+import android.location.LocationManager;
+import android.os.Bundle;
+import android.text.Spannable;
+import android.text.style.ForegroundColorSpan;
+import android.widget.TextView;
+
+interface PassFailLog {
+    public void log(String s);
+    public void pass();
+    public void fail(String s);
+}
+
+/**
+ * CTS Verifier case for verifying GPS.
+ */
+public class GpsTestActivity extends PassFailButtons.Activity implements PassFailLog {
+    private LocationManager mLocationManager;
+    private TextView mTextView;
+
+    LocationVerifier mLocationVerifier;
+    private int mTestNumber;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        mLocationManager = (LocationManager) getApplicationContext().getSystemService(
+                Context.LOCATION_SERVICE);
+
+        setContentView(R.layout.pass_fail_text);
+        setPassFailButtonClickListeners();
+        setInfoResources(R.string.location_gps_test, R.string.location_gps_test_info, -1);
+        mTextView = (TextView) findViewById(R.id.text);
+        mTextView.setTypeface(Typeface.MONOSPACE);
+        mTextView.setTextSize(15.0f);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+
+        mTextView.setText("");
+        getPassButton().setEnabled(false);
+        mTestNumber = 0;
+        nextTest();
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+
+        if (mLocationVerifier != null) {
+            mLocationVerifier.stop();
+        }
+    }
+
+    @Override
+    public String getTestDetails() {
+        return mTextView.getText().toString();
+    }
+
+    private void nextTest() {
+        mTestNumber++;
+        switch (mTestNumber) {
+            case 1:
+                // Test GPS with minTime = 0
+                mLocationVerifier = new LocationVerifier(this, mLocationManager,
+                        LocationManager.GPS_PROVIDER, 0, 10);
+                mLocationVerifier.start();
+                break;
+            case 2:
+                // Test GPS with minTime = 1s
+                mLocationVerifier = new LocationVerifier(this, mLocationManager,
+                        LocationManager.GPS_PROVIDER, 1000, 10);
+                mLocationVerifier.start();
+                break;
+            case 3:
+                // Test GPS with minTime = 15s
+                mLocationVerifier = new LocationVerifier(this, mLocationManager,
+                        LocationManager.GPS_PROVIDER, 15 * 1000, 4);
+                mLocationVerifier.start();
+                break;
+            case 4:
+                log("All GPS tests complete", Color.GREEN);
+                getPassButton().setEnabled(true);
+                break;
+        }
+    }
+
+    @Override
+    public void log(String s) {
+        mTextView.append(s);
+        mTextView.append("\n");
+    }
+
+    private void log(String s, int color) {
+        int start = mTextView.getText().length();
+        mTextView.append(s);
+        mTextView.append("\n");
+        int end = mTextView.getText().length();
+        Spannable spanText = (Spannable) mTextView.getText();
+        spanText.setSpan(new ForegroundColorSpan(color), start, end, 0);
+    }
+
+    @Override
+    public void fail(String s) {
+        log(s, Color.RED);
+        mLocationVerifier = null;
+    }
+
+    @Override
+    public void pass() {
+        log("OK!\n", Color.GREEN);
+        mLocationVerifier = null;
+        nextTest();
+    }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/location/LocationVerifier.java b/apps/CtsVerifier/src/com/android/cts/verifier/location/LocationVerifier.java
new file mode 100644
index 0000000..93c97eb
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/location/LocationVerifier.java
@@ -0,0 +1,143 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.verifier.location;
+
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.os.SystemClock;
+
+public class LocationVerifier implements LocationListener, Handler.Callback {
+    public static final String TAG = "CtsVerifierLocation";
+
+    private static final int MSG_TIMEOUT = 1;
+
+    private final LocationManager mLocationManager;
+    private final PassFailLog mCb;
+    private final String mProvider;
+    private final long mInterval;
+    private final long mMinInterval;
+    private final long mMaxInterval;
+    private final Handler mHandler;
+    private final int mRequestedUpdates;
+
+    private long mLastTimestamp = -1;
+    private int mNumUpdates = 0;
+    private boolean mRunning = false;
+
+    public LocationVerifier(PassFailLog cb, LocationManager locationManager,
+            String provider, long requestedInterval, int numUpdates) {
+        mProvider = provider;
+        mInterval = requestedInterval;
+        // Updates can be up to 100ms fast
+        mMinInterval = Math.max(0, requestedInterval - 100);
+        // timeout at 60 seconds
+        mMaxInterval = requestedInterval + 60 * 1000;
+        mRequestedUpdates = numUpdates;
+        mLocationManager = locationManager;
+        mCb = cb;
+        mHandler = new Handler(this);
+    }
+
+    public void start() {
+        mCb.log("enabling " + mProvider + " for " + mInterval + "ms updates");
+        mRunning = true;
+        expectNextUpdate(mMaxInterval);
+        mLastTimestamp = SystemClock.elapsedRealtime();
+        mLocationManager.requestLocationUpdates(mProvider, mInterval, 0,
+                LocationVerifier.this);
+    }
+
+    public void stop() {
+        mRunning = false;
+        mLocationManager.removeUpdates(LocationVerifier.this);
+        mHandler.removeMessages(MSG_TIMEOUT);
+    }
+
+    private void pass() {
+        stop();
+        mCb.log("disabling " + mProvider);
+        mCb.pass();
+    }
+
+    private void fail(String s) {
+        stop();
+        mCb.log("disabling");
+        mCb.fail(s);
+    }
+
+    private void expectNextUpdate(long timeout) {
+        mHandler.removeMessages(MSG_TIMEOUT);
+        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_TIMEOUT), timeout);
+    }
+
+    @Override
+    public boolean handleMessage(Message msg) {
+        if (!mRunning) return true;
+        fail("timeout (" + mMaxInterval + "ms) waiting for " +
+                mProvider + " location change");
+        return true;
+    }
+
+    @Override
+    public void onLocationChanged(Location location) {
+        if (!mRunning) return;
+
+        mNumUpdates++;
+        expectNextUpdate(mMaxInterval);
+
+        long timestamp = SystemClock.elapsedRealtime();
+        long delta = timestamp - mLastTimestamp;
+        mLastTimestamp = timestamp;
+
+        if (delta > mMaxInterval) {
+            fail(mProvider + " location changed too slow: " + delta + "ms > " +
+                    mMaxInterval + "ms");
+            return;
+        } else if (mNumUpdates == 1) {
+            mCb.log("received " + mProvider + " location (1st update, " + delta + "ms)");
+        } else if (delta < mMinInterval) {
+            fail(mProvider + " location updated too fast: " + delta + "ms < " +
+                    mMinInterval + "ms");
+            return;
+        } else {
+            mCb.log("received " + mProvider + " location (" + delta + "ms)");
+        }
+
+        if (!mProvider.equals(location.getProvider())) {
+            fail("wrong provider in callback, actual: " + location.getProvider() +
+                    " expected: " + mProvider);
+            return;
+        }
+
+        if (mNumUpdates >= mRequestedUpdates) {
+            pass();
+        }
+    }
+
+    @Override
+    public void onStatusChanged(String provider, int status, Bundle extras) {  }
+
+    @Override
+    public void onProviderEnabled(String provider) {  }
+
+    @Override
+    public void onProviderDisabled(String provider) {   }
+}
\ No newline at end of file