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