1 /* 2 * Copyright (C) 2022 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 android.server.wm; 18 19 import static android.server.wm.CliIntentExtra.extraBool; 20 import static android.server.wm.app.Components.TestInteractiveLiveWallpaperKeys.COMPONENT; 21 import static android.server.wm.app.Components.TestInteractiveLiveWallpaperKeys.LAST_RECEIVED_MOTION_EVENT; 22 import static android.server.wm.app.Components.WALLPAPER_TARGET_ACTIVITY; 23 import static android.server.wm.app.Components.WallpaperTargetActivity.EXTRA_ENABLE_WALLPAPER_TOUCH; 24 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER; 25 26 import static org.junit.Assert.assertEquals; 27 import static org.junit.Assert.assertNotNull; 28 import static org.junit.Assert.assertThrows; 29 import static org.junit.Assume.assumeTrue; 30 31 import android.app.WallpaperManager; 32 import android.graphics.Rect; 33 import android.os.SystemClock; 34 import android.platform.test.annotations.Presubmit; 35 import android.server.wm.app.Components; 36 import android.view.InputDevice; 37 import android.view.MotionEvent; 38 39 import com.android.compatibility.common.util.PollingCheck; 40 41 import junit.framework.AssertionFailedError; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 46 47 /** 48 * Ensure moving windows and tapping is done synchronously. 49 * 50 * Build/Install/Run: 51 * atest CtsWindowManagerDeviceTestCases:WallpaperWindowInputTests 52 */ 53 @Presubmit 54 @android.server.wm.annotation.Group2 55 public class WallpaperWindowInputTests extends ActivityManagerTestBase { 56 private static final String TAG = "WallpaperWindowInputTests"; 57 58 private MotionEvent mLastMotionEvent; 59 60 @Before setup()61 public void setup() { 62 WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext); 63 assumeTrue("Device does not support wallpapers", 64 wallpaperManager.isWallpaperSupported()); 65 } 66 67 @Test testShowWallpaper_withTouchEnabled()68 public void testShowWallpaper_withTouchEnabled() { 69 final ChangeWallpaperSession wallpaperSession = createManagedChangeWallpaperSession(); 70 wallpaperSession.setWallpaperComponent(Components.TEST_INTERACTIVE_LIVE_WALLPAPER_SERVICE); 71 72 launchActivity(WALLPAPER_TARGET_ACTIVITY, extraBool(EXTRA_ENABLE_WALLPAPER_TOUCH, true)); 73 mWmState.waitAndAssertWindowShown(TYPE_WALLPAPER, true); 74 TestJournalProvider.TestJournalContainer.start(); 75 final WindowManagerState.Task task = mWmState.getTaskByActivity(WALLPAPER_TARGET_ACTIVITY); 76 MotionEvent motionEvent = getDownEventForTaskCenter(task); 77 mInstrumentation.getUiAutomation().injectInputEvent(motionEvent, true, true); 78 79 PollingCheck.waitFor(2000 /* timeout */, this::updateLastMotionEventFromTestJournal, 80 "Waiting for wallpaper to receive the touch events"); 81 82 assertNotNull(mLastMotionEvent); 83 assertMotionEvent(mLastMotionEvent, motionEvent); 84 } 85 86 @Test testShowWallpaper_withWallpaperTouchDisabled()87 public void testShowWallpaper_withWallpaperTouchDisabled() { 88 final ChangeWallpaperSession wallpaperSession = createManagedChangeWallpaperSession(); 89 wallpaperSession.setWallpaperComponent(Components.TEST_INTERACTIVE_LIVE_WALLPAPER_SERVICE); 90 91 launchActivity(WALLPAPER_TARGET_ACTIVITY, extraBool(EXTRA_ENABLE_WALLPAPER_TOUCH, false)); 92 mWmState.waitAndAssertWindowShown(TYPE_WALLPAPER, true); 93 94 TestJournalProvider.TestJournalContainer.start(); 95 final WindowManagerState.Task task = mWmState.getTaskByActivity(WALLPAPER_TARGET_ACTIVITY); 96 MotionEvent motionEvent = getDownEventForTaskCenter(task); 97 mInstrumentation.getUiAutomation().injectInputEvent(motionEvent, true, true); 98 99 final String failMsg = "Waiting for wallpaper to receive the touch events"; 100 Throwable exception = assertThrows(AssertionFailedError.class, 101 () -> PollingCheck.waitFor(2000 /* timeout */, 102 this::updateLastMotionEventFromTestJournal, 103 "Waiting for wallpaper to receive the touch events")); 104 assertEquals(failMsg, exception.getMessage()); 105 } 106 updateLastMotionEventFromTestJournal()107 private boolean updateLastMotionEventFromTestJournal() { 108 TestJournalProvider.TestJournal journal = 109 TestJournalProvider.TestJournalContainer.get(COMPONENT); 110 TestJournalProvider.TestJournalContainer.withThreadSafeAccess(() -> { 111 mLastMotionEvent = journal.extras.containsKey(LAST_RECEIVED_MOTION_EVENT) 112 ? journal.extras.getParcelable(LAST_RECEIVED_MOTION_EVENT, 113 MotionEvent.class) : null; 114 }); 115 return mLastMotionEvent != null; 116 } 117 getDownEventForTaskCenter(WindowManagerState.Task task)118 private static MotionEvent getDownEventForTaskCenter(WindowManagerState.Task task) { 119 // Get anchor coordinates on the screen 120 final Rect bounds = task.getBounds(); 121 int xOnScreen = bounds.width() / 2; 122 int yOnScreen = bounds.height() / 2; 123 final long downTime = SystemClock.uptimeMillis(); 124 125 MotionEvent eventDown = MotionEvent.obtain( 126 downTime, downTime, MotionEvent.ACTION_DOWN, xOnScreen, yOnScreen, 1); 127 eventDown.setSource(InputDevice.SOURCE_TOUCHSCREEN); 128 129 return eventDown; 130 } 131 assertMotionEvent(MotionEvent event, MotionEvent expectedEvent)132 private static void assertMotionEvent(MotionEvent event, MotionEvent expectedEvent) { 133 assertEquals(TAG + "(action)", event.getAction(), expectedEvent.getAction()); 134 assertEquals(TAG + "(source)", event.getSource(), expectedEvent.getSource()); 135 assertEquals(TAG + "(down time)", event.getDownTime(), expectedEvent.getDownTime()); 136 assertEquals(TAG + "(event time)", event.getEventTime(), expectedEvent.getEventTime()); 137 assertEquals(TAG + "(position x)", event.getX(), expectedEvent.getX(), 0.01F); 138 assertEquals(TAG + "(position y)", event.getY(), expectedEvent.getY(), 0.01F); 139 } 140 } 141