Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 116386b

Browse files
committed
Fix flakey test
1 parent 87db162 commit 116386b

3 files changed

Lines changed: 21 additions & 58 deletions

File tree

packages/quick_actions/quick_actions_android/android/src/main/java/io/flutter/plugins/quickactions/MethodCallHandlerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ private Intent getIntentToOpenMainActivity(String type) {
163163
.getLaunchIntentForPackage(packageName)
164164
.setAction(Intent.ACTION_RUN)
165165
.putExtra(EXTRA_ACTION, type)
166-
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
166+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
167+
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
167168
}
168169

169170
private static class UiThreadExecutor implements Executor {

packages/quick_actions/quick_actions_android/example/android/app/src/androidTest/java/io/flutter/plugins/quickactionsexample/QuickActionsTest.java

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

@@ -9,6 +9,7 @@
99
import static org.junit.Assert.assertTrue;
1010

1111
import android.content.Context;
12+
import android.content.Intent;
1213
import android.content.pm.ShortcutInfo;
1314
import android.content.pm.ShortcutManager;
1415
import android.util.Log;
@@ -19,10 +20,6 @@
1920
import androidx.test.platform.app.InstrumentationRegistry;
2021
import androidx.test.uiautomator.By;
2122
import 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;
2623
import androidx.test.uiautomator.Until;
2724
import io.flutter.plugins.quickactions.QuickActionsPlugin;
2825
import 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
}

packages/quick_actions/quick_actions_android/example/lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ class _MyHomePageState extends State<MyHomePage> {
4242

4343
final QuickActionsAndroid quickActions = QuickActionsAndroid();
4444
quickActions.initialize((String shortcutType) {
45+
print('Handling shortcut: $shortcutType');
4546
setState(() {
4647
if (shortcutType != null) {
47-
shortcut = shortcutType;
48+
shortcut = '$shortcutType has launched';
4849
}
4950
});
5051
});

0 commit comments

Comments
 (0)