Skip to content
This repository has been archived by the owner on May 28, 2020. It is now read-only.

Commit

Permalink
ui testing
Browse files Browse the repository at this point in the history
  • Loading branch information
TonnyL committed May 14, 2017
1 parent 5502f2a commit d0a2768
Show file tree
Hide file tree
Showing 23 changed files with 459 additions and 80 deletions.
27 changes: 26 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,36 @@ android {
versionNameSuffix "debug"
}
}

lintOptions {
abortOnError false
}

productFlavors {
mock {
applicationIdSuffix = ".mock"
}
prod {

}
}

// Remove mockRelease as it's not needed.
android.variantFilter { variant ->
if (variant.buildType.name == 'release'
&& variant.getFlavors().get(0).name == 'mock') {
variant.setIgnore(true);
}
}

// Always show the result of every unit test, even if it passes.
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}

sourceSets { prod { java.srcDirs = ['src/prod/java', 'src/prod/java/'] } }
}

ext {
Expand Down Expand Up @@ -89,5 +115,4 @@ dependencies {
// Runner and rules
androidTestCompile "com.android.support.test:runner:$runnerRulesVersion"
androidTestCompile "com.android.support.test:rules:$runnerRulesVersion"

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.github.marktony.espresso.about;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
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 io.github.marktony.espresso.R;
import io.github.marktony.espresso.ui.PrefsActivity;

import static android.support.test.espresso.Espresso.onView;
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.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;

/**
* Created by lizhaotailang on 2017/5/13.
* Tests for the {@link io.github.marktony.espresso.ui.AboutFragment}.
*/

@RunWith(AndroidJUnit4.class)
@SmallTest
public class AboutScreenTest {

/**
* {@link ActivityTestRule} is a JUnit {@link Rule @Rule} to launch your activity under test.
*
* <p>
* Rules are interceptors which are executed for each test method and are important building
* blocks of Junit tests.
*/
@Rule
public ActivityTestRule<PrefsActivity> mPrefsActivityTestRule
= new ActivityTestRule<PrefsActivity>(PrefsActivity.class){
@Override
protected Intent getActivityIntent() {
Context targetContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext();
Intent intent = new Intent(targetContext, PrefsActivity.class);
intent.putExtra(PrefsActivity.EXTRA_FLAG, PrefsActivity.FLAG_ABOUT);
return intent;
}
};

@Test
public void test_AboutScreenDisplayed() {
onView(allOf(withParent(withId(R.id.toolbar)),
withText(R.string.nav_about)))
.check(matches(isDisplayed()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package io.github.marktony.espresso.addpackage;

import android.os.Build;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.github.marktony.espresso.R;
import io.github.marktony.espresso.mvp.addpackage.AddPackageActivity;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.InstrumentationRegistry.getTargetContext;
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.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
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.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;

/**
* Created by lizhaotailang on 2017/5/14.
* Tests the components of {@link AddPackageActivity} layout.
*/

@RunWith(AndroidJUnit4.class)
@LargeTest
public class AddPackageScreenTest {

private String validPackageNumber;
private String invalidPackageNumber;

/**
* {@link ActivityTestRule} is a JUnit {@link Rule @Rule} to launch your activity under test.
*
* <p>
* Rules are interceptors which are executed for each test method and are important building
* blocks of Junit tests.
*/
@Rule
public ActivityTestRule<AddPackageActivity> mAddPackageActivityTestRule
= new ActivityTestRule<>(AddPackageActivity.class);

@Before
public void initNumbers() {
validPackageNumber = "958381347318";
invalidPackageNumber = "12345";
}

@Before
public void grantCameraPermission() {
// In M+, trying to call a number will trigger a runtime dialog. Make sure
// the permission is granted before running this test.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getInstrumentation().getUiAutomation().executeShellCommand(
"pm grant " + getTargetContext().getPackageName()
+ " android.permission.CAMERA");
}
}

@Test
public void test_AddPackageScreenDisplayed() {
// Check that the toolbar title was correct.
onView(withText(R.string.activity_add_package))
.check(matches(withParent(withId(R.id.toolbar))));
}

@Test
public void clickOnFab_ShowErrorTip() {
// Click the floating action button without inputting anything.
onView(withId(R.id.fab)).perform(click());

// Check that the snack bar was displayed.
onView(allOf(withId(android.support.design.R.id.snackbar_text),
withText(R.string.wrong_number_and_check)))
.check(matches(isDisplayed()));
}

@Test
public void typeValidNumber_ShowHomeScreen() {
onView(withId(R.id.editTextNumber))
.check(matches(isCompletelyDisplayed()));

// Type the valid number.
onView(withId(R.id.editTextNumber))
.perform(typeText(validPackageNumber), closeSoftKeyboard());

// Click the floating action button.
onView(withId(R.id.fab)).perform(click());

// Check that the package name edit text was filled automatically.
String name = mAddPackageActivityTestRule.getActivity().getString(R.string.package_name_default_pre)
+ validPackageNumber.substring(0, 4);
onView(withId(R.id.editTextName))
.check(matches(withText(name)));

}

@Test
public void typeInvalidNumber_ShowErrorTip() {
onView(withId(R.id.editTextNumber))
.check(matches(isCompletelyDisplayed()));

// Type the valid number.
onView(withId(R.id.editTextNumber))
.perform(typeText(invalidPackageNumber), closeSoftKeyboard());

// Click the floating action button.
onView(withId(R.id.fab)).perform(click());

// Check that the package name edit text was filled automatically.
String name = mAddPackageActivityTestRule.getActivity().getString(R.string.package_name_default_pre)
+ invalidPackageNumber.substring(0, 4);
onView(withId(R.id.editTextName))
.check(matches(withText(name)));

// Check that the snack bar with error message was displayed.
onView(allOf(withId(android.support.design.R.id.snackbar_text),
withText(R.string.wrong_number_and_check)))
.check(matches(isDisplayed()));
}

@Test
public void typeEmptyNumber_ShowErrorTip() {
onView(withId(R.id.editTextNumber))
.check(matches(isCompletelyDisplayed()));

// Type empty.
onView(withId(R.id.editTextNumber))
.perform(typeText(""), closeSoftKeyboard());

// Click the floating action button.
onView(withId(R.id.fab)).perform(click());

// Check that the progress bar was gone.
onView(withId(R.id.progressBar))
.check(matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.GONE)));

// Check that the package name edit text was filled automatically.
onView(withId(R.id.editTextName))
.check(matches(withText("")));

// Check that the snack bar with error message was displayed.
onView(allOf(withId(android.support.design.R.id.snackbar_text),
withText(R.string.wrong_number_and_check)))
.check(matches(isDisplayed()));
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package io.github.marktony.espresso.onboarding;

import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.SmallTest;
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 io.github.marktony.espresso.R;
import io.github.marktony.espresso.ui.onboarding.OnboardingActivity;

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.swipeLeft;
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.isEnabled;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

/**
* Created by lizhaotailang on 2017/5/13.
* Tests for the {@link android.support.v4.view.ViewPager} and
Expand All @@ -27,7 +39,63 @@ public class OnboardingScreenTest {
* blocks of Junit tests.
*/
@Rule
ActivityTestRule<OnboardingActivity> mOnboardingActivityTestRule
public ActivityTestRule<OnboardingActivity> mOnboardingActivityTestRule
= new ActivityTestRule<>(OnboardingActivity.class);

@Test
public void swipeViewPager_scrollPage() {
// Check that the finish button was invisible.
onView(withId(R.id.buttonFinish))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));

// Check that the previous button was not available.
onView(withId(R.id.imageButtonPre))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));

// Check that the ViewPager was displayed.
onView(withId(R.id.view_pager))
.check(matches(isDisplayed()));

// Scroll the ViewPager
// Now the app is in the second position.
onView(withId(R.id.view_pager))
.perform(swipeLeft());

// Check that the finish button was gone.
onView(withId(R.id.buttonFinish))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));

// Check that the previous button was visible.
onView(withId(R.id.imageButtonPre))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));

// Check that the next button was visible.
onView(withId(R.id.imageButtonNext))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));

// Click the next button to scroll the ViewPager.
onView(withId(R.id.imageButtonNext))
.perform(click()); // Now the app is in the third position.

// Check that the finish button was visible and enabled.
onView(withId(R.id.buttonFinish))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));

// Check that the next button was not available.
onView(withId(R.id.imageButtonNext))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));

// Check the finish button is enabled.
onView(withId(R.id.buttonFinish))
.check(matches(isEnabled()));

// Check the finish button is visible.
onView(withId(R.id.buttonFinish))
.perform(click());

// Check that main activity was opened.
onView(withId(R.id.drawer_layout))
.check(matches(isDisplayed()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void clickOnNavigationDrawerItem_ShowsAboutScreen() {
.perform(navigateTo(R.id.nav_about));

// Check that title is correct.
onView(allOf(withParent(withId(R.id.toolbar)),
onView(allOf(withParent(withId(R.id.toolbar)),
withText(R.string.nav_about)))
.check(matches(isDisplayed()));
}
Expand Down
Loading

0 comments on commit d0a2768

Please sign in to comment.