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

Commit 199f1f5

Browse files
committed
Fix flakey test
1 parent 87db162 commit 199f1f5

File tree

2 files changed

+20
-60
lines changed

2 files changed

+20
-60
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
2-
// Use of this source code is governed by a BSD-style license that can be
3-
// found in the LICENSE file.
2+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
43

54
package io.flutter.plugins.quickactions;
65

@@ -163,7 +162,8 @@ private Intent getIntentToOpenMainActivity(String type) {
163162
.getLaunchIntentForPackage(packageName)
164163
.setAction(Intent.ACTION_RUN)
165164
.putExtra(EXTRA_ACTION, type)
166-
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
165+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
166+
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
167167
}
168168

169169
private static class UiThreadExecutor implements Executor {

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

+17-57
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
2-
// Use of this source code is governed by a BSD-style license that can be
3-
// found in the LICENSE file.
2+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
43

54
package io.flutter.plugins.quickactionsexample;
65

@@ -9,6 +8,7 @@
98
import static org.junit.Assert.assertTrue;
109

1110
import android.content.Context;
11+
import android.content.Intent;
1212
import android.content.pm.ShortcutInfo;
1313
import android.content.pm.ShortcutManager;
1414
import android.util.Log;
@@ -19,10 +19,6 @@
1919
import androidx.test.platform.app.InstrumentationRegistry;
2020
import androidx.test.uiautomator.By;
2121
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;
2622
import androidx.test.uiautomator.Until;
2723
import io.flutter.plugins.quickactions.QuickActionsPlugin;
2824
import java.util.ArrayList;
@@ -89,38 +85,27 @@ public void appShortcutsAreCreated() {
8985
}
9086

9187
@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-
88+
public void appShortcutLaunchActivityAfterStarting() {
11189
// Arrange
11290
List<ShortcutInfo> shortcuts = createMockShortcuts();
113-
String appName = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
11491
ShortcutInfo firstShortcut = shortcuts.get(0);
92+
ShortcutManager shortcutManager =
93+
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
94+
List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
95+
ShortcutInfo dynamicShortcut =
96+
dynamicShortcuts
97+
.stream()
98+
.filter(s -> s.getId().equals(firstShortcut.getId()))
99+
.findFirst()
100+
.get();
101+
Intent dynamicShortcutIntent = dynamicShortcut.getIntent();
115102
AtomicReference<QuickActionsTestActivity> initialActivity = new AtomicReference<>();
116103
scenario.onActivity(initialActivity::set);
104+
String appReadySentinel = " has launched";
117105

118106
// 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);
107+
context.startActivity(dynamicShortcutIntent);
108+
device.wait(Until.hasObject(By.descContains(appReadySentinel)), 2000);
124109
AtomicReference<QuickActionsTestActivity> currentActivity = new AtomicReference<>();
125110
scenario.onActivity(currentActivity::set);
126111

@@ -129,7 +114,7 @@ public void appShortcutLaunchActivityAfterPressing() throws UiObjectNotFoundExce
129114
"AppShortcut:" + firstShortcut.getId() + " does not launch the correct activity",
130115
// We can only find the shortcut type in content description while inspecting it in Ui
131116
// Automator Viewer.
132-
device.hasObject(By.desc(firstShortcut.getId())));
117+
device.hasObject(By.desc(firstShortcut.getId() + appReadySentinel)));
133118
// This is Android SingleTop behavior in which Android does not destroy the initial activity and
134119
// launch a new activity.
135120
Assert.assertEquals(initialActivity.get(), currentActivity.get());
@@ -165,29 +150,4 @@ private ActivityScenario<QuickActionsTestActivity> ensureAppRunToView() {
165150
scenario.moveToState(Lifecycle.State.STARTED);
166151
return scenario;
167152
}
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-
}
193153
}

0 commit comments

Comments
 (0)