updating ShareDialog according to code review

This commit is contained in:
Gerardo Vázquez 2020-06-28 16:49:49 -05:00
parent 5363b09f00
commit 61df26aacd
7 changed files with 65 additions and 58 deletions

View File

@ -1,18 +1,27 @@
package de.test.antennapod.ui; package de.test.antennapod.dialogs;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.view.View; import android.view.View;
import androidx.test.espresso.Espresso; import androidx.test.espresso.Espresso;
import androidx.test.espresso.contrib.RecyclerViewActions; import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.espresso.intent.rule.IntentsTestRule; import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.fragment.AddFeedFragment; import de.danoeh.antennapod.fragment.AddFeedFragment;
import de.danoeh.antennapod.fragment.DownloadsFragment; import de.danoeh.antennapod.fragment.DownloadsFragment;
import de.danoeh.antennapod.fragment.EpisodesFragment;
import de.danoeh.antennapod.fragment.QueueFragment; import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.fragment.SubscriptionFragment; import de.danoeh.antennapod.fragment.SubscriptionFragment;
import de.test.antennapod.EspressoTestUtils; import de.test.antennapod.EspressoTestUtils;
import de.test.antennapod.ui.UITestUtils;
import org.awaitility.Awaitility;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Before; import org.junit.Before;
@ -20,6 +29,10 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.clearText; import static androidx.test.espresso.action.ViewActions.clearText;
import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.click;
@ -27,7 +40,9 @@ import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.scrollTo; import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.action.ViewActions.typeText; import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static androidx.test.espresso.matcher.RootMatchers.isDialog; import static androidx.test.espresso.matcher.RootMatchers.isDialog;
import static androidx.test.espresso.matcher.ViewMatchers.hasMinimumChildCount;
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA; import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot; import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
@ -35,6 +50,9 @@ import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription; import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText; import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static de.test.antennapod.EspressoTestUtils.clickChildViewWithId;
import static de.test.antennapod.EspressoTestUtils.onDrawerItem;
import static de.test.antennapod.EspressoTestUtils.openNavDrawer;
import static de.test.antennapod.EspressoTestUtils.waitForView; import static de.test.antennapod.EspressoTestUtils.waitForView;
import static de.test.antennapod.NthMatcher.first; import static de.test.antennapod.NthMatcher.first;
import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.allOf;
@ -44,55 +62,55 @@ import static org.hamcrest.CoreMatchers.endsWith;
* User interface tests for queue fragment. * User interface tests for queue fragment.
*/ */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class DialogsTest { public class ShareDialogTest {
@Rule @Rule
public IntentsTestRule<MainActivity> activityRule = new IntentsTestRule<>(MainActivity.class, false, false); public IntentsTestRule<MainActivity> activityRule = new IntentsTestRule<>(MainActivity.class, false, false);
private UITestUtils uiTestUtils;
protected Context context;
@Before @Before
public void setUp() throws InterruptedException { public void setUp() throws Exception {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
EspressoTestUtils.clearPreferences(); EspressoTestUtils.clearPreferences();
EspressoTestUtils.clearDatabase(); EspressoTestUtils.clearDatabase();
EspressoTestUtils.setLastNavFragment(AddFeedFragment.TAG); EspressoTestUtils.setLastNavFragment(EpisodesFragment.TAG);
uiTestUtils = new UITestUtils(context);
uiTestUtils.setup();
uiTestUtils.addLocalFeedData(true);
activityRule.launchActivity(new Intent()); activityRule.launchActivity(new Intent());
String url = "https://omny.fm/shows/silence-is-not-an-option/why-not-being-racist-is-not-enough"; openNavDrawer();
onDrawerItem(withText(R.string.episodes_label)).perform(click());
onView(isRoot()).perform(waitForView(withText(R.string.all_episodes_short_label), 1000));
onView(withText(R.string.all_episodes_short_label)).perform(click());
onView(withId(R.id.btn_add_via_url)).perform(scrollTo()).perform(click()); final List<FeedItem> episodes = DBReader.getRecentlyPublishedEpisodes(0, 10);
onView(withId(R.id.text)).perform(clearText(), typeText(url)); Matcher<View> allEpisodesMatcher = Matchers.allOf(withId(android.R.id.list), isDisplayed(), hasMinimumChildCount(2));
onView(withText(R.string.confirm_label)).inRoot(isDialog()) onView(isRoot()).perform(waitForView(allEpisodesMatcher, 1000));
.check(matches(isDisplayed())) onView(allEpisodesMatcher).perform(actionOnItemAtPosition(0, click()));
.perform(closeSoftKeyboard())
.perform(scrollTo())
.perform(click());
Thread.sleep(5000);
onView(withId(R.id.butSubscribe)).perform(click());
Thread.sleep(5000);
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
Thread.sleep(3000);
onView(first(EspressoTestUtils.actionBarOverflow())).perform(click()); onView(first(EspressoTestUtils.actionBarOverflow())).perform(click());
} }
@Test @Test
public void testShareDialogDisplayed() { public void testShareDialogDisplayed() throws InterruptedException {
onView(withText(R.string.share_label)).perform(scrollTo()).perform(click()); onView(withText(R.string.share_label)).perform(click());
onView(allOf(isDisplayed(), withText(R.string.share_episode_label))); onView(allOf(isDisplayed(), withText(R.string.share_label)));
} }
@Test @Test
public void testShareDialogShareButton() throws InterruptedException { public void testShareDialogShareButton() throws InterruptedException {
onView(withText(R.string.share_label)).perform(scrollTo()).perform(click()); onView(withText(R.string.share_label)).perform(scrollTo()).perform(click());
onView(allOf(isDisplayed(), withText(R.string.share_label))); onView(allOf(isDisplayed(), withText(R.string.share_label)));
Thread.sleep(1000);
onView(withText(R.string.share_episode_positive_label_button)).perform(scrollTo()).perform(click()); onView(withText(R.string.share_episode_positive_label_button)).perform(scrollTo()).perform(click());
Thread.sleep(2000);
} }
@Test @Test
public void testShareDialogCancelButton() { public void testShareDialogCancelButton() {
onView(withText(R.string.share_label)).perform(scrollTo()).perform(click()); onView(withText(R.string.share_label)).perform(scrollTo()).perform(click());
onView(withText(R.string.cancel_label)).check(matches(isDisplayed())); onView(withText(R.string.cancel_label)).check(matches(isDisplayed())).perform(scrollTo()).perform(click());
} }
} }

View File

@ -391,8 +391,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
break; break;
case R.id.share_item: case R.id.share_item:
if (feedItem != null) { if (feedItem != null) {
new ShareDialog(this, feedItem) new ShareDialog(this, feedItem).show(getSupportFragmentManager(), "ShareEpisodeDialog");
.show(((AppCompatActivity) this).getSupportFragmentManager(), "ShareEpisodeDialog");
} }
break; break;
default: default:

View File

@ -27,6 +27,10 @@ public class ShareDialog extends DialogFragment {
private final Context ctx; private final Context ctx;
private FeedItem item; private FeedItem item;
private static final String PREF_SHARE_EPISODE_WEBSITE = "prefShareEpisodeWebsite";
private static final String PREF_SHARE_EPISODE_MEDIA = "prefShareEpisodeMedia";
private static final String PREF_SHARE_EPISODE_START_AT = "prefShareEpisodeStartAt";
private RadioGroup radioGroup; private RadioGroup radioGroup;
private RadioButton radioEpisodeWebsite; private RadioButton radioEpisodeWebsite;
private RadioButton radioMediaFile; private RadioButton radioMediaFile;
@ -36,7 +40,7 @@ public class ShareDialog extends DialogFragment {
public ShareDialog(Context ctx, FeedItem item) { public ShareDialog(Context ctx, FeedItem item) {
this.ctx = ctx; this.ctx = ctx;
this.item = item; this.item = item;
prefs = PreferenceManager.getDefaultSharedPreferences(ctx); prefs = ctx.getSharedPreferences("SHARE_DIALOG_PREFS", Context.MODE_PRIVATE);
} }
@NonNull @NonNull
@ -44,7 +48,7 @@ public class ShareDialog extends DialogFragment {
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View content = View.inflate(ctx, R.layout.share_episode_dialog, null); View content = View.inflate(ctx, R.layout.share_episode_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx); AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(R.string.share_episode_label); builder.setTitle(R.string.share_label);
builder.setView(content); builder.setView(content);
radioGroup = content.findViewById(R.id.share_dialog_radio_group); radioGroup = content.findViewById(R.id.share_dialog_radio_group);
@ -59,25 +63,17 @@ public class ShareDialog extends DialogFragment {
@Override @Override
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
if (radioEpisodeWebsite.isChecked()) { if (radioEpisodeWebsite.isChecked()) {
if (switchStartAt.isChecked()) { ShareUtils.shareFeedItemLink(ctx, item, switchStartAt.isChecked());
ShareUtils.shareFeedItemLink(ctx, item, true); prefs.edit().putBoolean(PREF_SHARE_EPISODE_START_AT, switchStartAt.isChecked()).apply();
prefs.edit().putBoolean(UserPreferences.PREF_SHARE_EPISODE_START_AT, true).apply();
} else { prefs.edit().putBoolean(PREF_SHARE_EPISODE_WEBSITE, true).apply();
ShareUtils.shareFeedItemLink(ctx, item); prefs.edit().putBoolean(PREF_SHARE_EPISODE_MEDIA, false).apply();
prefs.edit().putBoolean(UserPreferences.PREF_SHARE_EPISODE_START_AT, false).apply();
}
prefs.edit().putBoolean(UserPreferences.PREF_SHARE_EPISODE_WEBSITE, true).apply();
prefs.edit().putBoolean(UserPreferences.PREF_SHARE_EPISODE_MEDIA, false).apply();
} else { } else {
if (switchStartAt.isChecked()) { ShareUtils.shareFeedItemLink(ctx, item, switchStartAt.isChecked());
ShareUtils.shareFeedItemDownloadLink(ctx, item, true); prefs.edit().putBoolean(PREF_SHARE_EPISODE_START_AT, switchStartAt.isChecked()).apply();
prefs.edit().putBoolean(UserPreferences.PREF_SHARE_EPISODE_START_AT, true).apply();
} else { prefs.edit().putBoolean(PREF_SHARE_EPISODE_WEBSITE, false).apply();
ShareUtils.shareFeedItemDownloadLink(ctx, item); prefs.edit().putBoolean(PREF_SHARE_EPISODE_MEDIA, true).apply();
prefs.edit().putBoolean(UserPreferences.PREF_SHARE_EPISODE_START_AT, false).apply();
}
prefs.edit().putBoolean(UserPreferences.PREF_SHARE_EPISODE_MEDIA, true).apply();
prefs.edit().putBoolean(UserPreferences.PREF_SHARE_EPISODE_WEBSITE, false).apply();
} }
} }
}) })
@ -103,10 +99,10 @@ public class ShareDialog extends DialogFragment {
} }
if (radioEpisodeWebsite.getVisibility() == View.VISIBLE && radioMediaFile.getVisibility() == View.VISIBLE) { if (radioEpisodeWebsite.getVisibility() == View.VISIBLE && radioMediaFile.getVisibility() == View.VISIBLE) {
boolean radioEpisodeWebsiteIsChecked = prefs.getBoolean(UserPreferences.PREF_SHARE_EPISODE_WEBSITE, false); boolean radioEpisodeWebsiteIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_WEBSITE, false);
radioEpisodeWebsite.setChecked(radioEpisodeWebsiteIsChecked); radioEpisodeWebsite.setChecked(radioEpisodeWebsiteIsChecked);
boolean radioMediaIsChecked = prefs.getBoolean(UserPreferences.PREF_SHARE_EPISODE_MEDIA, false); boolean radioMediaIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_MEDIA, false);
radioMediaFile.setChecked(radioMediaIsChecked); radioMediaFile.setChecked(radioMediaIsChecked);
if (!radioEpisodeWebsiteIsChecked && !radioMediaIsChecked) { if (!radioEpisodeWebsiteIsChecked && !radioMediaIsChecked) {
@ -115,7 +111,7 @@ public class ShareDialog extends DialogFragment {
} }
} }
boolean switchIsChecked = prefs.getBoolean(UserPreferences.PREF_SHARE_EPISODE_START_AT, false); boolean switchIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_START_AT, false);
switchStartAt.setChecked(switchIsChecked); switchStartAt.setChecked(switchIsChecked);
} }
} }

View File

@ -237,7 +237,7 @@ public class FeedItemMenuHandler {
break; break;
case R.id.share_item: case R.id.share_item:
new ShareDialog(context, selectedItem) new ShareDialog(context, selectedItem)
.show(((AppCompatActivity) context).getSupportFragmentManager(), "ShareEpisodeDialog"); .show((fragment.getActivity().getSupportFragmentManager()), "ShareEpisodeDialog");
break; break;
default: default:
Log.d(TAG, "Unknown menuItemId: " + menuItemId); Log.d(TAG, "Unknown menuItemId: " + menuItemId);

View File

@ -35,6 +35,6 @@
android:id="@+id/share_start_at_timer_dialog" android:id="@+id/share_start_at_timer_dialog"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/share_start_at_timer_dialog_label" /> android:text="@string/share_playback_position_dialog_label" />
</LinearLayout> </LinearLayout>

View File

@ -112,11 +112,6 @@ public class UserPreferences {
public static final String PREF_IMAGE_CACHE_SIZE = "prefImageCacheSize"; public static final String PREF_IMAGE_CACHE_SIZE = "prefImageCacheSize";
public static final String PREF_DELETE_REMOVES_FROM_QUEUE = "prefDeleteRemovesFromQueue"; public static final String PREF_DELETE_REMOVES_FROM_QUEUE = "prefDeleteRemovesFromQueue";
// Dialogs
public static final String PREF_SHARE_EPISODE_WEBSITE = "prefShareEpisodeWebsite";
public static final String PREF_SHARE_EPISODE_MEDIA = "prefShareEpisodeMedia";
public static final String PREF_SHARE_EPISODE_START_AT = "prefShareEpisodeStartAt";
// Mediaplayer // Mediaplayer
public static final String PREF_MEDIA_PLAYER = "prefMediaPlayer"; public static final String PREF_MEDIA_PLAYER = "prefMediaPlayer";
public static final String PREF_MEDIA_PLAYER_EXOPLAYER = "exoplayer"; public static final String PREF_MEDIA_PLAYER_EXOPLAYER = "exoplayer";

View File

@ -766,10 +766,9 @@
<string name="rating_now_label">Sure, let\'s do this!</string> <string name="rating_now_label">Sure, let\'s do this!</string>
<!-- Share episode dialog --> <!-- Share episode dialog -->
<string name="share_episode_label">Share…</string>
<string name="share_episode_positive_label_button">Share</string> <string name="share_episode_positive_label_button">Share</string>
<string name="share_dialog_include_label">Include:</string> <string name="share_dialog_include_label">Include:</string>
<string name="share_start_at_timer_dialog_label">Start at (timer)</string> <string name="share_playback_position_dialog_label">Playback position</string>
<string name="share_dialog_media_file_label">Media file URL</string> <string name="share_dialog_media_file_label">Media file URL</string>
<string name="share_dialog_episode_website_label">Episode website</string> <string name="share_dialog_episode_website_label">Episode website</string>