1- // Copyright 2013 The Flutter Authors. All rights reserved.
1+ /// Copyright 2013 The Flutter Authors. All rights reserved.
22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
99import static org .junit .Assert .assertTrue ;
1010
1111import android .content .Context ;
12+ import android .content .Intent ;
1213import android .content .pm .ShortcutInfo ;
1314import android .content .pm .ShortcutManager ;
1415import android .util .Log ;
1920import androidx .test .platform .app .InstrumentationRegistry ;
2021import androidx .test .uiautomator .By ;
2122import androidx .test .uiautomator .UiDevice ;
22- import androidx .test .uiautomator .UiObject ;
23- import androidx .test .uiautomator .UiObjectNotFoundException ;
24- import androidx .test .uiautomator .UiScrollable ;
25- import androidx .test .uiautomator .UiSelector ;
2623import androidx .test .uiautomator .Until ;
2724import io .flutter .plugins .quickactions .QuickActionsPlugin ;
2825import java .util .ArrayList ;
@@ -89,38 +86,27 @@ public void appShortcutsAreCreated() {
8986 }
9087
9188 @ Test
92- public void appShortcutExistsAfterLongPressingAppIcon () throws UiObjectNotFoundException {
93- List <ShortcutInfo > shortcuts = createMockShortcuts ();
94- String appName = context .getApplicationInfo ().loadLabel (context .getPackageManager ()).toString ();
95-
96- findAppIcon (device , appName ).longClick ();
97-
98- for (ShortcutInfo shortcut : shortcuts ) {
99- Assert .assertTrue (
100- "The specified shortcut label '" + shortcut .getShortLabel () + "' does not exist." ,
101- device .hasObject (By .text (shortcut .getShortLabel ().toString ())));
102- }
103- }
104-
105- @ Test
106- public void appShortcutLaunchActivityAfterPressing () throws UiObjectNotFoundException {
107- Log .i (
108- QuickActionsTest .class .getSimpleName (),
109- "Start running appShortcutLaunchActivityAfterPressing test" );
110-
89+ public void appShortcutLaunchActivityAfterStarting () {
11190 // Arrange
11291 List <ShortcutInfo > shortcuts = createMockShortcuts ();
113- String appName = context .getApplicationInfo ().loadLabel (context .getPackageManager ()).toString ();
11492 ShortcutInfo firstShortcut = shortcuts .get (0 );
93+ ShortcutManager shortcutManager =
94+ (ShortcutManager ) context .getSystemService (Context .SHORTCUT_SERVICE );
95+ List <ShortcutInfo > dynamicShortcuts = shortcutManager .getDynamicShortcuts ();
96+ ShortcutInfo dynamicShortcut =
97+ dynamicShortcuts
98+ .stream ()
99+ .filter (s -> s .getId ().equals (firstShortcut .getId ()))
100+ .findFirst ()
101+ .get ();
102+ Intent dynamicShortcutIntent = dynamicShortcut .getIntent ();
115103 AtomicReference <QuickActionsTestActivity > initialActivity = new AtomicReference <>();
116104 scenario .onActivity (initialActivity ::set );
105+ String appReadySentinel = " has launched" ;
117106
118107 // Act
119- findAppIcon (device , appName ).longClick ();
120- UiObject appShortcut =
121- device .findObject (new UiSelector ().text (firstShortcut .getShortLabel ().toString ()));
122- appShortcut .clickAndWaitForNewWindow ();
123- device .wait (Until .hasObject (By .descContains ("On home screen" )), 1000 );
108+ context .startActivity (dynamicShortcutIntent );
109+ device .wait (Until .hasObject (By .descContains (appReadySentinel )), 2000 );
124110 AtomicReference <QuickActionsTestActivity > currentActivity = new AtomicReference <>();
125111 scenario .onActivity (currentActivity ::set );
126112
@@ -129,7 +115,7 @@ public void appShortcutLaunchActivityAfterPressing() throws UiObjectNotFoundExce
129115 "AppShortcut:" + firstShortcut .getId () + " does not launch the correct activity" ,
130116 // We can only find the shortcut type in content description while inspecting it in Ui
131117 // Automator Viewer.
132- device .hasObject (By .desc (firstShortcut .getId ())));
118+ device .hasObject (By .desc (firstShortcut .getId () + appReadySentinel )));
133119 // This is Android SingleTop behavior in which Android does not destroy the initial activity and
134120 // launch a new activity.
135121 Assert .assertEquals (initialActivity .get (), currentActivity .get ());
@@ -165,29 +151,4 @@ private ActivityScenario<QuickActionsTestActivity> ensureAppRunToView() {
165151 scenario .moveToState (Lifecycle .State .STARTED );
166152 return scenario ;
167153 }
168-
169- private UiObject findAppIcon (UiDevice device , String appName ) throws UiObjectNotFoundException {
170- Log .i (QuickActionsTest .class .getSimpleName (), "Find app icon, pressing home..." );
171- boolean pressHomeResult = device .pressHome ();
172- Log .i (QuickActionsTest .class .getSimpleName (), "Press home result: " + pressHomeResult );
173-
174- // Swipe up to open App Drawer
175- UiScrollable homeView = new UiScrollable (new UiSelector ().scrollable (true ));
176- homeView .scrollForward ();
177-
178- if (!device .hasObject (By .text (appName ))) {
179- Log .i (
180- QuickActionsTest .class .getSimpleName (),
181- "Attempting to scroll App Drawer for App Icon..." );
182- UiScrollable appDrawer = new UiScrollable (new UiSelector ().scrollable (true ));
183- // The scrollTextIntoView scrolls to the beginning before performing searching scroll; this
184- // causes an issue in a scenario where the view is already in the beginning. In this case, it
185- // scrolls back to home view. Therefore, we perform a dummy forward scroll to ensure it is not
186- // in the beginning.
187- appDrawer .scrollForward ();
188- appDrawer .scrollTextIntoView (appName );
189- }
190-
191- return device .findObject (new UiSelector ().text (appName ));
192- }
193154}
0 commit comments