Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent issues with assertions on collapsed notifications #5617

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package org.odk.collect.android.support.pages
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject2
import androidx.test.uiautomator.Until
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.not
import org.hamcrest.CoreMatchers.nullValue
import org.hamcrest.MatcherAssert.assertThat
import org.odk.collect.android.support.WaitFor.waitFor

Expand Down Expand Up @@ -34,15 +37,14 @@ class NotificationDrawer {
val device = waitForNotification(appName)

val titleElement = device.findObject(By.text(title))
assertThat(titleElement.text, `is`(title))
assertThat(titleElement, not(nullValue()))

body?.let {
val bodyElement = device.findObject(By.text(body))
assertThat(bodyElement.text, `is`(body))
assertThat(bodyElement, not(nullValue()))
}

val subtextElement = device.findObject(By.text(subtext))
assertThat(subtextElement.text, `is`(subtext))
assertExpandedText(device, appName, subtext)

return this
}
Expand All @@ -54,15 +56,14 @@ class NotificationDrawer {
): D {
val device = waitForNotification(appName)

var actionElement = device.findObject(By.text(actionText)) ?: device.findObject(By.text(actionText.uppercase()))
if (actionElement == null) {
device.findObject(By.text(appName)).click() // Expand notification to show actions
actionElement = device.findObject(By.text(actionText)) ?: device.findObject(By.text(actionText.uppercase()))
val actionElement = getExpandedElement(device, appName, actionText) ?: getExpandedElement(device, appName, actionText.uppercase())
if (actionElement != null) {
actionElement.click()
isOpen = false
} else {
throw AssertionError("Could not find \"$actionText\"")
}

actionElement.click()
isOpen = false

return waitFor {
destination.assertOnPage()
}
Expand All @@ -74,8 +75,7 @@ class NotificationDrawer {
destination: D
): D {
val device = waitForNotification(appName)
val titleElement = device.findObject(By.text(title))
assertThat(titleElement.text, `is`(title))
val titleElement = assertText(device, title)
titleElement.click()
isOpen = false

Expand Down Expand Up @@ -105,6 +105,41 @@ class NotificationDrawer {
isOpen = false
}

private fun assertText(device: UiDevice, text: String): UiObject2 {
val element = device.findObject(By.text(text))
if (element != null) {
return element
} else {
throw AssertionError("Could not find \"$text\"")
}
}

private fun assertExpandedText(
device: UiDevice,
appName: String,
text: String
) {
val element = getExpandedElement(device, appName, text)
assertThat("Could not find \"$text\"", element, not(nullValue()))
}

private fun getExpandedElement(
device: UiDevice,
appName: String,
text: String
): UiObject2? {
var element = device.findObject(By.text(text))
if (element == null) {
expandOrCollapseNotification(device, appName)
element = device.findObject(By.text(text))
}
return element
}

private fun expandOrCollapseNotification(device: UiDevice, appName: String) {
device.findObject(By.text(appName)).click()
}

private fun waitForNotification(appName: String): UiDevice {
return waitFor {
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
Expand Down