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 com.android.launcher3.tapl;
18 
19 import static com.android.launcher3.testing.shared.TestProtocol.OVERVIEW_MODAL_TASK_STATE_ORDINAL;
20 import static com.android.launcher3.testing.shared.TestProtocol.OVERVIEW_SPLIT_SELECT_ORDINAL;
21 
22 import androidx.annotation.NonNull;
23 import androidx.test.uiautomator.By;
24 import androidx.test.uiautomator.UiObject2;
25 
26 /** Represents the menu of an overview task. */
27 public class OverviewTaskMenu {
28 
29     private final LauncherInstrumentation mLauncher;
30     private final UiObject2 mMenu;
31 
OverviewTaskMenu(LauncherInstrumentation launcher)32     OverviewTaskMenu(LauncherInstrumentation launcher) {
33         mLauncher = launcher;
34 
35         mMenu = mLauncher.waitForLauncherObject("menu_option_layout");
36         mLauncher.assertTrue("The overview task menus is not visible",
37                 !mMenu.getVisibleBounds().isEmpty());
38     }
39 
40     /** Taps the split menu item from the overview task menu. */
41     @NonNull
tapSplitMenuItem()42     public SplitScreenSelect tapSplitMenuItem() {
43         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
44              LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
45                      "tap split menu item")) {
46             mLauncher.runToState(() -> mLauncher.clickLauncherObject(
47                             mLauncher.findObjectInContainer(mMenu, By.textStartsWith("Split"))),
48                     OVERVIEW_SPLIT_SELECT_ORDINAL,
49                     "tapping split menu item"
50             );
51 
52             try (LauncherInstrumentation.Closable c1 = mLauncher.addContextLayer(
53                     "tapped split menu item")) {
54                 return new SplitScreenSelect(mLauncher);
55             }
56         }
57     }
58 
59     /**
60      * Taps the app info item from the overview task menu and returns the LaunchedAppState
61      * representing the App info settings page.
62      */
63     @NonNull
tapAppInfoMenuItem()64     public LaunchedAppState tapAppInfoMenuItem() {
65         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
66              LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
67                      "before tapping the app info menu item")) {
68             mLauncher.executeAndWaitForLauncherStop(
69                     () -> mLauncher.clickLauncherObject(
70                             mLauncher.findObjectInContainer(mMenu, By.text("App info"))),
71                     "tapped app info menu item");
72 
73             try (LauncherInstrumentation.Closable c1 = mLauncher.addContextLayer(
74                     "tapped app info menu item")) {
75                 mLauncher.waitUntilSystemLauncherObjectGone("overview_panel");
76                 return new LaunchedAppState(mLauncher);
77             }
78         }
79     }
80 
81     /** Taps the select menu item from the overview task menu. */
82     @NonNull
tapSelectMenuItem()83     public SelectModeButtons tapSelectMenuItem() {
84         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
85              LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
86                      "before tapping the select menu item")) {
87 
88             mLauncher.runToState(
89                     () -> mLauncher.clickLauncherObject(
90                             mLauncher.findObjectInContainer(mMenu, By.text("Select"))),
91                     OVERVIEW_MODAL_TASK_STATE_ORDINAL, "tapping select menu item");
92 
93             try (LauncherInstrumentation.Closable c1 = mLauncher.addContextLayer(
94                     "select menu item opened")) {
95                 return new SelectModeButtons(mLauncher);
96             }
97         }
98     }
99 
100     /**
101      * Taps the Desktop item from the overview task menu and returns the LaunchedAppState
102      * representing the Desktop.
103      */
104     @NonNull
tapDesktopMenuItem()105     public LaunchedAppState tapDesktopMenuItem() {
106         try (LauncherInstrumentation.Closable ignored = mLauncher.eventsCheck();
107              LauncherInstrumentation.Closable ignored1 = mLauncher.addContextLayer(
108                      "before tapping the desktop menu item")) {
109             mLauncher.executeAndWaitForLauncherStop(
110                     () -> mLauncher.clickLauncherObject(
111                             mLauncher.findObjectInContainer(mMenu, By.text("Desktop"))),
112                     "tapped desktop menu item");
113 
114             try (LauncherInstrumentation.Closable ignored2 = mLauncher.addContextLayer(
115                     "tapped desktop menu item")) {
116                 mLauncher.waitUntilSystemLauncherObjectGone("overview_panel");
117                 mLauncher.waitForSystemUiObject("desktop_mode_caption");
118                 return new LaunchedAppState(mLauncher);
119             }
120         }
121     }
122 
123     /** Returns true if an item matching the given string is present in the menu. */
hasMenuItem(String expectedMenuItemText)124     public boolean hasMenuItem(String expectedMenuItemText) {
125         UiObject2 menuItem = mLauncher.findObjectInContainer(mMenu, By.text(expectedMenuItemText));
126         return menuItem != null;
127     }
128 
129     /**
130      * Taps outside task menu to dismiss it.
131      */
touchOutsideTaskMenuToDismiss()132     public void touchOutsideTaskMenuToDismiss() {
133         mLauncher.touchOutsideContainer(mMenu, false);
134     }
135 }
136