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

Commit

Permalink
Added AvatarRoomActivityTest #85
Browse files Browse the repository at this point in the history
  • Loading branch information
opticod committed Feb 28, 2016
1 parent 30ec543 commit cd85590
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 0 deletions.
16 changes: 16 additions & 0 deletions PowerUp/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,33 @@ android {
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions{
exclude 'LICENSE.txt'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.akexorcist:RoundCornerProgressBar:2.0.3'

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile "com.android.support:support-annotations:23.0.1"
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile "org.mockito:mockito-core:1.+"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package powerup.systers.com.powerup.espresso;

import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.widget.ImageView;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import powerup.systers.com.AvatarRoomActivity;
import powerup.systers.com.R;

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;
import static android.support.v4.content.res.ResourcesCompat.getDrawable;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class AvatarRoomActivityTest {
@Rule
public ActivityTestRule<AvatarRoomActivity> activityRule = new ActivityTestRule<>(
AvatarRoomActivity.class);

@Test
public void viewTest() {
onView(withId(R.id.eyes)).check(matches(isDisplayed()));
onView(withId(R.id.hair)).check(matches(isDisplayed()));
onView(withId(R.id.face)).check(matches(isDisplayed()));
onView(withId(R.id.clothes)).check(matches(isDisplayed()));
onView(withId(R.id.eyeLeft)).check(matches(isDisplayed()));
onView(withId(R.id.eyeRight)).check(matches(isDisplayed()));
onView(withId(R.id.hairLeft)).check(matches(isDisplayed()));
onView(withId(R.id.hairRight)).check(matches(isDisplayed()));
onView(withId(R.id.faceLeft)).check(matches(isDisplayed()));
onView(withId(R.id.faceRight)).check(matches(isDisplayed()));
onView(withId(R.id.clotheLeft)).check(matches(isDisplayed()));
onView(withId(R.id.clotheRight)).check(matches(isDisplayed()));
onView(withId(R.id.continueButtonAvatar)).check(matches(isDisplayed()));
}

@Test
public void checkIntent() {
onView(withId(R.id.continueButtonAvatar)).check(matches(isDisplayed())).perform(click());
onView(withId(R.id.continueButton)).check(matches(isDisplayed()));
}

@Test
public void clickEvents() {
String[] eyeList = new String[]{"eye3", "eye2", "eye1"};
String[] faceList = new String[]{"face3", "face2", "face1"};
String[] clothList = new String[]{"cloth4", "cloth3", "cloth2", "cloth1"};
String[] hairList = new String[]{"hair1"};

for (int i = 0; i < eyeList.length; i++) {
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
onView(withId(R.id.eyeLeft)).perform(click());
R.drawable ourRID = new R.drawable();
java.lang.reflect.Field photoNameField;
try {
photoNameField = ourRID.getClass().getField(eyeList[i]);
onView(withId(R.id.eyes)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
onView(withId(R.id.eyeLeft)).perform(click());
onView(withId(R.id.eyeRight)).perform(click());
onView(withId(R.id.eyes)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
onView(withId(R.id.eyes)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
} catch (NoSuchFieldException | IllegalAccessException
| IllegalArgumentException e) {
e.printStackTrace();
}
}
for (int i = 0; i < faceList.length; i++) {
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
onView(withId(R.id.faceLeft)).perform(click());
R.drawable ourRID = new R.drawable();
java.lang.reflect.Field photoNameField;
try {
photoNameField = ourRID.getClass().getField(faceList[i]);
onView(withId(R.id.face)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
onView(withId(R.id.faceLeft)).perform(click());
onView(withId(R.id.faceRight)).perform(click());
onView(withId(R.id.face)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
onView(withId(R.id.face)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
} catch (NoSuchFieldException | IllegalAccessException
| IllegalArgumentException e) {
e.printStackTrace();
}
}
for (int i = 0; i < clothList.length; i++) {
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
onView(withId(R.id.clotheLeft)).perform(click());
R.drawable ourRID = new R.drawable();
java.lang.reflect.Field photoNameField;
try {
photoNameField = ourRID.getClass().getField(clothList[i]);
onView(withId(R.id.clothes)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
onView(withId(R.id.clotheLeft)).perform(click());
onView(withId(R.id.clotheRight)).perform(click());
onView(withId(R.id.clothes)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
onView(withId(R.id.clothes)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
} catch (NoSuchFieldException | IllegalAccessException
| IllegalArgumentException e) {
e.printStackTrace();
}
}
for (int i = 0; i < hairList.length; i++) {
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
onView(withId(R.id.hairLeft)).perform(click());
R.drawable ourRID = new R.drawable();
java.lang.reflect.Field photoNameField;
try {
photoNameField = ourRID.getClass().getField(hairList[i]);
onView(withId(R.id.hair)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
onView(withId(R.id.hairLeft)).perform(click());
onView(withId(R.id.hairRight)).perform(click());
onView(withId(R.id.hair)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
onView(withId(R.id.hair)).check(matches(new DrawableMatcher(photoNameField.getInt(ourRID))));
} catch (NoSuchFieldException | IllegalAccessException
| IllegalArgumentException e) {
e.printStackTrace();
}
}
activityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

public class DrawableMatcher extends TypeSafeMatcher<View> {

private final int expectedId;
private String resourceName;

public DrawableMatcher(int expectedId) {
super(View.class);
this.expectedId = expectedId;
}

@Override
protected boolean matchesSafely(View target) {
if (!(target instanceof ImageView)) {
return false;
}
ImageView imageView = (ImageView) target;
if (expectedId < 0) {
return imageView.getDrawable() == null;
}
Resources resources = target.getContext().getResources();
Drawable expectedDrawable = getDrawable(resources, expectedId, target.getContext().getTheme());
resourceName = resources.getResourceEntryName(expectedId);
if (expectedDrawable == null) {
return false;
}
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Bitmap expectedBitmap = ((BitmapDrawable) expectedDrawable).getBitmap();
return bitmap.sameAs(expectedBitmap);
}

@Override
public void describeTo(Description description) {
description.appendValue(expectedId);
description.appendText(":");
if (resourceName != null) {
description.appendText(resourceName);
}
}
}
}

0 comments on commit cd85590

Please sign in to comment.