Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some test flakiness #809

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.espresso.DataInteraction;
import androidx.test.espresso.PerformException;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.action.CloseKeyboardAction;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.espresso.util.HumanReadables;
import androidx.test.espresso.util.TreeIterables;
import androidx.test.platform.app.InstrumentationRegistry;

import com.orgzly.R;
Expand All @@ -29,6 +32,8 @@
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;

import java.util.concurrent.TimeoutException;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
Expand All @@ -40,6 +45,7 @@
import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withHint;
Expand Down Expand Up @@ -247,9 +253,15 @@ public boolean matchesSafely(View view) {
}

static ViewInteraction onSnackbar() {
onView(isRoot()).perform(waitId(com.google.android.material.R.id.snackbar_text, 5000));
return onView(withId(com.google.android.material.R.id.snackbar_text));
}

static ViewInteraction onNoteTitle() {
onView(isRoot()).perform(waitId(R.id.fragment_note_title, 5000));
return onView(withId(R.id.fragment_note_title));
}

/*
* Regular expression matching.
* https://github.com/hamcrest/JavaHamcrest/issues/65
Expand Down Expand Up @@ -443,4 +455,53 @@ public void perform(UiController uiController, View view) {
}
};
}

/**
* from https://stackoverflow.com/a/49814995/116509
*
* Perform action of waiting for a specific view id.
* @param viewId The id of the view to wait for.
* @param millis The timeout of until when to wait for.
*/
public static ViewAction waitId(final int viewId, final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}

@Override
public String getDescription() {
return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
}

@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
final Matcher<View> viewMatcher = withId(viewId);

do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
// found view with required ID
if (viewMatcher.matches(child)) {
return;
}
}

uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);

// timeout happens
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import com.orgzly.R
import com.orgzly.android.OrgzlyTest
import com.orgzly.android.espresso.EspressoUtils.*
import com.orgzly.android.espresso.EspressoUtils.clickClickableSpan
import com.orgzly.android.espresso.EspressoUtils.onBook
import com.orgzly.android.espresso.EspressoUtils.onNoteInBook
import com.orgzly.android.espresso.EspressoUtils.onNoteTitle
import com.orgzly.android.ui.main.MainActivity
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -68,21 +73,21 @@ class InternalLinksTest : OrgzlyTest() {
fun testDifferentCaseUuidInternalLink() {
onNoteInBook(1, R.id.item_head_content)
.perform(clickClickableSpan("id:bdce923b-C3CD-41ED-B58E-8BDF8BABA54F"))
onView(withId(R.id.fragment_note_title)).check(matches(withText("Note [b-2]")))
onNoteTitle().check(matches(withText("Note [b-2]")))
}

@Test
fun testDifferentCaseCustomIdInternalLink() {
onNoteInBook(2, R.id.item_head_content)
.perform(clickClickableSpan("#Different case custom id"))
onView(withId(R.id.fragment_note_title)).check(matches(withText("Note [b-1]")))
onNoteTitle().check(matches(withText("Note [b-1]")))
}

@Test
fun testCustomIdLink() {
onNoteInBook(3, R.id.item_head_content)
.perform(clickClickableSpan("#Link to note in a different book"))
onView(withId(R.id.fragment_note_title)).check(matches(withText("Note [b-3]")))
onNoteTitle().check(matches(withText("Note [b-3]")))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class NoteFragmentTest : OrgzlyTest() {
onNoteInBook(1, R.id.item_head_title).check(matches(withText("Note #1.")))

onNoteInBook(1).perform(click())
onView(withId(R.id.fragment_note_title))
onNoteTitle()
.perform(*replaceTextCloseKeyboard("Note title changed"))
onView(withId(R.id.done)).perform(click())

Expand Down Expand Up @@ -230,7 +230,7 @@ class NoteFragmentTest : OrgzlyTest() {
@Test
fun testTitleCanNotBeEmptyForExistingNote() {
onNoteInBook(1).perform(click())
onView(withId(R.id.fragment_note_title)).perform(*replaceTextCloseKeyboard(""))
onNoteTitle().perform(*replaceTextCloseKeyboard(""))
onView(withId(R.id.done)).perform(click())
onSnackbar().check(matches(withText(R.string.title_can_not_be_empty)))
}
Expand Down Expand Up @@ -502,15 +502,15 @@ class NoteFragmentTest : OrgzlyTest() {
fun testBreadcrumbsFollowToNote() {
onNoteInBook(3).perform(click())
onView(withId(R.id.fragment_note_breadcrumbs_text)).perform(clickClickableSpan("Note #2."))
onView(withId(R.id.fragment_note_title)).check(matches(withText("Note #2.")))
onNoteTitle().check(matches(withText("Note #2.")))
}

@Test
fun testBreadcrumbsPromptWhenCreatingNewNote() {
onNoteInBook(1).perform(longClick())
onView(withId(R.id.bottom_action_bar_new)).perform(click())
onView(withText(R.string.new_under)).perform(click())
onView(withId(R.id.fragment_note_title)).perform(*replaceTextCloseKeyboard("1.1"))
onNoteTitle().perform(*replaceTextCloseKeyboard("1.1"))
onView(withId(R.id.fragment_note_breadcrumbs_text)).perform(clickClickableSpan("Note #1."))

// Dialog is displayed
Expand All @@ -521,7 +521,7 @@ class NoteFragmentTest : OrgzlyTest() {
onView(withText(R.string.cancel)).perform(click())

// Title remains the same
onView(withId(R.id.fragment_note_title)).check(matches(withText("1.1")))
onNoteTitle().check(matches(withText("1.1")))
}

// https://github.com/orgzly/orgzly-android/issues/605
Expand Down