Fix tests in SettingsActivity (#2625)

* Fix SettingsActivityTest

* Add test in SettingsActivity to set recent upload limit to 0

* Add tests to recent upload limit setting

* Simplify activity test rule
This commit is contained in:
Adam Jones 2019-03-17 05:51:10 +00:00 committed by Vivek Maskara
parent 561f9ea23c
commit 701764d974
2 changed files with 107 additions and 18 deletions

View file

@ -66,11 +66,11 @@ dependencies {
// Android testing // Android testing
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION" androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.annotation:annotation:1.0.2'
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.10.0' androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.10.0'
androidTestImplementation 'androidx.test:rules:1.1.2-alpha02'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
androidTestImplementation "androidx.annotation:annotation:1.0.2"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
androidTestImplementation 'org.mockito:mockito-core:2.10.0' androidTestImplementation 'org.mockito:mockito-core:2.10.0'
// Debugging // Debugging

View file

@ -1,18 +1,20 @@
package fr.free.nrw.commons; package fr.free.nrw.commons;
import android.content.Context; import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.espresso.Espresso; import com.google.gson.Gson;
import androidx.test.espresso.matcher.PreferenceMatchers;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import androidx.test.InstrumentationRegistry;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.matcher.PreferenceMatchers;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import fr.free.nrw.commons.kvstore.JsonKvStore; import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.settings.Prefs; import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.settings.SettingsActivity; import fr.free.nrw.commons.settings.SettingsActivity;
@ -20,37 +22,66 @@ import fr.free.nrw.commons.settings.SettingsActivity;
import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.replaceText; import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isEnabled;
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 org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@LargeTest @LargeTest
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class SettingsActivityTest { public class SettingsActivityTest {
BasicKvStore prefs; JsonKvStore defaultKvStore;
@Rule @Rule
public ActivityTestRule<SettingsActivity> activityRule = public ActivityTestRule activityRule = new ActivityTestRule<>(SettingsActivity.class);
new ActivityTestRule<>(SettingsActivity.class, false, true);
@Before @Before
public void setup() { public void setup() {
Context context = InstrumentationRegistry.getTargetContext(); Context context = InstrumentationRegistry.getTargetContext();
String storeName = context.getPackageName() + "_preferences"; String storeName = context.getPackageName() + "_preferences";
prefs = new BasicKvStore(context, storeName); defaultKvStore = new JsonKvStore(context, storeName, new Gson());
} }
@Test @Test
public void setRecentUploadLimitTo100() { public void setRecentUploadLimitTo123() {
// Open "Use external storage" preference // Open "Use external storage" preference
Espresso.onData(PreferenceMatchers.withKey("uploads")) Espresso.onData(PreferenceMatchers.withKey("uploads"))
.inAdapterView(withId(android.R.id.list)) .inAdapterView(withId(android.R.id.list))
.perform(click()); .perform(click());
// Try setting it to 100 // Try setting it to 123
Espresso.onView(withId(android.R.id.edit)) Espresso.onView(withId(android.R.id.edit))
.perform(replaceText("100")); .perform(replaceText("123"));
// Click "OK"
Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
.perform(click());
// Check setting set to 123 in SharedPreferences
assertEquals(
123,
defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0)
);
// Check displaying 123 in summary text
Espresso.onData(PreferenceMatchers.withKey("uploads"))
.inAdapterView(withId(android.R.id.list))
.onChildView(withId(android.R.id.summary))
.check(matches(withText("123")));
}
@Test
public void setRecentUploadLimitTo0() {
// Open "Use external storage" preference
Espresso.onData(PreferenceMatchers.withKey("uploads"))
.inAdapterView(withId(android.R.id.list))
.perform(click());
// Try setting it to 0
Espresso.onView(withId(android.R.id.edit))
.perform(replaceText("0"));
// Click "OK" // Click "OK"
Espresso.onView(allOf(withId(android.R.id.button1), withText("OK"))) Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
@ -59,7 +90,7 @@ public class SettingsActivityTest {
// Check setting set to 100 in SharedPreferences // Check setting set to 100 in SharedPreferences
assertEquals( assertEquals(
100, 100,
prefs.getInt(Prefs.UPLOADS_SHOWING, 0) defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0)
); );
// Check displaying 100 in summary text // Check displaying 100 in summary text
@ -68,4 +99,62 @@ public class SettingsActivityTest {
.onChildView(withId(android.R.id.summary)) .onChildView(withId(android.R.id.summary))
.check(matches(withText("100"))); .check(matches(withText("100")));
} }
@Test
public void setRecentUploadLimitTo700() {
// Open "Use external storage" preference
Espresso.onData(PreferenceMatchers.withKey("uploads"))
.inAdapterView(withId(android.R.id.list))
.perform(click());
// Try setting it to 700
Espresso.onView(withId(android.R.id.edit))
.perform(replaceText("700"));
// Click "OK"
Espresso.onView(allOf(withId(android.R.id.button1), withText("OK")))
.perform(click());
// Check setting set to 500 in SharedPreferences
assertEquals(
500,
defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 0)
);
// Check displaying 100 in summary text
Espresso.onData(PreferenceMatchers.withKey("uploads"))
.inAdapterView(withId(android.R.id.list))
.onChildView(withId(android.R.id.summary))
.check(matches(withText("500")));
}
@Test
public void useAuthorNameTogglesOn() {
// Turn on "Use external storage" preference if currently off
if (!defaultKvStore.getBoolean("useAuthorName", true)) {
Espresso.onData(PreferenceMatchers.withKey("useAuthorName"))
.inAdapterView(withId(android.R.id.list))
.perform(click());
}
// Check authorName preference is enabled
Espresso.onData(PreferenceMatchers.withKey("authorName"))
.inAdapterView(withId(android.R.id.list))
.check(matches(isEnabled()));
}
@Test
public void useAuthorNameTogglesOff() {
// Turn off "Use external storage" preference if currently on
if (defaultKvStore.getBoolean("useAuthorName", false)) {
Espresso.onData(PreferenceMatchers.withKey("useAuthorName"))
.inAdapterView(withId(android.R.id.list))
.perform(click());
}
// Check authorName preference is enabled
Espresso.onData(PreferenceMatchers.withKey("authorName"))
.inAdapterView(withId(android.R.id.list))
.check(matches(not(isEnabled())));
}
} }