Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 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.pip.phone; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.mockito.ArgumentMatchers.any; |
| 21 | import static org.mockito.ArgumentMatchers.anyInt; |
| 22 | import static org.mockito.Mockito.times; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | |
| 25 | import android.app.IActivityManager; |
| 26 | import android.app.IActivityTaskManager; |
| 27 | import android.graphics.Point; |
| 28 | import android.graphics.Rect; |
| 29 | import android.testing.AndroidTestingRunner; |
| 30 | import android.testing.TestableLooper; |
| 31 | import android.util.Size; |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 32 | |
| 33 | import androidx.test.filters.SmallTest; |
| 34 | |
| 35 | import com.android.systemui.R; |
| 36 | import com.android.systemui.SysuiTestCase; |
| 37 | import com.android.systemui.pip.PipBoundsHandler; |
| 38 | import com.android.systemui.pip.PipSnapAlgorithm; |
| 39 | import com.android.systemui.pip.PipTaskOrganizer; |
| 40 | import com.android.systemui.shared.system.InputConsumerController; |
| 41 | import com.android.systemui.util.DeviceConfigProxy; |
| 42 | import com.android.systemui.util.FloatingContentCoordinator; |
| 43 | |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.Mockito; |
| 49 | import org.mockito.MockitoAnnotations; |
| 50 | |
| 51 | /** |
| 52 | * Unit tests against {@link PipTouchHandler}, including but not limited to: |
| 53 | * - Update movement bounds based on new bounds |
| 54 | * - Update movement bounds based on IME/shelf |
| 55 | * - Update movement bounds to PipResizeHandler |
| 56 | */ |
| 57 | @RunWith(AndroidTestingRunner.class) |
| 58 | @SmallTest |
| 59 | @TestableLooper.RunWithLooper(setAsMainLooper = true) |
| 60 | public class PipTouchHandlerTest extends SysuiTestCase { |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 61 | |
| 62 | private PipTouchHandler mPipTouchHandler; |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 63 | |
| 64 | @Mock |
| 65 | private IActivityManager mActivityManager; |
| 66 | |
| 67 | @Mock |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 68 | private PipMenuActivityController mPipMenuActivityController; |
| 69 | |
| 70 | @Mock |
| 71 | private InputConsumerController mInputConsumerController; |
| 72 | |
| 73 | @Mock |
| 74 | private PipBoundsHandler mPipBoundsHandler; |
| 75 | |
| 76 | @Mock |
| 77 | private PipTaskOrganizer mPipTaskOrganizer; |
| 78 | |
| 79 | @Mock |
| 80 | private FloatingContentCoordinator mFloatingContentCoordinator; |
| 81 | |
| 82 | @Mock |
| 83 | private DeviceConfigProxy mDeviceConfigProxy; |
| 84 | |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 85 | private PipSnapAlgorithm mPipSnapAlgorithm; |
| 86 | private PipMotionHelper mMotionHelper; |
| 87 | private PipResizeGestureHandler mPipResizeGestureHandler; |
| 88 | |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 89 | private Rect mInsetBounds; |
| 90 | private Rect mMinBounds; |
| 91 | private Rect mCurBounds; |
| 92 | private boolean mFromImeAdjustment; |
| 93 | private boolean mFromShelfAdjustment; |
| 94 | private int mDisplayRotation; |
| 95 | private int mImeHeight; |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 96 | |
| 97 | @Before |
| 98 | public void setUp() throws Exception { |
| 99 | MockitoAnnotations.initMocks(this); |
| 100 | mPipSnapAlgorithm = new PipSnapAlgorithm(mContext); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 101 | mPipTouchHandler = new PipTouchHandler(mContext, mActivityManager, |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 102 | mPipMenuActivityController, mInputConsumerController, mPipBoundsHandler, |
| 103 | mPipTaskOrganizer, mFloatingContentCoordinator, mDeviceConfigProxy, |
| 104 | mPipSnapAlgorithm); |
| 105 | mMotionHelper = Mockito.spy(mPipTouchHandler.getMotionHelper()); |
| 106 | mPipResizeGestureHandler = Mockito.spy(mPipTouchHandler.getPipResizeGestureHandler()); |
| 107 | mPipTouchHandler.setPipMotionHelper(mMotionHelper); |
| 108 | mPipTouchHandler.setPipResizeGestureHandler(mPipResizeGestureHandler); |
| 109 | |
| 110 | // Assume a display of 1000 x 1000 |
| 111 | // inset of 10 |
| 112 | mInsetBounds = new Rect(10, 10, 990, 990); |
| 113 | // minBounds of 100x100 bottom right corner |
| 114 | mMinBounds = new Rect(890, 890, 990, 990); |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 115 | mCurBounds = new Rect(mMinBounds); |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 116 | mFromImeAdjustment = false; |
| 117 | mFromShelfAdjustment = false; |
| 118 | mDisplayRotation = 0; |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 119 | mImeHeight = 100; |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void updateMovementBounds_minBounds() { |
| 124 | Rect expectedMinMovementBounds = new Rect(); |
| 125 | mPipSnapAlgorithm.getMovementBounds(mMinBounds, mInsetBounds, expectedMinMovementBounds, 0); |
| 126 | |
| 127 | mPipTouchHandler.onMovementBoundsChanged(mInsetBounds, mMinBounds, mCurBounds, |
| 128 | mFromImeAdjustment, mFromShelfAdjustment, mDisplayRotation); |
| 129 | |
| 130 | assertEquals(expectedMinMovementBounds, mPipTouchHandler.mNormalMovementBounds); |
| 131 | verify(mPipResizeGestureHandler, times(1)) |
| 132 | .updateMinSize(mMinBounds.width(), mMinBounds.height()); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void updateMovementBounds_maxBounds() { |
| 137 | Point displaySize = new Point(); |
| 138 | mContext.getDisplay().getRealSize(displaySize); |
| 139 | Size maxSize = mPipSnapAlgorithm.getSizeForAspectRatio(1, |
| 140 | mContext.getResources().getDimensionPixelSize( |
| 141 | R.dimen.pip_expanded_shortest_edge_size), displaySize.x, displaySize.y); |
| 142 | Rect maxBounds = new Rect(0, 0, maxSize.getWidth(), maxSize.getHeight()); |
| 143 | Rect expectedMaxMovementBounds = new Rect(); |
| 144 | mPipSnapAlgorithm.getMovementBounds(maxBounds, mInsetBounds, expectedMaxMovementBounds, 0); |
| 145 | |
| 146 | mPipTouchHandler.onMovementBoundsChanged(mInsetBounds, mMinBounds, mCurBounds, |
| 147 | mFromImeAdjustment, mFromShelfAdjustment, mDisplayRotation); |
| 148 | |
| 149 | assertEquals(expectedMaxMovementBounds, mPipTouchHandler.mExpandedMovementBounds); |
| 150 | verify(mPipResizeGestureHandler, times(1)) |
| 151 | .updateMaxSize(maxBounds.width(), maxBounds.height()); |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void updateMovementBounds_withImeAdjustment_movesPip() { |
| 156 | mFromImeAdjustment = true; |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 157 | mPipTouchHandler.onImeVisibilityChanged(true /* imeVisible */, mImeHeight); |
| 158 | |
Ben Lin | 094c775 | 2020-03-11 12:10:51 -0700 | [diff] [blame] | 159 | mPipTouchHandler.onMovementBoundsChanged(mInsetBounds, mMinBounds, mCurBounds, |
| 160 | mFromImeAdjustment, mFromShelfAdjustment, mDisplayRotation); |
| 161 | |
| 162 | verify(mMotionHelper, times(1)).animateToOffset(any(), anyInt()); |
| 163 | } |
| 164 | } |