From ebfda200e0a74946a95d418b01b8fc38dda3f7cb Mon Sep 17 00:00:00 2001 From: Patrick Demers Date: Sat, 24 Dec 2022 10:07:43 -0600 Subject: [PATCH] Refresh Feed after Credentials Change (#6236) --- .../test/antennapod/ui/FeedSettingsTest.java | 56 +++++++++++++++++++ .../de/test/antennapod/ui/UITestUtils.java | 2 +- .../fragment/FeedSettingsFragment.java | 16 +++++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/app/src/androidTest/java/de/test/antennapod/ui/FeedSettingsTest.java b/app/src/androidTest/java/de/test/antennapod/ui/FeedSettingsTest.java index bae12936f..f3cd99b2c 100644 --- a/app/src/androidTest/java/de/test/antennapod/ui/FeedSettingsTest.java +++ b/app/src/androidTest/java/de/test/antennapod/ui/FeedSettingsTest.java @@ -6,7 +6,9 @@ import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.ext.junit.runners.AndroidJUnit4; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; +import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.model.feed.Feed; +import de.danoeh.antennapod.model.feed.FeedPreferences; import de.test.antennapod.EspressoTestUtils; import org.junit.After; import org.junit.Before; @@ -16,6 +18,8 @@ import org.junit.runner.RunWith; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; +import static androidx.test.espresso.action.ViewActions.pressBack; +import static androidx.test.espresso.action.ViewActions.typeText; import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; import static androidx.test.espresso.matcher.ViewMatchers.isRoot; @@ -24,6 +28,10 @@ import static androidx.test.espresso.matcher.ViewMatchers.withText; import static de.test.antennapod.EspressoTestUtils.clickPreference; import static de.test.antennapod.EspressoTestUtils.waitForView; import static org.hamcrest.Matchers.allOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; @RunWith(AndroidJUnit4.class) public class FeedSettingsTest { @@ -76,4 +84,52 @@ public class FeedSettingsTest { clickPreference(R.string.feed_volume_reduction); onView(withText(R.string.cancel_label)).perform(click()); } + + /** + * Test that modifying a feed's authentication settings results in proper behavior. + * Expect: + * - Feed is refreshed automatically + * - Database has updated username and password + */ + @Test + public void testAuthenticationSettingsUpdate() throws IOException { + onView(isRoot()).perform(waitForView(allOf(isDescendantOfA(withId(R.id.appBar)), + withText(feed.getTitle()), isDisplayed()), 1000)); + + String updatedTitle = "modified episode title"; + String username = "username"; + String password = "password"; + + // update feed hosted on server + feed.getItems().get(0).setTitle(updatedTitle); + uiTestUtils.hostFeed(feed); + + // interact with UI to update authentication settings + updateAuthenticationSettings(username, password); + + // expect feed to have refreshed and be showing new episode title + onView(isRoot()).perform(waitForView(withText(updatedTitle), 5000)); + + // expect database to be updated with correct username and password + Feed updatedFeed = DBReader.getFeed(feed.getId()); + assertNotNull(updatedFeed); + + FeedPreferences updatedFeedPreferences = updatedFeed.getPreferences(); + assertNotNull(updatedFeedPreferences); + + assertEquals("database updated with username", username, updatedFeedPreferences.getUsername()); + assertEquals("database updated with password", password, updatedFeedPreferences.getPassword()); + } + + private void updateAuthenticationSettings(String username, String password) { + onView(withId(R.id.butShowSettings)).perform(click()); + + clickPreference(R.string.authentication_label); + onView(withId(R.id.usernameEditText)).perform(typeText(username)); + onView(withId(R.id.passwordEditText)).perform(typeText(password)); + onView(withText(R.string.confirm_label)).perform(click()); + + onView(isRoot()).perform(pressBack()); + } + } diff --git a/app/src/androidTest/java/de/test/antennapod/ui/UITestUtils.java b/app/src/androidTest/java/de/test/antennapod/ui/UITestUtils.java index 44a11d9cf..c03480fc6 100644 --- a/app/src/androidTest/java/de/test/antennapod/ui/UITestUtils.java +++ b/app/src/androidTest/java/de/test/antennapod/ui/UITestUtils.java @@ -78,7 +78,7 @@ public class UITestUtils { } } - private String hostFeed(Feed feed) throws IOException { + public String hostFeed(Feed feed) throws IOException { File feedFile = new File(hostedFeedDir, feed.getTitle()); FileOutputStream out = new FileOutputStream(feedFile); Rss2Generator generator = new Rss2Generator(); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java index 1fe4f8ee3..47c2e4dcd 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java @@ -16,6 +16,7 @@ import androidx.preference.PreferenceFragmentCompat; import androidx.preference.SwitchPreferenceCompat; import androidx.recyclerview.widget.RecyclerView; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.event.settings.SkipIntroEndingChangedEvent; import de.danoeh.antennapod.event.settings.SpeedPresetChangedEvent; import de.danoeh.antennapod.event.settings.VolumeAdaptionChangedEvent; @@ -36,10 +37,13 @@ import io.reactivex.MaybeOnSubscribe; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; + import org.greenrobot.eventbus.EventBus; import java.util.Collections; import java.util.Locale; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; public class FeedSettingsFragment extends Fragment { private static final String TAG = "FeedSettingsFragment"; @@ -255,7 +259,17 @@ public class FeedSettingsFragment extends Fragment { protected void onConfirmed(String username, String password) { feedPreferences.setUsername(username); feedPreferences.setPassword(password); - DBWriter.setFeedPreferences(feedPreferences); + Future setPreferencesFuture = DBWriter.setFeedPreferences(feedPreferences); + + new Thread(() -> { + try { + setPreferencesFuture.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + + DBTasks.forceRefreshFeed(requireContext(), feed, true); + }, "RefreshAfterCredentialChange").start(); } }.show(); return false;