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

Commit

Permalink
add some ui testing cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TonnyL committed May 13, 2017
1 parent fde43f4 commit 5502f2a
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 46 deletions.
59 changes: 41 additions & 18 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* Rules are interceptors which are executed for each test method and are important building
* blocks of Junit tests.
*/
@Rule
ActivityTestRule<OnboardingActivity> mOnboardingActivityTestRule
= new ActivityTestRule<>(OnboardingActivity.class);

}
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* Rules are interceptors which are executed for each test method and are important building
* blocks of Junit tests.
*/
@Rule
public ActivityTestRule<MainActivity> 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<View> withBackgroundColor(final int backgroundColor) {
return new TypeSafeMatcher<View>() {

@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);
}
};
}

}
Original file line number Diff line number Diff line change
@@ -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<MainActivity> mainActivityActivityTestRule
= new ActivityTestRule<>(MainActivity.class);



}
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_packages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/xml/settings_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
-->

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preferences_settings">

<PreferenceCategory
android:title="@string/notifications">
Expand Down

0 comments on commit 5502f2a

Please sign in to comment.