From cb75a863214a247ced7c161604afcb747ac32bef Mon Sep 17 00:00:00 2001 From: Victor Andreasson Date: Sun, 17 Mar 2024 14:48:40 +0100 Subject: [PATCH] fixup! Ensure that activities are fully deleted between tests --- .../orgzly/android/espresso/NewNoteTest.java | 20 +++++-- .../orgzly/android/espresso/NoteEventsTest.kt | 44 +++++++++------ .../orgzly/android/espresso/SyncingTest.java | 56 +++++++++---------- 3 files changed, 69 insertions(+), 51 deletions(-) diff --git a/app/src/androidTest/java/com/orgzly/android/espresso/NewNoteTest.java b/app/src/androidTest/java/com/orgzly/android/espresso/NewNoteTest.java index 9e7f72c8c..577867f41 100644 --- a/app/src/androidTest/java/com/orgzly/android/espresso/NewNoteTest.java +++ b/app/src/androidTest/java/com/orgzly/android/espresso/NewNoteTest.java @@ -6,6 +6,7 @@ import com.orgzly.android.OrgzlyTest; import com.orgzly.android.ui.main.MainActivity; +import org.junit.After; import org.junit.Test; import static androidx.test.espresso.Espresso.onView; @@ -24,10 +25,19 @@ import static org.hamcrest.Matchers.endsWith; public class NewNoteTest extends OrgzlyTest { + private ActivityScenario scenario; + + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + scenario.close(); + } + @Test public void testNewNoteInEmptyNotebook() { testUtils.setupBook("notebook", ""); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onBook(0).perform(click()); @@ -43,7 +53,7 @@ public void testNewNoteInEmptyNotebook() { @Test public void testNewNoteUnder() { testUtils.setupBook("notebook", "description\n* 1\n** 2\n*** 3\n*** 4\n** 5\n* 6"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onBook(0).perform(click()); @@ -62,7 +72,7 @@ public void testNewNoteUnder() { @Test public void testNewNoteAbove() { testUtils.setupBook("notebook", "description\n* 1\n** 2\n*** 3\n*** 4\n** 5\n* 6"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onBook(0).perform(click()); @@ -86,7 +96,7 @@ public void testNewNoteBelow() { "*** 4\n" + "** 5\n" + "* 6"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onBook(0).perform(click()); @@ -115,7 +125,7 @@ public void testNewNoteBelow() { @Test public void testNewNoteAfterMovingNotesAround() { testUtils.setupBook("notebook-1", ""); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onBook(0).perform(click()); diff --git a/app/src/androidTest/java/com/orgzly/android/espresso/NoteEventsTest.kt b/app/src/androidTest/java/com/orgzly/android/espresso/NoteEventsTest.kt index f0c2e0581..3e944cda3 100644 --- a/app/src/androidTest/java/com/orgzly/android/espresso/NoteEventsTest.kt +++ b/app/src/androidTest/java/com/orgzly/android/espresso/NoteEventsTest.kt @@ -13,10 +13,12 @@ import com.orgzly.android.ui.main.MainActivity import com.orgzly.org.datetime.OrgDateTime import org.hamcrest.Matchers.not import org.hamcrest.Matchers.startsWith +import org.junit.After import org.junit.Test class NoteEventsTest : OrgzlyTest() { + private lateinit var scenario: ActivityScenario private val now: String get() = OrgDateTime(true).toString() @@ -55,10 +57,16 @@ class NoteEventsTest : OrgzlyTest() { .build() .toString() + @After + override fun tearDown() { + super.tearDown() + scenario.close() + } + @Test fun search_OneInTitle() { testUtils.setupBook("book-a", "* Note $now") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("e.ge.today") onNotesInSearch().check(matches(recyclerViewItemCount(1))) @@ -67,7 +75,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun search_OneInContent() { testUtils.setupBook("book-a", "* Note\n$now") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("e.ge.today") onNotesInSearch().check(matches(recyclerViewItemCount(1))) @@ -76,7 +84,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun search_TwoSameInContent() { testUtils.setupBook("book-a", "* Note\n$now $now") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("e.ge.today") onNotesInSearch().check(matches(recyclerViewItemCount(1))) @@ -85,7 +93,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun agenda_OneInTitle() { testUtils.setupBook("book-a", "* Note $now") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("ad.1") onNotesInAgenda().check(matches(recyclerViewItemCount(2))) @@ -94,7 +102,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun agenda_TwoInTitle() { testUtils.setupBook("book-a", "* Note $now $tomorrow") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("ad.2") onNotesInAgenda().check(matches(recyclerViewItemCount(4))) @@ -103,7 +111,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun agenda_OneInContent() { testUtils.setupBook("book-a", "* Note\n$now") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("ad.1") onNotesInAgenda().check(matches(recyclerViewItemCount(2))) @@ -112,7 +120,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun agenda_TwoInContent() { testUtils.setupBook("book-a", "* Note\n$now $tomorrow") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("ad.2") onNotesInAgenda().check(matches(recyclerViewItemCount(4))) @@ -138,7 +146,7 @@ class NoteEventsTest : OrgzlyTest() { Tomorrow: ${time(1000 * 60 * 60 * 24, hasTime = true)}" """.trimIndent()) - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("ad.5") @@ -182,7 +190,7 @@ class NoteEventsTest : OrgzlyTest() { Tomorrow: ${time(1000 * 60 * 60 * 24, hasTime = true)}" """.trimIndent()) - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("b.book-a") @@ -198,7 +206,7 @@ class NoteEventsTest : OrgzlyTest() { testUtils.setupBook( "book-a", "* Today $today\n* In few days $inFewDays\n* Today & In few days $today $inFewDays") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("e.gt.1d") onNotesInSearch().check(matches(recyclerViewItemCount(2))) @@ -207,7 +215,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun agenda_PastEvent() { testUtils.setupBook("book-a", "* Few days ago\n$fewDaysAgo") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("ad.2") onNotesInAgenda().check(matches(recyclerViewItemCount(2))) @@ -216,7 +224,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun agendaSearch_TwoWithScheduledTime() { testUtils.setupBook("book-a", "* $yesterday $fewDaysAgo\nSCHEDULED: $tomorrow") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("e.lt.now ad.3") onNotesInAgenda().check(matches(recyclerViewItemCount(4))) @@ -230,7 +238,7 @@ class NoteEventsTest : OrgzlyTest() { * Note A-01 $today $tomorrow """.trimIndent()) - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("e.today") onNotesInSearch().check(matches(recyclerViewItemCount(1))) @@ -247,7 +255,7 @@ class NoteEventsTest : OrgzlyTest() { * Note A-02 <2000-01-12> """.trimIndent()) - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("e.lt.now o.e") onNotesInSearch().check(matches(recyclerViewItemCount(2))) @@ -265,7 +273,7 @@ class NoteEventsTest : OrgzlyTest() { * Note A-02 <2000-01-12> """.trimIndent()) - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText("e.lt.now .o.e") onNotesInSearch().check(matches(recyclerViewItemCount(2))) @@ -276,7 +284,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun shiftFromList() { testUtils.setupBook("Book A", "* Note A-01 <2000-01-10 +1d>") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) onBook(0).perform(click()) onNoteInBook(1, R.id.item_head_title_view).check(matches(withText("Note A-01 <2000-01-10 +1d>"))) @@ -288,7 +296,7 @@ class NoteEventsTest : OrgzlyTest() { @Test fun shiftFromNote() { testUtils.setupBook("Book A", "* Note A-01 <2000-01-10 +1d>") - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) onBook(0).perform(click()) onNoteInBook(1).perform(click()) @@ -306,7 +314,7 @@ class NoteEventsTest : OrgzlyTest() { * Note A-01 SCHEDULED: $tomorrow """.trimIndent()) - ActivityScenario.launch(MainActivity::class.java) + scenario = ActivityScenario.launch(MainActivity::class.java) searchForText(".it.done ad.7 o.e") diff --git a/app/src/androidTest/java/com/orgzly/android/espresso/SyncingTest.java b/app/src/androidTest/java/com/orgzly/android/espresso/SyncingTest.java index 791cbfd8c..6a5444f6e 100644 --- a/app/src/androidTest/java/com/orgzly/android/espresso/SyncingTest.java +++ b/app/src/androidTest/java/com/orgzly/android/espresso/SyncingTest.java @@ -73,7 +73,7 @@ private void sync() { @Test public void testRunSync() { - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); } @@ -104,7 +104,7 @@ public void testForceLoadingBookWithLink() { public void testAutoSyncIsTriggeredAfterCreatingNote() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupRook(repo, "mock://repo-a/booky.org", "", "abc", 1234567890000L); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); // Set preference @@ -138,7 +138,7 @@ public void testAutoSyncIsTriggeredAfterCreatingNote() { public void testPrefaceModificationMakesBookOutOfSync() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupRook(repo, "mock://repo-a/booky.org", "", "abc", 1234567890000L); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); onBook(0, R.id.item_book_sync_needed_icon).check(matches(not(isDisplayed()))); @@ -158,7 +158,7 @@ public void testPrefaceModificationMakesBookOutOfSync() { @Test public void nonLinkedBookCannotBeMadeOutOfSync() { testUtils.setupBook("booky", "* Note A"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onBook(0, R.id.item_book_sync_needed_icon).check(matches(not(isDisplayed()))); @@ -176,7 +176,7 @@ public void nonLinkedBookCannotBeMadeOutOfSync() { public void testForceLoadingBookWithNoLinkNoRepos() { testUtils.setupBook("booky", "First book used for testing\n* Note A"); testUtils.setupBook("book-two", "Second book used for testing\n* Note 1\n* Note 2"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onView(allOf(withText("booky"), isDisplayed())).perform(longClick()); onView(withId(R.id.books_context_menu_force_load)).perform(click()); @@ -189,7 +189,7 @@ public void testForceLoadingBookWithNoLinkSingleRepo() { testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupBook("booky", "First book used for testing\n* Note A"); testUtils.setupBook("book-two", "Second book used for testing\n* Note 1\n* Note 2"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onView(allOf(withText("booky"), isDisplayed())).perform(longClick()); onView(withId(R.id.books_context_menu_force_load)).perform(click()); @@ -206,7 +206,7 @@ public void testForceLoadingMultipleTimes() { testUtils.setupRook(repo, "mock://repo-a/book-one.org", "New content", "abc", 1234567890000L); testUtils.setupBook("book-one", "First book used for testing\n* Note A"); testUtils.setupBook("book-two", "Second book used for testing\n* Note 1\n* Note 2"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onView(allOf(withText("book-one"), isDisplayed())).perform(longClick()); contextualToolbarOverflowMenu().perform(click()); @@ -238,7 +238,7 @@ public void testForceLoadingMultipleTimes() { public void testForceLoadingAfterModification() { testUtils.setupBook("book-one", "* Note"); testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); // Force save onView(allOf(withText("book-one"), isDisplayed())).perform(longClick()); @@ -267,7 +267,7 @@ public void testForceSavingBookWithNoLinkAndMultipleRepos() { testUtils.setupRepo(RepoType.MOCK, "mock://repo-b"); testUtils.setupBook("book-one", "First book used for testing\n* Note A"); testUtils.setupBook("book-two", "Second book used for testing\n* Note 1\n* Note 2"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onView(allOf(withText("book-one"), isDisplayed())).perform(longClick()); onView(withId(R.id.books_context_menu_force_save)).perform(click()); @@ -283,7 +283,7 @@ public void testForceSavingBookWithNoLinkAndMultipleRepos() { public void testForceSavingBookWithNoLinkNoRepos() { testUtils.setupBook("book-one", "First book used for testing\n* Note A"); testUtils.setupBook("book-two", "Second book used for testing\n* Note 1\n* Note 2"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onView(allOf(withText("book-one"), isDisplayed())).perform(longClick()); onView(withId(R.id.books_context_menu_force_save)).perform(click()); @@ -298,7 +298,7 @@ public void testForceSavingBookWithNoLinkSingleRepo() { testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupBook("book-one", "First book used for testing\n* Note A"); testUtils.setupBook("book-two", "Second book used for testing\n* Note 1\n* Note 2"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onView(allOf(withText("book-one"), isDisplayed())).perform(longClick()); onView(withId(R.id.books_context_menu_force_save)).perform(click()); @@ -313,7 +313,7 @@ public void testForceSavingBookWithNoLinkSingleRepo() { public void testForceSavingBookWithLink() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupBook("booky", "First book used for testing\n* Note A", repo); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onView(allOf(withText("booky"), isDisplayed())).perform(longClick()); onView(withId(R.id.books_context_menu_force_save)).perform(click()); @@ -329,7 +329,7 @@ public void testSyncButton() { testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupBook("book-one", "First book used for testing\n* Note A"); testUtils.setupBook("book-two", "Second book used for testing\n* Note 1\n* Note 2"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); } @@ -342,7 +342,7 @@ public void testSavingAndLoadingBookBySyncing() { "* Note #1.\n" + "* Note #2.\n" + ""); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); onView(allOf(withText("booky"), isDisplayed())).check(matches(isDisplayed())); @@ -377,7 +377,7 @@ public void testBackToModifiedBookAfterSyncing() { "** ANTIVIVISECTIONISTS Note #10.\n" + "** Note #11. DIE PERSER (Ü: Andreas Röhler) Schauspiel 1 D 3 H Stand:\n" + ""); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); onView(allOf(withText("booky"), isDisplayed())).perform(click()); @@ -417,7 +417,7 @@ public void testBookParsingAfterKeywordsSettingChange() { "** ANTIVIVISECTIONISTS Note #10.\n" + "** Note #11. DIE PERSER (Ü: Andreas Röhler) Schauspiel 1 D 3 H Stand:\n" + ""); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); @@ -454,7 +454,7 @@ public void testChangeBookLink() { Repo repoB = testUtils.setupRepo(RepoType.MOCK, "mock://repo-b"); testUtils.setupRook(repoA, "mock://repo-a/book-1.org", "Remote content for book in repo a", "abc", 1234567890); testUtils.setupRook(repoB, "mock://repo-b/book-1.org", "Remote content for book in repo b", "def", 1234567891); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); @@ -507,7 +507,7 @@ public void testSyncTwiceInARow() { testUtils.setupRook(repoB, "mock://repo-b/book-3.org", "Remote content for book 3", "def", 1234567891000L); testUtils.setupBook("book-1", "Local content for book 1"); testUtils.setupBook("book-2", "Local content for book 2", repoB); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); @@ -565,7 +565,7 @@ public void testSyncTwiceInARow() { public void testEncodingAfterSyncSaving() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupRook(repo, "mock://repo-a/book-one.org", "Täht", "1abcde", 1400067156000L); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); onBook(0, R.id.item_book_encoding_used) @@ -590,7 +590,7 @@ public void testSettingLinkToRenamedRepo() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupRook(repo, "mock://repo-a/booky.org", "Täht", "1abcde", 1400067156000L); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); onBook(0, R.id.item_book_link_repo) @@ -647,7 +647,7 @@ public void testRenamingReposRemovesLinksWhatUsedThem() { testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupRepo(RepoType.MOCK, "mock://repo-b"); testUtils.setupBook("booky", ""); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onView(allOf(withText("booky"), isDisplayed())).perform(longClick()); contextualToolbarOverflowMenu().perform(click()); @@ -680,7 +680,7 @@ public void testRenamingReposRemovesLinksWhatUsedThem() { public void testRemovingLinkFromBook() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupBook("booky", "", repo); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); onBook(0, R.id.item_book_link_repo).check(matches(allOf(withText("mock://repo-a"), isDisplayed()))); @@ -696,7 +696,7 @@ public void testRemovingLinkFromBook() { public void testSettingLinkForLoadedOrgTxtBook() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupRook(repo, "mock://repo-a/booky.org.txt", "", "1abcdef", 1400067155); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); @@ -717,7 +717,7 @@ public void testSpaceSeparatedBookName() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupRook(repo, "mock://repo-a/Book%20Name.org", "", "1abcdef", 1400067155); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); @@ -731,7 +731,7 @@ public void testSpaceSeparatedBookName() { public void testRenameModifiedBook() { testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupBook("booky", "* Note"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); @@ -766,7 +766,7 @@ public void testDeSelectRemovedNote() { "* TODO Note [a-1]\n* TODO Note [a-2]", "1520077116000", 1520077116000L); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); @@ -800,7 +800,7 @@ public void testDeSelectRemovedNote() { public void testDeleteNonExistentRemoteFile() throws IOException { testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupBook("booky", "Sample book used for tests"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync(); @@ -818,7 +818,7 @@ public void testDeleteNonExistentRemoteFile() throws IOException { public void testDeleteExistingRemoteFile() { Repo repo = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a"); testUtils.setupBook("booky", "Sample book used for tests"); - ActivityScenario.launch(MainActivity.class); + scenario = ActivityScenario.launch(MainActivity.class); sync();