This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Improve reliability of UI tests running on CI emulator (#872)
* Dismiss ANR in UI tests * Retry test on failure
- Loading branch information
1 parent
97e3c85
commit 628b612
Showing
13 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed | ||
plugins { | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.kotlin.android) | ||
} | ||
|
||
android { | ||
namespace = "io.element.android.wysiwyg.test" | ||
compileSdk = 34 | ||
|
||
defaultConfig { | ||
minSdk = 21 | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(11) | ||
} | ||
|
||
dependencies { | ||
implementation(libs.test.androidx.uiautomator) | ||
implementation(libs.test.junit) | ||
implementation(libs.test.androidx.espresso) | ||
} |
29 changes: 29 additions & 0 deletions
29
platforms/android/test/src/main/java/io/element/android/wysiwyg/test/rules/DismissAnrRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.element.android.wysiwyg.test.rules | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation | ||
import androidx.test.uiautomator.UiDevice | ||
import androidx.test.uiautomator.UiObject | ||
import androidx.test.uiautomator.UiSelector | ||
import org.junit.rules.TestWatcher | ||
import org.junit.runner.Description | ||
|
||
internal class DismissAnrRule : TestWatcher() { | ||
override fun starting(description: Description) { | ||
dismissAnr() | ||
} | ||
} | ||
|
||
private fun dismissAnr() { | ||
val device = UiDevice.getInstance(getInstrumentation()) | ||
val dialog = device.findAnrDialog() | ||
if (dialog.exists()) { | ||
device.findWaitButton().click() | ||
} | ||
} | ||
|
||
private fun UiDevice.findAnrDialog(): UiObject = | ||
findObject(UiSelector().textContains("isn't responding")) | ||
|
||
private fun UiDevice.findWaitButton(): UiObject = | ||
findObject(UiSelector().text("Wait").enabled(true)) | ||
.apply { waitForExists(5000) } |
11 changes: 11 additions & 0 deletions
11
...rms/android/test/src/main/java/io/element/android/wysiwyg/test/rules/FlakyEmulatorRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.element.android.wysiwyg.test.rules | ||
|
||
import org.junit.rules.RuleChain | ||
import org.junit.rules.TestRule | ||
|
||
/** | ||
* Creates a rule that helps to reduce emulator related flakiness. | ||
*/ | ||
fun createFlakyEmulatorRule(): TestRule = RuleChain | ||
.outerRule(RetryOnFailureRule()) | ||
.around(DismissAnrRule()) |
24 changes: 24 additions & 0 deletions
24
...ms/android/test/src/main/java/io/element/android/wysiwyg/test/rules/RetryOnFailureRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.element.android.wysiwyg.test.rules | ||
|
||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
internal class RetryOnFailureRule : TestRule { | ||
override fun apply( | ||
base: Statement, | ||
description: Description | ||
): Statement = | ||
RetryStatement(base) | ||
} | ||
|
||
private class RetryStatement(private val base: Statement) : Statement() { | ||
override fun evaluate() { | ||
try { | ||
base.evaluate() | ||
return | ||
} catch (t: Throwable) { | ||
base.evaluate() | ||
} | ||
} | ||
} |