From 71ea52119898f2e9552b542b2bcc40ad430ae8a3 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 15 Jun 2015 04:05:07 +0200 Subject: clean up helper code, add withKeyItemId matcher for KeyListAdapter --- .../keychain/EncryptDecryptSymmetricTests.java | 20 +------- .../keychain/EncryptDecryptTests.java | 25 +--------- .../sufficientlysecure/keychain/TestHelpers.java | 55 ++++++++++++++++++++++ .../keychain/actions/CheckHelpers.java | 31 ------------ .../keychain/actions/CustomMatchers.java | 27 ----------- .../keychain/matcher/CustomMatchers.java | 48 +++++++++++++++++++ 6 files changed, 105 insertions(+), 101 deletions(-) create mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/TestHelpers.java delete mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/CheckHelpers.java delete mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/CustomMatchers.java create mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/matcher/CustomMatchers.java (limited to 'OpenKeychain/src/androidTest') diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptSymmetricTests.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptSymmetricTests.java index fd9f99cf0..f668472b2 100644 --- a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptSymmetricTests.java +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptSymmetricTests.java @@ -18,33 +18,19 @@ package org.sufficientlysecure.keychain; -import android.app.Activity; -import android.app.Instrumentation; import android.content.Intent; -import android.support.test.InstrumentationRegistry; -import android.support.test.espresso.assertion.ViewAssertions; -import android.support.test.espresso.core.deps.guava.collect.Iterables; import android.support.test.espresso.matcher.ViewMatchers; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; -import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; -import android.support.test.runner.lifecycle.Stage; import android.test.suitebuilder.annotation.LargeTest; -import com.nispok.snackbar.Snackbar; -import org.hamcrest.CoreMatchers; -import org.hamcrest.Matcher; import org.junit.FixMethodOrder; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; -import org.sufficientlysecure.keychain.actions.CheckHelpers; -import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; -import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.MainActivity; import org.sufficientlysecure.keychain.ui.util.Notify.Style; -import org.sufficientlysecure.keychain.util.ProgressScaler; import static android.support.test.InstrumentationRegistry.getInstrumentation; import static android.support.test.espresso.Espresso.onView; @@ -53,13 +39,9 @@ import static android.support.test.espresso.Espresso.pressBack; import static android.support.test.espresso.action.ViewActions.click; 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.hasDescendant; -import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; -import static android.support.test.espresso.matcher.ViewMatchers.withChild; -import static android.support.test.espresso.matcher.ViewMatchers.withClassName; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; -import static org.sufficientlysecure.keychain.actions.CheckHelpers.checkSnackbar; +import static org.sufficientlysecure.keychain.TestHelpers.checkSnackbar; import static org.sufficientlysecure.keychain.actions.CustomActions.actionOpenDrawer; diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptTests.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptTests.java index 604c77ece..01a748172 100644 --- a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptTests.java +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptTests.java @@ -19,7 +19,6 @@ package org.sufficientlysecure.keychain; import android.app.Activity; -import android.content.Context; import android.content.Intent; import android.support.test.espresso.matcher.ViewMatchers; import android.support.test.rule.ActivityTestRule; @@ -32,21 +31,15 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; -import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; -import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow; -import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.MainActivity; -import org.sufficientlysecure.keychain.util.ProgressScaler; -import static android.support.test.InstrumentationRegistry.getInstrumentation; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.pressBack; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.typeText; -import static android.support.test.espresso.matcher.RootMatchers.isDialog; -import static android.support.test.espresso.matcher.RootMatchers.isPlatformPopup; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.sufficientlysecure.keychain.TestHelpers.importKeysFromResource; import static org.sufficientlysecure.keychain.actions.CustomActions.actionOpenDrawer; import static org.sufficientlysecure.keychain.actions.CustomActions.tokenEncryptViewAddToken; @@ -105,20 +98,4 @@ public class EncryptDecryptTests { } - static void importKeysFromResource(Context context, String name) throws Exception { - IteratorWithIOThrow stream = UncachedKeyRing.fromStream( - getInstrumentation().getContext().getAssets().open(name)); - - ProviderHelper helper = new ProviderHelper(context); - while(stream.hasNext()) { - UncachedKeyRing ring = stream.next(); - if (ring.isSecret()) { - helper.saveSecretKeyRing(ring, new ProgressScaler()); - } else { - helper.saveSecretKeyRing(ring, new ProgressScaler()); - } - } - - } - } diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/TestHelpers.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/TestHelpers.java new file mode 100644 index 000000000..0adc6b264 --- /dev/null +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/TestHelpers.java @@ -0,0 +1,55 @@ +package org.sufficientlysecure.keychain; + + +import android.content.Context; +import android.support.annotation.StringRes; + +import org.hamcrest.CoreMatchers; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow; +import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.ui.util.Notify.Style; +import org.sufficientlysecure.keychain.util.ProgressScaler; + +import static android.support.test.InstrumentationRegistry.getInstrumentation; +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.hasDescendant; +import static android.support.test.espresso.matcher.ViewMatchers.withClassName; +import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSnackbarLineColor; + + +public class TestHelpers { + + + public static void checkSnackbar(Style style, @StringRes Integer text) { + + onView(withClassName(CoreMatchers.endsWith("Snackbar"))) + .check(matches(withSnackbarLineColor(style.mLineColor))); + + if (text != null) { + onView(withClassName(CoreMatchers.endsWith("Snackbar"))) + .check(matches(hasDescendant(withText(text)))); + } + + } + + + static void importKeysFromResource(Context context, String name) throws Exception { + IteratorWithIOThrow stream = UncachedKeyRing.fromStream( + getInstrumentation().getContext().getAssets().open(name)); + + ProviderHelper helper = new ProviderHelper(context); + while(stream.hasNext()) { + UncachedKeyRing ring = stream.next(); + if (ring.isSecret()) { + helper.saveSecretKeyRing(ring, new ProgressScaler()); + } else { + helper.saveSecretKeyRing(ring, new ProgressScaler()); + } + } + + } + +} diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/CheckHelpers.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/CheckHelpers.java deleted file mode 100644 index a950ceeaa..000000000 --- a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/CheckHelpers.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.sufficientlysecure.keychain.actions; - - -import android.support.annotation.StringRes; - -import org.hamcrest.CoreMatchers; -import org.sufficientlysecure.keychain.ui.util.Notify.Style; - -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.hasDescendant; -import static android.support.test.espresso.matcher.ViewMatchers.withClassName; -import static android.support.test.espresso.matcher.ViewMatchers.withText; -import static org.sufficientlysecure.keychain.actions.CustomMatchers.withSnackbarLineColor; - - -abstract public class CheckHelpers { - - public static void checkSnackbar(Style style, @StringRes Integer text) { - - onView(withClassName(CoreMatchers.endsWith("Snackbar"))) - .check(matches(withSnackbarLineColor(style.mLineColor))); - - if (text != null) { - onView(withClassName(CoreMatchers.endsWith("Snackbar"))) - .check(matches(hasDescendant(withText(text)))); - } - - } - -} diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/CustomMatchers.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/CustomMatchers.java deleted file mode 100644 index 29e875b95..000000000 --- a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/CustomMatchers.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.sufficientlysecure.keychain.actions; - - -import android.support.annotation.ColorRes; -import android.support.test.espresso.matcher.BoundedMatcher; -import android.view.View; - -import com.nispok.snackbar.Snackbar; -import org.hamcrest.Description; -import org.hamcrest.Matcher; - -public abstract class CustomMatchers { - - public static Matcher withSnackbarLineColor(@ColorRes final int colorRes) { - return new BoundedMatcher(Snackbar.class) { - public void describeTo(Description description) { - description.appendText("with color resource id: " + colorRes); - } - - @Override - public boolean matchesSafely(Snackbar snackbar) { - return snackbar.getResources().getColor(colorRes) == snackbar.getLineColor(); - } - }; - } - -} diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/matcher/CustomMatchers.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/matcher/CustomMatchers.java new file mode 100644 index 000000000..c023f6411 --- /dev/null +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/matcher/CustomMatchers.java @@ -0,0 +1,48 @@ +package org.sufficientlysecure.keychain.matcher; + + +import android.support.annotation.ColorRes; +import android.support.test.espresso.matcher.BoundedMatcher; +import android.support.test.internal.util.Checks; +import android.view.View; + +import com.nispok.snackbar.Snackbar; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; +import org.sufficientlysecure.keychain.ui.KeyListFragment.KeyListAdapter; +import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter.KeyItem; + +import static android.support.test.internal.util.Checks.checkNotNull; + + +public abstract class CustomMatchers { + + public static Matcher withSnackbarLineColor(@ColorRes final int colorRes) { + return new BoundedMatcher(Snackbar.class) { + public void describeTo(Description description) { + description.appendText("with color resource id: " + colorRes); + } + + @Override + public boolean matchesSafely(Snackbar snackbar) { + return snackbar.getResources().getColor(colorRes) == snackbar.getLineColor(); + } + }; + } + + public static Matcher withKeyItemId(final long keyId) { + return new BoundedMatcher(KeyItem.class) { + @Override + public boolean matchesSafely(KeyItem item) { + return item.mKeyId == keyId; + } + + @Override + public void describeTo(Description description) { + description.appendText("with key id: " + keyId); + } + }; + } + +} -- cgit v1.2.3