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 package com.android.launcher3.tapl; 17 18 import static com.android.launcher3.tapl.LauncherInstrumentation.TASKBAR_RES_ID; 19 import static com.android.launcher3.testing.shared.TestProtocol.REQUEST_DISABLE_MANUAL_TASKBAR_STASHING; 20 import static com.android.launcher3.testing.shared.TestProtocol.REQUEST_ENABLE_MANUAL_TASKBAR_STASHING; 21 22 import android.graphics.Point; 23 import android.os.SystemClock; 24 import android.text.TextUtils; 25 import android.view.MotionEvent; 26 import android.widget.TextView; 27 28 import androidx.annotation.NonNull; 29 import androidx.test.uiautomator.By; 30 import androidx.test.uiautomator.BySelector; 31 import androidx.test.uiautomator.UiObject2; 32 33 import java.util.List; 34 import java.util.stream.Collectors; 35 36 /** 37 * Operations on the Taskbar from LaunchedApp. 38 */ 39 public final class Taskbar { 40 41 private final LauncherInstrumentation mLauncher; 42 Taskbar(LauncherInstrumentation launcher)43 Taskbar(LauncherInstrumentation launcher) { 44 mLauncher = launcher; 45 } 46 47 /** 48 * Returns an app icon with the given name. This fails if the icon is not found. 49 */ 50 @NonNull getAppIcon(String appName)51 public TaskbarAppIcon getAppIcon(String appName) { 52 try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer( 53 "want to get a taskbar icon")) { 54 return new TaskbarAppIcon(mLauncher, mLauncher.waitForObjectInContainer( 55 mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID), 56 AppIcon.getAppIconSelector(appName, mLauncher))); 57 } 58 } 59 60 /** 61 * Hides this taskbar. 62 * 63 * The taskbar must already be visible when calling this method. 64 */ hide()65 public void hide() { 66 mLauncher.getTestInfo(REQUEST_ENABLE_MANUAL_TASKBAR_STASHING); 67 68 try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer( 69 "want to hide the taskbar"); 70 LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 71 mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID); 72 73 final long downTime = SystemClock.uptimeMillis(); 74 Point stashTarget = new Point( 75 mLauncher.getRealDisplaySize().x - 1, mLauncher.getRealDisplaySize().y - 1); 76 77 mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_DOWN, stashTarget, 78 LauncherInstrumentation.GestureScope.INSIDE); 79 LauncherInstrumentation.log("hideTaskbar: sent down"); 80 81 try (LauncherInstrumentation.Closable c2 = mLauncher.addContextLayer("pressed down")) { 82 mLauncher.waitUntilSystemLauncherObjectGone(TASKBAR_RES_ID); 83 mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_UP, stashTarget, 84 LauncherInstrumentation.GestureScope.INSIDE); 85 } 86 } finally { 87 mLauncher.getTestInfo(REQUEST_DISABLE_MANUAL_TASKBAR_STASHING); 88 } 89 } 90 91 /** 92 * Opens the Taskbar all apps page. 93 */ openAllApps()94 public AllAppsFromTaskbar openAllApps() { 95 try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer( 96 "want to open taskbar all apps"); 97 LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) { 98 99 mLauncher.clickLauncherObject(mLauncher.waitForObjectInContainer( 100 mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID), 101 getAllAppsButtonSelector())); 102 103 return new AllAppsFromTaskbar(mLauncher); 104 } 105 } 106 107 /** Returns a list of app icon names on the Taskbar */ getIconNames()108 public List<String> getIconNames() { 109 try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer( 110 "want to get all taskbar icons")) { 111 return mLauncher.waitForObjectsInContainer( 112 mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID), 113 AppIcon.getAnyAppIconSelector()) 114 .stream() 115 .map(UiObject2::getText) 116 .filter(text -> !TextUtils.isEmpty(text)) // Filter out the all apps button 117 .collect(Collectors.toList()); 118 } 119 } 120 getAllAppsButtonSelector()121 private static BySelector getAllAppsButtonSelector() { 122 // Look for an icon with no text 123 return By.clazz(TextView.class).text(""); 124 } 125 } 126