Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.classifier.brightline; |
| 18 | |
| 19 | import static com.android.systemui.classifier.Classifier.GENERIC; |
| 20 | import static com.android.systemui.classifier.Classifier.QUICK_SETTINGS; |
| 21 | |
| 22 | import static org.hamcrest.CoreMatchers.is; |
| 23 | import static org.junit.Assert.assertThat; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.hardware.Sensor; |
| 27 | import android.hardware.SensorEvent; |
| 28 | import android.testing.AndroidTestingRunner; |
| 29 | import android.testing.TestableLooper; |
| 30 | import android.view.MotionEvent; |
| 31 | |
| 32 | import androidx.test.filters.SmallTest; |
| 33 | |
Dave Mankoff | f5d210e | 2019-06-25 12:23:05 -0400 | [diff] [blame] | 34 | import org.junit.After; |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.mockito.Mock; |
| 39 | import org.mockito.Mockito; |
| 40 | import org.mockito.MockitoAnnotations; |
| 41 | |
| 42 | import java.lang.reflect.Field; |
| 43 | |
| 44 | @SmallTest |
| 45 | @RunWith(AndroidTestingRunner.class) |
| 46 | @TestableLooper.RunWithLooper |
Dave Mankoff | f5d210e | 2019-06-25 12:23:05 -0400 | [diff] [blame] | 47 | public class ProximityClassifierTest extends ClassifierTest { |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 48 | |
| 49 | private static final long NS_PER_MS = 1000000; |
| 50 | |
| 51 | @Mock |
| 52 | private FalsingDataProvider mDataProvider; |
| 53 | @Mock |
| 54 | private DistanceClassifier mDistanceClassifier; |
| 55 | private FalsingClassifier mClassifier; |
| 56 | |
| 57 | @Before |
| 58 | public void setup() { |
Dave Mankoff | f5d210e | 2019-06-25 12:23:05 -0400 | [diff] [blame] | 59 | super.setup(); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 60 | MockitoAnnotations.initMocks(this); |
| 61 | when(mDataProvider.getInteractionType()).thenReturn(GENERIC); |
| 62 | when(mDistanceClassifier.isLongSwipe()).thenReturn(false); |
| 63 | mClassifier = new ProximityClassifier(mDistanceClassifier, mDataProvider); |
| 64 | } |
| 65 | |
Dave Mankoff | f5d210e | 2019-06-25 12:23:05 -0400 | [diff] [blame] | 66 | @After |
| 67 | public void tearDown() { |
| 68 | super.tearDown(); |
| 69 | } |
| 70 | |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 71 | @Test |
| 72 | public void testPass_uncovered() { |
| 73 | touchDown(); |
| 74 | touchUp(10); |
| 75 | assertThat(mClassifier.isFalseTouch(), is(false)); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testPass_mostlyUncovered() { |
| 80 | touchDown(); |
| 81 | mClassifier.onSensorEvent(createSensorEvent(true, 1)); |
| 82 | mClassifier.onSensorEvent(createSensorEvent(false, 2)); |
| 83 | touchUp(20); |
| 84 | assertThat(mClassifier.isFalseTouch(), is(false)); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testPass_quickSettings() { |
| 89 | touchDown(); |
| 90 | when(mDataProvider.getInteractionType()).thenReturn(QUICK_SETTINGS); |
| 91 | mClassifier.onSensorEvent(createSensorEvent(true, 1)); |
| 92 | mClassifier.onSensorEvent(createSensorEvent(false, 11)); |
| 93 | touchUp(10); |
| 94 | assertThat(mClassifier.isFalseTouch(), is(false)); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testFail_covered() { |
| 99 | touchDown(); |
| 100 | mClassifier.onSensorEvent(createSensorEvent(true, 1)); |
| 101 | mClassifier.onSensorEvent(createSensorEvent(false, 11)); |
| 102 | touchUp(10); |
| 103 | assertThat(mClassifier.isFalseTouch(), is(true)); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testFail_mostlyCovered() { |
| 108 | touchDown(); |
| 109 | mClassifier.onSensorEvent(createSensorEvent(true, 1)); |
| 110 | mClassifier.onSensorEvent(createSensorEvent(true, 95)); |
| 111 | mClassifier.onSensorEvent(createSensorEvent(true, 96)); |
| 112 | mClassifier.onSensorEvent(createSensorEvent(false, 100)); |
| 113 | touchUp(100); |
| 114 | assertThat(mClassifier.isFalseTouch(), is(true)); |
| 115 | } |
| 116 | |
| 117 | @Test |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 118 | public void testPass_coveredWithLongSwipe() { |
| 119 | touchDown(); |
| 120 | mClassifier.onSensorEvent(createSensorEvent(true, 1)); |
| 121 | mClassifier.onSensorEvent(createSensorEvent(false, 11)); |
| 122 | touchUp(10); |
| 123 | when(mDistanceClassifier.isLongSwipe()).thenReturn(true); |
| 124 | assertThat(mClassifier.isFalseTouch(), is(false)); |
| 125 | } |
| 126 | |
| 127 | private void touchDown() { |
| 128 | MotionEvent motionEvent = MotionEvent.obtain(1, 1, MotionEvent.ACTION_DOWN, 0, 0, 0); |
| 129 | mClassifier.onTouchEvent(motionEvent); |
| 130 | motionEvent.recycle(); |
| 131 | } |
| 132 | |
| 133 | private void touchUp(long duration) { |
| 134 | MotionEvent motionEvent = MotionEvent.obtain(1, 1 + duration, MotionEvent.ACTION_UP, 0, |
| 135 | 100, 0); |
| 136 | |
| 137 | mClassifier.onTouchEvent(motionEvent); |
| 138 | |
| 139 | motionEvent.recycle(); |
| 140 | } |
| 141 | |
| 142 | private SensorEvent createSensorEvent(boolean covered, long timestampMs) { |
| 143 | SensorEvent sensorEvent = Mockito.mock(SensorEvent.class); |
| 144 | Sensor sensor = Mockito.mock(Sensor.class); |
| 145 | when(sensor.getType()).thenReturn(Sensor.TYPE_PROXIMITY); |
| 146 | when(sensor.getMaximumRange()).thenReturn(1f); |
| 147 | sensorEvent.sensor = sensor; |
| 148 | sensorEvent.timestamp = timestampMs * NS_PER_MS; |
| 149 | try { |
| 150 | Field valuesField = SensorEvent.class.getField("values"); |
| 151 | valuesField.setAccessible(true); |
| 152 | float[] sensorValue = {covered ? 0 : 1}; |
| 153 | try { |
| 154 | valuesField.set(sensorEvent, sensorValue); |
| 155 | } catch (IllegalAccessException e) { |
| 156 | e.printStackTrace(); |
| 157 | } |
| 158 | } catch (NoSuchFieldException e) { |
| 159 | e.printStackTrace(); |
| 160 | } |
| 161 | |
| 162 | return sensorEvent; |
| 163 | } |
| 164 | } |