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 21 import android.hardware.cts.helpers.SensorStats; 22 import android.hardware.cts.helpers.TestSensorEnvironment; 23 import android.hardware.cts.helpers.TestSensorEvent; 24 25 import java.util.ArrayList; 26 import java.util.Collection; 27 28 /** 29 * Tests for {@link MagnitudeVerification}. 30 */ 31 public class MagnitudeVerificationTest extends TestCase { 32 33 /** 34 * Test {@link MagnitudeVerification#verify(TestSensorEnvironment, SensorStats)}. 35 */ 36 public void testVerify() { 37 float[][] values = { 38 {0, 3, 4}, 39 {4, 0, 3}, 40 {3, 4, 0}, 41 {0, 0, 4}, 42 {6, 0, 0}, 43 }; 44 45 runStats(5.0f, 0.1f, values, true, 5.0f); 46 runStats(4.5f, 0.6f, values, true, 5.0f); 47 runStats(5.5f, 0.6f, values, true, 5.0f); 48 runStats(4.5f, 0.1f, values, false, 5.0f); 49 runStats(5.5f, 0.1f, values, false, 5.0f); 50 } 51 52 private void runStats(float expected, float threshold, float[][] values, boolean pass, float magnitude) { 53 SensorStats stats = new SensorStats(); 54 MagnitudeVerification verification = getVerification(expected, threshold, values); 55 if (pass) { 56 verification.verify(stats); 57 } else { 58 try { 59 verification.verify(stats); 60 fail("Expected an AssertionError"); 61 } catch (AssertionError e) { 62 // Expected; 63 } 64 } 65 assertEquals(pass, stats.getValue(MagnitudeVerification.PASSED_KEY)); 66 assertEquals(magnitude, (Float) stats.getValue(SensorStats.MAGNITUDE_KEY), 0.01); 67 } 68 69 private static MagnitudeVerification getVerification(float expected, float threshold, 70 float[] ... values) { 71 Collection<TestSensorEvent> events = new ArrayList<>(values.length); 72 for (float[] value : values) { 73 events.add(new TestSensorEvent(null, 0, 0, value)); 74 } 75 MagnitudeVerification verification = new MagnitudeVerification(expected, threshold); 76 verification.addSensorEvents(events); 77 return verification; 78 } 79 } 80