tests: add UI tests for onboarding activity

This commit is contained in:
Akash Yadav
2024-01-07 13:42:26 +05:30
parent 2a5bca1d6f
commit c30dabe50a
15 changed files with 728 additions and 308 deletions
+3
View File
@@ -35,6 +35,9 @@ dependencies {
api(libs.tests.androidx.uiautomator)
api(libs.tests.junit)
api(libs.tests.google.truth)
api(libs.tests.barista) {
exclude("org.jetbrains.kotlin")
}
api(projects.buildInfo)
api(projects.common)
@@ -20,6 +20,18 @@ package com.itsaky.androidide.testing.android
import android.content.Context
import androidx.annotation.StringRes
import androidx.test.core.app.ApplicationProvider
import com.itsaky.androidide.app.BaseApplication
/**
* Get the application context.
*/
fun <AppContext : Context> getApplicationContext(): AppContext =
ApplicationProvider.getApplicationContext()
/**
* Get the [BaseApplication] instance.
*/
fun getBaseApplication() = getApplicationContext<BaseApplication>()
/**
* Get the value of the string resource.
@@ -18,11 +18,11 @@
package com.itsaky.androidide.testing.android
import android.view.KeyCharacterMap
import android.view.KeyEvent
import androidx.annotation.StringRes
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject2
import com.google.common.truth.Truth.assertThat
import com.itsaky.androidide.buildinfo.BuildInfo
private val keyCharMap by lazy {
@@ -45,7 +45,7 @@ fun UiDevice.hasObjectWithText(text: String) =
* @see UiDevice.hasObject
*/
fun UiDevice.hasObjectWithText(@StringRes text: Int) =
hasObject(By.text(stringRes(text)))
hasObjectWithText(stringRes(text))
/**
* @see UiDevice.findObject
@@ -57,7 +57,16 @@ fun UiDevice.findObjectWithText(text: String): UiObject2? =
* @see UiDevice.findObjectWithText
*/
fun UiDevice.findObjectWithText(@StringRes text: Int): UiObject2? =
findObject(By.text(stringRes(text)))
findObjectWithText(stringRes(text))
fun UiDevice.getObjectWithText(text: String): UiObject2 {
val obj = findObjectWithText(text)
assertThat(obj).isNotNull()
return obj!!
}
fun UiDevice.getObjectWithText(@StringRes text: Int): UiObject2 = getObjectWithText(stringRes(text))
/**
* Sends key events to this [UiDevice] to type the given [text].
@@ -67,7 +76,6 @@ fun UiDevice.sendKeyEvents(text: String) {
val keyEvents = keyCharMap.getEvents(text.toCharArray())
for (keyEvent in keyEvents) {
uiAutomation.injectInputEvent(keyEvent, true)
// pressKeyCode(keyEvent.keyCode, keyEvent.metaState)
waitForIdle()
}
}
@@ -17,6 +17,8 @@
package com.itsaky.androidide.testing.android
import androidx.annotation.StringRes
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject2
import androidx.test.uiautomator.Until
@@ -43,4 +45,37 @@ fun UiObject2?.clickAndWaitForNewWindow(timeout: Long = LAUNCH_TIMEOUT) =
run {
assertThat(this).isNotNull()
this!!.clickAndWait(Until.newWindow(), timeout)
}
}
/**
* @see UiDevice.hasObject
*/
fun UiObject2.hasObjectWithText(text: String) =
hasObject(By.text(text))
/**
* @see UiObject2.hasObject
*/
fun UiObject2.hasObjectWithText(@StringRes text: Int) =
hasObjectWithText(stringRes(text))
/**
* @see UiObject2.findObject
*/
fun UiObject2.findObjectWithText(text: String): UiObject2? =
findObject(By.text(text))
/**
* @see UiObject2.findObjectWithText
*/
fun UiObject2.findObjectWithText(@StringRes text: Int): UiObject2? =
findObjectWithText(stringRes(text))
fun UiObject2.getObjectWithText(text: String): UiObject2 {
val obj = findObjectWithText(text)
assertThat(obj).isNotNull()
return obj!!
}
fun UiObject2.getObjectWithText(@StringRes text: Int): UiObject2 = getObjectWithText(stringRes(text))