blob: 469bdc0918d9cd7715aca8e1065177368193a19a [file] [log] [blame]
Adrian Roosb1f77242017-08-04 12:35:24 +02001/*
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
17package com.android.systemui.util;
18
19import static org.junit.Assert.assertTrue;
20import static org.mockito.ArgumentMatchers.any;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.verifyNoMoreInteractions;
24
25import android.hardware.SensorEventListener;
26import android.hardware.SensorManager;
27import android.support.test.filters.SmallTest;
28import android.testing.AndroidTestingRunner;
29
30import com.android.systemui.SysuiTestCase;
31import com.android.systemui.utils.hardware.FakeSensorManager;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36
37@SmallTest
38@RunWith(AndroidTestingRunner.class)
39public class AsyncSensorManagerTest extends SysuiTestCase {
40
41 private TestableAsyncSensorManager mAsyncSensorManager;
42 private FakeSensorManager mFakeSensorManager;
43 private SensorEventListener mListener;
44 private FakeSensorManager.MockProximitySensor mSensor;
45
46 @Before
47 public void setUp() throws Exception {
48 mFakeSensorManager = new FakeSensorManager(mContext);
49 mAsyncSensorManager = new TestableAsyncSensorManager(mFakeSensorManager);
50 mSensor = mFakeSensorManager.getMockProximitySensor();
51 mListener = mock(SensorEventListener.class);
52 }
53
54 @Test
55 public void registerListenerImpl() throws Exception {
56 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
57
58 mAsyncSensorManager.waitUntilRequestsCompleted();
59
60 // Verify listener was registered.
61 mSensor.sendProximityResult(true);
62 verify(mListener).onSensorChanged(any());
63 }
64
65 @Test
66 public void unregisterListenerImpl_withNullSensor() throws Exception {
67 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
68 mAsyncSensorManager.unregisterListener(mListener);
69
70 mAsyncSensorManager.waitUntilRequestsCompleted();
71
72 // Verify listener was unregistered.
73 mSensor.sendProximityResult(true);
74 verifyNoMoreInteractions(mListener);
75 }
76
77 @Test
78 public void unregisterListenerImpl_withSensor() throws Exception {
79 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
80 mAsyncSensorManager.unregisterListener(mListener, mSensor.getSensor());
81
82 mAsyncSensorManager.waitUntilRequestsCompleted();
83
84 // Verify listener was unregistered.
85 mSensor.sendProximityResult(true);
86 verifyNoMoreInteractions(mListener);
87 }
88
89 private class TestableAsyncSensorManager extends AsyncSensorManager {
90 public TestableAsyncSensorManager(SensorManager sensorManager) {
91 super(sensorManager);
92 }
93
94 public void waitUntilRequestsCompleted() {
95 assertTrue(mHandler.runWithScissors(() -> {}, 0));
96 }
97 }
98}