app/src/androidTest/java/ch/epfl/sweng/favors/main/AuthenticationUITest.java
package ch.epfl.sweng.favors.main;
import android.support.test.espresso.ViewInteraction;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiSelector;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import ch.epfl.sweng.favors.R;
import ch.epfl.sweng.favors.database.internal_db.InternalSqliteDb;
import ch.epfl.sweng.favors.utils.ExecutionMode;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class AuthenticationUITest {
private UiDevice device;
@Rule
public ActivityTestRule<SplashScreenActivity> mActivityTestRule = new ActivityTestRule<>(SplashScreenActivity.class);
@Before
public void setUp(){
device = UiDevice.getInstance(getInstrumentation());
ExecutionMode.getInstance().setTest(true);
ExecutionMode.getInstance().setInvalidAuthTest(true);
InternalSqliteDb.openDb(mActivityTestRule.getActivity().getApplicationContext());
}
@Test
public void bigTestAuth() throws Exception{
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
UiObject allowButton = device.findObject(new UiSelector().text("ALLOW"));
if(allowButton.exists()){
allowButton.click();
}
ViewInteraction appCompatButton = onView(
allOf(withId(R.id.registerButton), withText("Register"),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.ScrollView")),
0),
2)));
appCompatButton.perform(scrollTo(), click());
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ViewInteraction textView = onView(
allOf(withId(R.id.loginMessageText), withText("Welcome here! Just some small steps..."),
childAtPosition(
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class),
0),
0),
isDisplayed()));
textView.check(matches(withText("Welcome here! Just some small steps...")));
ViewInteraction editText = onView(
allOf(withId(R.id.emailTextField),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.ScrollView")),
0),
1)));
editText.perform(scrollTo(), replaceText("nicolas@test.com"), closeSoftKeyboard());
ViewInteraction imageView = onView(
allOf(withId(R.id.checkEmailImage),
childAtPosition(
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class),
0),
5),
isDisplayed()));
imageView.check(matches(isDisplayed()));
ViewInteraction editText2 = onView(
allOf(withId(R.id.passwordTextField),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.ScrollView")),
0),
2)));
editText2.perform(scrollTo(), replaceText("test448899"), closeSoftKeyboard());
ViewInteraction imageView2 = onView(
allOf(withId(R.id.checkPasswordImage),
childAtPosition(
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class),
0), 6),
isDisplayed()));
imageView2.check(matches(isDisplayed()));
ViewInteraction button = onView(
allOf(withId(R.id.authenticationButton),
childAtPosition(
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class),
0),
4),
isDisplayed()));
button.check(matches(isDisplayed()));
pressBack();
//pressBack();
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ViewInteraction appCompatButton2 = onView(
allOf(withId(R.id.loginButton), withText("Login"),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.ScrollView")),
0),
1)));
appCompatButton2.perform(scrollTo(), click());
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ViewInteraction textView2 = onView(
allOf(withId(R.id.loginMessageText), withText("Please enter your login informations:"),
childAtPosition(
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class),
0),
0),
isDisplayed()));
textView2.check(matches(withText("Please enter your login informations:")));
}
@After
public void end(){
ExecutionMode.getInstance().setInvalidAuthTest(false);
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}