Skip to content

Commit

Permalink
Add retry test rule and tidy up more between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amberin committed Apr 3, 2024
1 parent 1cc6818 commit 5b4de55
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/src/androidTest/java/com/orgzly/android/OrgzlyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public void setUp() throws Exception {
// localStorage.cleanup();

setupPreferences();

dataRepository.clearDatabase();
}

@After
public void tearDown() throws Exception {
restorePreferences();

database.close();

dataRepository.clearDatabase();
}

private void setupPreferences() {
Expand Down
42 changes: 42 additions & 0 deletions app/src/androidTest/java/com/orgzly/android/RetryTestRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.orgzly.android

import android.util.Log
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

/**
* Retry test rule used to retry test that failed. Retry failed test 3 times
*/
class RetryTestRule(val retryCount: Int = 3) : TestRule {

private val TAG = RetryTestRule::class.java.simpleName

override fun apply(base: Statement, description: Description): Statement {
return statement(base, description)
}

private fun statement(base: Statement, description: Description): Statement {
return object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
var caughtThrowable: Throwable? = null

for (i in 0 until retryCount) {
try {
base.evaluate()
return
} catch (t: Throwable) {
caughtThrowable = t
Log.e(TAG, description.displayName + ": run " + (i + 1) + " failed")
val sleep = 1000 * (i + 1).toLong() // Increase sleep time with each failed attempt
Thread.sleep(sleep)
}
}

Log.e(TAG, description.displayName + ": giving up after " + retryCount + " failures")
throw caughtThrowable!!
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import com.orgzly.R
import com.orgzly.android.App
import com.orgzly.android.OrgzlyTest
import com.orgzly.android.espresso.util.EspressoUtils.*
import com.orgzly.android.RetryTestRule
import com.orgzly.android.espresso.util.EspressoUtils.clickClickableSpan
import com.orgzly.android.espresso.util.EspressoUtils.onBook
import com.orgzly.android.espresso.util.EspressoUtils.onNoteInBook
import com.orgzly.android.espresso.util.EspressoUtils.onSnackbar
import com.orgzly.android.ui.main.MainActivity
import org.hamcrest.Matchers.startsWith
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.junit.runners.Parameterized


@RunWith(value = Parameterized::class)
class ExternalLinksTest(private val param: Parameter) : OrgzlyTest() {

@Rule
@JvmField
val mRetryTestRule: TestRule = RetryTestRule()

data class Parameter(val link: String, val check: () -> Any)

companion object {
Expand Down Expand Up @@ -53,14 +63,14 @@ class ExternalLinksTest(private val param: Parameter) : OrgzlyTest() {
fun testLink() {
testUtils.setupBook("book", "* Note\n${param.link}")

ActivityScenario.launch(MainActivity::class.java)

// Open book
onBook(0).perform(click())
ActivityScenario.launch(MainActivity::class.java).use {
// Open book
onBook(0).perform(click())

// Click on link
onNoteInBook(1, R.id.item_head_content_view).perform(clickClickableSpan(param.link))
// Click on link
onNoteInBook(1, R.id.item_head_content_view).perform(clickClickableSpan(param.link))

param.check()
param.check()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.orgzly.android.OrgzlyTest
import com.orgzly.android.espresso.util.EspressoUtils.*
import com.orgzly.android.ui.main.MainActivity
import org.hamcrest.Matchers.*
import org.junit.After
import org.junit.Before
import org.junit.Test

Expand Down Expand Up @@ -66,6 +67,12 @@ class NoteFragmentTest : OrgzlyTest() {
onBook(0).perform(click())
}

@After
override fun tearDown() {
super.tearDown()
scenario.close()
}

@Test
fun testDeleteNote() {
onNoteInBook(1).perform(click())
Expand Down

0 comments on commit 5b4de55

Please sign in to comment.