Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | package com.android.systemui.statusbar.phone; |
| 16 | |
| 17 | import static org.mockito.ArgumentMatchers.any; |
| 18 | import static org.mockito.ArgumentMatchers.eq; |
| 19 | import static org.mockito.Mockito.doAnswer; |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 20 | import static org.mockito.Mockito.never; |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 21 | import static org.mockito.Mockito.spy; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 25 | import android.content.res.Configuration; |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 26 | import android.support.test.filters.SmallTest; |
| 27 | import android.testing.AndroidTestingRunner; |
| 28 | import android.testing.TestableLooper.RunWithLooper; |
| 29 | import android.view.MotionEvent; |
| 30 | import android.view.View; |
| 31 | |
| 32 | import com.android.systemui.SysuiTestCase; |
| 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | |
| 38 | @RunWith(AndroidTestingRunner.class) |
| 39 | @RunWithLooper |
| 40 | @SmallTest |
| 41 | public class NearestTouchFrameTest extends SysuiTestCase { |
| 42 | |
| 43 | private NearestTouchFrame mNearestTouchFrame; |
| 44 | |
| 45 | @Before |
| 46 | public void setup() { |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 47 | 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 Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Test |
Jason Monk | 3dd17a7 | 2017-09-06 14:10:39 -0400 | [diff] [blame] | 73 | 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 Shah | 2adfe95 | 2018-03-05 10:00:10 -0800 | [diff] [blame] | 90 | |
| 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 Monk | 3dd17a7 | 2017-09-06 14:10:39 -0400 | [diff] [blame] | 109 | @Test |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 110 | 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 Ng | 8181876 | 2019-01-18 11:35:54 -0800 | [diff] [blame] | 173 | @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 Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 186 | 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 Shah | 2adfe95 | 2018-03-05 10:00:10 -0800 | [diff] [blame] | 195 | when(v.isAttachedToWindow()).thenReturn(true); |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 196 | |
| 197 | // Stupid final methods. |
| 198 | v.setLeft(0); |
| 199 | v.setRight(width); |
| 200 | v.setTop(0); |
| 201 | v.setBottom(height); |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 202 | return v; |
| 203 | } |
| 204 | } |