Merge "Fix tests to handle sensors not supported. b/17578209" into lmp-dev
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index ea86111..6676a01 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -545,7 +545,7 @@
     <string name="snsr_significant_motion_test_in_hand">Once you begin the test, hold the device in your hand while you perform natural hand movements.</string>
     <string name="snsr_significant_motion_test_sitting">Once you begin the test, keep the device in your pocket and move naturally while sitting in a chair.</string>
     <string name="snsr_significant_motion_test_deactivation">Once you begin the test, you will need to walk to ensure Significant Motion triggers only once.</string>
-    <string name="snsr_significant_motion_registration">Expected to be able to register for TriggerSensor. Found=%b.</string>
+    <string name="snsr_significant_motion_registration">Expected to be able to register for TriggerSensor. Found=%1$b.</string>
     <string name="snsr_significant_motion_cancelation">Expected to be able to cancel TriggerSensor. Found=%b.</string>
 
     <!-- Strings for Sensor CTS tests inside CtsVerifier -->
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/BatchingTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/BatchingTestActivity.java
index 42bd6ad..d6a6e36 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/BatchingTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/BatchingTestActivity.java
@@ -61,15 +61,12 @@
     protected void activitySetUp() throws InterruptedException {
         PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
         mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "BatchingTests");
-
-        deactivateSensorFeatures();
         mWakeLock.acquire();
     }
 
     @Override
     protected void activityCleanUp() throws InterruptedException {
         mWakeLock.release();
-        restoreSensorFeatures();
     }
 
     // TODO: refactor to discover all available sensors of each type and dynamically generate test
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorPowerTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorPowerTestActivity.java
index 85ab6c5..245d115 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorPowerTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorPowerTestActivity.java
@@ -62,7 +62,6 @@
     @Override
     protected void activitySetUp() throws InterruptedException {
         setScreenOffTimeout(15, TimeUnit.SECONDS);
-        deactivateSensorFeatures();
     }
 
     @Override
@@ -70,8 +69,6 @@
         if (mHostLink != null) {
             mHostLink.close();
         }
-
-        restoreSensorFeatures();
         resetScreenOffTimeout();
     }
 
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorValueAccuracyActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorValueAccuracyActivity.java
index d8075f0..39d02ac 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorValueAccuracyActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorValueAccuracyActivity.java
@@ -57,8 +57,6 @@
     private static final float RANGE_ATMOSPHERIC_PRESSURE = 35f;
     private static final float AMBIENT_TEMPERATURE_AVERAGE = 22.5f;
     private static final float AMBIENT_TEMPERATURE_THRESHOLD = 7.5f;
-    private static final float PROXIMITY_AVERAGE = 50f;
-    private static final float PROXIMITY_THRESHOLD = 50f;
     private static final double ONE_HUNDRED_EIGHTY_DEGREES = 180.0f;
 
     private static final double GYROSCOPE_INTEGRATION_THRESHOLD_DEGREES = 10.0f;
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SignificantMotionTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SignificantMotionTestActivity.java
index 73608f1..936145d 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SignificantMotionTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SignificantMotionTestActivity.java
@@ -26,7 +26,7 @@
 import android.hardware.SensorManager;
 import android.hardware.TriggerEvent;
 import android.hardware.TriggerEventListener;
-import android.os.Bundle;
+import android.hardware.cts.helpers.SensorNotSupportedException;
 import android.os.SystemClock;
 
 import java.util.concurrent.CountDownLatch;
@@ -144,9 +144,10 @@
         }
 
         TriggerVerifier verifier = new TriggerVerifier();
+        boolean success = mSensorManager.requestTriggerSensor(verifier, mSensorSignificantMotion);
         Assert.assertTrue(
-                getString(R.string.snsr_significant_motion_registration),
-                mSensorManager.requestTriggerSensor(verifier, mSensorSignificantMotion));
+                getString(R.string.snsr_significant_motion_registration, success),
+                success);
         if (cancelEventNotification) {
             Assert.assertTrue(
                     getString(R.string.snsr_significant_motion_cancelation),
@@ -168,14 +169,13 @@
     }
 
     @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
+    protected void activitySetUp() {
         mSensorManager = (SensorManager) getApplicationContext()
                 .getSystemService(Context.SENSOR_SERVICE);
-        mSensorSignificantMotion = mSensorManager
-                .getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
-
+        mSensorSignificantMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
+        if (mSensorSignificantMotion == null) {
+            throw new SensorNotSupportedException(Sensor.TYPE_SIGNIFICANT_MOTION);
+        }
     }
 
     /**
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/StepCounterTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/StepCounterTestActivity.java
index 221c92a..2868a0c 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/StepCounterTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/StepCounterTestActivity.java
@@ -27,6 +27,7 @@
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
 import android.hardware.cts.helpers.MovementDetectorHelper;
+import android.hardware.cts.helpers.SensorTestStateNotSupportedException;
 import android.hardware.cts.helpers.TestSensorEvent;
 import android.os.SystemClock;
 import android.view.MotionEvent;
@@ -79,6 +80,11 @@
         mSensorStepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
         mSensorStepDetector = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
 
+        if (mSensorStepCounter == null && mSensorStepDetector == null) {
+            throw new SensorTestStateNotSupportedException(
+                    "Sensors Step Counter/Detector are not supported.");
+        }
+
         ScrollView scrollView = (ScrollView) findViewById(R.id.log_scroll_view);
         scrollView.setOnTouchListener(new View.OnTouchListener() {
             @Override
@@ -131,7 +137,7 @@
         mMoveDetected = false;
         mCheckForMotion = false;
 
-        getTestLogger().logMessage(instructionsResId);
+        getTestLogger().logInstructions(instructionsResId);
         waitForUserToBegin();
 
         mCheckForMotion = (expectedSteps > 0);
@@ -148,7 +154,7 @@
         return verifyMeasurements(expectedSteps);
     }
 
-    private void startMeasurements() throws Throwable {
+    private void startMeasurements() {
         if (mSensorStepCounter != null) {
             mSensorManager.registerListener(this, mSensorStepCounter,
                     SensorManager.SENSOR_DELAY_NORMAL);
@@ -168,7 +174,7 @@
         mMovementDetectorHelper.start();
     }
 
-    private String verifyMeasurements(int stepsExpected) throws Throwable {
+    private String verifyMeasurements(int stepsExpected) {
         mSensorManager.unregisterListener(this);
         mMovementDetectorHelper.stop();
 
@@ -192,8 +198,12 @@
     }
 
     private void verifyStepCounterMeasurements() {
-        final int userReportedSteps = mTimestampsUserReported.size();
+        if (mSensorStepCounter == null) {
+            // sensor not supported, so no-op
+            return;
+        }
 
+        final int userReportedSteps = mTimestampsUserReported.size();
         int totalStepsCounted = 0;
         int initialStepCount = -1;
         for (TestSensorEvent counterEvent : mStepCounterEvents) {
@@ -263,8 +273,12 @@
     }
 
     private void verifyStepDetectorMeasurements() {
-        final int userReportedSteps = mTimestampsUserReported.size();
+        if (mSensorStepDetector == null) {
+            // sensor not supported, so no-op
+            return;
+        }
 
+        final int userReportedSteps = mTimestampsUserReported.size();
         int stepsDetected = mStepDetectorEvents.size();
         int stepsDetectedDelta = Math.abs(stepsDetected - userReportedSteps);
         String stepsDetectedMessage = getString(
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/BaseSensorSemiAutomatedTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/BaseSensorSemiAutomatedTestActivity.java
index 7f32a07..9f2f5c4 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/BaseSensorSemiAutomatedTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/BaseSensorSemiAutomatedTestActivity.java
@@ -18,7 +18,7 @@
 
 import com.android.cts.verifier.sensors.reporting.SensorTestDetails;
 
-import android.hardware.cts.helpers.SensorNotSupportedException;
+import android.hardware.cts.helpers.SensorTestStateNotSupportedException;
 
 /**
  * Base class to author a single Sensor semi-automated test case.
@@ -37,8 +37,8 @@
         SensorTestDetails.ResultCode resultCode = SensorTestDetails.ResultCode.PASS;
         try {
             onRun();
-        } catch(SensorNotSupportedException e) {
-            // the sensor is not supported/available in the device, log a warning and skip the test
+        } catch(SensorTestStateNotSupportedException e) {
+            // the sensor state is not supported in the device, log a warning and skip the test
             resultCode = SensorTestDetails.ResultCode.SKIPPED;
             summary = e.getMessage();
         } catch(Throwable e) {
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/BaseSensorTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/BaseSensorTestActivity.java
index 04603cb..d4ef516 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/BaseSensorTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/BaseSensorTestActivity.java
@@ -28,6 +28,7 @@
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
+import android.hardware.cts.helpers.SensorTestStateNotSupportedException;
 import android.media.MediaPlayer;
 import android.os.Bundle;
 import android.os.Vibrator;
@@ -143,12 +144,18 @@
     @Override
     public void run() {
         SensorTestDetails testDetails = null;
+        mSensorFeaturesDeactivator.requestDeactivationOfFeatures();
         try {
             activitySetUp();
-        } catch (Throwable e) {
+        } catch (SensorTestStateNotSupportedException e) {
             testDetails = new SensorTestDetails(
                     getTestClassName(),
                     SensorTestDetails.ResultCode.SKIPPED,
+                    e.getMessage());
+        } catch (Throwable e) {
+            testDetails = new SensorTestDetails(
+                    getTestClassName(),
+                    SensorTestDetails.ResultCode.FAIL,
                     "[ActivitySetUp] " + e.getMessage());
         }
 
@@ -167,6 +174,7 @@
                     SensorTestDetails.ResultCode.FAIL,
                     "[ActivityCleanUp] " + e.getMessage());
         }
+        mSensorFeaturesDeactivator.requestToRestoreFeatures();
         mTestLogger.logInstructions(R.string.snsr_test_complete);
 
         // log to screen and save the overall test summary (activity level)
@@ -200,26 +208,6 @@
     protected abstract SensorTestDetails executeTests();
 
     /**
-     * Guides the operator throughout the process of deactivating features that are known to use
-     * Sensor data.
-     *
-     * @throws InterruptedException
-     */
-    protected void deactivateSensorFeatures() throws InterruptedException {
-        mSensorFeaturesDeactivator.requestDeactivationOfFeatures();
-    }
-
-    /**
-     * Guides the operator throughout the process of restoring the state of features that are known
-     * to use Sensor data, to their original state.
-     *
-     * @throws InterruptedException
-     */
-    protected void restoreSensorFeatures() throws InterruptedException {
-        mSensorFeaturesDeactivator.requestToRestoreFeatures();
-    }
-
-    /**
      * Guides the operator throughout the process of setting the Screen Off timeout to a required
      * value.
      *
@@ -545,11 +533,15 @@
         }
 
         @Override
-        public void launchAndWaitForSubactivity(String action) throws InterruptedException {
+        public void launchAndWaitForSubactivity(String action) {
             mCountDownLatch = new CountDownLatch(1);
             Intent intent = new Intent(action);
             startActivityForResult(intent, SENSOR_FEATURES_DEACTIVATOR_RESULT);
-            mCountDownLatch.await();
+            try {
+                mCountDownLatch.await();
+            } catch (InterruptedException e) {
+                Log.e(LOG_TAG, "Error waiting for sub-activity.", e);
+            }
         }
 
         public void onActivityResult() {
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/SensorCtsVerifierTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/SensorCtsVerifierTestActivity.java
index 5987036..148e457 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/SensorCtsVerifierTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/SensorCtsVerifierTestActivity.java
@@ -19,7 +19,7 @@
 
 import com.android.cts.verifier.sensors.reporting.SensorTestDetails;
 
-import android.hardware.cts.helpers.SensorNotSupportedException;
+import android.hardware.cts.helpers.SensorTestStateNotSupportedException;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -114,7 +114,7 @@
             // get the inner exception, because we use reflection APIs to execute the test
             Throwable cause = e.getCause();
             testSummary = cause.getMessage();
-            if (cause instanceof SensorNotSupportedException) {
+            if (cause instanceof SensorTestStateNotSupportedException) {
                 testResultCode = SensorTestDetails.ResultCode.SKIPPED;
                 ++mTestSkippedCounter;
             } else {
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorFeaturesDeactivator.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorFeaturesDeactivator.java
index 48b069d..ed29e4c 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorFeaturesDeactivator.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorFeaturesDeactivator.java
@@ -52,7 +52,7 @@
         ContentResolver getContentResolver();
         void logInstructions(int instructionsResId, Object ... params);
         void waitForUser();
-        void launchAndWaitForSubactivity(String action) throws InterruptedException;
+        void launchAndWaitForSubactivity(String action);
         String getString(int resId, Object ... params);
     }
 
@@ -65,7 +65,7 @@
         mLocationMode = new LocationModeSettingContainer();
     }
 
-    public synchronized void requestDeactivationOfFeatures() throws InterruptedException {
+    public synchronized void requestDeactivationOfFeatures() {
         captureInitialState();
 
         mAirplaneMode.requestToSetMode(true);
@@ -80,7 +80,7 @@
         mActivityHandler.waitForUser();
     }
 
-    public synchronized void requestToRestoreFeatures() throws InterruptedException {
+    public synchronized void requestToRestoreFeatures() {
         if (!isInitialStateCaptured()) {
             return;
         }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorSettingContainer.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorSettingContainer.java
index aa73d6c..00a96b5 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorSettingContainer.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorSettingContainer.java
@@ -47,7 +47,7 @@
         mCapturedModeOn = getCurrentSettingMode();
     }
 
-    public synchronized void requestToSetMode(boolean modeOn) throws InterruptedException {
+    public synchronized void requestToSetMode(boolean modeOn) {
         String settingName = mActivityHandler.getString(mSettingNameResId);
         if (getCurrentSettingMode() == modeOn) {
             mActivityHandler.logInstructions(R.string.snsr_setting_mode_set, settingName, modeOn);
@@ -66,7 +66,7 @@
         }
     }
 
-    public synchronized void requestToResetMode() throws InterruptedException {
+    public synchronized void requestToResetMode() {
         try {
             requestToSetMode(mCapturedModeOn);
         } catch (IllegalStateException e) {
diff --git a/tests/tests/hardware/src/android/hardware/cts/SensorTestCase.java b/tests/tests/hardware/src/android/hardware/cts/SensorTestCase.java
index aee00ed..54c7ffe 100644
--- a/tests/tests/hardware/src/android/hardware/cts/SensorTestCase.java
+++ b/tests/tests/hardware/src/android/hardware/cts/SensorTestCase.java
@@ -22,8 +22,8 @@
 
 import android.app.Instrumentation;
 import android.cts.util.DeviceReportLog;
-import android.hardware.cts.helpers.SensorNotSupportedException;
 import android.hardware.cts.helpers.SensorStats;
+import android.hardware.cts.helpers.SensorTestStateNotSupportedException;
 import android.test.AndroidTestCase;
 import android.util.Log;
 
@@ -40,8 +40,8 @@
     public void runTest() throws Throwable {
         try {
             super.runTest();
-        } catch (SensorNotSupportedException e) {
-            // the sensor is not supported/available in the device, log a warning and skip the test
+        } catch (SensorTestStateNotSupportedException e) {
+            // the sensor state is not supported in the device, log a warning and skip the test
             Log.w(LOG_TAG, e.getMessage());
         }
     }
diff --git a/tests/tests/hardware/src/android/hardware/cts/helpers/SensorNotSupportedException.java b/tests/tests/hardware/src/android/hardware/cts/helpers/SensorNotSupportedException.java
index 4c413d7..53e4cae 100644
--- a/tests/tests/hardware/src/android/hardware/cts/helpers/SensorNotSupportedException.java
+++ b/tests/tests/hardware/src/android/hardware/cts/helpers/SensorNotSupportedException.java
@@ -18,14 +18,11 @@
 
 /**
  * Exception that indicates that a given sensor is not supported in the device.
- * This exception can be caught in tests to safely ignore test cases that are not necessary in a
- * given environment.
  */
-public class SensorNotSupportedException extends RuntimeException {
+public class SensorNotSupportedException extends SensorTestStateNotSupportedException {
     public SensorNotSupportedException(int sensorType) {
-        super(String.format(
-                "Sensor '%s' of type %d is not supported.",
+        super("Sensor '%s' of type %d is not supported.",
                 SensorTestInformation.getSensorName(sensorType),
-                sensorType));
+                sensorType);
     }
 }
diff --git a/tests/tests/hardware/src/android/hardware/cts/helpers/SensorTestStateNotSupportedException.java b/tests/tests/hardware/src/android/hardware/cts/helpers/SensorTestStateNotSupportedException.java
new file mode 100644
index 0000000..435632d
--- /dev/null
+++ b/tests/tests/hardware/src/android/hardware/cts/helpers/SensorTestStateNotSupportedException.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2014 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 android.hardware.cts.helpers;
+
+/**
+ * Exception that indicates that a given state in sensor tests is not supported in the device.
+ * This exception can be caught in tests to safely ignore test cases that are not necessary in a
+ * given environment.
+ */
+public class SensorTestStateNotSupportedException extends RuntimeException {
+    public SensorTestStateNotSupportedException(String format, Object ... params) {
+        super(String.format(format, params));
+    }
+
+    public SensorTestStateNotSupportedException(String message) {
+        super(message);
+    }
+}