Home | History | Annotate | Download | only in sensors
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      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 com.android.cts.verifier.sensors;
     18 
     19 import android.hardware.Sensor;
     20 import android.hardware.SensorEvent;
     21 import android.hardware.SensorEventListener;
     22 import android.hardware.SensorManager;
     23 import android.hardware.TriggerEvent;
     24 import android.hardware.TriggerEventListener;
     25 import android.hardware.cts.helpers.SensorNotSupportedException;
     26 import com.android.cts.verifier.R;
     27 import com.android.cts.verifier.sensors.base.SensorCtsVerifierTestActivity;
     28 import junit.framework.Assert;
     29 
     30 import java.util.concurrent.CountDownLatch;
     31 import java.util.concurrent.TimeUnit;
     32 
     33 /**
     34  * Tests about event policy when the observer's UID is idle.
     35  */
     36 public class EventSanitizationTestActivity extends SensorCtsVerifierTestActivity {
     37     public EventSanitizationTestActivity() {
     38         super(EventSanitizationTestActivity.class);
     39     }
     40 
     41     // time for the test to wait for an event
     42     private static final int EVENT_TIMEOUT = 30;
     43 
     44     @Override
     45     protected void activitySetUp() throws Exception {
     46         getTestLogger().logInstructions(R.string.snsr_event_sanitization_test_setup);
     47         waitForUserToBegin();
     48     }
     49 
     50     @Override
     51     protected void activityCleanUp() throws Exception {
     52         getTestLogger().logInstructions(R.string.snsr_event_sanitization_test_cleanup);
     53         waitForUserToContinue();
     54     }
     55 
     56     /**
     57      * Test that no trigger events are triggered while the UID is idle.
     58      */
     59     public String testNoTriggerEventsWhileUidIdle() throws Exception {
     60         // Not significant motion sensor, nothing to do.
     61         final SensorManager sensorManager = getApplicationContext()
     62                 .getSystemService(SensorManager.class);
     63         final Sensor sensor = sensorManager.getDefaultSensor(
     64                 Sensor.TYPE_SIGNIFICANT_MOTION);
     65         if (sensor == null) {
     66             throw new SensorNotSupportedException(Sensor.TYPE_SIGNIFICANT_MOTION);
     67         }
     68 
     69         // Let us begin.
     70         final SensorTestLogger logger = getTestLogger();
     71         logger.logInstructions(R.string.snsr_significant_motion_test_uid_idle);
     72         waitForUserToBegin();
     73 
     74         // Watch for the trigger event.
     75         final CountDownLatch latch = new CountDownLatch(1);
     76         final TriggerEventListener listener = new TriggerEventListener() {
     77             @Override
     78             public void onTrigger(TriggerEvent event) {
     79                 latch.countDown();
     80             }
     81         };
     82         sensorManager.requestTriggerSensor(listener, sensor);
     83 
     84         // Tell the user now when the test completes.
     85         logger.logWaitForSound();
     86 
     87         // We shouldn't be getting an event.
     88         try {
     89             Assert.assertFalse(getString(R.string
     90                     .snsr_significant_motion_test_uid_idle_expectation),
     91                     latch.await(EVENT_TIMEOUT, TimeUnit.SECONDS));
     92         } finally {
     93             sensorManager.cancelTriggerSensor(listener, sensor);
     94             playSound();
     95         }
     96 
     97         return null;
     98     }
     99 
    100     /**
    101      * Test that no on-change events are triggered while the UID is idle.
    102      */
    103     public String testNoOnChangeEventsWhileUidIdle() throws Exception {
    104         // Not significant motion sensor, nothing to do.
    105         final SensorManager sensorManager = getApplicationContext()
    106                 .getSystemService(SensorManager.class);
    107         final Sensor sensor = sensorManager.getDefaultSensor(
    108                 Sensor.TYPE_PROXIMITY);
    109         if (sensor == null) {
    110             throw new SensorNotSupportedException(Sensor.TYPE_PROXIMITY);
    111         }
    112 
    113         // Let us begin.
    114         final SensorTestLogger logger = getTestLogger();
    115         logger.logInstructions(R.string.snsr_proximity_test_uid_idle);
    116         waitForUserToBegin();
    117 
    118         // Watch for the change event.
    119         final CountDownLatch latch = new CountDownLatch(1);
    120         final SensorEventListener listener = new SensorEventListener() {
    121             @Override
    122             public void onSensorChanged(SensorEvent event) {
    123                 latch.countDown();
    124             }
    125 
    126             @Override
    127             public void onAccuracyChanged(Sensor sensor, int accuracy) {
    128                 /* do nothing */
    129             }
    130         };
    131         sensorManager.registerListener(listener, sensor,
    132                 sensor.getMinDelay(), sensor.getMaxDelay());
    133 
    134         // Tell the user now when the test completes.
    135         logger.logWaitForSound();
    136 
    137         // We shouldn't be getting an event.
    138         try {
    139             Assert.assertFalse(getString(R.string
    140                     .snsr_proximity_test_uid_idle_expectation),
    141                     latch.await(EVENT_TIMEOUT, TimeUnit.SECONDS));
    142         } finally {
    143             sensorManager.unregisterListener(listener, sensor);
    144             playSound();
    145         }
    146 
    147         return null;
    148     }
    149 }
    150