OSDN Git Service

Merge "docs: Add documentation for equals() method" into qt-dev
[android-x86/frameworks-base.git] / packages / SystemUI / tests / src / com / android / systemui / classifier / brightline / ZigZagClassifierTest.java
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 org.hamcrest.CoreMatchers.is;
20 import static org.junit.Assert.assertThat;
21
22 import android.testing.AndroidTestingRunner;
23 import android.testing.TestableLooper;
24
25 import androidx.test.filters.SmallTest;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31
32 import java.util.Random;
33
34 @SmallTest
35 @RunWith(AndroidTestingRunner.class)
36 @TestableLooper.RunWithLooper
37 public class ZigZagClassifierTest extends ClassifierTest {
38
39     private FalsingClassifier mClassifier;
40
41     @Before
42     public void setup() {
43         super.setup();
44         mClassifier = new ZigZagClassifier(getDataProvider());
45     }
46
47     @After
48     public void tearDown() {
49         super.tearDown();
50     }
51
52     @Test
53     public void testPass_fewTouchesVertical() {
54         assertThat(mClassifier.isFalseTouch(), is(false));
55         appendMoveEvent(0, 0);
56         assertThat(mClassifier.isFalseTouch(), is(false));
57         appendMoveEvent(0, 100);
58         assertThat(mClassifier.isFalseTouch(), is(false));
59     }
60
61     @Test
62     public void testPass_vertical() {
63         appendMoveEvent(0, 0);
64         appendMoveEvent(0, 100);
65         appendMoveEvent(0, 200);
66         assertThat(mClassifier.isFalseTouch(), is(false));
67     }
68
69     @Test
70     public void testPass_fewTouchesHorizontal() {
71         assertThat(mClassifier.isFalseTouch(), is(false));
72         appendMoveEvent(0, 0);
73         assertThat(mClassifier.isFalseTouch(), is(false));
74         appendMoveEvent(100, 0);
75         assertThat(mClassifier.isFalseTouch(), is(false));
76     }
77
78     @Test
79     public void testPass_horizontal() {
80         appendMoveEvent(0, 0);
81         appendMoveEvent(100, 0);
82         appendMoveEvent(200, 0);
83         assertThat(mClassifier.isFalseTouch(), is(false));
84     }
85
86
87     @Test
88     public void testFail_minimumTouchesVertical() {
89         appendMoveEvent(0, 0);
90         appendMoveEvent(0, 100);
91         appendMoveEvent(0, 1);
92         assertThat(mClassifier.isFalseTouch(), is(true));
93     }
94
95     @Test
96     public void testFail_minimumTouchesHorizontal() {
97         appendMoveEvent(0, 0);
98         appendMoveEvent(100, 0);
99         appendMoveEvent(1, 0);
100         assertThat(mClassifier.isFalseTouch(), is(true));
101     }
102
103     @Test
104     public void testPass_fortyFiveDegreesStraight() {
105         appendMoveEvent(0, 0);
106         appendMoveEvent(10, 10);
107         appendMoveEvent(20, 20);
108         assertThat(mClassifier.isFalseTouch(), is(false));
109     }
110
111     @Test
112     public void testPass_horizontalZigZagVerticalStraight() {
113         // This test looks just like testFail_horizontalZigZagVerticalStraight but with
114         // a longer y range, making it look straighter.
115         appendMoveEvent(0, 0);
116         appendMoveEvent(5, 100);
117         appendMoveEvent(-5, 200);
118         assertThat(mClassifier.isFalseTouch(), is(false));
119     }
120
121     @Test
122     public void testPass_horizontalStraightVerticalZigZag() {
123         // This test looks just like testFail_horizontalStraightVerticalZigZag but with
124         // a longer x range, making it look straighter.
125         appendMoveEvent(0, 0);
126         appendMoveEvent(100, 5);
127         appendMoveEvent(200, -5);
128         assertThat(mClassifier.isFalseTouch(), is(false));
129     }
130
131     @Test
132     public void testFail_horizontalZigZagVerticalStraight() {
133         // This test looks just like testPass_horizontalZigZagVerticalStraight but with
134         // a shorter y range, making it look more crooked.
135         appendMoveEvent(0, 0);
136         appendMoveEvent(5, 10);
137         appendMoveEvent(-5, 20);
138         assertThat(mClassifier.isFalseTouch(), is(true));
139     }
140
141     @Test
142     public void testFail_horizontalStraightVerticalZigZag() {
143         // This test looks just like testPass_horizontalStraightVerticalZigZag but with
144         // a shorter x range, making it look more crooked.
145         appendMoveEvent(0, 0);
146         appendMoveEvent(10, 5);
147         appendMoveEvent(20, -5);
148         assertThat(mClassifier.isFalseTouch(), is(true));
149     }
150
151     @Test
152     public void test_between0And45() {
153         appendMoveEvent(0, 0);
154         appendMoveEvent(100, 5);
155         appendMoveEvent(200, 10);
156         assertThat(mClassifier.isFalseTouch(), is(false));
157
158         resetDataProvider();
159         appendMoveEvent(0, 0);
160         appendMoveEvent(100, 0);
161         appendMoveEvent(200, 10);
162         assertThat(mClassifier.isFalseTouch(), is(false));
163
164         resetDataProvider();
165         appendMoveEvent(0, 0);
166         appendMoveEvent(100, -10);
167         appendMoveEvent(200, 10);
168         assertThat(mClassifier.isFalseTouch(), is(false));
169
170         resetDataProvider();
171         appendMoveEvent(0, 0);
172         appendMoveEvent(100, -10);
173         appendMoveEvent(200, 50);
174         assertThat(mClassifier.isFalseTouch(), is(true));
175     }
176
177     @Test
178     public void test_between45And90() {
179         appendMoveEvent(0, 0);
180         appendMoveEvent(10, 50);
181         appendMoveEvent(8, 100);
182         assertThat(mClassifier.isFalseTouch(), is(false));
183
184         resetDataProvider();
185         appendMoveEvent(0, 0);
186         appendMoveEvent(1, 800);
187         appendMoveEvent(2, 900);
188         assertThat(mClassifier.isFalseTouch(), is(false));
189
190         resetDataProvider();
191         appendMoveEvent(0, 0);
192         appendMoveEvent(-10, 600);
193         appendMoveEvent(30, 700);
194         assertThat(mClassifier.isFalseTouch(), is(false));
195
196         resetDataProvider();
197         appendMoveEvent(0, 0);
198         appendMoveEvent(40, 100);
199         appendMoveEvent(0, 101);
200         assertThat(mClassifier.isFalseTouch(), is(true));
201     }
202
203     @Test
204     public void test_between90And135() {
205         appendMoveEvent(0, 0);
206         appendMoveEvent(-10, 50);
207         appendMoveEvent(-24, 100);
208         assertThat(mClassifier.isFalseTouch(), is(false));
209
210         resetDataProvider();
211         appendMoveEvent(0, 0);
212         appendMoveEvent(-20, 800);
213         appendMoveEvent(-20, 900);
214         assertThat(mClassifier.isFalseTouch(), is(false));
215
216         resetDataProvider();
217         appendMoveEvent(0, 0);
218         appendMoveEvent(30, 600);
219         appendMoveEvent(-10, 700);
220         assertThat(mClassifier.isFalseTouch(), is(false));
221
222         resetDataProvider();
223         appendMoveEvent(0, 0);
224         appendMoveEvent(-80, 100);
225         appendMoveEvent(-10, 101);
226         assertThat(mClassifier.isFalseTouch(), is(true));
227     }
228
229     @Test
230     public void test_between135And180() {
231         appendMoveEvent(0, 0);
232         appendMoveEvent(-120, 10);
233         appendMoveEvent(-200, 20);
234         assertThat(mClassifier.isFalseTouch(), is(false));
235
236         resetDataProvider();
237         appendMoveEvent(0, 0);
238         appendMoveEvent(-20, 8);
239         appendMoveEvent(-40, 2);
240         assertThat(mClassifier.isFalseTouch(), is(false));
241
242         resetDataProvider();
243         appendMoveEvent(0, 0);
244         appendMoveEvent(-500, -2);
245         appendMoveEvent(-600, 70);
246         assertThat(mClassifier.isFalseTouch(), is(false));
247
248         resetDataProvider();
249         appendMoveEvent(0, 0);
250         appendMoveEvent(-80, 100);
251         appendMoveEvent(-100, 1);
252         assertThat(mClassifier.isFalseTouch(), is(true));
253     }
254
255     @Test
256     public void test_between180And225() {
257         appendMoveEvent(0, 0);
258         appendMoveEvent(-120, -10);
259         appendMoveEvent(-200, -20);
260         assertThat(mClassifier.isFalseTouch(), is(false));
261
262         resetDataProvider();
263         appendMoveEvent(0, 0);
264         appendMoveEvent(-20, -8);
265         appendMoveEvent(-40, -2);
266         assertThat(mClassifier.isFalseTouch(), is(false));
267
268         resetDataProvider();
269         appendMoveEvent(0, 0);
270         appendMoveEvent(-500, 2);
271         appendMoveEvent(-600, -70);
272         assertThat(mClassifier.isFalseTouch(), is(false));
273
274         resetDataProvider();
275         appendMoveEvent(0, 0);
276         appendMoveEvent(-80, -100);
277         appendMoveEvent(-100, -1);
278         assertThat(mClassifier.isFalseTouch(), is(true));
279     }
280
281     @Test
282     public void test_between225And270() {
283         appendMoveEvent(0, 0);
284         appendMoveEvent(-12, -20);
285         appendMoveEvent(-20, -40);
286         assertThat(mClassifier.isFalseTouch(), is(false));
287
288         resetDataProvider();
289         appendMoveEvent(0, 0);
290         appendMoveEvent(-20, -130);
291         appendMoveEvent(-40, -260);
292         assertThat(mClassifier.isFalseTouch(), is(false));
293
294         resetDataProvider();
295         appendMoveEvent(0, 0);
296         appendMoveEvent(1, -100);
297         appendMoveEvent(-6, -200);
298         assertThat(mClassifier.isFalseTouch(), is(false));
299
300         resetDataProvider();
301         appendMoveEvent(0, 0);
302         appendMoveEvent(-80, -100);
303         appendMoveEvent(-10, -110);
304         assertThat(mClassifier.isFalseTouch(), is(true));
305     }
306
307     @Test
308     public void test_between270And315() {
309         appendMoveEvent(0, 0);
310         appendMoveEvent(12, -20);
311         appendMoveEvent(20, -40);
312         assertThat(mClassifier.isFalseTouch(), is(false));
313
314         resetDataProvider();
315         appendMoveEvent(0, 0);
316         appendMoveEvent(20, -130);
317         appendMoveEvent(40, -260);
318         assertThat(mClassifier.isFalseTouch(), is(false));
319
320         resetDataProvider();
321         appendMoveEvent(0, 0);
322         appendMoveEvent(-1, -100);
323         appendMoveEvent(6, -200);
324         assertThat(mClassifier.isFalseTouch(), is(false));
325
326         resetDataProvider();
327         appendMoveEvent(0, 0);
328         appendMoveEvent(80, -100);
329         appendMoveEvent(10, -110);
330         assertThat(mClassifier.isFalseTouch(), is(true));
331     }
332
333     @Test
334     public void test_between315And360() {
335         appendMoveEvent(0, 0);
336         appendMoveEvent(120, -20);
337         appendMoveEvent(200, -40);
338         assertThat(mClassifier.isFalseTouch(), is(false));
339
340         resetDataProvider();
341         appendMoveEvent(0, 0);
342         appendMoveEvent(200, -13);
343         appendMoveEvent(400, -30);
344         assertThat(mClassifier.isFalseTouch(), is(false));
345
346         resetDataProvider();
347         appendMoveEvent(0, 0);
348         appendMoveEvent(100, 10);
349         appendMoveEvent(600, -20);
350         assertThat(mClassifier.isFalseTouch(), is(false));
351
352         resetDataProvider();
353         appendMoveEvent(0, 0);
354         appendMoveEvent(80, -100);
355         appendMoveEvent(100, -1);
356         assertThat(mClassifier.isFalseTouch(), is(true));
357     }
358
359     @Test
360     public void test_randomOrigins() {
361         // The purpose of this test is to try all the other tests from different starting points.
362         // We use a pre-determined seed to make this test repeatable.
363         Random rand = new Random(23);
364         for (int i = 0; i < 100; i++) {
365             setOffsetX(rand.nextInt(2000) - 1000);
366             setOffsetY(rand.nextInt(2000) - 1000);
367             try {
368                 resetDataProvider();
369                 testPass_fewTouchesVertical();
370                 resetDataProvider();
371                 testPass_vertical();
372                 resetDataProvider();
373                 testFail_horizontalStraightVerticalZigZag();
374                 resetDataProvider();
375                 testFail_horizontalZigZagVerticalStraight();
376                 resetDataProvider();
377                 testFail_minimumTouchesHorizontal();
378                 resetDataProvider();
379                 testFail_minimumTouchesVertical();
380                 resetDataProvider();
381                 testPass_fewTouchesHorizontal();
382                 resetDataProvider();
383                 testPass_fortyFiveDegreesStraight();
384                 resetDataProvider();
385                 testPass_horizontal();
386                 resetDataProvider();
387                 testPass_horizontalStraightVerticalZigZag();
388                 resetDataProvider();
389                 testPass_horizontalZigZagVerticalStraight();
390                 resetDataProvider();
391                 test_between0And45();
392                 resetDataProvider();
393                 test_between45And90();
394                 resetDataProvider();
395                 test_between90And135();
396                 resetDataProvider();
397                 test_between135And180();
398                 resetDataProvider();
399                 test_between180And225();
400                 resetDataProvider();
401                 test_between225And270();
402                 resetDataProvider();
403                 test_between270And315();
404                 resetDataProvider();
405                 test_between315And360();
406             } catch (AssertionError e) {
407                 throw new AssertionError("Random origin failure in iteration " + i, e);
408             }
409         }
410     }
411 }