blob: a6cabbf49458d47dded13ef2178331e558aba1ae [file] [log] [blame]
Dave Mankoff8bfbe332019-06-12 17:58:30 -04001/*
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
17package com.android.systemui.classifier.brightline;
18
19import static com.android.systemui.classifier.Classifier.GENERIC;
20import static com.android.systemui.classifier.Classifier.QUICK_SETTINGS;
21
22import static org.hamcrest.CoreMatchers.is;
23import static org.junit.Assert.assertThat;
24import static org.mockito.Mockito.when;
25
26import android.hardware.Sensor;
27import android.hardware.SensorEvent;
28import android.testing.AndroidTestingRunner;
29import android.testing.TestableLooper;
30import android.view.MotionEvent;
31
32import androidx.test.filters.SmallTest;
33
Dave Mankofff5d210e2019-06-25 12:23:05 -040034import org.junit.After;
Dave Mankoff8bfbe332019-06-12 17:58:30 -040035import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38import org.mockito.Mock;
39import org.mockito.Mockito;
40import org.mockito.MockitoAnnotations;
41
42import java.lang.reflect.Field;
43
44@SmallTest
45@RunWith(AndroidTestingRunner.class)
46@TestableLooper.RunWithLooper
Dave Mankofff5d210e2019-06-25 12:23:05 -040047public class ProximityClassifierTest extends ClassifierTest {
Dave Mankoff8bfbe332019-06-12 17:58:30 -040048
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 Mankofff5d210e2019-06-25 12:23:05 -040059 super.setup();
Dave Mankoff8bfbe332019-06-12 17:58:30 -040060 MockitoAnnotations.initMocks(this);
61 when(mDataProvider.getInteractionType()).thenReturn(GENERIC);
62 when(mDistanceClassifier.isLongSwipe()).thenReturn(false);
63 mClassifier = new ProximityClassifier(mDistanceClassifier, mDataProvider);
64 }
65
Dave Mankofff5d210e2019-06-25 12:23:05 -040066 @After
67 public void tearDown() {
68 super.tearDown();
69 }
70
Dave Mankoff8bfbe332019-06-12 17:58:30 -040071 @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 Mankoff8bfbe332019-06-12 17:58:30 -0400118 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}