From 5502f2ab0a2986d0c5e96610c43faca8537461c1 Mon Sep 17 00:00:00 2001 From: Tonny Date: Sat, 13 May 2017 21:02:27 +0800 Subject: [PATCH] add some ui testing cases --- app/build.gradle | 59 +++++-- .../espresso/ExampleInstrumentedTest.java | 26 --- .../onboarding/OnboardingScreenTest.java | 33 ++++ .../espresso/packages/AppNavigationTest.java | 167 ++++++++++++++++++ .../espresso/packages/PackagesScreenTest.java | 26 +++ app/src/main/res/layout/fragment_packages.xml | 3 +- app/src/main/res/xml/settings_prefs.xml | 3 +- 7 files changed, 271 insertions(+), 46 deletions(-) delete mode 100644 app/src/androidTest/java/io/github/marktony/espresso/ExampleInstrumentedTest.java create mode 100644 app/src/androidTest/java/io/github/marktony/espresso/onboarding/OnboardingScreenTest.java create mode 100644 app/src/androidTest/java/io/github/marktony/espresso/packages/AppNavigationTest.java create mode 100644 app/src/androidTest/java/io/github/marktony/espresso/packages/PackagesScreenTest.java diff --git a/app/build.gradle b/app/build.gradle index 83bb1aa..693109d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,32 +39,55 @@ ext { zxingLibVersion = '3.3.0' datetimePickerVersion = '3.1.3' circleimageviewVersion = '2.1.0' + espressoVersion = '2.2.2' + runnerRulesVersion = '0.5' + junitVersion = '4.12' + mockitoVersion = '2.0.2-beta' } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') - androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { - exclude group: 'com.android.support', module: 'support-annotations' - }) + // Support libraries - compile "com.android.support:appcompat-v7:${supportLibVersion}" - compile "com.android.support:support-v4:${supportLibVersion}" - compile "com.android.support:design:${supportLibVersion}" - compile "com.android.support:cardview-v7:${supportLibVersion}" - compile "com.android.support:preference-v14:${supportLibVersion}" - compile "com.android.support:customtabs:${supportLibVersion}" + compile "com.android.support:appcompat-v7:$supportLibVersion" + compile "com.android.support:support-v4:$supportLibVersion" + compile "com.android.support:design:$supportLibVersion" + compile "com.android.support:cardview-v7:$supportLibVersion" + compile "com.android.support:preference-v14:$supportLibVersion" + compile "com.android.support:customtabs:$supportLibVersion" + // ReactiveX series - compile "io.reactivex.rxjava2:rxjava:${rxjavaVersion}" - compile "io.reactivex.rxjava2:rxandroid:${rxjavaVersion}" + compile "io.reactivex.rxjava2:rxjava:$rxjavaVersion" + compile "io.reactivex.rxjava2:rxandroid:$rxjavaVersion" // Retrofit series - compile "com.squareup.retrofit2:retrofit:${retrofitVersion}" - compile "com.squareup.retrofit2:adapter-rxjava2:${retrofitVersion}" - compile "com.squareup.retrofit2:converter-gson:${retrofitVersion}" + compile "com.squareup.retrofit2:retrofit:$retrofitVersion" + compile "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion" + compile "com.squareup.retrofit2:converter-gson:$retrofitVersion" // Zxing - compile "com.google.zxing:core:${zxingLibVersion}" + compile "com.google.zxing:core:$zxingLibVersion" // Material date time picker - compile "com.wdullaer:materialdatetimepicker:${datetimePickerVersion}" + compile "com.wdullaer:materialdatetimepicker:$datetimePickerVersion" // CircleImageView - compile "de.hdodenhof:circleimageview:${circleimageviewVersion}" - testCompile 'junit:junit:4.12' + compile "de.hdodenhof:circleimageview:$circleimageviewVersion" + + testCompile "junit:junit:$junitVersion" + testCompile "org.mockito:mockito-all:$mockitoVersion" + + // Mockito + androidTestCompile "org.mockito:mockito-core:$mockitoVersion" + + // Espresso + androidTestCompile "com.android.support:support-annotations:$supportLibVersion" + androidTestCompile "com.android.support.test.espresso:espresso-core:$espressoVersion" + androidTestCompile("com.android.support.test.espresso:espresso-contrib:$espressoVersion") { + exclude group: 'com.android.support' + } + androidTestCompile("com.android.support.test.espresso:espresso-intents:$espressoVersion") { + exclude group: 'com.android.support' + } + + // Runner and rules + androidTestCompile "com.android.support.test:runner:$runnerRulesVersion" + androidTestCompile "com.android.support.test:rules:$runnerRulesVersion" + } diff --git a/app/src/androidTest/java/io/github/marktony/espresso/ExampleInstrumentedTest.java b/app/src/androidTest/java/io/github/marktony/espresso/ExampleInstrumentedTest.java deleted file mode 100644 index 841beb3..0000000 --- a/app/src/androidTest/java/io/github/marktony/espresso/ExampleInstrumentedTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.github.marktony.espresso; - -import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.*; - -/** - * Instrumentation test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() throws Exception { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("io.github.marktony.espresso", appContext.getPackageName()); - } -} diff --git a/app/src/androidTest/java/io/github/marktony/espresso/onboarding/OnboardingScreenTest.java b/app/src/androidTest/java/io/github/marktony/espresso/onboarding/OnboardingScreenTest.java new file mode 100644 index 0000000..b454f24 --- /dev/null +++ b/app/src/androidTest/java/io/github/marktony/espresso/onboarding/OnboardingScreenTest.java @@ -0,0 +1,33 @@ +package io.github.marktony.espresso.onboarding; + +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.runner.RunWith; + +import io.github.marktony.espresso.ui.onboarding.OnboardingActivity; + +/** + * Created by lizhaotailang on 2017/5/13. + * Tests for the {@link android.support.v4.view.ViewPager} and + * other layout components in {@link OnboardingActivity}. + */ + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class OnboardingScreenTest { + + /** + * {@link ActivityTestRule} is a JUnit {@link Rule @Rule} to launch your activity under test. + * + *

+ * Rules are interceptors which are executed for each test method and are important building + * blocks of Junit tests. + */ + @Rule + ActivityTestRule mOnboardingActivityTestRule + = new ActivityTestRule<>(OnboardingActivity.class); + +} diff --git a/app/src/androidTest/java/io/github/marktony/espresso/packages/AppNavigationTest.java b/app/src/androidTest/java/io/github/marktony/espresso/packages/AppNavigationTest.java new file mode 100644 index 0000000..526ac45 --- /dev/null +++ b/app/src/androidTest/java/io/github/marktony/espresso/packages/AppNavigationTest.java @@ -0,0 +1,167 @@ +package io.github.marktony.espresso.packages; + +import android.graphics.drawable.ColorDrawable; +import android.support.test.filters.LargeTest; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.support.v7.widget.Toolbar; +import android.view.Gravity; +import android.view.View; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +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.packages.MainActivity; + +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.contrib.DrawerActions.open; +import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; +import static android.support.test.espresso.contrib.NavigationViewActions.navigateTo; +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.Matchers.allOf; +import static org.hamcrest.Matchers.not; + +/** + * Created by lizhaotailang on 2017/5/12. + * Tests for the {@link android.support.v4.widget.DrawerLayout} layout + * component in {@link MainActivity} which manages the navigation + * within the app. + */ + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class AppNavigationTest { + + /** + * {@link ActivityTestRule} is a JUnit {@link Rule @Rule} to launch your activity under test. + * + *

+ * Rules are interceptors which are executed for each test method and are important building + * blocks of Junit tests. + */ + @Rule + public ActivityTestRule mainActivityActivityTestRule + = new ActivityTestRule<>(MainActivity.class); + + @Test + public void clickOnNavigationDrawerItem_ShowsPackagesScreen() { + // Open drawer to click on navigation. + onView(withId(R.id.drawer_layout)) + .check(matches(isClosed(Gravity.LEFT))) // Left drawer should be closed. + .perform(open()); // Open the drawer. + + // Start packages screen + onView(withId(R.id.nav_view)) + .perform(navigateTo(R.id.nav_home)); + + // Check that packages fragment was opened. + onView(withId(R.id.fragment_packages)) + .check(matches(isDisplayed())); + + } + + @Test + public void clickOnNavigationDrawerItem_ShowsCompaniesScreen() { + // Open drawer to click on navigation. + onView(withId(R.id.drawer_layout)) + .check(matches(isClosed(Gravity.LEFT))) // Left drawer should be closed. + .perform(open()); // Open the drawer. + + // Start companies screen + onView(withId(R.id.nav_view)) + .perform(navigateTo(R.id.nav_companies)); + + // Check that companies fragment was opened. + onView(withId(R.id.recyclerViewCompaniesList)) + .check(matches(isDisplayed())); + } + + @Test + public void clickOnNavigationDrawerItem_ShowsSettingsScreen() { + // Open drawer to click on navigation. + onView(withId(R.id.drawer_layout)) + .check(matches(isClosed(Gravity.LEFT))) + .perform(open()); + + // Start settings screen. + onView(withId(R.id.nav_view)) + .perform(navigateTo(R.id.nav_settings)); + + // Check that title is correct. + onView(allOf(withParent(withId(R.id.toolbar)), + withText(R.string.nav_settings))) + .check(matches(isDisplayed())); + } + + @Test + public void clickOnNavigationDrawerItem_ShowsAboutScreen() { + // Open drawer to click on navigation. + onView(withId(R.id.drawer_layout)) + .check(matches(isClosed(Gravity.LEFT))) + .perform(open()); + + // Start about screen. + onView(withId(R.id.nav_view)) + .perform(navigateTo(R.id.nav_about)); + + // Check that title is correct. + onView(allOf(withParent(withId(R.id.toolbar)), + withText(R.string.nav_about))) + .check(matches(isDisplayed())); + } + + @Test + public void clickOnNavigationDrawerItem_ChangeTheme() { + // Open drawer to click on navigation. + onView(withId(R.id.drawer_layout)) + .check(matches(isClosed(Gravity.LEFT))) + .perform(open()); + + // Get the color value of toolbar in package fragment. + Toolbar toolbar = (Toolbar) mainActivityActivityTestRule.getActivity().findViewById(R.id.toolbar); + int color = ((ColorDrawable) toolbar.getBackground()).getColor(); + + // Click the navigate item of changing theme. + onView(withId(R.id.nav_view)) + .perform(navigateTo(R.id.nav_switch_theme)); + + // Compare the current color with the old one. + // If not match, the action of changing theme is successful. + onView(withId(R.id.toolbar)) + .check(matches(not(withBackgroundColor(color)))); + + } + + /** + * A customized {@link Matcher} for testing that + * if one color match the background color of current view. + * @param backgroundColor A color int value. + * + * @return Match or not. + */ + public static Matcher withBackgroundColor(final int backgroundColor) { + return new TypeSafeMatcher() { + + @Override + public boolean matchesSafely(View view) { + int color = ((ColorDrawable) view.getBackground().getCurrent()).getColor(); + return color == backgroundColor; + } + + @Override + public void describeTo(Description description) { + description.appendText("with background color value: " + backgroundColor); + } + }; + } + +} diff --git a/app/src/androidTest/java/io/github/marktony/espresso/packages/PackagesScreenTest.java b/app/src/androidTest/java/io/github/marktony/espresso/packages/PackagesScreenTest.java new file mode 100644 index 0000000..c8e8666 --- /dev/null +++ b/app/src/androidTest/java/io/github/marktony/espresso/packages/PackagesScreenTest.java @@ -0,0 +1,26 @@ +package io.github.marktony.espresso.packages; + +import android.support.test.filters.LargeTest; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.runner.RunWith; + +import io.github.marktony.espresso.mvp.packages.MainActivity; + +/** + * Created by lizhaotailang on 2017/5/12. + */ + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class PackagesScreenTest { + + @Rule + public ActivityTestRule mainActivityActivityTestRule + = new ActivityTestRule<>(MainActivity.class); + + + +} diff --git a/app/src/main/res/layout/fragment_packages.xml b/app/src/main/res/layout/fragment_packages.xml index 4cc77bb..2aff070 100644 --- a/app/src/main/res/layout/fragment_packages.xml +++ b/app/src/main/res/layout/fragment_packages.xml @@ -19,7 +19,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:id="@+id/fragment_packages"> + xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/preferences_settings">