Merge pull request #3396 from ByteHamster/crash-keep-empty-queue-sorted

Fixed crash when clicking keep sorted on empty queue
This commit is contained in:
H. Lehmann 2019-09-09 18:52:53 +02:00 committed by GitHub
commit 60faccb27a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package de.test.antennapod; package de.test.antennapod;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.PerformException; import android.support.test.espresso.PerformException;
@ -15,6 +16,7 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.dialog.RatingDialog; import de.danoeh.antennapod.dialog.RatingDialog;
import de.danoeh.antennapod.fragment.QueueFragment;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import java.io.File; import java.io.File;
@ -97,6 +99,13 @@ public class EspressoTestUtils {
RatingDialog.saveRated(); RatingDialog.saveRated();
} }
public static void setLastNavFragment(String tag) {
InstrumentationRegistry.getTargetContext().getSharedPreferences(MainActivity.PREF_NAME, Context.MODE_PRIVATE)
.edit()
.putString(MainActivity.PREF_LAST_FRAGMENT_TAG, tag)
.commit();
}
public static void clearDatabase() { public static void clearDatabase() {
PodDBAdapter.init(InstrumentationRegistry.getTargetContext()); PodDBAdapter.init(InstrumentationRegistry.getTargetContext());
PodDBAdapter.deleteDatabase(); PodDBAdapter.deleteDatabase();

View File

@ -0,0 +1,58 @@
package de.test.antennapod.ui;
import android.content.Intent;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.intent.rule.IntentsTestRule;
import android.support.test.runner.AndroidJUnit4;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.test.antennapod.EspressoTestUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
/**
* User interface tests for queue fragment
*/
@RunWith(AndroidJUnit4.class)
public class QueueFragmentTest {
@Rule
public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(MainActivity.class, false, false);
@Before
public void setUp() {
EspressoTestUtils.clearPreferences();
EspressoTestUtils.makeNotFirstRun();
EspressoTestUtils.clearDatabase();
EspressoTestUtils.setLastNavFragment(QueueFragment.TAG);
mActivityRule.launchActivity(new Intent());
}
@Test
public void testLockEmptyQueue() {
onView(withContentDescription(R.string.lock_queue)).perform(click());
onView(withContentDescription(R.string.unlock_queue)).perform(click());
}
@Test
public void testSortEmptyQueue() {
Espresso.openContextualActionModeOverflowMenu();
onView(withText(R.string.sort)).perform(click());
onView(withText(R.string.random)).perform(click());
}
@Test
public void testKeepEmptyQueueSorted() {
Espresso.openContextualActionModeOverflowMenu();
onView(withText(R.string.sort)).perform(click());
onView(withText(R.string.keep_sorted)).perform(click());
}
}

View File

@ -87,7 +87,7 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
public static final String PREF_NAME = "MainActivityPrefs"; public static final String PREF_NAME = "MainActivityPrefs";
public static final String PREF_IS_FIRST_LAUNCH = "prefMainActivityIsFirstLaunch"; public static final String PREF_IS_FIRST_LAUNCH = "prefMainActivityIsFirstLaunch";
private static final String PREF_LAST_FRAGMENT_TAG = "prefMainActivityLastFragmentTag"; public static final String PREF_LAST_FRAGMENT_TAG = "prefMainActivityLastFragmentTag";
public static final String EXTRA_NAV_TYPE = "nav_type"; public static final String EXTRA_NAV_TYPE = "nav_type";
public static final String EXTRA_NAV_INDEX = "nav_index"; public static final String EXTRA_NAV_INDEX = "nav_index";

View File

@ -378,8 +378,10 @@ public class QueueFragment extends Fragment {
if (keepSortedNew) { if (keepSortedNew) {
SortOrder sortOrder = UserPreferences.getQueueKeepSortedOrder(); SortOrder sortOrder = UserPreferences.getQueueKeepSortedOrder();
QueueSorter.sort(sortOrder, true); QueueSorter.sort(sortOrder, true);
recyclerAdapter.setLocked(true); if (recyclerAdapter != null) {
} else { recyclerAdapter.setLocked(true);
}
} else if (recyclerAdapter != null) {
recyclerAdapter.setLocked(UserPreferences.isQueueLocked()); recyclerAdapter.setLocked(UserPreferences.isQueueLocked());
} }
getActivity().invalidateOptionsMenu(); getActivity().invalidateOptionsMenu();