Skip to content

Commit

Permalink
Merge pull request #6395 from grzesiek2010/COLLECT-6391
Browse files Browse the repository at this point in the history
Fix closing notification drawer
  • Loading branch information
seadowg authored Sep 9, 2024
2 parents e076a34 + e3ee0ca commit 35d05ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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\"")
Expand All @@ -79,31 +81,26 @@ class NotificationDrawer {
): D {
val device = waitForNotification(appName, title)
device.findObject(By.text(title)).click()
closeNotificationDrawerIfOpened()
isOpen = false

return waitFor {
destination.assertOnPage()
}
}

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"))
if (clearAll != null) {
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
}

Expand Down Expand Up @@ -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()
}
}
}
1 change: 1 addition & 0 deletions shared/src/main/java/org/odk/collect/shared/TimeInMs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 35d05ec

Please sign in to comment.