0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 03:52:15 +02:00

fix: close App not responding dialog to reduce test flakiness

This commit is contained in:
kuroahna 2023-12-05 15:18:53 -05:00 committed by Mike Hardy
parent 218dafef30
commit 72d162ddb7
3 changed files with 24 additions and 0 deletions

View File

@ -398,6 +398,7 @@ dependencies {
androidTestImplementation "androidx.test:core:$androidx_test_version"
androidTestImplementation "androidx.test.ext:junit:$androidx_test_junit_version"
androidTestImplementation "androidx.test:rules:$androidx_test_version"
androidTestImplementation "androidx.test.uiautomator:uiautomator:$uiautomator_version"
androidTestImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
androidTestImplementation("androidx.fragment:fragment-testing:$fragments_version")

View File

@ -20,6 +20,8 @@ package com.ichi2.anki.tests
import android.content.Context
import android.os.Build
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import com.ichi2.anki.CollectionHelper
import com.ichi2.anki.utils.EnsureAllFilesAccessRule
import com.ichi2.annotations.DuplicatedCode
@ -27,6 +29,7 @@ import com.ichi2.libanki.Card
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Note
import org.junit.Before
import org.junit.Rule
import java.io.File
import java.io.IOException
@ -80,6 +83,25 @@ abstract class InstrumentedTest {
}
}
@Before
fun runBeforeEachTest() {
closeAndroidNotRespondingDialog()
}
// Instrumented tests can fail if there's a "App not responding"
// System dialog blocking our test from proceeding
//
// See: https://stackoverflow.com/questions/39457305/android-testing-waited-for-the-root-of-the-view-hierarchy-to-have-window-focus/54203607#54203607
private fun closeAndroidNotRespondingDialog() {
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
var waitButton = device.findObject(UiSelector().textContains("Wait"))
// There may be multiple dialogs
while (waitButton.exists()) {
waitButton.click()
waitButton = device.findObject(UiSelector().textContains("Wait"))
}
}
@DuplicatedCode("This is copied from RobolectricTest. This will be refactored into a shared library later")
protected fun Card.moveToReviewQueue() {
this.queue = Consts.QUEUE_TYPE_REV

View File

@ -20,6 +20,7 @@ buildscript {
ext.robolectric_version = '4.11.1'
ext.android_gradle_plugin = "8.1.4"
ext.dokka_version = "1.9.10" // not the same with kotlin version!
ext.uiautomator_version = "2.3.0-alpha05"
configurations.configureEach {
resolutionStrategy.eachDependency { details ->