|
| 1 | +package fr.free.nrw.commons; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.allOf; |
| 4 | +import static org.hamcrest.Matchers.anyOf; |
| 5 | +import static org.hamcrest.Matchers.anything; |
| 6 | + |
| 7 | +import android.content.SharedPreferences; |
| 8 | +import android.preference.PreferenceManager; |
| 9 | +import android.support.test.espresso.Espresso; |
| 10 | +import android.support.test.espresso.action.ViewActions; |
| 11 | +import android.support.test.espresso.assertion.ViewAssertions; |
| 12 | +import android.support.test.espresso.matcher.ViewMatchers; |
| 13 | +import android.support.test.filters.LargeTest; |
| 14 | +import android.support.test.rule.ActivityTestRule; |
| 15 | +import android.support.test.runner.AndroidJUnit4; |
| 16 | +import android.view.View; |
| 17 | + |
| 18 | +import fr.free.nrw.commons.settings.SettingsActivity; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.hamcrest.Matcher; |
| 23 | +import org.junit.Rule; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | + |
| 27 | +@LargeTest |
| 28 | +@RunWith(AndroidJUnit4.class) |
| 29 | +public class SettingsActivityTest { |
| 30 | + private SharedPreferences prefs; |
| 31 | + private Map<String,?> prefValues; |
| 32 | + |
| 33 | + @Rule |
| 34 | + public ActivityTestRule<SettingsActivity> activityRule = |
| 35 | + new ActivityTestRule<SettingsActivity>(SettingsActivity.class, |
| 36 | + false /* Initial touch mode */, true /* launch activity */) { |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void afterActivityLaunched() { |
| 40 | + // save preferences |
| 41 | + prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); |
| 42 | + prefValues = prefs.getAll(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void afterActivityFinished() { |
| 47 | + // restore preferences |
| 48 | + SharedPreferences.Editor editor = prefs.edit(); |
| 49 | + for (Map.Entry<String,?> entry: prefValues.entrySet()) { |
| 50 | + String key = entry.getKey(); |
| 51 | + Object val = entry.getValue(); |
| 52 | + if (val instanceof String) { |
| 53 | + editor.putString(key, (String)val); |
| 54 | + } else if (val instanceof Boolean) { |
| 55 | + editor.putBoolean(key, (Boolean)val); |
| 56 | + } else if (val instanceof Integer) { |
| 57 | + editor.putInt(key, (Integer)val); |
| 58 | + } else { |
| 59 | + throw new RuntimeException("type not implemented: " + entry); |
| 60 | + } |
| 61 | + } |
| 62 | + editor.apply(); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + @Test |
| 67 | + public void oneLicenseIsChecked() { |
| 68 | + // click "License" (the first item) |
| 69 | + Espresso.onData(anything()) |
| 70 | + .inAdapterView(findPreferenceList()) |
| 71 | + .atPosition(0) |
| 72 | + .perform(ViewActions.click()); |
| 73 | + |
| 74 | + // test the selected item |
| 75 | + Espresso.onView(ViewMatchers.isChecked()) |
| 76 | + .check(ViewAssertions.matches(ViewMatchers.isDisplayed())); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void afterClickingCcby4ItWillStay() { |
| 81 | + // click "License" (the first item) |
| 82 | + Espresso.onData(anything()) |
| 83 | + .inAdapterView(findPreferenceList()) |
| 84 | + .atPosition(0) |
| 85 | + .perform(ViewActions.click()); |
| 86 | + |
| 87 | + // click "CC BY-4.0" |
| 88 | + Espresso.onView( |
| 89 | + // FIXME: just R.string.license_name_cc_by_four should be fine but fails on Travis |
| 90 | + textAnyOf(R.string.license_name_cc_by_four, R.string.license_name_cc_by_4_0) |
| 91 | + ).perform(ViewActions.click()); |
| 92 | + |
| 93 | + // click "License" (the first item) |
| 94 | + Espresso.onData(anything()) |
| 95 | + .inAdapterView(findPreferenceList()) |
| 96 | + .atPosition(0) |
| 97 | + .perform(ViewActions.click()); |
| 98 | + |
| 99 | + // test the value remains "CC BY-4.0" |
| 100 | + Espresso.onView(ViewMatchers.isChecked()) |
| 101 | + .check(ViewAssertions.matches( |
| 102 | + textAnyOf(R.string.license_name_cc_by_four, R.string.license_name_cc_by_4_0) |
| 103 | + )); |
| 104 | + } |
| 105 | + |
| 106 | + private Matcher<View> textAnyOf(int id1, int id2) { |
| 107 | + return anyOf(ViewMatchers.withText(id1), ViewMatchers.withText(id2)); |
| 108 | + } |
| 109 | + |
| 110 | + private static Matcher<View> findPreferenceList() { |
| 111 | + return allOf( |
| 112 | + ViewMatchers.isDescendantOfA(ViewMatchers.withId(android.R.id.content)), |
| 113 | + ViewMatchers.withId(android.R.id.list), |
| 114 | + ViewMatchers.hasFocus() |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments