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 | |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 26 | import android.testing.AndroidTestingRunner; |
| 27 | import android.testing.TestableLooper; |
| 28 | import android.view.MotionEvent; |
| 29 | |
| 30 | import androidx.test.filters.SmallTest; |
| 31 | |
Dave Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 32 | import com.android.systemui.util.ProximitySensor; |
| 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; |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 39 | import org.mockito.MockitoAnnotations; |
| 40 | |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 41 | @SmallTest |
| 42 | @RunWith(AndroidTestingRunner.class) |
| 43 | @TestableLooper.RunWithLooper |
Dave Mankoff | f5d210e | 2019-06-25 12:23:05 -0400 | [diff] [blame] | 44 | public class ProximityClassifierTest extends ClassifierTest { |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 45 | |
| 46 | private static final long NS_PER_MS = 1000000; |
| 47 | |
| 48 | @Mock |
| 49 | private FalsingDataProvider mDataProvider; |
| 50 | @Mock |
| 51 | private DistanceClassifier mDistanceClassifier; |
| 52 | private FalsingClassifier mClassifier; |
| 53 | |
| 54 | @Before |
| 55 | public void setup() { |
Dave Mankoff | f5d210e | 2019-06-25 12:23:05 -0400 | [diff] [blame] | 56 | super.setup(); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 57 | MockitoAnnotations.initMocks(this); |
| 58 | when(mDataProvider.getInteractionType()).thenReturn(GENERIC); |
| 59 | when(mDistanceClassifier.isLongSwipe()).thenReturn(false); |
| 60 | mClassifier = new ProximityClassifier(mDistanceClassifier, mDataProvider); |
| 61 | } |
| 62 | |
Dave Mankoff | f5d210e | 2019-06-25 12:23:05 -0400 | [diff] [blame] | 63 | @After |
| 64 | public void tearDown() { |
| 65 | super.tearDown(); |
| 66 | } |
| 67 | |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 68 | @Test |
| 69 | public void testPass_uncovered() { |
| 70 | touchDown(); |
| 71 | touchUp(10); |
| 72 | assertThat(mClassifier.isFalseTouch(), is(false)); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testPass_mostlyUncovered() { |
| 77 | touchDown(); |
Dave Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 78 | mClassifier.onProximityEvent(createSensorEvent(true, 1)); |
| 79 | mClassifier.onProximityEvent(createSensorEvent(false, 2)); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 80 | touchUp(20); |
| 81 | assertThat(mClassifier.isFalseTouch(), is(false)); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testPass_quickSettings() { |
| 86 | touchDown(); |
| 87 | when(mDataProvider.getInteractionType()).thenReturn(QUICK_SETTINGS); |
Dave Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 88 | mClassifier.onProximityEvent(createSensorEvent(true, 1)); |
| 89 | mClassifier.onProximityEvent(createSensorEvent(false, 11)); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 90 | touchUp(10); |
| 91 | assertThat(mClassifier.isFalseTouch(), is(false)); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testFail_covered() { |
| 96 | touchDown(); |
Dave Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 97 | mClassifier.onProximityEvent(createSensorEvent(true, 1)); |
| 98 | mClassifier.onProximityEvent(createSensorEvent(false, 11)); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 99 | touchUp(10); |
| 100 | assertThat(mClassifier.isFalseTouch(), is(true)); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testFail_mostlyCovered() { |
| 105 | touchDown(); |
Dave Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 106 | mClassifier.onProximityEvent(createSensorEvent(true, 1)); |
| 107 | mClassifier.onProximityEvent(createSensorEvent(true, 95)); |
| 108 | mClassifier.onProximityEvent(createSensorEvent(true, 96)); |
| 109 | mClassifier.onProximityEvent(createSensorEvent(false, 100)); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 110 | touchUp(100); |
| 111 | assertThat(mClassifier.isFalseTouch(), is(true)); |
| 112 | } |
| 113 | |
| 114 | @Test |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 115 | public void testPass_coveredWithLongSwipe() { |
| 116 | touchDown(); |
Dave Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 117 | mClassifier.onProximityEvent(createSensorEvent(true, 1)); |
| 118 | mClassifier.onProximityEvent(createSensorEvent(false, 11)); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 119 | touchUp(10); |
| 120 | when(mDistanceClassifier.isLongSwipe()).thenReturn(true); |
| 121 | assertThat(mClassifier.isFalseTouch(), is(false)); |
| 122 | } |
| 123 | |
| 124 | private void touchDown() { |
| 125 | MotionEvent motionEvent = MotionEvent.obtain(1, 1, MotionEvent.ACTION_DOWN, 0, 0, 0); |
| 126 | mClassifier.onTouchEvent(motionEvent); |
| 127 | motionEvent.recycle(); |
| 128 | } |
| 129 | |
| 130 | private void touchUp(long duration) { |
| 131 | MotionEvent motionEvent = MotionEvent.obtain(1, 1 + duration, MotionEvent.ACTION_UP, 0, |
| 132 | 100, 0); |
| 133 | |
| 134 | mClassifier.onTouchEvent(motionEvent); |
| 135 | |
| 136 | motionEvent.recycle(); |
| 137 | } |
| 138 | |
Dave Mankoff | 46b9d68 | 2019-09-12 13:39:42 -0400 | [diff] [blame] | 139 | private ProximitySensor.ProximityEvent createSensorEvent(boolean covered, long timestampMs) { |
| 140 | return new ProximitySensor.ProximityEvent(covered, timestampMs * NS_PER_MS); |
Dave Mankoff | 8bfbe33 | 2019-06-12 17:58:30 -0400 | [diff] [blame] | 141 | } |
| 142 | } |