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; |
| 20 | import static org.mockito.Mockito.mock; |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 21 | import static org.mockito.Mockito.never; |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 22 | import static org.mockito.Mockito.spy; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 27 | import android.support.test.filters.SmallTest; |
| 28 | import android.testing.AndroidTestingRunner; |
| 29 | import android.testing.TestableLooper.RunWithLooper; |
| 30 | import android.view.MotionEvent; |
| 31 | import android.view.View; |
| 32 | |
| 33 | import com.android.systemui.SysuiTestCase; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | |
| 39 | @RunWith(AndroidTestingRunner.class) |
| 40 | @RunWithLooper |
| 41 | @SmallTest |
| 42 | public class NearestTouchFrameTest extends SysuiTestCase { |
| 43 | |
| 44 | private NearestTouchFrame mNearestTouchFrame; |
| 45 | |
| 46 | @Before |
| 47 | public void setup() { |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 48 | 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 Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
Jason Monk | 3dd17a7 | 2017-09-06 14:10:39 -0400 | [diff] [blame] | 74 | public void testInvisibleViews() { |
| 75 | View left = mockViewAt(0, 0, 10, 10); |
| 76 | View right = mockViewAt(20, 0, 10, 10); |
| 77 | when(left.getVisibility()).thenReturn(View.INVISIBLE); |
| 78 | |
| 79 | mNearestTouchFrame.addView(left); |
| 80 | mNearestTouchFrame.addView(right); |
| 81 | mNearestTouchFrame.onMeasure(0, 0); |
| 82 | |
| 83 | MotionEvent ev = MotionEvent.obtain(0, 0, 0, |
| 84 | 12 /* x */, 5 /* y */, 0); |
| 85 | mNearestTouchFrame.onTouchEvent(ev); |
| 86 | verify(left, never()).onTouchEvent(eq(ev)); |
| 87 | verify(right, never()).onTouchEvent(eq(ev)); |
| 88 | ev.recycle(); |
| 89 | } |
| 90 | |
| 91 | @Test |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 92 | public void testHorizontalSelection_Left() { |
| 93 | View left = mockViewAt(0, 0, 10, 10); |
| 94 | View right = mockViewAt(20, 0, 10, 10); |
| 95 | |
| 96 | mNearestTouchFrame.addView(left); |
| 97 | mNearestTouchFrame.addView(right); |
| 98 | mNearestTouchFrame.onMeasure(0, 0); |
| 99 | |
| 100 | MotionEvent ev = MotionEvent.obtain(0, 0, 0, |
| 101 | 12 /* x */, 5 /* y */, 0); |
| 102 | mNearestTouchFrame.onTouchEvent(ev); |
| 103 | verify(left).onTouchEvent(eq(ev)); |
| 104 | ev.recycle(); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testHorizontalSelection_Right() { |
| 109 | View left = mockViewAt(0, 0, 10, 10); |
| 110 | View right = mockViewAt(20, 0, 10, 10); |
| 111 | |
| 112 | mNearestTouchFrame.addView(left); |
| 113 | mNearestTouchFrame.addView(right); |
| 114 | mNearestTouchFrame.onMeasure(0, 0); |
| 115 | |
| 116 | MotionEvent ev = MotionEvent.obtain(0, 0, 0, |
| 117 | 18 /* x */, 5 /* y */, 0); |
| 118 | mNearestTouchFrame.onTouchEvent(ev); |
| 119 | verify(right).onTouchEvent(eq(ev)); |
| 120 | ev.recycle(); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testVerticalSelection_Top() { |
| 125 | View top = mockViewAt(0, 0, 10, 10); |
| 126 | View bottom = mockViewAt(0, 20, 10, 10); |
| 127 | |
| 128 | mNearestTouchFrame.addView(top); |
| 129 | mNearestTouchFrame.addView(bottom); |
| 130 | mNearestTouchFrame.onMeasure(0, 0); |
| 131 | |
| 132 | MotionEvent ev = MotionEvent.obtain(0, 0, 0, |
| 133 | 5 /* x */, 12 /* y */, 0); |
| 134 | mNearestTouchFrame.onTouchEvent(ev); |
| 135 | verify(top).onTouchEvent(eq(ev)); |
| 136 | ev.recycle(); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testVerticalSelection_Bottom() { |
| 141 | View top = mockViewAt(0, 0, 10, 10); |
| 142 | View bottom = mockViewAt(0, 20, 10, 10); |
| 143 | |
| 144 | mNearestTouchFrame.addView(top); |
| 145 | mNearestTouchFrame.addView(bottom); |
| 146 | mNearestTouchFrame.onMeasure(0, 0); |
| 147 | |
| 148 | MotionEvent ev = MotionEvent.obtain(0, 0, 0, |
| 149 | 5 /* x */, 18 /* y */, 0); |
| 150 | mNearestTouchFrame.onTouchEvent(ev); |
| 151 | verify(bottom).onTouchEvent(eq(ev)); |
| 152 | ev.recycle(); |
| 153 | } |
| 154 | |
| 155 | private View mockViewAt(int x, int y, int width, int height) { |
| 156 | View v = spy(new View(mContext)); |
| 157 | doAnswer(invocation -> { |
| 158 | int[] pos = (int[]) invocation.getArguments()[0]; |
| 159 | pos[0] = x; |
| 160 | pos[1] = y; |
| 161 | return null; |
| 162 | }).when(v).getLocationInWindow(any()); |
| 163 | when(v.isClickable()).thenReturn(true); |
| 164 | |
| 165 | // Stupid final methods. |
| 166 | v.setLeft(0); |
| 167 | v.setRight(width); |
| 168 | v.setTop(0); |
| 169 | v.setBottom(height); |
| 170 | return v; |
| 171 | } |
| 172 | } |