Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.util; |
| 18 | |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | import static org.mockito.ArgumentMatchers.any; |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.eq; |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 22 | import static org.mockito.Mockito.mock; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 25 | |
| 26 | import android.hardware.SensorEventListener; |
| 27 | import android.hardware.SensorManager; |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 28 | import android.testing.AndroidTestingRunner; |
| 29 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 30 | import androidx.test.filters.SmallTest; |
| 31 | |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 32 | import com.android.systemui.SysuiTestCase; |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 33 | import com.android.systemui.plugins.SensorManagerPlugin; |
| 34 | import com.android.systemui.shared.plugins.PluginManager; |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 35 | import com.android.systemui.utils.hardware.FakeSensorManager; |
| 36 | |
| 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | |
| 41 | @SmallTest |
| 42 | @RunWith(AndroidTestingRunner.class) |
| 43 | public class AsyncSensorManagerTest extends SysuiTestCase { |
| 44 | |
| 45 | private TestableAsyncSensorManager mAsyncSensorManager; |
| 46 | private FakeSensorManager mFakeSensorManager; |
| 47 | private SensorEventListener mListener; |
| 48 | private FakeSensorManager.MockProximitySensor mSensor; |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 49 | private PluginManager mPluginManager; |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 50 | |
| 51 | @Before |
| 52 | public void setUp() throws Exception { |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 53 | mPluginManager = mock(PluginManager.class); |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 54 | 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 Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 95 | @Test |
| 96 | public void registersPlugin_whenLoaded() { |
| 97 | verify(mPluginManager).addPluginListener(eq(mAsyncSensorManager), |
| 98 | eq(SensorManagerPlugin.class), eq(true) /* allowMultiple */); |
| 99 | } |
| 100 | |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 101 | private class TestableAsyncSensorManager extends AsyncSensorManager { |
| 102 | public TestableAsyncSensorManager(SensorManager sensorManager) { |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 103 | super(sensorManager, mPluginManager); |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | public void waitUntilRequestsCompleted() { |
| 107 | assertTrue(mHandler.runWithScissors(() -> {}, 0)); |
| 108 | } |
| 109 | } |
| 110 | } |