1 /* 2 * Copyright (C) 2014 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 android.hardware.cts.helpers.sensorverification; 18 19 import junit.framework.TestCase; 20 import android.content.Context; 21 import android.hardware.Sensor; 22 import android.hardware.SensorManager; 23 import android.hardware.cts.helpers.SensorStats; 24 import android.hardware.cts.helpers.TestSensorEnvironment; 25 import android.hardware.cts.helpers.TestSensorEvent; 26 import android.test.AndroidTestCase; 27 28 import java.util.ArrayList; 29 import java.util.Collection; 30 31 /** 32 * Tests for {@link EventBasicVerification}. 33 */ 34 public class EventBasicVerificationTest extends AndroidTestCase { 35 36 /** 37 * Test {@link EventBasicVerification#verify(TestSensorEnvironment, SensorStats)}. 38 */ 39 public void testVerify() { 40 41 /* Sensor contents is not used in this verification, use Object as mock */ 42 SensorManager mgr = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); 43 44 Sensor sensor1 = null; 45 46 // accelerometer is the most likely sensor to exist 47 Sensor sensor2 = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 48 49 SensorStats stats; 50 51 EventBasicVerification verification; 52 53 // case 1 54 verification = getVerification(10, sensor1, sensor1, new float[20][3]); 55 stats = new SensorStats(); 56 57 verification.verify(stats); 58 assertEquals(true, stats.getValue(EventBasicVerification.PASSED_KEY)); 59 assertEquals(20, (long) stats.getValue(SensorStats.EVENT_COUNT_KEY)); 60 assertEquals(false, stats.getValue(SensorStats.WRONG_SENSOR_KEY)); 61 62 // case 2 63 verification = getVerification(10, sensor1, sensor1, new float[5][3]); 64 stats = new SensorStats(); 65 66 try { 67 verification.verify(stats); 68 throw new Error("Expect an AssertionError due to insufficient samples"); 69 } catch (AssertionError e) { 70 //Expected 71 } 72 assertEquals(false, stats.getValue(EventBasicVerification.PASSED_KEY)); 73 assertEquals(5, (long) stats.getValue(SensorStats.EVENT_COUNT_KEY)); 74 assertEquals(false, stats.getValue(SensorStats.WRONG_SENSOR_KEY)); 75 76 // case 3 77 if (sensor1 != sensor2) { 78 // if we cannot even get a second sensor then do not bother this test. 79 verification = getVerification(10, sensor1, sensor2, new float[15][3]); 80 stats = new SensorStats(); 81 82 try { 83 verification.verify(stats); 84 throw new Error("Expect an AssertionError due to wrong sensor event"); 85 } catch (AssertionError e) { 86 //Expected 87 } 88 assertEquals(false, stats.getValue(EventBasicVerification.PASSED_KEY)); 89 // zero here because wrong sensor is used. 90 assertEquals(0, (long) stats.getValue(SensorStats.EVENT_COUNT_KEY)); 91 assertEquals(true, stats.getValue(SensorStats.WRONG_SENSOR_KEY)); 92 } 93 } 94 95 private static EventBasicVerification getVerification( 96 int expectedMinNumEvent, Sensor sensor, Sensor eventSensor, float[] ... values) { 97 98 Collection<TestSensorEvent> events = new ArrayList<>(values.length); 99 for (float[] value : values) { 100 events.add(new TestSensorEvent(eventSensor, 0, 0, value)); 101 } 102 EventBasicVerification verification = 103 new EventBasicVerification(expectedMinNumEvent, sensor); 104 verification.addSensorEvents(events); 105 return verification; 106 } 107 } 108