blob: 4a9b1b3c4006948fda205812205d8af848ae869a [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;
Kevin Chynac56f742018-10-09 16:20:25 -070021import static org.mockito.ArgumentMatchers.eq;
Adrian Roosb1f77242017-08-04 12:35:24 +020022import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.verifyNoMoreInteractions;
25
26import android.hardware.SensorEventListener;
27import android.hardware.SensorManager;
Adrian Roosb1f77242017-08-04 12:35:24 +020028import android.testing.AndroidTestingRunner;
29
Brett Chabot84151d92019-02-27 15:37:59 -080030import androidx.test.filters.SmallTest;
31
Adrian Roosb1f77242017-08-04 12:35:24 +020032import com.android.systemui.SysuiTestCase;
Kevin Chynac56f742018-10-09 16:20:25 -070033import com.android.systemui.plugins.SensorManagerPlugin;
34import com.android.systemui.shared.plugins.PluginManager;
Adrian Roosb1f77242017-08-04 12:35:24 +020035import com.android.systemui.utils.hardware.FakeSensorManager;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40
41@SmallTest
42@RunWith(AndroidTestingRunner.class)
43public class AsyncSensorManagerTest extends SysuiTestCase {
44
45 private TestableAsyncSensorManager mAsyncSensorManager;
46 private FakeSensorManager mFakeSensorManager;
47 private SensorEventListener mListener;
48 private FakeSensorManager.MockProximitySensor mSensor;
Kevin Chynac56f742018-10-09 16:20:25 -070049 private PluginManager mPluginManager;
Adrian Roosb1f77242017-08-04 12:35:24 +020050
51 @Before
52 public void setUp() throws Exception {
Kevin Chynac56f742018-10-09 16:20:25 -070053 mPluginManager = mock(PluginManager.class);
Adrian Roosb1f77242017-08-04 12:35:24 +020054 mFakeSensorManager = new FakeSensorManager(mContext);
55 mAsyncSensorManager = new TestableAsyncSensorManager(mFakeSensorManager);
56 mSensor = mFakeSensorManager.getMockProximitySensor();
57 mListener = mock(SensorEventListener.class);
58 }
59
60 @Test
61 public void registerListenerImpl() throws Exception {
62 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
63
64 mAsyncSensorManager.waitUntilRequestsCompleted();
65
66 // Verify listener was registered.
67 mSensor.sendProximityResult(true);
68 verify(mListener).onSensorChanged(any());
69 }
70
71 @Test
72 public void unregisterListenerImpl_withNullSensor() throws Exception {
73 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
74 mAsyncSensorManager.unregisterListener(mListener);
75
76 mAsyncSensorManager.waitUntilRequestsCompleted();
77
78 // Verify listener was unregistered.
79 mSensor.sendProximityResult(true);
80 verifyNoMoreInteractions(mListener);
81 }
82
83 @Test
84 public void unregisterListenerImpl_withSensor() throws Exception {
85 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
86 mAsyncSensorManager.unregisterListener(mListener, mSensor.getSensor());
87
88 mAsyncSensorManager.waitUntilRequestsCompleted();
89
90 // Verify listener was unregistered.
91 mSensor.sendProximityResult(true);
92 verifyNoMoreInteractions(mListener);
93 }
94
Kevin Chynac56f742018-10-09 16:20:25 -070095 @Test
96 public void registersPlugin_whenLoaded() {
97 verify(mPluginManager).addPluginListener(eq(mAsyncSensorManager),
98 eq(SensorManagerPlugin.class), eq(true) /* allowMultiple */);
99 }
100
Adrian Roosb1f77242017-08-04 12:35:24 +0200101 private class TestableAsyncSensorManager extends AsyncSensorManager {
102 public TestableAsyncSensorManager(SensorManager sensorManager) {
Kevin Chynac56f742018-10-09 16:20:25 -0700103 super(sensorManager, mPluginManager);
Adrian Roosb1f77242017-08-04 12:35:24 +0200104 }
105
106 public void waitUntilRequestsCompleted() {
107 assertTrue(mHandler.runWithScissors(() -> {}, 0));
108 }
109 }
110}