blob: 4dee43857c65ec15c720fa4998d318685cc0794c [file] [log] [blame]
Jason Monk9262c942017-07-28 14:35:13 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.statusbar.phone;
16
17import static org.mockito.ArgumentMatchers.any;
18import static org.mockito.ArgumentMatchers.eq;
19import static org.mockito.Mockito.doAnswer;
Jason Monke57e9dc2017-08-04 10:59:13 -040020import static org.mockito.Mockito.never;
Jason Monk9262c942017-07-28 14:35:13 -040021import static org.mockito.Mockito.spy;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
Jason Monke57e9dc2017-08-04 10:59:13 -040025import android.content.res.Configuration;
Jason Monk9262c942017-07-28 14:35:13 -040026import android.support.test.filters.SmallTest;
27import android.testing.AndroidTestingRunner;
28import android.testing.TestableLooper.RunWithLooper;
29import android.view.MotionEvent;
30import android.view.View;
31
32import com.android.systemui.SysuiTestCase;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38@RunWith(AndroidTestingRunner.class)
39@RunWithLooper
40@SmallTest
41public class NearestTouchFrameTest extends SysuiTestCase {
42
43 private NearestTouchFrame mNearestTouchFrame;
44
45 @Before
46 public void setup() {
Jason Monke57e9dc2017-08-04 10:59:13 -040047 Configuration c = new Configuration(mContext.getResources().getConfiguration());
48 c.smallestScreenWidthDp = 500;
49 mNearestTouchFrame = new NearestTouchFrame(mContext, null, c);
50 }
51
52 @Test
53 public void testNoActionOnLargeDevices() {
54 Configuration c = new Configuration(mContext.getResources().getConfiguration());
55 c.smallestScreenWidthDp = 700;
56 mNearestTouchFrame = new NearestTouchFrame(mContext, null, c);
57
58 View left = mockViewAt(0, 0, 10, 10);
59 View right = mockViewAt(20, 0, 10, 10);
60
61 mNearestTouchFrame.addView(left);
62 mNearestTouchFrame.addView(right);
63 mNearestTouchFrame.onMeasure(0, 0);
64
65 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
66 12 /* x */, 5 /* y */, 0);
67 mNearestTouchFrame.onTouchEvent(ev);
68 verify(left, never()).onTouchEvent(eq(ev));
69 ev.recycle();
Jason Monk9262c942017-07-28 14:35:13 -040070 }
71
72 @Test
Jason Monk3dd17a72017-09-06 14:10:39 -040073 public void testInvisibleViews() {
74 View left = mockViewAt(0, 0, 10, 10);
75 View right = mockViewAt(20, 0, 10, 10);
76 when(left.getVisibility()).thenReturn(View.INVISIBLE);
77
78 mNearestTouchFrame.addView(left);
79 mNearestTouchFrame.addView(right);
80 mNearestTouchFrame.onMeasure(0, 0);
81
82 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
83 12 /* x */, 5 /* y */, 0);
84 mNearestTouchFrame.onTouchEvent(ev);
85 verify(left, never()).onTouchEvent(eq(ev));
86 verify(right, never()).onTouchEvent(eq(ev));
87 ev.recycle();
88 }
89
Rohan Shah2adfe952018-03-05 10:00:10 -080090
91 @Test
92 public void testNearestView_DetachedViewsExcluded() {
93 View left = mockViewAt(0, 0, 10, 10);
94 when(left.isAttachedToWindow()).thenReturn(false);
95 View right = mockViewAt(20, 0, 10, 10);
96
97 mNearestTouchFrame.addView(left);
98 mNearestTouchFrame.addView(right);
99 mNearestTouchFrame.onMeasure(0, 0);
100
101 // Would go to left view if attached, but goes to right instead as left should be detached.
102 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
103 12 /* x */, 5 /* y */, 0);
104 mNearestTouchFrame.onTouchEvent(ev);
105 verify(right).onTouchEvent(eq(ev));
106 ev.recycle();
107 }
108
Jason Monk3dd17a72017-09-06 14:10:39 -0400109 @Test
Jason Monk9262c942017-07-28 14:35:13 -0400110 public void testHorizontalSelection_Left() {
111 View left = mockViewAt(0, 0, 10, 10);
112 View right = mockViewAt(20, 0, 10, 10);
113
114 mNearestTouchFrame.addView(left);
115 mNearestTouchFrame.addView(right);
116 mNearestTouchFrame.onMeasure(0, 0);
117
118 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
119 12 /* x */, 5 /* y */, 0);
120 mNearestTouchFrame.onTouchEvent(ev);
121 verify(left).onTouchEvent(eq(ev));
122 ev.recycle();
123 }
124
125 @Test
126 public void testHorizontalSelection_Right() {
127 View left = mockViewAt(0, 0, 10, 10);
128 View right = mockViewAt(20, 0, 10, 10);
129
130 mNearestTouchFrame.addView(left);
131 mNearestTouchFrame.addView(right);
132 mNearestTouchFrame.onMeasure(0, 0);
133
134 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
135 18 /* x */, 5 /* y */, 0);
136 mNearestTouchFrame.onTouchEvent(ev);
137 verify(right).onTouchEvent(eq(ev));
138 ev.recycle();
139 }
140
141 @Test
142 public void testVerticalSelection_Top() {
143 View top = mockViewAt(0, 0, 10, 10);
144 View bottom = mockViewAt(0, 20, 10, 10);
145
146 mNearestTouchFrame.addView(top);
147 mNearestTouchFrame.addView(bottom);
148 mNearestTouchFrame.onMeasure(0, 0);
149
150 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
151 5 /* x */, 12 /* y */, 0);
152 mNearestTouchFrame.onTouchEvent(ev);
153 verify(top).onTouchEvent(eq(ev));
154 ev.recycle();
155 }
156
157 @Test
158 public void testVerticalSelection_Bottom() {
159 View top = mockViewAt(0, 0, 10, 10);
160 View bottom = mockViewAt(0, 20, 10, 10);
161
162 mNearestTouchFrame.addView(top);
163 mNearestTouchFrame.addView(bottom);
164 mNearestTouchFrame.onMeasure(0, 0);
165
166 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
167 5 /* x */, 18 /* y */, 0);
168 mNearestTouchFrame.onTouchEvent(ev);
169 verify(bottom).onTouchEvent(eq(ev));
170 ev.recycle();
171 }
172
Matthew Ng81818762019-01-18 11:35:54 -0800173 @Test
174 public void testViewNotAttachedNoCrash() {
175 View view = mockViewAt(0, 20, 10, 10);
176 when(view.isAttachedToWindow()).thenReturn(false);
177 mNearestTouchFrame.addView(view);
178 mNearestTouchFrame.onMeasure(0, 0);
179
180 MotionEvent ev = MotionEvent.obtain(0, 0, 0, 5 /* x */, 18 /* y */, 0);
181 mNearestTouchFrame.onTouchEvent(ev);
182 verify(view, never()).onTouchEvent(eq(ev));
183 ev.recycle();
184 }
185
Jason Monk9262c942017-07-28 14:35:13 -0400186 private View mockViewAt(int x, int y, int width, int height) {
187 View v = spy(new View(mContext));
188 doAnswer(invocation -> {
189 int[] pos = (int[]) invocation.getArguments()[0];
190 pos[0] = x;
191 pos[1] = y;
192 return null;
193 }).when(v).getLocationInWindow(any());
194 when(v.isClickable()).thenReturn(true);
Rohan Shah2adfe952018-03-05 10:00:10 -0800195 when(v.isAttachedToWindow()).thenReturn(true);
Jason Monk9262c942017-07-28 14:35:13 -0400196
197 // Stupid final methods.
198 v.setLeft(0);
199 v.setRight(width);
200 v.setTop(0);
201 v.setBottom(height);
Jason Monk9262c942017-07-28 14:35:13 -0400202 return v;
203 }
204}