1 /*
2  * Copyright (C) 2020 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;
18 
19 import static org.hamcrest.CoreMatchers.is;
20 import static org.junit.Assert.assertThat;
21 
22 import android.testing.AndroidTestingRunner;
23 import android.view.MotionEvent;
24 
25 import androidx.test.filters.SmallTest;
26 
27 import com.android.systemui.SysuiTestCase;
28 
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.MockitoAnnotations;
34 
35 @SmallTest
36 @RunWith(AndroidTestingRunner.class)
37 public class TimeLimitedMotionEventBufferTest extends SysuiTestCase {
38 
39     private static final long MAX_AGE_MS = 100;
40 
41     private TimeLimitedMotionEventBuffer mBuffer;
42 
43     @Before
setup()44     public void setup() {
45         MockitoAnnotations.initMocks(this);
46         mBuffer = new TimeLimitedMotionEventBuffer(MAX_AGE_MS);
47     }
48 
49     @After
tearDown()50     public void tearDown() {
51         for (MotionEvent motionEvent : mBuffer) {
52             motionEvent.recycle();
53         }
54         mBuffer.clear();
55     }
56 
57     @Test
testAllEventsRetained()58     public void testAllEventsRetained() {
59         MotionEvent eventA = MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0, 0, 0);
60         MotionEvent eventB = MotionEvent.obtain(0, 1, MotionEvent.ACTION_MOVE, 0, 0, 0);
61         MotionEvent eventC = MotionEvent.obtain(0, 2, MotionEvent.ACTION_MOVE, 0, 0, 0);
62         MotionEvent eventD = MotionEvent.obtain(0, 3, MotionEvent.ACTION_UP, 0, 0, 0);
63 
64         mBuffer.add(eventA);
65         mBuffer.add(eventB);
66         mBuffer.add(eventC);
67         mBuffer.add(eventD);
68 
69         assertThat(mBuffer.size(), is(4));
70     }
71 
72     @Test
testOlderEventsRemoved()73     public void testOlderEventsRemoved() {
74         MotionEvent eventA = MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0, 0, 0);
75         MotionEvent eventB = MotionEvent.obtain(0, 1, MotionEvent.ACTION_MOVE, 0, 0, 0);
76         MotionEvent eventC = MotionEvent.obtain(
77                 0, MAX_AGE_MS + 1, MotionEvent.ACTION_MOVE, 0, 0, 0);
78         MotionEvent eventD = MotionEvent.obtain(
79                 0, MAX_AGE_MS + 2, MotionEvent.ACTION_UP, 0, 0, 0);
80 
81         mBuffer.add(eventA);
82         mBuffer.add(eventB);
83         assertThat(mBuffer.size(), is(2));
84 
85         mBuffer.add(eventC);
86         mBuffer.add(eventD);
87         assertThat(mBuffer.size(), is(2));
88 
89         assertThat(mBuffer.get(0), is(eventC));
90         assertThat(mBuffer.get(1), is(eventD));
91     }
92 }
93