Implemented some basic tests with the first-run dialog

pull/316/head
Loren M. Lang 2022-01-03 16:29:57 -08:00
rodzic 355aefc705
commit bf9ebfdec5
2 zmienionych plików z 145 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,116 @@
package org.aprsdroid.app;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withHint;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.StringContains.containsString;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.test.espresso.Espresso;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import scala.Int;
@RunWith(AndroidJUnit4.class)
public class FirstRunDialog {
private final String pref_callsign = "callsign";
private final String pref_passcode = "passcode";
public ActivityScenarioRule activityRule = new ActivityScenarioRule<>(LogActivity.class);
public SharedPreferencesRule prefsRule = new SharedPreferencesRule() {
@Override
protected void modifyPreferences(SharedPreferences preferences) {
preferences.edit().clear().commit();
}
};
@Rule
public RuleChain rules = RuleChain.outerRule(prefsRule).around(activityRule);
@Test
public void givenAFirstTimeRun_whenProvidedABadPasscode_ThenDialogStaysOpen() {
onView(isRoot())
.inRoot(isDialog())
.check(matches(allOf(
hasDescendant(withText(containsString("Welcome to APRSdroid"))),
hasDescendant(withId(R.id.callsign)),
hasDescendant(withId(R.id.passcode)))));
onView(withId(R.id.callsign))
.check(matches(withHint(containsString("Callsign"))))
.perform(typeText("XA1AAA"));
onView(withId(R.id.passcode))
.check(matches(withHint(containsString("Passcode"))))
.perform(typeText("12345"));
onView(withId(android.R.id.button1)).perform(click()); // OK Button
onView(isRoot())
.inRoot(isDialog())
.check(matches(allOf(
hasDescendant(withText(containsString("Welcome to APRSdroid"))),
hasDescendant(withId(R.id.callsign)),
hasDescendant(withId(R.id.passcode)))));
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
}
Assert.assertTrue(true);
}
@Test
public void givenAFirstTimeRun_whenProvidedAGoodPasscode_ThenDialogCloses() {
onView(isRoot())
.inRoot(isDialog())
.check(matches(allOf(
hasDescendant(withText(containsString("Welcome to APRSdroid"))),
hasDescendant(withId(R.id.callsign)),
hasDescendant(withId(R.id.passcode)))));
onView(withId(R.id.callsign))
.check(matches(withHint(containsString("Callsign"))))
.perform(typeText("XA1AAA"));
onView(withId(R.id.passcode))
.check(matches(withHint(containsString("Passcode"))))
.perform(typeText("23459"));
onView(withId(android.R.id.button1)).perform(click()); // OK Button
onView(allOf(
isRoot(),
hasDescendant(withText(containsString("Welcome to APRSdroid"))),
hasDescendant(withId(R.id.callsign)),
hasDescendant(withId(R.id.passcode))))
.check(doesNotExist());
}
@Test
public void givenAFirstTimeRun_whenProvidedAGoodPasscode_ThenPrefsSaved() {
String expected_callsign = "XA1AAA";
String expected_passcode = "23459";
SharedPreferences prefs = prefsRule.getPreferences();
Assert.assertNull("Callsign", prefs.getString(pref_callsign, null));
Assert.assertNull("Passcode", prefs.getString(pref_passcode, null));
onView(withId(R.id.callsign))
.check(matches(withHint(containsString("Callsign"))))
.perform(typeText(expected_callsign));
onView(withId(R.id.passcode))
.check(matches(withHint(containsString("Passcode"))))
.perform(typeText(expected_passcode));
onView(withId(android.R.id.button1)).perform(click()); // OK Button
Assert.assertEquals("Callsign", expected_callsign, prefs.getString(pref_callsign, null));
Assert.assertEquals("Passcode", expected_passcode, prefs.getString(pref_passcode, null));
}
}

Wyświetl plik

@ -0,0 +1,29 @@
package org.aprsdroid.app;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public abstract class SharedPreferencesRule implements TestRule {
private SharedPreferences preferences;
protected abstract void modifyPreferences(SharedPreferences preferences);
public SharedPreferences getPreferences() {
return preferences;
}
@Override
public Statement apply(Statement base, Description description) {
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
preferences = PreferenceManager.getDefaultSharedPreferences(appContext);
modifyPreferences(preferences);
return base;
}
}