1 /*
2  * Copyright (C) 2016 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 import android.os.SystemClock;
25 
26 import java.util.ArrayList;
27 import java.util.Collection;
28 
29 import android.util.Log;
30 
31 /**
32  * Tests for {@link TimestampClockSourceVerification}.
33  */
34 public class TimestampClockSourceVerificationTest extends TestCase {
35     private final String TAG = "TimestampClockSourceVerificationTest";
36 
37     private final int MIN_DELTA_BETWEEN_CLOCKS_MS = 2000;
38     private boolean mAdjustUptime = false;
39 
getValidTimestamp()40     private long getValidTimestamp() {
41         return SystemClock.elapsedRealtimeNanos();
42     }
43 
getInvalidTimestamp()44     private long getInvalidTimestamp() {
45         long ms = SystemClock.uptimeMillis();
46         if (mAdjustUptime == true) {
47             ms -= MIN_DELTA_BETWEEN_CLOCKS_MS;
48         }
49         return (ms * 1000000);
50     }
51 
verifyClockDelta()52     private void verifyClockDelta() throws Throwable {
53         long uptimeMs = SystemClock.uptimeMillis();
54         long realtimeNs = SystemClock.elapsedRealtimeNanos();
55         long deltaMs = (realtimeNs/1000000 - uptimeMs);
56         if (deltaMs < MIN_DELTA_BETWEEN_CLOCKS_MS) {
57             Log.i(TAG, "Device has not slept, will use different clock source for test purposes");
58             mAdjustUptime = true;
59         } else {
60             mAdjustUptime = false;
61             Log.i(TAG, "CLOCK_MONOTONIC="+uptimeMs*1000000+", CLOCK_BOOTTIME="+realtimeNs+", delta=" + deltaMs + " mS");
62         }
63     }
64 
65 
66     /**
67      * Test that the verification passes when there are not missing events.
68      */
testVerify_pass()69     public void testVerify_pass() throws Throwable {
70         try {
71             verifyClockDelta();
72             long ts = getValidTimestamp();
73             long[] timestamps = {ts-4000000, ts-3000000, ts-2000000, ts-1000000, ts};
74             // Timestamps in ns, expected in us
75             runVerification(MIN_DELTA_BETWEEN_CLOCKS_MS*1000, timestamps, true, new int[]{});
76         } finally {
77         }
78     }
79 
80     /**
81      * Test that the verification fails when there are not missing events,
82      * but wrong clock source is used.
83      */
testVerify_fail()84     public void testVerify_fail() throws Throwable {
85         try {
86             verifyClockDelta();
87             long ts = getInvalidTimestamp();
88             long[] timestamps = {ts-4000000, ts-3000000, ts-2000000, ts-1000000, ts};
89             // Timestamps in ns, expected in us
90             runVerification(MIN_DELTA_BETWEEN_CLOCKS_MS*1000, timestamps, false, new int[]{0,1,2,3,4});
91         } finally {
92         }
93     }
94 
95     /**
96      * Test that the verification passes when there are not missing events but some jitter.
97      */
testVerify_jitter_pass()98     public void testVerify_jitter_pass() throws Throwable {
99         try {
100             verifyClockDelta();
101             long ts = getValidTimestamp();
102             long[] timestamps = {ts-3900000, ts-2950000, ts-2050000, ts-1000000, ts-50000};
103             // Timestamps in ns, expected in us
104             runVerification(MIN_DELTA_BETWEEN_CLOCKS_MS*1000, timestamps, true, new int[]{});
105         } finally {
106         }
107     }
108 
109     /**
110      * Test that the verification passes when there are not missing events but some jitter.
111      */
testVerify_jitter_fail()112     public void testVerify_jitter_fail() throws Throwable {
113         try {
114             verifyClockDelta();
115             long ts = getInvalidTimestamp();
116             long[] timestamps = {ts-3900000, ts-2950000, ts-2050000, ts-1000000, ts-50000};
117             // Timestamps in ns, expected in us
118             runVerification(MIN_DELTA_BETWEEN_CLOCKS_MS*1000, timestamps, false, new int[]{0,1,2,3,4});
119         } finally {
120         }
121     }
122 
123     /**
124      * Test that the verification does not fail when there are missing events.
125      */
testVerify_missing_events_pass()126     public void testVerify_missing_events_pass() throws Throwable {
127         try {
128             verifyClockDelta();
129             long ts = getValidTimestamp();
130             long[] timestamps = {ts-4000000, ts-3000000, ts-1000000, ts};
131             // Timestamps in ns, expected in us
132             runVerification(MIN_DELTA_BETWEEN_CLOCKS_MS*1000, timestamps, true, new int[]{});
133         } finally {
134         }
135     }
136 
137     /**
138      * Test that the verification fails when there are missing events, but wrong
139      * timestamp
140      */
testVerify_missing_events_fail()141     public void testVerify_missing_events_fail() throws Throwable {
142         try {
143             verifyClockDelta();
144             long ts = getInvalidTimestamp();
145             long[] timestamps = {ts-4000000, ts-3000000, ts-1000000, ts};
146             // Timestamps in ns, expected in us
147             runVerification(MIN_DELTA_BETWEEN_CLOCKS_MS*1000, timestamps, false, new int[]{0,1,2,3});
148         } finally {
149         }
150     }
151 
152     /**
153      * Test that the verification fails when there are no results.
154      */
testVerify_no_events_fail()155     public void testVerify_no_events_fail() throws Throwable {
156         try {
157             verifyClockDelta();
158             // Timestamps in ns, expected in us
159             runVerification(MIN_DELTA_BETWEEN_CLOCKS_MS*1000, new long[]{}, false, new int[]{});
160         } finally {
161         }
162     }
163 
runVerification(int expectedUs, long[] timestamps, boolean pass, int[] indices)164     private void runVerification(int expectedUs, long[] timestamps, boolean pass,
165             int[] indices) {
166         SensorStats stats = new SensorStats();
167         ISensorVerification verification = getVerification(expectedUs, timestamps);
168         TestSensorEnvironment environment = new TestSensorEnvironment(null, null, false, 0, 0);
169         if (pass) {
170             verification.verify(environment, stats);
171         } else {
172             boolean failed = false;
173             try {
174                 verification.verify(environment, stats);
175             } catch (AssertionError e) {
176                 // Expected;
177                 failed = true;
178             }
179             assertTrue("Expected an AssertionError", failed);
180         }
181 
182         assertEquals(pass, stats.getValue(TimestampClockSourceVerification.PASSED_KEY));
183         assertEquals(indices.length, stats.getValue(SensorStats.EVENT_TIME_WRONG_CLOCKSOURCE_COUNT_KEY));
184         if (0 != (Integer) stats.getValue(SensorStats.EVENT_TIME_WRONG_CLOCKSOURCE_COUNT_KEY)) {
185             assertNotNull(stats.getValue(SensorStats.EVENT_TIME_WRONG_CLOCKSOURCE_POSITIONS_KEY));
186         }
187         try {
188             int[] actualIndices = (int[]) stats.getValue(SensorStats.EVENT_TIME_WRONG_CLOCKSOURCE_POSITIONS_KEY);
189             assertEquals(indices.length, actualIndices.length);
190 
191             for (int i = 0; i < indices.length; i++) {
192                 assertEquals(indices[i], actualIndices[i]);
193             }
194         } catch (Exception t) {
195         }
196     }
197 
getVerification(int expectedUs, long ... timestamps)198     private static TimestampClockSourceVerification getVerification(int expectedUs, long ... timestamps) {
199         Collection<TestSensorEvent> events = new ArrayList<>(timestamps.length);
200         long expectedNs = expectedUs * 1000;
201         long now = SystemClock.elapsedRealtimeNanos();
202         long receiveTime;
203         for (long timestamp : timestamps) {
204             //receiveTime = now - (expectedNs * count);
205             receiveTime = SystemClock.elapsedRealtimeNanos();
206             events.add(new TestSensorEvent(null, timestamp, receiveTime, 0, null));
207         }
208         TimestampClockSourceVerification verification = new TimestampClockSourceVerification(expectedUs);
209         verification.addSensorEvents(events);
210         return verification;
211     }
212 }
213