|
4 | 4 |
|
5 | 5 | package io.flutter.plugins.quickactionsexample;
|
6 | 6 |
|
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.junit.Assert.assertFalse; |
7 | 9 | import static org.junit.Assert.assertTrue;
|
8 | 10 |
|
| 11 | +import android.content.Context; |
| 12 | +import android.content.Intent; |
| 13 | +import android.content.pm.ShortcutInfo; |
| 14 | +import android.content.pm.ShortcutManager; |
| 15 | +import android.util.Log; |
| 16 | +import androidx.lifecycle.Lifecycle; |
9 | 17 | import androidx.test.core.app.ActivityScenario;
|
| 18 | +import androidx.test.core.app.ApplicationProvider; |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 20 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 21 | +import androidx.test.uiautomator.By; |
| 22 | +import androidx.test.uiautomator.UiDevice; |
| 23 | +import androidx.test.uiautomator.Until; |
10 | 24 | import io.flutter.plugins.quickactions.QuickActionsPlugin;
|
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Before; |
11 | 31 | import org.junit.Test;
|
| 32 | +import org.junit.runner.RunWith; |
12 | 33 |
|
| 34 | +@RunWith(AndroidJUnit4.class) |
13 | 35 | public class QuickActionsTest {
|
| 36 | + private Context context; |
| 37 | + private UiDevice device; |
| 38 | + private ActivityScenario<QuickActionsTestActivity> scenario; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setUp() { |
| 42 | + context = ApplicationProvider.getApplicationContext(); |
| 43 | + device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); |
| 44 | + scenario = ensureAppRunToView(); |
| 45 | + ensureAllAppShortcutsAreCreated(); |
| 46 | + } |
| 47 | + |
| 48 | + @After |
| 49 | + public void tearDown() { |
| 50 | + scenario.close(); |
| 51 | + Log.i(QuickActionsTest.class.getSimpleName(), "Run to completion"); |
| 52 | + } |
| 53 | + |
14 | 54 | @Test
|
15 |
| - public void imagePickerPluginIsAdded() { |
| 55 | + public void quickActionPluginIsAdded() { |
16 | 56 | final ActivityScenario<QuickActionsTestActivity> scenario =
|
17 | 57 | ActivityScenario.launch(QuickActionsTestActivity.class);
|
18 | 58 | scenario.onActivity(
|
19 | 59 | activity -> {
|
20 | 60 | assertTrue(activity.engine.getPlugins().has(QuickActionsPlugin.class));
|
21 | 61 | });
|
22 | 62 | }
|
| 63 | + |
| 64 | + @Test |
| 65 | + public void appShortcutsAreCreated() { |
| 66 | + List<ShortcutInfo> expectedShortcuts = createMockShortcuts(); |
| 67 | + |
| 68 | + ShortcutManager shortcutManager = |
| 69 | + (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); |
| 70 | + List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts(); |
| 71 | + |
| 72 | + // Assert the app shortcuts defined in ../lib/main.dart. |
| 73 | + assertFalse(dynamicShortcuts.isEmpty()); |
| 74 | + assertEquals(expectedShortcuts.size(), dynamicShortcuts.size()); |
| 75 | + for (ShortcutInfo expectedShortcut : expectedShortcuts) { |
| 76 | + ShortcutInfo dynamicShortcut = |
| 77 | + dynamicShortcuts |
| 78 | + .stream() |
| 79 | + .filter(s -> s.getId().equals(expectedShortcut.getId())) |
| 80 | + .findFirst() |
| 81 | + .get(); |
| 82 | + |
| 83 | + assertEquals(expectedShortcut.getShortLabel(), dynamicShortcut.getShortLabel()); |
| 84 | + assertEquals(expectedShortcut.getLongLabel(), dynamicShortcut.getLongLabel()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void appShortcutLaunchActivityAfterStarting() { |
| 90 | + // Arrange |
| 91 | + List<ShortcutInfo> shortcuts = createMockShortcuts(); |
| 92 | + 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(); |
| 103 | + AtomicReference<QuickActionsTestActivity> initialActivity = new AtomicReference<>(); |
| 104 | + scenario.onActivity(initialActivity::set); |
| 105 | + String appReadySentinel = " has launched"; |
| 106 | + |
| 107 | + // Act |
| 108 | + context.startActivity(dynamicShortcutIntent); |
| 109 | + device.wait(Until.hasObject(By.descContains(appReadySentinel)), 2000); |
| 110 | + AtomicReference<QuickActionsTestActivity> currentActivity = new AtomicReference<>(); |
| 111 | + scenario.onActivity(currentActivity::set); |
| 112 | + |
| 113 | + // Assert |
| 114 | + Assert.assertTrue( |
| 115 | + "AppShortcut:" + firstShortcut.getId() + " does not launch the correct activity", |
| 116 | + // We can only find the shortcut type in content description while inspecting it in Ui |
| 117 | + // Automator Viewer. |
| 118 | + device.hasObject(By.desc(firstShortcut.getId() + appReadySentinel))); |
| 119 | + // This is Android SingleTop behavior in which Android does not destroy the initial activity and |
| 120 | + // launch a new activity. |
| 121 | + Assert.assertEquals(initialActivity.get(), currentActivity.get()); |
| 122 | + } |
| 123 | + |
| 124 | + private void ensureAllAppShortcutsAreCreated() { |
| 125 | + device.wait(Until.hasObject(By.text("actions ready")), 1000); |
| 126 | + } |
| 127 | + |
| 128 | + private List<ShortcutInfo> createMockShortcuts() { |
| 129 | + List<ShortcutInfo> expectedShortcuts = new ArrayList<>(); |
| 130 | + |
| 131 | + String actionOneLocalizedTitle = "Action one"; |
| 132 | + expectedShortcuts.add( |
| 133 | + new ShortcutInfo.Builder(context, "action_one") |
| 134 | + .setShortLabel(actionOneLocalizedTitle) |
| 135 | + .setLongLabel(actionOneLocalizedTitle) |
| 136 | + .build()); |
| 137 | + |
| 138 | + String actionTwoLocalizedTitle = "Action two"; |
| 139 | + expectedShortcuts.add( |
| 140 | + new ShortcutInfo.Builder(context, "action_two") |
| 141 | + .setShortLabel(actionTwoLocalizedTitle) |
| 142 | + .setLongLabel(actionTwoLocalizedTitle) |
| 143 | + .build()); |
| 144 | + |
| 145 | + return expectedShortcuts; |
| 146 | + } |
| 147 | + |
| 148 | + private ActivityScenario<QuickActionsTestActivity> ensureAppRunToView() { |
| 149 | + final ActivityScenario<QuickActionsTestActivity> scenario = |
| 150 | + ActivityScenario.launch(QuickActionsTestActivity.class); |
| 151 | + scenario.moveToState(Lifecycle.State.STARTED); |
| 152 | + return scenario; |
| 153 | + } |
23 | 154 | }
|
0 commit comments