diff --git a/collect_app/src/androidTest/java/org/odk/collect/android/support/pages/NotificationDrawer.kt b/collect_app/src/androidTest/java/org/odk/collect/android/support/pages/NotificationDrawer.kt index e98ac7a7277..cd5ed68dcbe 100644 --- a/collect_app/src/androidTest/java/org/odk/collect/android/support/pages/NotificationDrawer.kt +++ b/collect_app/src/androidTest/java/org/odk/collect/android/support/pages/NotificationDrawer.kt @@ -10,6 +10,7 @@ import org.hamcrest.CoreMatchers.`is` import org.hamcrest.CoreMatchers.not import org.hamcrest.CoreMatchers.nullValue import org.hamcrest.MatcherAssert.assertThat +import org.odk.collect.shared.TimeInMs import org.odk.collect.testshared.WaitFor.waitFor class NotificationDrawer { @@ -59,6 +60,7 @@ class NotificationDrawer { val actionElement = getExpandedElement(device, appName, actionText) ?: getExpandedElement(device, appName, actionText.uppercase()) if (actionElement != null) { actionElement.click() + closeNotificationDrawerIfOpened() isOpen = false } else { throw AssertionError("Could not find \"$actionText\"") @@ -79,6 +81,7 @@ class NotificationDrawer { ): D { val device = waitForNotification(appName, title) device.findObject(By.text(title)).click() + closeNotificationDrawerIfOpened() isOpen = false return waitFor { @@ -86,12 +89,6 @@ class NotificationDrawer { } } - fun pressBack() { - val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - device.pressBack() - isOpen = false - } - fun clearAll() { val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) val clearAll = device.findObject(By.text("Clear all")) @@ -99,11 +96,11 @@ class NotificationDrawer { clearAll.click() } else { // "Clear all" doesn't exist because there are notifications to clear - just press back - pressBack() + device.pressBack() } - device.wait(Until.gone(By.text("Notifications")), 1000L) - + device.wait(Until.gone(By.text("No notifications")), 1000L) + closeNotificationDrawerIfOpened() isOpen = false } @@ -154,4 +151,16 @@ class NotificationDrawer { device } } + + /** + * It appears that sometimes the notification drawer does not close automatically when it should + * such as after clicking on a notification or its action. This could be due to a bug in Android. + */ + private fun closeNotificationDrawerIfOpened() { + val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + val isManageButtonGone = device.wait(Until.gone(By.text("Manage")), TimeInMs.THREE_SECONDS) + if (!isManageButtonGone) { + device.pressBack() + } + } } diff --git a/shared/src/main/java/org/odk/collect/shared/TimeInMs.kt b/shared/src/main/java/org/odk/collect/shared/TimeInMs.kt index f157b5fa00c..d64b50d6712 100644 --- a/shared/src/main/java/org/odk/collect/shared/TimeInMs.kt +++ b/shared/src/main/java/org/odk/collect/shared/TimeInMs.kt @@ -2,6 +2,7 @@ package org.odk.collect.shared object TimeInMs { const val ONE_SECOND = 1000L + const val THREE_SECONDS = 3000L const val ONE_MINUTE = 60000L const val ONE_HOUR = 3600000L const val ONE_DAY = 86400000L