• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.Rect;
24 import android.os.SystemClock;
25 import android.text.TextUtils;
26 import android.view.MotionEvent;
27 import android.widget.TextView;
28 
29 import androidx.annotation.NonNull;
30 import androidx.test.uiautomator.By;
31 import androidx.test.uiautomator.BySelector;
32 import androidx.test.uiautomator.UiObject2;
33 
34 import java.util.List;
35 import java.util.stream.Collectors;
36 
37 /**
38  * Operations on the Taskbar from LaunchedApp.
39  */
40 public final class Taskbar {
41 
42     private final LauncherInstrumentation mLauncher;
43 
Taskbar(LauncherInstrumentation launcher)44     Taskbar(LauncherInstrumentation launcher) {
45         mLauncher = launcher;
46     }
47 
48     /**
49      * Returns an app icon with the given name. This fails if the icon is not found.
50      */
51     @NonNull
getAppIcon(String appName)52     public TaskbarAppIcon getAppIcon(String appName) {
53         try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
54                 "want to get a taskbar icon")) {
55             return new TaskbarAppIcon(mLauncher, mLauncher.waitForObjectInContainer(
56                     mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID),
57                     AppIcon.getAppIconSelector(appName, mLauncher)));
58         }
59     }
60 
61     /**
62      * Hides this taskbar.
63      *
64      * The taskbar must already be visible when calling this method.
65      */
hide()66     public void hide() {
67         mLauncher.getTestInfo(REQUEST_ENABLE_MANUAL_TASKBAR_STASHING);
68 
69         try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
70                 "want to hide the taskbar");
71              LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) {
72             mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID);
73 
74             final long downTime = SystemClock.uptimeMillis();
75             Point stashTarget = new Point(
76                     mLauncher.getRealDisplaySize().x - 1, mLauncher.getRealDisplaySize().y - 1);
77 
78             mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_DOWN, stashTarget,
79                     LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
80             LauncherInstrumentation.log("hideTaskbar: sent down");
81 
82             try (LauncherInstrumentation.Closable c2 = mLauncher.addContextLayer("pressed down")) {
83                 mLauncher.waitUntilSystemLauncherObjectGone(TASKBAR_RES_ID);
84                 mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_UP, stashTarget,
85                         LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
86             }
87         } finally {
88             mLauncher.getTestInfo(REQUEST_DISABLE_MANUAL_TASKBAR_STASHING);
89         }
90     }
91 
92     /**
93      * Opens the Taskbar all apps page.
94      */
openAllApps()95     public AllAppsFromTaskbar openAllApps() {
96         try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
97                 "want to open taskbar all apps");
98              LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) {
99 
100             mLauncher.clickLauncherObject(mLauncher.waitForObjectInContainer(
101                     mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID),
102                     getAllAppsButtonSelector()));
103 
104             return new AllAppsFromTaskbar(mLauncher);
105         }
106     }
107 
108     /** Returns a list of app icon names on the Taskbar */
getIconNames()109     public List<String> getIconNames() {
110         try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
111                 "want to get all taskbar icons")) {
112             return mLauncher.waitForObjectsInContainer(
113                     mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID),
114                     AppIcon.getAnyAppIconSelector())
115                     .stream()
116                     .map(UiObject2::getText)
117                     .filter(text -> !TextUtils.isEmpty(text)) // Filter out the all apps button
118                     .collect(Collectors.toList());
119         }
120     }
121 
getAllAppsButtonSelector()122     private static BySelector getAllAppsButtonSelector() {
123         // Look for an icon with no text
124         return By.clazz(TextView.class).text("");
125     }
126 
getVisibleBounds()127     private Rect getVisibleBounds() {
128         return mLauncher.waitForSystemLauncherObject(TASKBAR_RES_ID).getVisibleBounds();
129     }
130 
131     /**
132      * Touch either on the right or the left corner of the screen, 1 pixel from the bottom and
133      * from the sides.
134      */
touchBottomCorner(boolean tapRight)135     void touchBottomCorner(boolean tapRight) {
136         try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
137                 "want to tap bottom corner on the " + (tapRight ? "right" : "left"))) {
138             final long downTime = SystemClock.uptimeMillis();
139             final Point tapTarget = new Point(
140                     tapRight
141                             ?
142                             getVisibleBounds().right
143                                     - mLauncher.getTargetInsets().right
144                                     - 1
145                             : getVisibleBounds().left
146                                     + 1,
147                     mLauncher.getRealDisplaySize().y - 1);
148 
149             mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_DOWN, tapTarget,
150                     LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
151             mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_UP, tapTarget,
152                     LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
153         }
154     }
155 }
156