|
| 1 | +package com.example.InstabugSample; |
| 2 | + |
| 3 | +import androidx.test.espresso.action.ViewActions; |
| 4 | +import androidx.test.espresso.flutter.action.FlutterActions; |
| 5 | +import androidx.test.espresso.flutter.assertion.FlutterAssertions; |
| 6 | +import androidx.test.espresso.flutter.matcher.FlutterMatchers; |
| 7 | +import androidx.test.espresso.matcher.ViewMatchers; |
| 8 | +import androidx.test.rule.ActivityTestRule; |
| 9 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 10 | +import androidx.test.uiautomator.UiDevice; |
| 11 | +import androidx.test.uiautomator.UiObject; |
| 12 | +import androidx.test.uiautomator.UiObjectNotFoundException; |
| 13 | +import androidx.test.uiautomator.UiSelector; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Rule; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.runner.RunWith; |
| 19 | + |
| 20 | +import static androidx.test.espresso.Espresso.onView; |
| 21 | +import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist; |
| 22 | +import static androidx.test.espresso.assertion.ViewAssertions.matches; |
| 23 | +import static androidx.test.espresso.flutter.EspressoFlutter.onFlutterWidget; |
| 24 | +import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 25 | + |
| 26 | +import static com.example.InstabugSample.util.InstabugAssertions.assertViewWillBeVisible; |
| 27 | +import static com.example.InstabugSample.util.InstabugViewMatchers.isToTheLeft; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertTrue; |
| 30 | + |
| 31 | +import android.app.Instrumentation; |
| 32 | +import android.graphics.Point; |
| 33 | + |
| 34 | +import com.example.InstabugSample.util.Keyboard; |
| 35 | + |
| 36 | +@RunWith(AndroidJUnit4.class) |
| 37 | +public class BugReportingUITest { |
| 38 | + Instrumentation instrumentation = getInstrumentation(); |
| 39 | + UiDevice device = UiDevice.getInstance(instrumentation); |
| 40 | + |
| 41 | + @Rule |
| 42 | + public ActivityTestRule<MainActivity> mActivityRule = |
| 43 | + new ActivityTestRule<>(MainActivity.class); |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() { |
| 47 | + onFlutterWidget(FlutterMatchers.withText("Restart Instabug")) |
| 48 | + .perform(FlutterActions.click()); |
| 49 | + } |
| 50 | + |
| 51 | + private void assertOptionsPromptIsVisible() { |
| 52 | + assertViewWillBeVisible("instabug_main_prompt_container", 2000); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void floatingButtonInvocationEvent() { |
| 57 | + onFlutterWidget(FlutterMatchers.withText("Floating Button")).perform(FlutterActions.click()); |
| 58 | + onView(ViewMatchers.withResourceName("instabug_floating_button")).perform(ViewActions.click()); |
| 59 | + |
| 60 | + assertOptionsPromptIsVisible(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void twoFingersSwipeLeftInvocationEvent() throws InterruptedException { |
| 65 | + onFlutterWidget(FlutterMatchers.withText("Two Fingers Swipe Left")) |
| 66 | + .perform(FlutterActions.click()); |
| 67 | + |
| 68 | + // Two-fingers swipe left |
| 69 | + UiObject text = device.findObject(new UiSelector().textContains("Hello")); |
| 70 | + int width = device.getDisplayWidth(); |
| 71 | + text.performTwoPointerGesture( |
| 72 | + new Point(width - 50, 100), |
| 73 | + new Point(width - 50, 130), |
| 74 | + new Point(50, 100), |
| 75 | + new Point(50, 130), |
| 76 | + // Small steps number for fast swiping |
| 77 | + 20 |
| 78 | + ); |
| 79 | + |
| 80 | + Thread.sleep(1000); |
| 81 | + |
| 82 | + assertOptionsPromptIsVisible(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void noneInvocationEvent() { |
| 87 | + onFlutterWidget(FlutterMatchers.withText("None")).perform(FlutterActions.click()); |
| 88 | + |
| 89 | + onView(ViewMatchers.withResourceName("instabug_floating_button")).check(doesNotExist()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void manualInvocation() { |
| 94 | + onFlutterWidget(FlutterMatchers.withText("Invoke")).perform(FlutterActions.click()); |
| 95 | + |
| 96 | + assertOptionsPromptIsVisible(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void multipleScreenshotsInReproSteps() throws InterruptedException, UiObjectNotFoundException { |
| 101 | + String screen = "My Screen"; |
| 102 | + |
| 103 | + onFlutterWidget(FlutterMatchers.withText("Enter screen name")) |
| 104 | + .perform(FlutterActions.scrollTo(), FlutterActions.typeText(screen)); |
| 105 | + Thread.sleep(1000); |
| 106 | + Keyboard.closeKeyboard(); |
| 107 | + onFlutterWidget(FlutterMatchers.withText("Report Screen Change")) |
| 108 | + .perform(FlutterActions.scrollTo(), FlutterActions.click(), FlutterActions.click()); |
| 109 | + onFlutterWidget(FlutterMatchers.withText("Send Bug Report")) |
| 110 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 111 | + UiObject reproStepsDisclaimer = assertViewWillBeVisible("instabug_text_view_repro_steps_disclaimer", 2000); |
| 112 | + reproStepsDisclaimer.click(); |
| 113 | + |
| 114 | + String screenshotsListId = "instabug_vus_list"; |
| 115 | + assertViewWillBeVisible(screenshotsListId, 5000); |
| 116 | + onView(ViewMatchers.withResourceName(screenshotsListId)) |
| 117 | + .check(matches(ViewMatchers.hasChildCount(2))); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void onDismissCallbackIsCalled() throws InterruptedException { |
| 122 | + onFlutterWidget(FlutterMatchers.withText("Set On Dismiss Callback")) |
| 123 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 124 | + |
| 125 | + onFlutterWidget(FlutterMatchers.withText("Invoke")) |
| 126 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 127 | + Thread.sleep(1000); |
| 128 | + device.pressBack(); |
| 129 | + |
| 130 | + Thread.sleep(1000); |
| 131 | + |
| 132 | + onFlutterWidget(FlutterMatchers.withText("onDismiss callback called with DismissType.cancel and ReportType.other")) |
| 133 | + .check(FlutterAssertions.matches(FlutterMatchers.isExisting())); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void changeReportTypes() throws UiObjectNotFoundException { |
| 138 | + onFlutterWidget(FlutterMatchers.withText("Bug")) |
| 139 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 140 | + onFlutterWidget(FlutterMatchers.withText("Invoke")) |
| 141 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 142 | + |
| 143 | + // Shows bug reporting screen |
| 144 | + assertViewWillBeVisible("instabug_edit_text_message", 2000); |
| 145 | + |
| 146 | + // Close bug reporting screen |
| 147 | + device.pressBack(); |
| 148 | + device.findObject(new UiSelector().text("DISCARD")).click(); |
| 149 | + |
| 150 | + // Enable feedback reports |
| 151 | + onFlutterWidget(FlutterMatchers.withText("Feedback")) |
| 152 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 153 | + onFlutterWidget(FlutterMatchers.withText("Invoke")) |
| 154 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 155 | + |
| 156 | + // Shows both bug reporting and feature requests in prompt options |
| 157 | + assertOptionsPromptIsVisible(); |
| 158 | + onView(ViewMatchers.withText("Report a bug")) |
| 159 | + .check(matches(ViewMatchers.isDisplayed())); |
| 160 | + onView(ViewMatchers.withText("Suggest an improvement")) |
| 161 | + .check(matches(ViewMatchers.isDisplayed())); |
| 162 | + onView(ViewMatchers.withText("Ask a question")) |
| 163 | + .check(doesNotExist()); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void changeFloatingButtonEdge() { |
| 168 | + onFlutterWidget(FlutterMatchers.withText("Floating Button")) |
| 169 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 170 | + onFlutterWidget(FlutterMatchers.withText("Move Floating Button to Left")) |
| 171 | + .perform(FlutterActions.scrollTo(), FlutterActions.click()); |
| 172 | + onView(ViewMatchers.withResourceName("instabug_floating_button")) |
| 173 | + .check(matches(isToTheLeft())); |
| 174 | + } |
| 175 | +} |
| 176 | + |
0 commit comments