Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.wm; |
| 18 | |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD; |
| 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 21 | import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION; |
Tadashi G. Takaoka | dee5a4d | 2018-09-19 11:59:40 +0900 | [diff] [blame] | 22 | |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 23 | import static org.junit.Assert.assertNotNull; |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 24 | import static org.junit.Assert.assertTrue; |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 25 | import static org.mockito.Mockito.any; |
| 26 | import static org.mockito.Mockito.mock; |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 27 | import static org.mockito.Mockito.spy; |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 28 | import static org.mockito.Mockito.when; |
| 29 | |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 30 | import android.content.ClipData; |
Daichi Hirono | a1fb9be | 2017-12-18 17:02:54 +0900 | [diff] [blame] | 31 | import android.graphics.PixelFormat; |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 32 | import android.os.IBinder; |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 33 | import android.os.Looper; |
| 34 | import android.os.UserHandle; |
| 35 | import android.os.UserManagerInternal; |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 36 | import android.platform.test.annotations.Presubmit; |
| 37 | import android.support.test.filters.SmallTest; |
| 38 | import android.support.test.runner.AndroidJUnit4; |
| 39 | import android.view.InputChannel; |
Daichi Hirono | a1fb9be | 2017-12-18 17:02:54 +0900 | [diff] [blame] | 40 | import android.view.SurfaceControl; |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 41 | import android.view.SurfaceSession; |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 42 | import android.view.View; |
Tadashi G. Takaoka | dee5a4d | 2018-09-19 11:59:40 +0900 | [diff] [blame] | 43 | |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 44 | import com.android.internal.annotations.GuardedBy; |
| 45 | import com.android.server.LocalServices; |
Tadashi G. Takaoka | dee5a4d | 2018-09-19 11:59:40 +0900 | [diff] [blame] | 46 | |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 47 | import org.junit.After; |
| 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 51 | |
Tadashi G. Takaoka | dee5a4d | 2018-09-19 11:59:40 +0900 | [diff] [blame] | 52 | import java.util.concurrent.CountDownLatch; |
| 53 | import java.util.concurrent.TimeUnit; |
| 54 | |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 55 | /** |
| 56 | * Tests for the {@link DragDropController} class. |
| 57 | * |
Tadashi G. Takaoka | dee5a4d | 2018-09-19 11:59:40 +0900 | [diff] [blame] | 58 | * Build/Install/Run: |
| 59 | * atest WmTests:DragDropControllerTests |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 60 | */ |
| 61 | @SmallTest |
| 62 | @RunWith(AndroidJUnit4.class) |
| 63 | @Presubmit |
| 64 | public class DragDropControllerTests extends WindowTestsBase { |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 65 | private static final int TIMEOUT_MS = 3000; |
| 66 | private TestDragDropController mTarget; |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 67 | private WindowState mWindow; |
| 68 | private IBinder mToken; |
| 69 | |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 70 | static class TestDragDropController extends DragDropController { |
| 71 | @GuardedBy("sWm.mWindowMap") |
| 72 | private Runnable mCloseCallback; |
| 73 | |
| 74 | TestDragDropController(WindowManagerService service, Looper looper) { |
| 75 | super(service, looper); |
| 76 | } |
| 77 | |
| 78 | void setOnClosedCallbackLocked(Runnable runnable) { |
| 79 | assertTrue(dragDropActiveLocked()); |
| 80 | mCloseCallback = runnable; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | void onDragStateClosedLocked(DragState dragState) { |
| 85 | super.onDragStateClosedLocked(dragState); |
| 86 | if (mCloseCallback != null) { |
| 87 | mCloseCallback.run(); |
| 88 | mCloseCallback = null; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Creates a window state which can be used as a drop target. |
| 95 | */ |
| 96 | private WindowState createDropTargetWindow(String name, int ownerId) { |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 97 | final WindowTestUtils.TestAppWindowToken token = WindowTestUtils.createTestAppWindowToken( |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 98 | mDisplayContent); |
| 99 | final TaskStack stack = createStackControllerOnStackOnDisplay( |
| 100 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, mDisplayContent).mContainer; |
| 101 | final Task task = createTaskInStack(stack, ownerId); |
| 102 | task.addChild(token, 0); |
| 103 | |
| 104 | final WindowState window = createWindow( |
| 105 | null, TYPE_BASE_APPLICATION, token, name, ownerId, false); |
| 106 | window.mInputChannel = new InputChannel(); |
| 107 | window.mHasSurface = true; |
| 108 | return window; |
| 109 | } |
| 110 | |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 111 | @Before |
| 112 | public void setUp() throws Exception { |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 113 | final UserManagerInternal userManager = mock(UserManagerInternal.class); |
| 114 | LocalServices.addService(UserManagerInternal.class, userManager); |
| 115 | |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 116 | super.setUp(); |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 117 | |
| 118 | mTarget = new TestDragDropController(sWm, sWm.mH.getLooper()); |
| 119 | mDisplayContent = spy(mDisplayContent); |
| 120 | mWindow = createDropTargetWindow("Drag test window", 0); |
| 121 | when(mDisplayContent.getTouchableWinAtPointLocked(0, 0)).thenReturn(mWindow); |
| 122 | when(sWm.mInputManager.transferTouchFocus(any(), any())).thenReturn(true); |
| 123 | |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 124 | synchronized (sWm.mWindowMap) { |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 125 | sWm.mWindowMap.put(mWindow.mClient.asBinder(), mWindow); |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | @After |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 130 | public void tearDown() throws Exception { |
| 131 | LocalServices.removeServiceForTest(UserManagerInternal.class); |
| 132 | final CountDownLatch latch; |
| 133 | synchronized (sWm.mWindowMap) { |
| 134 | if (!mTarget.dragDropActiveLocked()) { |
| 135 | return; |
| 136 | } |
| 137 | if (mToken != null) { |
| 138 | mTarget.cancelDragAndDrop(mToken); |
| 139 | } |
| 140 | latch = new CountDownLatch(1); |
| 141 | mTarget.setOnClosedCallbackLocked(() -> { |
| 142 | latch.countDown(); |
| 143 | }); |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 144 | } |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 145 | assertTrue(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Test |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 149 | public void testDragFlow() throws Exception { |
| 150 | dragFlow(0, ClipData.newPlainText("label", "Test"), 0, 0); |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 154 | public void testPerformDrag_NullDataWithGrantUri() throws Exception { |
| 155 | dragFlow(View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ, null, 0, 0); |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | public void testPerformDrag_NullDataToOtherUser() throws Exception { |
| 160 | final WindowState otherUsersWindow = |
| 161 | createDropTargetWindow("Other user's window", 1 * UserHandle.PER_USER_RANGE); |
| 162 | when(mDisplayContent.getTouchableWinAtPointLocked(10, 10)) |
| 163 | .thenReturn(otherUsersWindow); |
| 164 | |
| 165 | dragFlow(0, null, 10, 10); |
| 166 | } |
| 167 | |
| 168 | private void dragFlow(int flag, ClipData data, float dropX, float dropY) { |
Daichi Hirono | a1fb9be | 2017-12-18 17:02:54 +0900 | [diff] [blame] | 169 | final SurfaceSession appSession = new SurfaceSession(); |
| 170 | try { |
| 171 | final SurfaceControl surface = new SurfaceControl.Builder(appSession) |
| 172 | .setName("drag surface") |
| 173 | .setSize(100, 100) |
| 174 | .setFormat(PixelFormat.TRANSLUCENT) |
| 175 | .build(); |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 176 | |
Daichi Hirono | a1fb9be | 2017-12-18 17:02:54 +0900 | [diff] [blame] | 177 | assertTrue(sWm.mInputManager.transferTouchFocus(null, null)); |
Daichi Hirono | fabca09 | 2017-12-19 15:02:50 +0900 | [diff] [blame] | 178 | mToken = mTarget.performDrag( |
| 179 | new SurfaceSession(), 0, 0, mWindow.mClient, flag, surface, 0, 0, 0, 0, 0, |
| 180 | data); |
| 181 | assertNotNull(mToken); |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 182 | |
Daichi Hirono | a1fb9be | 2017-12-18 17:02:54 +0900 | [diff] [blame] | 183 | mTarget.handleMotionEvent(false, dropX, dropY); |
| 184 | mToken = mWindow.mClient.asBinder(); |
| 185 | } finally { |
| 186 | appSession.kill(); |
| 187 | } |
Daichi Hirono | da0748d | 2017-12-13 12:48:59 +0900 | [diff] [blame] | 188 | } |
Daichi Hirono | 1821c5a | 2017-12-12 17:54:12 +0900 | [diff] [blame] | 189 | } |