blob: ed1491d31294824823d40afda497d9c0656a5cfa [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;
20import static org.mockito.Mockito.mock;
Jason Monke57e9dc2017-08-04 10:59:13 -040021import static org.mockito.Mockito.never;
Jason Monk9262c942017-07-28 14:35:13 -040022import static org.mockito.Mockito.spy;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
Jason Monke57e9dc2017-08-04 10:59:13 -040026import android.content.res.Configuration;
Jason Monk9262c942017-07-28 14:35:13 -040027import android.support.test.filters.SmallTest;
28import android.testing.AndroidTestingRunner;
29import android.testing.TestableLooper.RunWithLooper;
30import android.view.MotionEvent;
31import android.view.View;
32
33import com.android.systemui.SysuiTestCase;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39@RunWith(AndroidTestingRunner.class)
40@RunWithLooper
41@SmallTest
42public class NearestTouchFrameTest extends SysuiTestCase {
43
44 private NearestTouchFrame mNearestTouchFrame;
45
46 @Before
47 public void setup() {
Jason Monke57e9dc2017-08-04 10:59:13 -040048 Configuration c = new Configuration(mContext.getResources().getConfiguration());
49 c.smallestScreenWidthDp = 500;
50 mNearestTouchFrame = new NearestTouchFrame(mContext, null, c);
51 }
52
53 @Test
54 public void testNoActionOnLargeDevices() {
55 Configuration c = new Configuration(mContext.getResources().getConfiguration());
56 c.smallestScreenWidthDp = 700;
57 mNearestTouchFrame = new NearestTouchFrame(mContext, null, c);
58
59 View left = mockViewAt(0, 0, 10, 10);
60 View right = mockViewAt(20, 0, 10, 10);
61
62 mNearestTouchFrame.addView(left);
63 mNearestTouchFrame.addView(right);
64 mNearestTouchFrame.onMeasure(0, 0);
65
66 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
67 12 /* x */, 5 /* y */, 0);
68 mNearestTouchFrame.onTouchEvent(ev);
69 verify(left, never()).onTouchEvent(eq(ev));
70 ev.recycle();
Jason Monk9262c942017-07-28 14:35:13 -040071 }
72
73 @Test
74 public void testHorizontalSelection_Left() {
75 View left = mockViewAt(0, 0, 10, 10);
76 View right = mockViewAt(20, 0, 10, 10);
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).onTouchEvent(eq(ev));
86 ev.recycle();
87 }
88
89 @Test
90 public void testHorizontalSelection_Right() {
91 View left = mockViewAt(0, 0, 10, 10);
92 View right = mockViewAt(20, 0, 10, 10);
93
94 mNearestTouchFrame.addView(left);
95 mNearestTouchFrame.addView(right);
96 mNearestTouchFrame.onMeasure(0, 0);
97
98 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
99 18 /* x */, 5 /* y */, 0);
100 mNearestTouchFrame.onTouchEvent(ev);
101 verify(right).onTouchEvent(eq(ev));
102 ev.recycle();
103 }
104
105 @Test
106 public void testVerticalSelection_Top() {
107 View top = mockViewAt(0, 0, 10, 10);
108 View bottom = mockViewAt(0, 20, 10, 10);
109
110 mNearestTouchFrame.addView(top);
111 mNearestTouchFrame.addView(bottom);
112 mNearestTouchFrame.onMeasure(0, 0);
113
114 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
115 5 /* x */, 12 /* y */, 0);
116 mNearestTouchFrame.onTouchEvent(ev);
117 verify(top).onTouchEvent(eq(ev));
118 ev.recycle();
119 }
120
121 @Test
122 public void testVerticalSelection_Bottom() {
123 View top = mockViewAt(0, 0, 10, 10);
124 View bottom = mockViewAt(0, 20, 10, 10);
125
126 mNearestTouchFrame.addView(top);
127 mNearestTouchFrame.addView(bottom);
128 mNearestTouchFrame.onMeasure(0, 0);
129
130 MotionEvent ev = MotionEvent.obtain(0, 0, 0,
131 5 /* x */, 18 /* y */, 0);
132 mNearestTouchFrame.onTouchEvent(ev);
133 verify(bottom).onTouchEvent(eq(ev));
134 ev.recycle();
135 }
136
137 private View mockViewAt(int x, int y, int width, int height) {
138 View v = spy(new View(mContext));
139 doAnswer(invocation -> {
140 int[] pos = (int[]) invocation.getArguments()[0];
141 pos[0] = x;
142 pos[1] = y;
143 return null;
144 }).when(v).getLocationInWindow(any());
145 when(v.isClickable()).thenReturn(true);
146
147 // Stupid final methods.
148 v.setLeft(0);
149 v.setRight(width);
150 v.setTop(0);
151 v.setBottom(height);
152 return v;
153 }
154}