update sso / refactor tests

This commit is contained in:
David Luhmer 2018-10-08 15:54:05 +02:00
parent a233d72b0b
commit e4c77664f6
4 changed files with 189 additions and 15 deletions

View file

@ -98,10 +98,10 @@ final SUPPORT_VERSION = '28.0.0'
dependencies {
// core android studio module
//compile project(':core')
// You must install or update the Google Repository through the SDK manager to use this dependency.
// You must install or update the UpdateCurrentRssViewTaskGoogle Repository through the SDK manager to use this dependency.
// The Google Repository (separate from the corresponding library) can be found in the Extras category.
// implementation 'com.google.android.gms:play-services:4.2.42'
implementation "com.github.nextcloud:android-SingleSignOn:0.1.0"
implementation "com.github.nextcloud:Android-SingleSignOn:0.1.2"
implementation "com.android.support:support-v4:${SUPPORT_VERSION}"
implementation "com.android.support:support-compat:${SUPPORT_VERSION}"
implementation "com.android.support:appcompat-v7:${SUPPORT_VERSION}"
@ -165,6 +165,7 @@ dependencies {
androidTestImplementation group: 'com.squareup.okhttp3', name: 'mockwebserver', version: '3.8.0'
androidTestAnnotationProcessor 'com.google.auto.value:auto-value:1.5.2'
androidTestImplementation "com.android.support:support-annotations:${SUPPORT_VERSION}"
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

View file

@ -0,0 +1,122 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.TypeSafeMatcher;
public class CustomMatchers {
private static final String TAG = CustomMatchers.class.getCanonicalName();
public static Matcher<View> withBackgroundColor(final int resourceColorId) {
return new TypeSafeDiagnosingMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("HALLO!!!!!!");
}
@Override
protected boolean matchesSafely(View view, Description mismatchDescription) {
Drawable drawable = view.getBackground();
Drawable otherDrawable = view.getContext().getResources().getDrawable(resourceColorId);
if (drawable instanceof ColorDrawable && otherDrawable instanceof ColorDrawable) {
int colorId = ((ColorDrawable) drawable).getColor();
if(colorId == resourceColorId) {
return true;
} else {
mismatchDescription.appendText("Got: " + colorId);
}
} else {
mismatchDescription.appendText("Not color drawables!!");
}
return false;
}
};
}
public static Matcher<View> withBackground(final int resourceId) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
return sameBitmap(view.getContext(), view.getBackground(), resourceId);
}
@Override
protected void describeMismatchSafely(View item, Description mismatchDescription) {
mismatchDescription.appendText("view.getBackground() returned: " + item.getBackground());
}
@Override
public void describeTo(Description description) {
description.appendText("" + resourceId);
}
};
}
public static Matcher<View> withCompoundDrawable(final int resourceId) {
return new BoundedMatcher<View, TextView>(TextView.class) {
@Override
public void describeTo(Description description) {
description.appendText("has compound drawable resource " + resourceId);
}
@Override
public boolean matchesSafely(TextView textView) {
for (Drawable drawable : textView.getCompoundDrawables()) {
if (sameBitmap(textView.getContext(), drawable, resourceId)) {
return true;
}
}
return false;
}
};
}
public static Matcher<View> withImageDrawable(final int resourceId) {
return new BoundedMatcher<View, ImageView>(ImageView.class) {
@Override
public void describeTo(Description description) {
description.appendText("has image drawable resource " + resourceId);
}
@Override
public boolean matchesSafely(ImageView imageView) {
return sameBitmap(imageView.getContext(), imageView.getDrawable(), resourceId);
}
};
}
private static boolean sameBitmap(Context context, Drawable drawable, int resourceId) {
Drawable otherDrawable = context.getResources().getDrawable(resourceId);
if (drawable == null || otherDrawable == null) {
Log.e(TAG, "drawable null!!");
return false;
}
if (drawable instanceof StateListDrawable && otherDrawable instanceof StateListDrawable) {
Log.e(TAG, "other drawable!!");
drawable = drawable.getCurrent();
otherDrawable = otherDrawable.getCurrent();
}
if (drawable instanceof BitmapDrawable) {
Log.e(TAG, "bitmap drawable!!");
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap otherBitmap = ((BitmapDrawable) otherDrawable).getBitmap();
return bitmap.sameAs(otherBitmap);
}
return false;
}
}

View file

@ -1,3 +1,4 @@
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.preference.PreferenceManager;
@ -5,11 +6,15 @@ import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.test.ActivityInstrumentationTestCase2;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@ -29,30 +34,32 @@ import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
public class NewsReaderListActivityUiTests
extends ActivityInstrumentationTestCase2<NewsReaderListActivity> {
public class NewsReaderListActivityUiTests {
public NewsReaderListActivityUiTests() {
super(NewsReaderListActivity.class);
@Rule
public ActivityTestRule<NewsReaderListActivity> mActivityRule = new ActivityTestRule<>(
NewsReaderListActivity.class);
Activity getActivity() {
return mActivityRule.getActivity();
}
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
//injectInstrumentation(InstrumentationRegistry.getInstrumentation()); // TODO ?!!
getActivity();
onView(isRoot()).perform(OrientationChangeAction.orientationLandscape());
sleep(0.3f);
}
@Override
@After
protected void tearDown() throws Exception {
onView(isRoot()).perform(OrientationChangeAction.orientationLandscape());
super.tearDown();
}
@Test
public void testPositionAfterOrientationChange_sameActivity() {
NewsReaderDetailFragment ndf = (NewsReaderDetailFragment) waitForFragment(R.id.content_frame, 5000);
@ -75,7 +82,7 @@ public class NewsReaderListActivityUiTests
onView(withId(R.id.tv_no_items_available)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
}
@Test
public void testPositionAfterActivityRestart_sameActivity() {
NewsReaderDetailFragment ndf = (NewsReaderDetailFragment) waitForFragment(R.id.content_frame, 5000);
@ -101,14 +108,17 @@ public class NewsReaderListActivityUiTests
}
@Test
public void testSyncFinishedRefreshRecycler_sameActivity() {
syncResultTest(true);
}
@Test
public void testSyncFinishedSnackbar_sameActivity() {
syncResultTest(false);
}
@Test
private void syncResultTest(boolean testFirstPosition) {
if(!testFirstPosition) {
onView(withId(R.id.list)).perform(RecyclerViewActions.scrollToPosition(20));
@ -133,7 +143,7 @@ public class NewsReaderListActivityUiTests
}
}
});
getInstrumentation().waitForIdleSync();
//getInstrumentation().waitForIdleSync(); // TOD?!!!
if(!testFirstPosition)
onView(withId(android.support.design.R.id.snackbar_text)).check(matches(isDisplayed()));
@ -155,7 +165,7 @@ public class NewsReaderListActivityUiTests
long endTime = SystemClock.uptimeMillis() + timeout;
while (SystemClock.uptimeMillis() <= endTime) {
Fragment fragment = getActivity().getSupportFragmentManager().findFragmentById(id);
Fragment fragment = ((FragmentActivity) getActivity()).getSupportFragmentManager().findFragmentById(id);
if (fragment != null) {
return fragment;
}

View file

@ -0,0 +1,41 @@
import org.hamcrest.CustomMatcher;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import android.support.test.espresso.ViewAssertion;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.R;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class NightModeTest {
private String mStringToBetyped;
@Rule
public ActivityTestRule<NewsReaderListActivity> mActivityRule = new ActivityTestRule<>(
NewsReaderListActivity.class);
@Before
public void initTestData() {
// Specify a valid string.
mStringToBetyped = "Espresso";
}
@Test
public void testBackgroundDaylight_sameActivity() {
// Type text and then press the button.
onView(withId(R.id.sliding_layout))
.check(ViewAssertions.matches(CustomMatchers.withBackgroundColor(android.R.color.white)));
}
}