1 /* 2 * Copyright (C) 2018 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.launcher3.tapl; 18 19 import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED; 20 21 import android.graphics.Rect; 22 23 import androidx.test.uiautomator.UiObject2; 24 25 import com.android.launcher3.testing.TestProtocol; 26 27 import java.util.regex.Pattern; 28 29 /** 30 * A recent task in the overview panel carousel. 31 */ 32 public final class OverviewTask { 33 static final Pattern TASK_START_EVENT = 34 Pattern.compile("startActivityFromRecentsAsync"); 35 private final LauncherInstrumentation mLauncher; 36 private final UiObject2 mTask; 37 private final BaseOverview mOverview; 38 OverviewTask(LauncherInstrumentation launcher, UiObject2 task, BaseOverview overview)39 OverviewTask(LauncherInstrumentation launcher, UiObject2 task, BaseOverview overview) { 40 mLauncher = launcher; 41 mTask = task; 42 mOverview = overview; 43 verifyActiveContainer(); 44 } 45 verifyActiveContainer()46 private void verifyActiveContainer() { 47 mOverview.verifyActiveContainer(); 48 } 49 50 /** 51 * Swipes the task up. 52 */ dismiss()53 public void dismiss() { 54 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck(); 55 LauncherInstrumentation.Closable c = mLauncher.addContextLayer( 56 "want to dismiss a task")) { 57 verifyActiveContainer(); 58 // Dismiss the task via flinging it up. 59 final Rect taskBounds = mLauncher.getVisibleBounds(mTask); 60 final int centerX = taskBounds.centerX(); 61 final int centerY = taskBounds.centerY(); 62 mLauncher.linearGesture(centerX, centerY, centerX, 0, 10, false, 63 LauncherInstrumentation.GestureScope.INSIDE); 64 mLauncher.waitForIdle(); 65 } 66 } 67 68 /** 69 * Clicks at the task. 70 */ open()71 public Background open() { 72 try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 73 verifyActiveContainer(); 74 mLauncher.executeAndWaitForEvent( 75 () -> mLauncher.clickLauncherObject(mTask), 76 event -> event.getEventType() == TYPE_WINDOW_STATE_CHANGED, 77 () -> "Launching task didn't open a new window: " 78 + mTask.getParent().getContentDescription(), 79 "clicking an overview task"); 80 mLauncher.expectEvent(TestProtocol.SEQUENCE_MAIN, TASK_START_EVENT); 81 return new Background(mLauncher); 82 } 83 } 84 } 85