blob: 77df7919ed291c3aa7b8d7c969ee8e57ec6e0923 [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;
28import android.support.test.filters.SmallTest;
29import android.testing.AndroidTestingRunner;
30
31import com.android.systemui.SysuiTestCase;
Kevin Chynac56f742018-10-09 16:20:25 -070032import com.android.systemui.plugins.SensorManagerPlugin;
33import com.android.systemui.shared.plugins.PluginManager;
Adrian Roosb1f77242017-08-04 12:35:24 +020034import com.android.systemui.utils.hardware.FakeSensorManager;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39
40@SmallTest
41@RunWith(AndroidTestingRunner.class)
42public class AsyncSensorManagerTest extends SysuiTestCase {
43
44 private TestableAsyncSensorManager mAsyncSensorManager;
45 private FakeSensorManager mFakeSensorManager;
46 private SensorEventListener mListener;
47 private FakeSensorManager.MockProximitySensor mSensor;
Kevin Chynac56f742018-10-09 16:20:25 -070048 private PluginManager mPluginManager;
Adrian Roosb1f77242017-08-04 12:35:24 +020049
50 @Before
51 public void setUp() throws Exception {
Kevin Chynac56f742018-10-09 16:20:25 -070052 mPluginManager = mock(PluginManager.class);
Adrian Roosb1f77242017-08-04 12:35:24 +020053 mFakeSensorManager = new FakeSensorManager(mContext);
54 mAsyncSensorManager = new TestableAsyncSensorManager(mFakeSensorManager);
55 mSensor = mFakeSensorManager.getMockProximitySensor();
56 mListener = mock(SensorEventListener.class);
57 }
58
59 @Test
60 public void registerListenerImpl() throws Exception {
61 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
62
63 mAsyncSensorManager.waitUntilRequestsCompleted();
64
65 // Verify listener was registered.
66 mSensor.sendProximityResult(true);
67 verify(mListener).onSensorChanged(any());
68 }
69
70 @Test
71 public void unregisterListenerImpl_withNullSensor() throws Exception {
72 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
73 mAsyncSensorManager.unregisterListener(mListener);
74
75 mAsyncSensorManager.waitUntilRequestsCompleted();
76
77 // Verify listener was unregistered.
78 mSensor.sendProximityResult(true);
79 verifyNoMoreInteractions(mListener);
80 }
81
82 @Test
83 public void unregisterListenerImpl_withSensor() throws Exception {
84 mAsyncSensorManager.registerListener(mListener, mSensor.getSensor(), 100);
85 mAsyncSensorManager.unregisterListener(mListener, mSensor.getSensor());
86
87 mAsyncSensorManager.waitUntilRequestsCompleted();
88
89 // Verify listener was unregistered.
90 mSensor.sendProximityResult(true);
91 verifyNoMoreInteractions(mListener);
92 }
93
Kevin Chynac56f742018-10-09 16:20:25 -070094 @Test
95 public void registersPlugin_whenLoaded() {
96 verify(mPluginManager).addPluginListener(eq(mAsyncSensorManager),
97 eq(SensorManagerPlugin.class), eq(true) /* allowMultiple */);
98 }
99
Adrian Roosb1f77242017-08-04 12:35:24 +0200100 private class TestableAsyncSensorManager extends AsyncSensorManager {
101 public TestableAsyncSensorManager(SensorManager sensorManager) {
Kevin Chynac56f742018-10-09 16:20:25 -0700102 super(sensorManager, mPluginManager);
Adrian Roosb1f77242017-08-04 12:35:24 +0200103 }
104
105 public void waitUntilRequestsCompleted() {
106 assertTrue(mHandler.runWithScissors(() -> {}, 0));
107 }
108 }
109}