Skip to content

Commit

Permalink
Apply "retry rule" in MiscTest and NoteFragmentTest
Browse files Browse the repository at this point in the history
MiscTest.fragmentTest() is insanely flaky, and it already has sleeps in many
places since way back. Sometimes those sleeps just aren't enough, but
increasing them would mean slowing everything down a lot. A retry rule
is the only working solution I have found.

NoteFragmentTest.testContentLineCountUpdatedOnNoteUpdate() is also
just too damn flaky, and I cannot understand where the problem is. It
just seems that, sometimes, onRecyclerViewItem() will not work, and
waiting for the right ID does not help.
  • Loading branch information
amberin committed Apr 11, 2024
1 parent 463b377 commit 1f09837
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
41 changes: 41 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,41 @@
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

// implement retry logic here
for (i in 0 until retryCount) {
try {
base.evaluate()
return
} catch (t: Throwable) {
caughtThrowable = t
Log.e(TAG, description.displayName + ": run " + (i + 1) + " failed")
}
}

Log.e(TAG, description.displayName + ": giving up after " + retryCount + " failures")
throw caughtThrowable!!
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,26 @@
import com.orgzly.BuildConfig;
import com.orgzly.R;
import com.orgzly.android.OrgzlyTest;
import com.orgzly.android.RetryTestRule;
import com.orgzly.android.db.entity.NotePosition;
import com.orgzly.android.repos.RepoType;
import com.orgzly.android.ui.main.MainActivity;
import com.orgzly.android.ui.repos.ReposActivity;

import org.hamcrest.Matcher;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class MiscTest extends OrgzlyTest {

@Rule
public TestRule mRetryTestRule = new RetryTestRule();

@Test
public void testLftRgt() {
testUtils.setupBook("booky", "Preface\n* Note 1\n** Note 2\n* Note 3\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ import androidx.test.espresso.matcher.RootMatchers.isDialog
import androidx.test.espresso.matcher.ViewMatchers.*
import com.orgzly.R
import com.orgzly.android.OrgzlyTest
import com.orgzly.android.RetryTestRule
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.Rule
import org.junit.Test

class NoteFragmentTest : OrgzlyTest() {
private lateinit var scenario: ActivityScenario<MainActivity>

@Rule
@JvmField
val mRetryTestRule = RetryTestRule()

@Before
@Throws(Exception::class)
override fun setUp() {
Expand Down Expand Up @@ -492,6 +498,7 @@ class NoteFragmentTest : OrgzlyTest() {
onView(withId(R.id.content)).perform(click())
onView(withId(R.id.content_edit)).perform(typeTextIntoFocusedView("a\nb\nc"))
onView(withId(R.id.done)).perform(click()) // Note done
SystemClock.sleep(500)
onNoteInBook(1, R.id.item_head_fold_button).perform(click())
onNoteInBook(1, R.id.item_head_title_view).check(matches(withText(endsWith("3"))))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
* - replaceText() is preferred over typeText() as it is much faster.
*/
public class EspressoUtils {


public static ViewInteraction onListView() {
return onView(allOf(isAssignableFrom(ListView.class), isDisplayed()));
}
Expand Down Expand Up @@ -499,4 +501,4 @@ public void perform(final UiController uiController, final View view) {
}
};
}
}
}

0 comments on commit 1f09837

Please sign in to comment.