/* * Copyright (C) 2015 Vincent Breitmoser * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ package org.sufficientlysecure.keychain.matcher; import androidx.annotation.ColorRes; import androidx.annotation.IdRes; import androidx.test.espresso.matcher.BoundedMatcher; import androidx.recyclerview.widget.RecyclerView; import android.view.View; import android.widget.ViewAnimator; import com.nispok.snackbar.Snackbar; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.sufficientlysecure.keychain.R; import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant; import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withParent; import static androidx.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.not; import static org.sufficientlysecure.keychain.matcher.DrawableMatcher.withDrawable; public abstract class CustomMatchers { public static Matcher withDisplayedChild(final int child) { return new BoundedMatcher(ViewAnimator.class) { public void describeTo(Description description) { description.appendText("with displayed child: " + child); } @Override public boolean matchesSafely(ViewAnimator viewAnimator) { return viewAnimator.getDisplayedChild() == child; } }; } 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); } }; } public static Matcher withKeyHolderId(final long keyId) { return new BoundedMatcher(RecyclerView.ViewHolder.class) { @Override public void describeTo(Description description) { description.appendText("with ViewHolder id: " + keyId); } @Override protected boolean matchesSafely(View item) { return item.getItemId() == keyId; } }; } public static Matcher withKeyToken(@ColorRes final long keyId) { return new BoundedMatcher(EncryptKeyCompletionView.class) { public void describeTo(Description description) { description.appendText("with key id token: " + keyId); } @Override public boolean matchesSafely(EncryptKeyCompletionView tokenView) { for (Object object : tokenView.getObjects()) { if (object instanceof KeyItem && ((KeyItem) object).mKeyId == keyId) { return true; } } return false; } }; } public static Matcher withRecyclerView(@IdRes int viewId) { return allOf(isAssignableFrom(RecyclerView.class), withId(viewId)); } public static Matcher isRecyclerItemView(@IdRes int recyclerId, Matcher specificChildMatcher) { return allOf(withParent(withRecyclerView(recyclerId)), specificChildMatcher); } public static Matcher withEncryptionStatus(boolean encrypted) { if (encrypted) { return allOf( hasDescendant(allOf( withId(R.id.result_encryption_text), withText(R.string.decrypt_result_encrypted))), hasDescendant(allOf( withId(R.id.result_encryption_icon), withDrawable(R.drawable.status_lock_closed_24dp, true))) ); } else { return allOf( hasDescendant(allOf( withId(R.id.result_encryption_text), withText(R.string.decrypt_result_not_encrypted))), hasDescendant(allOf( withId(R.id.result_encryption_icon), withDrawable(R.drawable.status_lock_open_24dp, true))) ); } } public static Matcher withSignatureNone() { return allOf( hasDescendant(allOf( withId(R.id.result_signature_text), withText(R.string.decrypt_result_no_signature))), hasDescendant(allOf( withId(R.id.result_signature_icon), withDrawable(R.drawable.status_signature_invalid_cutout_24dp, true))), hasDescendant(allOf( withId(R.id.result_signature_layout), not(isDisplayed()))) ); } public static Matcher withSignatureMyKey() { return allOf( hasDescendant(allOf( withId(R.id.result_signature_text), withText(R.string.decrypt_result_signature_certified))), hasDescendant(allOf( withId(R.id.result_signature_icon), withDrawable(R.drawable.status_signature_verified_cutout_24dp, true))), hasDescendant(allOf( withId(R.id.result_signature_layout), isDisplayed())) ); } }