Skip to content

Commit

Permalink
UI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingtoncosta committed Nov 15, 2017
1 parent 0b329ae commit 4bbc5e1
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
public class ConfirmPasswordValidator extends AbstractValidator {

private EditText passwordEditText;
private String password;

ConfirmPasswordValidator(String password) {
Expand All @@ -18,21 +19,28 @@ public ConfirmPasswordValidator(TextInputLayout passwordLayout, TextInputLayout
super(confirmPasswordLayout, errorMessage);

if (passwordLayout.getEditText() != null) {
this.password = passwordLayout.getEditText().getText().toString();
this.passwordEditText = passwordLayout.getEditText();
}
}

public ConfirmPasswordValidator(EditText passwordEditText, EditText confirmPasswordEditText, String errorMessage) {
super(confirmPasswordEditText, errorMessage);

if (passwordEditText != null) {
this.password = passwordEditText.getText().toString();
this.passwordEditText = passwordEditText;
}
}

@Override
boolean isNotValid(String value) {
return value == null || !password.equals(value);
if (passwordEditText != null) {
password = passwordEditText.getText().toString();
}

System.out.println("Password: " + password);
System.out.println("Value: " + value);

return !password.equals(value);
}

}
6 changes: 6 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ android {
targetSdkVersion versions.compileSdk
versionCode 1
versionName '1.0.0'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand All @@ -36,4 +37,9 @@ dependencies {

implementation deps.support.compat
implementation deps.support.design

testImplementation deps.junit

androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package convalida.sample;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

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.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
* @author Wellington Costa on 14/11/2017.
*/
@RunWith(AndroidJUnit4.class)
public class AnotherSampleActivityTest {

@Rule
public ActivityTestRule<AnotherSampleActivity> activityTestRule = new ActivityTestRule<>(AnotherSampleActivity.class);

@Test
public void executeValidationsWithEmptyFields() {
onView(withId(R.id.validate_button))
.perform(closeSoftKeyboard())
.check(matches(isDisplayed()))
.perform(click());

onView(withText(R.string.field_required))
.check(matches(isDisplayed()));

onView(withText(R.string.min_3_characters))
.check(matches(isDisplayed()));

onView(withText(R.string.only_numbers))
.check(matches(isDisplayed()));

onView(withText(R.string.invalid_phone))
.check(matches(isDisplayed()));
}

@Test
public void clearAllValidations() {
onView(withId(R.id.validate_button))
.perform(closeSoftKeyboard())
.check(matches(isDisplayed()))
.perform(click());

onView(withId(R.id.clear_button))
.check(matches(isDisplayed()))
.perform(click());

onView(withText(R.string.field_required))
.check(doesNotExist());

onView(withText(R.string.min_3_characters))
.check(doesNotExist());

onView(withText(R.string.only_numbers))
.check(doesNotExist());

onView(withText(R.string.invalid_phone))
.check(doesNotExist());
}

@Test
public void testNameField() {
TestUtils.testFieldWithEmptyValue(R.id.validate_button, R.string.field_required);

TestUtils.testFieldWithAValidValue(R.id.name_field, R.string.field_required, "Wellington");
}

@Test
public void testNicknameField() {
TestUtils.testFieldWithEmptyValue(R.id.validate_button, R.string.min_3_characters);

TestUtils.testFieldWithAnInvalidValue(R.id.nickname_field, R.string.min_3_characters, "We");

TestUtils.testFieldWithAValidValue(R.id.nickname_field, R.string.min_3_characters, "Well");
}

@Test
public void testAgeField() {
TestUtils.testFieldWithEmptyValue(R.id.validate_button, R.string.only_numbers);

TestUtils.testFieldWithAnInvalidValue(R.id.age_field, R.string.only_numbers, "abc");

TestUtils.testFieldWithAValidValue(R.id.age_field, R.string.only_numbers, "21");
}

@Test
public void testPhoneField() {
TestUtils.testFieldWithEmptyValue(R.id.validate_button, R.string.invalid_phone);

TestUtils.testFieldWithAnInvalidValue(R.id.phone_field, R.string.invalid_phone, "558586846409");

TestUtils.testFieldWithAValidValue(R.id.phone_field, R.string.invalid_phone, "+55(85)8684-6409");
}

}
39 changes: 39 additions & 0 deletions sample/src/androidTest/java/convalida/sample/MainActivityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package convalida.sample;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

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.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

/**
* @author Wellington Costa on 12/11/2017.
*/
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void showSample1() throws Exception {
onView(withId(R.id.sample_1))
.check(matches(isDisplayed()))
.perform(click());
}

@Test
public void showSample2() throws Exception {
onView(withId(R.id.sample_2))
.check(matches(isDisplayed()))
.perform(click());
}

}
103 changes: 103 additions & 0 deletions sample/src/androidTest/java/convalida/sample/SampleActivityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package convalida.sample;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

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.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
* @author Wellington Costa on 12/11/2017.
*/
@RunWith(AndroidJUnit4.class)
public class SampleActivityTest {

@Rule
public ActivityTestRule<SampleActivity> activityTestRule = new ActivityTestRule<>(SampleActivity.class);

@Test
public void executeValidationsWithEmptyFields() {
onView(withId(R.id.validate_button))
.perform(closeSoftKeyboard())
.check(matches(isDisplayed()))
.perform(click());

onView(withText(R.string.field_required))
.check(matches(isDisplayed()));

onView(withText(R.string.invalid_email))
.check(matches(isDisplayed()));

onView(withText(R.string.invalid_password))
.check(matches(isDisplayed()));
}

@Test
public void clearAllValidations() {
onView(withId(R.id.validate_button))
.perform(closeSoftKeyboard())
.check(matches(isDisplayed()))
.perform(click());

onView(withId(R.id.clear_button))
.check(matches(isDisplayed()))
.perform(click());

onView(withText(R.string.field_required))
.check(doesNotExist());

onView(withText(R.string.invalid_email))
.check(doesNotExist());

onView(withText(R.string.invalid_password))
.check(doesNotExist());

onView(withText(R.string.passwords_not_match))
.check(doesNotExist());
}

@Test
public void testNameField() {
TestUtils.testFieldWithEmptyValue(R.id.validate_button, R.string.field_required);

TestUtils.testFieldWithAValidValue(R.id.name_field, R.string.field_required, "Wellington");
}

@Test
public void testEmailField() {
TestUtils.testFieldWithEmptyValue(R.id.validate_button, R.string.invalid_email);

TestUtils.testFieldWithAnInvalidValue(R.id.email_field, R.string.invalid_email, "well@email");

TestUtils.testFieldWithAValidValue(R.id.email_field, R.string.invalid_email, "[email protected]");
}

@Test
public void testPasswordField() {
TestUtils.testFieldWithEmptyValue(R.id.validate_button, R.string.invalid_password);

TestUtils.testFieldWithAnInvalidValue(R.id.password_field, R.string.invalid_password, "asdASD");

TestUtils.testFieldWithAValidValue(R.id.password_field, R.string.invalid_password, "asdASD123");
}

@Test
public void testConfirmPasswordField() {
TestUtils.testFieldWithAValidValue(R.id.password_field, R.string.invalid_password, "asdASD123");

TestUtils.testFieldWithAnInvalidValue(R.id.confirm_password_field, R.string.passwords_not_match, "asdASD");

TestUtils.testFieldWithAValidValue(R.id.confirm_password_field, R.string.passwords_not_match, "asdASD123");
}

}
50 changes: 50 additions & 0 deletions sample/src/androidTest/java/convalida/sample/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package convalida.sample;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.clearText;
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.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
* @author Wellington Costa on 14/11/2017.
*/

class TestUtils {

static void testFieldWithEmptyValue(int fieldResId, int errorMessageResId) {
onView(withId(fieldResId))
.check(matches(isDisplayed()))
.perform(closeSoftKeyboard())
.perform(click());

onView(withText(errorMessageResId))
.check(matches(isDisplayed()));
}

static void testFieldWithAValidValue(int fieldResId, int errorMessageResId, String value) {
onView(withId(fieldResId))
.perform(clearText(), typeText(value))
.perform(closeSoftKeyboard())
.check(matches(withText(value)));

onView(withText(errorMessageResId))
.check(doesNotExist());
}

static void testFieldWithAnInvalidValue(int fieldResId, int errorMessageResId, String value) {
onView(withId(fieldResId))
.perform(clearText(), typeText(value))
.perform(closeSoftKeyboard())
.check(matches(withText(value)));

onView(withText(errorMessageResId))
.check(matches(isDisplayed()));
}

}
Loading

0 comments on commit 4bbc5e1

Please sign in to comment.