diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/files/details/FileDetailsFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/files/details/FileDetailsFragmentTest.kt index 03d1816b732f..8201a2bca29f 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/files/details/FileDetailsFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/files/details/FileDetailsFragmentTest.kt @@ -20,6 +20,7 @@ import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_SPACE import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_WITHOUT_PERSONAL_SPACE import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AVAILABLE_OFFLINE import com.owncloud.android.utils.DisplayUtils +import com.owncloud.android.utils.RetryFlakyTestUntilSuccessRule import com.owncloud.android.utils.matchers.assertVisibility import com.owncloud.android.utils.matchers.isDisplayed import com.owncloud.android.utils.matchers.withDrawable @@ -29,6 +30,7 @@ import io.mockk.mockk import kotlinx.coroutines.flow.MutableStateFlow import org.junit.Before import org.junit.Ignore +import org.junit.Rule import org.junit.Test import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.core.context.startKoin @@ -47,6 +49,10 @@ class FileDetailsFragmentTest { private var currentFileSyncInfo: MutableStateFlow = MutableStateFlow(OC_FILE_WITH_SYNC_INFO) private var currentFileAvailableOffline: MutableStateFlow = MutableStateFlow(OC_FILE_WITH_SYNC_INFO_AVAILABLE_OFFLINE) + @Rule + @JvmField + val retryFlakyTestUntilSuccessRule = RetryFlakyTestUntilSuccessRule() + @Before fun setUp() { context = ApplicationProvider.getApplicationContext() diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/utils/RetryFlakyTestUnilSuccessRule.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/utils/RetryFlakyTestUnilSuccessRule.kt new file mode 100644 index 000000000000..ae56ab5f51cb --- /dev/null +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/utils/RetryFlakyTestUnilSuccessRule.kt @@ -0,0 +1,37 @@ +package com.owncloud.android.utils + +import android.util.Log +import org.junit.rules.TestRule +import org.junit.runner.Description +import org.junit.runners.model.Statement + +class RetryFlakyTestUntilSuccessRule(val count: Int = 3) : TestRule { + + companion object { + private const val TAG = "RetryFlakyTestUntilSuccessRule" + } + + override fun apply(base: Statement, description: Description): Statement = statement(base, description) + + private fun statement(base: Statement, description: Description): Statement { + return object : Statement() { + @Throws(Throwable::class) + override fun evaluate() { + var throwable: Throwable? = null + val displayName = description.displayName + for (i in 1 until count + 1) { + try { + Log.i(TAG, "$displayName: Run $i") + base.evaluate() + return + } catch (t: Throwable) { + throwable = t + Log.e(TAG, "$displayName: Run $i failed.") + } + } + Log.e(TAG, "$displayName: Giving up after run $count failures.") + throw throwable!! + } + } + } +} \ No newline at end of file