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.testing.shared.TestProtocol.NORMAL_STATE_ORDINAL;
19 
20 import androidx.annotation.NonNull;
21 import androidx.test.uiautomator.UiObject2;
22 
23 import com.android.launcher3.testing.shared.TestProtocol;
24 
25 public class HomeAllApps extends AllApps {
26 
HomeAllApps(LauncherInstrumentation launcher)27     HomeAllApps(LauncherInstrumentation launcher) {
28         super(launcher);
29     }
30 
31     /**
32      * Swipes up or down to dismiss to Workspace.
33      *
34      * @param swipeDown Swipe all apps down to dismiss, otherwise swipe up to dismiss by going home.
35      * @return the Workspace object.
36      */
37     @NonNull
switchToWorkspace(boolean swipeDown)38     public Workspace switchToWorkspace(boolean swipeDown) {
39         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
40              LauncherInstrumentation.Closable c =
41                      mLauncher.addContextLayer("want to switch from all apps to workspace")) {
42             UiObject2 allAppsContainer = verifyActiveContainer();
43 
44             final int startX = allAppsContainer.getVisibleCenter().x;
45             final int startY = swipeDown ? getTopVisibleIconBounds(allAppsContainer).centerY()
46                     : mLauncher.getDevice().getDisplayHeight();
47             final int endY =
48                     swipeDown ? mLauncher.getDevice().getDisplayHeight() : getTopVisibleIconBounds(
49                             allAppsContainer).centerY();
50             LauncherInstrumentation.log(
51                     "switchToWorkspace: startY = " + startY + ", endY = " + endY
52                             + ", slop = " + mLauncher.getTouchSlop()
53                             + ", swipeDown = " + swipeDown);
54 
55             mLauncher.swipeToState(
56                     startX,
57                     startY,
58                     startX,
59                     endY,
60                     5 /* steps */,
61                     NORMAL_STATE_ORDINAL,
62                     swipeDown ? LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER
63                             : LauncherInstrumentation.GestureScope.EXPECT_PILFER);
64 
65             try (LauncherInstrumentation.Closable c1 = mLauncher.addContextLayer(
66                     "swiped to workspace")) {
67                 return mLauncher.getWorkspace();
68             }
69         }
70     }
71 
72     @Override
getContainerType()73     protected LauncherInstrumentation.ContainerType getContainerType() {
74         return LauncherInstrumentation.ContainerType.HOME_ALL_APPS;
75     }
76 
77     @NonNull
78     @Override
getAppIcon(String appName)79     public HomeAppIcon getAppIcon(String appName) {
80         return (AllAppsAppIcon) super.getAppIcon(appName);
81     }
82 
83     @NonNull
84     @Override
createAppIcon(UiObject2 icon)85     protected HomeAppIcon createAppIcon(UiObject2 icon) {
86         return new AllAppsAppIcon(mLauncher, icon);
87     }
88 
89     @Override
hasSearchBox()90     protected boolean hasSearchBox() {
91         return true;
92     }
93 
94     @Override
getAppsListRecyclerTopPadding()95     protected int getAppsListRecyclerTopPadding() {
96         return mLauncher.getTestInfo(TestProtocol.REQUEST_ALL_APPS_TOP_PADDING)
97                 .getInt(TestProtocol.TEST_INFO_RESPONSE_FIELD);
98     }
99 
100     @NonNull
101     @Override
getQsb()102     public Qsb getQsb() {
103         return new AllAppsQsb(mLauncher, verifyActiveContainer());
104     }
105 
106     @Override
getAllAppsScroll()107     protected int getAllAppsScroll() {
108         return mLauncher.getTestInfo(TestProtocol.REQUEST_APPS_LIST_SCROLL_Y)
109                 .getInt(TestProtocol.TEST_INFO_RESPONSE_FIELD);
110     }
111 
112     @Override
verifyVisibleContainerOnDismiss()113     protected void verifyVisibleContainerOnDismiss() {
114         mLauncher.getWorkspace();
115     }
116 
117     @Override
isHomeState()118     public boolean isHomeState() {
119         return true;
120     }
121 
122     /** Send the "back" gesture to go to workspace. */
pressBackToWorkspace()123     public Workspace pressBackToWorkspace() {
124         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
125              LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
126                      "want to press back from all apps to workspace")) {
127             mLauncher.runToState(
128                     () -> mLauncher.pressBackImpl(),
129                     NORMAL_STATE_ORDINAL,
130                     "pressing back");
131             return new Workspace(mLauncher);
132         }
133     }
134 
135     @Override
touchOutside(boolean tapRight, UiObject2 container)136     protected void touchOutside(boolean tapRight, UiObject2 container) {
137         mLauncher.runToState(
138                 () -> super.touchOutside(tapRight, container),
139                 NORMAL_STATE_ORDINAL,
140                 "touching outside");
141     }
142 
143     @Override
pressMetaKey()144     protected void pressMetaKey() {
145         mLauncher.runToState(
146                 () -> super.pressMetaKey(),
147                 NORMAL_STATE_ORDINAL,
148                 "pressing meta key");
149     }
150 }
151