Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 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 com.android.cts.verifier.sensors; |
| 18 | |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 19 | import java.util.Timer; |
| 20 | import java.util.TimerTask; |
| 21 | import java.util.concurrent.CountDownLatch; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 24 | import com.android.cts.verifier.R; |
destradaa | a0b3bbc | 2014-09-03 14:08:00 -0700 | [diff] [blame] | 25 | import com.android.cts.verifier.sensors.base.SensorCtsVerifierTestActivity; |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 26 | import com.android.cts.verifier.sensors.helpers.SensorTestScreenManipulator; |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 27 | |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 28 | import android.app.AlarmManager; |
| 29 | import android.app.PendingIntent; |
| 30 | import android.content.BroadcastReceiver; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 31 | import android.content.Context; |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 34 | import android.hardware.Sensor; |
| 35 | import android.hardware.SensorManager; |
| 36 | import android.hardware.TriggerEvent; |
| 37 | import android.hardware.TriggerEventListener; |
destradaa | 9155399 | 2014-09-19 17:50:15 -0700 | [diff] [blame] | 38 | import android.hardware.cts.helpers.SensorNotSupportedException; |
destradaa | 7bed810 | 2014-09-24 13:18:16 -0700 | [diff] [blame] | 39 | import android.hardware.cts.helpers.TestSensorEnvironment; |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 40 | import android.hardware.cts.helpers.SuspendStateMonitor; |
| 41 | import android.os.Handler; |
| 42 | import android.os.Looper; |
| 43 | import android.os.PowerManager; |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 44 | import android.os.SystemClock; |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 45 | import android.support.v4.content.LocalBroadcastManager; |
| 46 | import android.util.Log; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 47 | |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 48 | import junit.framework.Assert; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 49 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Test cases for Significant Motion sensor. |
| 52 | * They use walking motion to change the location and trigger Significant Motion. |
| 53 | */ |
destradaa | a0b3bbc | 2014-09-03 14:08:00 -0700 | [diff] [blame] | 54 | public class SignificantMotionTestActivity extends SensorCtsVerifierTestActivity { |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 55 | public SignificantMotionTestActivity() { |
| 56 | super(SignificantMotionTestActivity.class); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 57 | } |
| 58 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 59 | // acceptable time difference between event time and system time |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 60 | private static final long MAX_ACCEPTABLE_EVENT_TIME_DELAY_NANOS = |
| 61 | TimeUnit.MILLISECONDS.toNanos(500); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 62 | |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 63 | // acceptable time difference between event time and AP wake up time. |
| 64 | private static final long MAX_ACCEPTABLE_DELAY_EVENT_AP_WAKE_UP_NS = |
| 65 | TimeUnit.MILLISECONDS.toNanos(2000); |
| 66 | |
| 67 | // time to wait for SMD after the device has gone into suspend. Even after |
| 68 | // 45 secs if SMD does not trigger, the test will fail. |
| 69 | private static final long ALARM_WAKE_TIME_DELAY_MS = TimeUnit.SECONDS.toMillis(45); |
| 70 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 71 | // time for the test to wait for a trigger |
| 72 | private static final int TRIGGER_MAX_DELAY_SECONDS = 30; |
| 73 | private static final int VIBRATE_DURATION_MILLIS = 10000; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 74 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 75 | private static final int EVENT_VALUES_LENGTH = 1; |
| 76 | private static final float EXPECTED_EVENT_VALUE = 1.0f; |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 77 | private static String ACTION_ALARM = "SignificantMotionTestActivity.ACTION_ALARM"; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 78 | |
| 79 | private SensorManager mSensorManager; |
| 80 | private Sensor mSensorSignificantMotion; |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 81 | private TriggerVerifier mVerifier; |
| 82 | private SensorTestScreenManipulator mScreenManipulator; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 83 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Test cases. |
| 86 | */ |
destradaa | fd031b9 | 2014-10-10 10:45:51 -0700 | [diff] [blame] | 87 | @SuppressWarnings("unused") |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 88 | public String testTrigger() throws Throwable { |
| 89 | return runTest( |
| 90 | R.string.snsr_significant_motion_test_trigger, |
| 91 | true /* isMotionExpected */, |
| 92 | false /* cancelEventNotification */, |
| 93 | false /* vibrate */); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 94 | } |
| 95 | |
destradaa | fd031b9 | 2014-10-10 10:45:51 -0700 | [diff] [blame] | 96 | @SuppressWarnings("unused") |
destradaa | 16203b4 | 2014-09-29 13:26:51 -0700 | [diff] [blame] | 97 | public String testNotTriggerAfterCancel() throws Throwable { |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 98 | return runTest( |
| 99 | R.string.snsr_significant_motion_test_cancel, |
| 100 | false /* isMotionExpected */, |
| 101 | true /* cancelEventNotification */, |
| 102 | false /* vibrate */); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 106 | * Verifies that Significant Motion is not trigger by the vibrator motion. |
| 107 | */ |
destradaa | fd031b9 | 2014-10-10 10:45:51 -0700 | [diff] [blame] | 108 | @SuppressWarnings("unused") |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 109 | public String testVibratorDoesNotTrigger() throws Throwable { |
| 110 | return runTest( |
| 111 | R.string.snsr_significant_motion_test_vibration, |
| 112 | false /* isMotionExpected */, |
| 113 | false /* cancelEventNotification */, |
| 114 | true /* vibrate */); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Verifies that the natural motion of keeping the device in hand does not change the location. |
| 119 | * It ensures that Significant Motion will not trigger in that scenario. |
| 120 | */ |
destradaa | fd031b9 | 2014-10-10 10:45:51 -0700 | [diff] [blame] | 121 | @SuppressWarnings("unused") |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 122 | public String testInHandDoesNotTrigger() throws Throwable { |
| 123 | return runTest( |
| 124 | R.string.snsr_significant_motion_test_in_hand, |
| 125 | false /* isMotionExpected */, |
| 126 | false /* cancelEventNotification */, |
| 127 | false /* vibrate */); |
| 128 | } |
| 129 | |
destradaa | fd031b9 | 2014-10-10 10:45:51 -0700 | [diff] [blame] | 130 | @SuppressWarnings("unused") |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 131 | public String testSittingDoesNotTrigger() throws Throwable { |
| 132 | return runTest( |
| 133 | R.string.snsr_significant_motion_test_sitting, |
| 134 | false /* isMotionExpected */, |
| 135 | false /* cancelEventNotification */, |
| 136 | false /* vibrate */); |
| 137 | } |
| 138 | |
destradaa | fd031b9 | 2014-10-10 10:45:51 -0700 | [diff] [blame] | 139 | @SuppressWarnings("unused") |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 140 | public String testTriggerDeactivation() throws Throwable { |
destradaa | 16203b4 | 2014-09-29 13:26:51 -0700 | [diff] [blame] | 141 | SensorTestLogger logger = getTestLogger(); |
| 142 | logger.logInstructions(R.string.snsr_significant_motion_test_deactivation); |
| 143 | waitForUserToBegin(); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 144 | |
| 145 | TriggerVerifier verifier = new TriggerVerifier(); |
| 146 | mSensorManager.requestTriggerSensor(verifier, mSensorSignificantMotion); |
destradaa | 16203b4 | 2014-09-29 13:26:51 -0700 | [diff] [blame] | 147 | logger.logWaitForSound(); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 148 | |
| 149 | // wait for the first event to trigger |
| 150 | verifier.verifyEventTriggered(); |
| 151 | |
| 152 | // wait for a second event not to trigger |
| 153 | String result = verifier.verifyEventNotTriggered(); |
| 154 | playSound(); |
| 155 | return result; |
| 156 | } |
| 157 | |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 158 | public static class AlarmReceiver extends BroadcastReceiver { |
| 159 | @Override |
| 160 | public void onReceive(Context context, Intent intent) { |
| 161 | Intent alarm_intent = new Intent(context, SignificantMotionTestActivity.class); |
| 162 | alarm_intent.setAction(SignificantMotionTestActivity.ACTION_ALARM); |
| 163 | LocalBroadcastManager.getInstance(context).sendBroadcastSync(alarm_intent); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | public BroadcastReceiver myBroadCastReceiver = new BroadcastReceiver() { |
| 168 | @Override |
| 169 | public void onReceive(Context context, Intent intent) { |
| 170 | mVerifier.releaseLatch(); |
| 171 | mScreenManipulator.turnScreenOn(); |
| 172 | try { |
| 173 | playSound(); |
| 174 | } catch (InterruptedException e) { |
| 175 | // Ignore ... |
| 176 | } |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | @SuppressWarnings("unused") |
| 181 | public String testAPWakeUpOnSMDTrigger() throws Throwable { |
| 182 | SensorTestLogger logger = getTestLogger(); |
| 183 | logger.logInstructions(R.string.snsr_significant_motion_ap_suspend); |
| 184 | waitForUserToBegin(); |
| 185 | mVerifier = new TriggerVerifier(); |
| 186 | mSensorManager.requestTriggerSensor(mVerifier, mSensorSignificantMotion); |
| 187 | long testStartTimeNs = SystemClock.elapsedRealtimeNanos(); |
| 188 | Handler handler = new Handler(Looper.getMainLooper()); |
| 189 | SuspendStateMonitor suspendStateMonitor = new SuspendStateMonitor(); |
| 190 | |
| 191 | Intent intent = new Intent(this, AlarmReceiver.class); |
| 192 | PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); |
| 193 | |
| 194 | AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); |
| 195 | am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
| 196 | SystemClock.elapsedRealtime() + ALARM_WAKE_TIME_DELAY_MS, pendingIntent); |
| 197 | try { |
| 198 | // Wait for the first event to trigger. Device is expected to go into suspend here. |
| 199 | mVerifier.verifyEventTriggered(); |
| 200 | long eventTimeStampNs = mVerifier.getTimeStampForTriggerEvent(); |
| 201 | long endTimeNs = SystemClock.elapsedRealtimeNanos(); |
| 202 | long lastWakeupTimeNs = TimeUnit.MILLISECONDS.toNanos( |
| 203 | suspendStateMonitor.getLastWakeUpTime()); |
| 204 | Assert.assertTrue(getString(R.string.snsr_device_did_not_go_into_suspend), |
| 205 | testStartTimeNs < lastWakeupTimeNs && lastWakeupTimeNs < endTimeNs); |
| 206 | long timestampDelta = Math.abs(lastWakeupTimeNs - eventTimeStampNs); |
| 207 | Assert.assertTrue( |
| 208 | String.format(getString(R.string.snsr_device_did_not_wake_up_at_trigger), |
| 209 | TimeUnit.NANOSECONDS.toMillis(lastWakeupTimeNs), |
| 210 | TimeUnit.NANOSECONDS.toMillis(eventTimeStampNs)), |
| 211 | timestampDelta < MAX_ACCEPTABLE_DELAY_EVENT_AP_WAKE_UP_NS); |
| 212 | } finally { |
| 213 | am.cancel(pendingIntent); |
| 214 | suspendStateMonitor.cancel(); |
| 215 | mScreenManipulator.turnScreenOn(); |
| 216 | playSound(); |
| 217 | } |
| 218 | return null; |
| 219 | } |
| 220 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 221 | /** |
| 222 | * @param instructionsResId Instruction to be shown to testers |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 223 | * @param isMotionExpected Should the device detect significant motion event |
| 224 | * for this test? |
| 225 | * @param cancelEventNotification If TRUE, motion notifications will be |
| 226 | * requested first and request will be cancelled |
| 227 | * @param vibrate If TRUE, vibration will be concurrent with the test |
| 228 | * @throws Throwable |
| 229 | */ |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 230 | private String runTest( |
| 231 | int instructionsResId, |
| 232 | boolean isMotionExpected, |
| 233 | boolean cancelEventNotification, |
| 234 | boolean vibrate) throws Throwable { |
destradaa | 16203b4 | 2014-09-29 13:26:51 -0700 | [diff] [blame] | 235 | SensorTestLogger logger = getTestLogger(); |
| 236 | logger.logInstructions(instructionsResId); |
| 237 | waitForUserToBegin(); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 238 | |
| 239 | if (vibrate) { |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 240 | vibrate(VIBRATE_DURATION_MILLIS); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 241 | } |
| 242 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 243 | TriggerVerifier verifier = new TriggerVerifier(); |
destradaa | 9155399 | 2014-09-19 17:50:15 -0700 | [diff] [blame] | 244 | boolean success = mSensorManager.requestTriggerSensor(verifier, mSensorSignificantMotion); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 245 | Assert.assertTrue( |
destradaa | 9155399 | 2014-09-19 17:50:15 -0700 | [diff] [blame] | 246 | getString(R.string.snsr_significant_motion_registration, success), |
| 247 | success); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 248 | if (cancelEventNotification) { |
| 249 | Assert.assertTrue( |
| 250 | getString(R.string.snsr_significant_motion_cancelation), |
| 251 | mSensorManager.cancelTriggerSensor(verifier, mSensorSignificantMotion)); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 252 | } |
destradaa | 16203b4 | 2014-09-29 13:26:51 -0700 | [diff] [blame] | 253 | logger.logWaitForSound(); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 254 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 255 | String result; |
| 256 | try { |
| 257 | if (isMotionExpected) { |
| 258 | result = verifier.verifyEventTriggered(); |
| 259 | } else { |
| 260 | result = verifier.verifyEventNotTriggered(); |
| 261 | } |
| 262 | } finally { |
| 263 | mSensorManager.cancelTriggerSensor(verifier, mSensorSignificantMotion); |
destradaa | fd031b9 | 2014-10-10 10:45:51 -0700 | [diff] [blame] | 264 | playSound(); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 265 | } |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 266 | return result; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
destradaa | 9155399 | 2014-09-19 17:50:15 -0700 | [diff] [blame] | 270 | protected void activitySetUp() { |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 271 | mSensorManager = (SensorManager) getApplicationContext() |
| 272 | .getSystemService(Context.SENSOR_SERVICE); |
destradaa | 9155399 | 2014-09-19 17:50:15 -0700 | [diff] [blame] | 273 | mSensorSignificantMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION); |
| 274 | if (mSensorSignificantMotion == null) { |
| 275 | throw new SensorNotSupportedException(Sensor.TYPE_SIGNIFICANT_MOTION); |
| 276 | } |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 277 | |
| 278 | mScreenManipulator = new SensorTestScreenManipulator(this); |
| 279 | try { |
| 280 | mScreenManipulator.initialize(this); |
| 281 | } catch (InterruptedException e) { |
| 282 | } |
| 283 | PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE); |
| 284 | LocalBroadcastManager.getInstance(this).registerReceiver(myBroadCastReceiver, |
| 285 | new IntentFilter(ACTION_ALARM)); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | protected void activityCleanUp() { |
Aravind Akella | 80ae6ff | 2015-09-14 16:07:42 -0700 | [diff] [blame] | 290 | if (mScreenManipulator != null) { |
| 291 | mScreenManipulator.turnScreenOff(); |
| 292 | } |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 293 | LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadCastReceiver); |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | protected void onDestroy() { |
| 298 | super.onDestroy(); |
| 299 | mScreenManipulator.close(); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 300 | } |
| 301 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 302 | /** |
| 303 | * Helper Trigger listener for testing. |
| 304 | * It cannot be reused. |
| 305 | */ |
| 306 | private class TriggerVerifier extends TriggerEventListener { |
Aravind Akella | 8f2596c | 2015-08-27 12:07:52 -0700 | [diff] [blame] | 307 | private volatile CountDownLatch mCountDownLatch; |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 308 | private volatile TriggerEventRegistry mEventRegistry; |
Aravind Akella | 8f2596c | 2015-08-27 12:07:52 -0700 | [diff] [blame] | 309 | private volatile long mTimestampForTriggeredEvent = 0; |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 310 | |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 311 | // TODO: refactor out if needed |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 312 | private class TriggerEventRegistry { |
| 313 | public final TriggerEvent triggerEvent; |
| 314 | public final long realtimeTimestampNanos; |
| 315 | |
| 316 | public TriggerEventRegistry(TriggerEvent event, long realtimeTimestampNanos) { |
| 317 | this.triggerEvent = event; |
| 318 | this.realtimeTimestampNanos = realtimeTimestampNanos; |
| 319 | } |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 320 | } |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 321 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 322 | public void onTrigger(TriggerEvent event) { |
| 323 | long elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos(); |
| 324 | mEventRegistry = new TriggerEventRegistry(event, elapsedRealtimeNanos); |
| 325 | mCountDownLatch.countDown(); |
| 326 | } |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 327 | |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 328 | public void releaseLatch() { |
| 329 | if (mCountDownLatch != null) { |
| 330 | mCountDownLatch.countDown(); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | public long getTimeStampForTriggerEvent() { |
Aravind Akella | 8f2596c | 2015-08-27 12:07:52 -0700 | [diff] [blame] | 335 | return mTimestampForTriggeredEvent; |
Aravind Akella | dbc95c5 | 2015-06-09 10:22:16 -0700 | [diff] [blame] | 336 | } |
| 337 | |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 338 | public String verifyEventTriggered() throws Throwable { |
| 339 | TriggerEventRegistry registry = awaitForEvent(); |
| 340 | |
| 341 | // verify an event arrived, and it is indeed a Significant Motion event |
| 342 | TriggerEvent event = registry.triggerEvent; |
| 343 | String eventArrivalMessage = |
destradaa | a771454 | 2014-09-04 16:23:03 -0700 | [diff] [blame] | 344 | getString(R.string.snsr_significant_motion_event_arrival, event != null); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 345 | Assert.assertNotNull(eventArrivalMessage, event); |
| 346 | |
| 347 | int eventType = event.sensor.getType(); |
| 348 | String eventTypeMessage = getString( |
| 349 | R.string.snsr_significant_motion_event_type, |
| 350 | Sensor.TYPE_SIGNIFICANT_MOTION, |
| 351 | eventType); |
| 352 | Assert.assertEquals(eventTypeMessage, Sensor.TYPE_SIGNIFICANT_MOTION, eventType); |
| 353 | |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 354 | String sensorName = event.sensor.getName(); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 355 | int valuesLength = event.values.length; |
| 356 | String valuesLengthMessage = getString( |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 357 | R.string.snsr_event_length, |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 358 | EVENT_VALUES_LENGTH, |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 359 | valuesLength, |
| 360 | sensorName); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 361 | Assert.assertEquals(valuesLengthMessage, EVENT_VALUES_LENGTH, valuesLength); |
| 362 | |
| 363 | float value = event.values[0]; |
| 364 | String valuesMessage = getString( |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 365 | R.string.snsr_event_value, |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 366 | EXPECTED_EVENT_VALUE, |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 367 | value, |
| 368 | sensorName); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 369 | Assert.assertEquals(valuesMessage, EXPECTED_EVENT_VALUE, value); |
| 370 | |
destradaa | 7bed810 | 2014-09-24 13:18:16 -0700 | [diff] [blame] | 371 | long deltaThreshold = MAX_ACCEPTABLE_EVENT_TIME_DELAY_NANOS |
| 372 | + TestSensorEnvironment.getSensorMaxDetectionLatencyNs(event.sensor); |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 373 | return assertTimestampSynchronization( |
| 374 | event.timestamp, |
| 375 | registry.realtimeTimestampNanos, |
destradaa | 7bed810 | 2014-09-24 13:18:16 -0700 | [diff] [blame] | 376 | deltaThreshold, |
destradaa | 139500e | 2014-08-22 16:01:52 -0700 | [diff] [blame] | 377 | sensorName); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | public String verifyEventNotTriggered() throws Throwable { |
| 381 | TriggerEventRegistry registry = awaitForEvent(); |
| 382 | |
| 383 | TriggerEvent event = registry.triggerEvent; |
| 384 | String eventMessage = |
destradaa | a771454 | 2014-09-04 16:23:03 -0700 | [diff] [blame] | 385 | getString(R.string.snsr_significant_motion_event_unexpected, event != null); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 386 | Assert.assertNull(eventMessage, event); |
| 387 | return eventMessage; |
| 388 | } |
| 389 | |
| 390 | private TriggerEventRegistry awaitForEvent() throws InterruptedException { |
Aravind Akella | 8f2596c | 2015-08-27 12:07:52 -0700 | [diff] [blame] | 391 | mCountDownLatch = new CountDownLatch(1); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 392 | mCountDownLatch.await(TRIGGER_MAX_DELAY_SECONDS, TimeUnit.SECONDS); |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 393 | TriggerEventRegistry registry = mEventRegistry; |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 394 | |
Aravind Akella | 8f2596c | 2015-08-27 12:07:52 -0700 | [diff] [blame] | 395 | // Save the last timestamp when the event triggered. |
| 396 | if (mEventRegistry != null && mEventRegistry.triggerEvent != null) { |
| 397 | mTimestampForTriggeredEvent = mEventRegistry.triggerEvent.timestamp; |
| 398 | } |
| 399 | |
| 400 | mEventRegistry = null; |
destradaa | a07a7a2 | 2014-08-21 17:09:03 -0700 | [diff] [blame] | 401 | playSound(); |
| 402 | return registry != null ? registry : new TriggerEventRegistry(null, 0); |
Jim Steele | c57c843 | 2014-05-24 09:53:32 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |