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

Commit

Permalink
Fix flakey test
Browse files Browse the repository at this point in the history
  • Loading branch information
TabooSun committed Jul 6, 2022
1 parent 87db162 commit 199f1f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package io.flutter.plugins.quickactions;

Expand Down Expand Up @@ -163,7 +162,8 @@ private Intent getIntentToOpenMainActivity(String type) {
.getLaunchIntentForPackage(packageName)
.setAction(Intent.ACTION_RUN)
.putExtra(EXTRA_ACTION, type)
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
}

private static class UiThreadExecutor implements Executor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package io.flutter.plugins.quickactionsexample;

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

import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.util.Log;
Expand All @@ -19,10 +19,6 @@
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiScrollable;
import androidx.test.uiautomator.UiSelector;
import androidx.test.uiautomator.Until;
import io.flutter.plugins.quickactions.QuickActionsPlugin;
import java.util.ArrayList;
Expand Down Expand Up @@ -89,38 +85,27 @@ public void appShortcutsAreCreated() {
}

@Test
public void appShortcutExistsAfterLongPressingAppIcon() throws UiObjectNotFoundException {
List<ShortcutInfo> shortcuts = createMockShortcuts();
String appName = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();

findAppIcon(device, appName).longClick();

for (ShortcutInfo shortcut : shortcuts) {
Assert.assertTrue(
"The specified shortcut label '" + shortcut.getShortLabel() + "' does not exist.",
device.hasObject(By.text(shortcut.getShortLabel().toString())));
}
}

@Test
public void appShortcutLaunchActivityAfterPressing() throws UiObjectNotFoundException {
Log.i(
QuickActionsTest.class.getSimpleName(),
"Start running appShortcutLaunchActivityAfterPressing test");

public void appShortcutLaunchActivityAfterStarting() {
// Arrange
List<ShortcutInfo> shortcuts = createMockShortcuts();
String appName = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
ShortcutInfo firstShortcut = shortcuts.get(0);
ShortcutManager shortcutManager =
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
ShortcutInfo dynamicShortcut =
dynamicShortcuts
.stream()
.filter(s -> s.getId().equals(firstShortcut.getId()))
.findFirst()
.get();
Intent dynamicShortcutIntent = dynamicShortcut.getIntent();
AtomicReference<QuickActionsTestActivity> initialActivity = new AtomicReference<>();
scenario.onActivity(initialActivity::set);
String appReadySentinel = " has launched";

// Act
findAppIcon(device, appName).longClick();
UiObject appShortcut =
device.findObject(new UiSelector().text(firstShortcut.getShortLabel().toString()));
appShortcut.clickAndWaitForNewWindow();
device.wait(Until.hasObject(By.descContains("On home screen")), 1000);
context.startActivity(dynamicShortcutIntent);
device.wait(Until.hasObject(By.descContains(appReadySentinel)), 2000);
AtomicReference<QuickActionsTestActivity> currentActivity = new AtomicReference<>();
scenario.onActivity(currentActivity::set);

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

private UiObject findAppIcon(UiDevice device, String appName) throws UiObjectNotFoundException {
Log.i(QuickActionsTest.class.getSimpleName(), "Find app icon, pressing home...");
boolean pressHomeResult = device.pressHome();
Log.i(QuickActionsTest.class.getSimpleName(), "Press home result: " + pressHomeResult);

// Swipe up to open App Drawer
UiScrollable homeView = new UiScrollable(new UiSelector().scrollable(true));
homeView.scrollForward();

if (!device.hasObject(By.text(appName))) {
Log.i(
QuickActionsTest.class.getSimpleName(),
"Attempting to scroll App Drawer for App Icon...");
UiScrollable appDrawer = new UiScrollable(new UiSelector().scrollable(true));
// The scrollTextIntoView scrolls to the beginning before performing searching scroll; this
// causes an issue in a scenario where the view is already in the beginning. In this case, it
// scrolls back to home view. Therefore, we perform a dummy forward scroll to ensure it is not
// in the beginning.
appDrawer.scrollForward();
appDrawer.scrollTextIntoView(appName);
}

return device.findObject(new UiSelector().text(appName));
}
}

0 comments on commit 199f1f5

Please sign in to comment.