aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/matcher/CustomMatchers.java
blob: 6713cd237a81d25dda599fd4588fbf2045297e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2015 Vincent Breitmoser <look@my.amazin.horse>
 *
 * 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 <http://www.gnu.org/licenses/>.
 *
 */

package org.sufficientlysecure.keychain.matcher;


import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.support.v7.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 org.sufficientlysecure.keychain.ui.adapter.KeyAdapter.KeyItem;
import org.sufficientlysecure.keychain.ui.widget.EncryptKeyCompletionView;

import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
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.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.not;
import static org.sufficientlysecure.keychain.matcher.DrawableMatcher.withDrawable;


public abstract class CustomMatchers {

    public static Matcher<View> withDisplayedChild(final int child) {
        return new BoundedMatcher<View, ViewAnimator>(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<View> withSnackbarLineColor(@ColorRes final int colorRes) {
        return new BoundedMatcher<View, Snackbar>(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<Object> withKeyItemId(final long keyId) {
        return new BoundedMatcher<Object, KeyItem>(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<View> withKeyToken(@ColorRes final long keyId) {
        return new BoundedMatcher<View, EncryptKeyCompletionView>(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<View> withRecyclerView(@IdRes int viewId) {
        return allOf(isAssignableFrom(RecyclerView.class), withId(viewId));
    }

    public static Matcher<View> isRecyclerItemView(@IdRes int recyclerId, Matcher<View> specificChildMatcher) {
        return allOf(withParent(withRecyclerView(recyclerId)), specificChildMatcher);
    }

    public static Matcher<View> 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<View> 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<View> 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()))
        );

    }

}