aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/CreateKeyActivityTest.java
blob: 049b389b58b43182918f5d6ab8e90f7e7521c1bd (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * 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;


import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sufficientlysecure.keychain.ui.MainActivity;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.swipeLeft;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.isDialog;
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
import static android.support.test.espresso.matcher.ViewMatchers.hasSibling;
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.withText;
import static org.hamcrest.Matchers.allOf;
import static org.sufficientlysecure.keychain.matcher.EditTextMatchers.withError;
import static org.sufficientlysecure.keychain.matcher.EditTextMatchers.withTransformationMethod;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class CreateKeyActivityTest {

    public static final String SAMPLE_NAME = "Sample Name";
    public static final String SAMPLE_EMAIL = "sample_email@gmail.com";
    public static final String SAMPLE_ADDITIONAL_EMAIL = "sample_additional_email@gmail.com";
    public static final String SAMPLE_PASSWORD = "sample_password";

    @Rule
    public final ActivityTestRule<MainActivity> mActivity
            = new ActivityTestRule<MainActivity>(MainActivity.class) {
        @Override
        protected Intent getActivityIntent() {
            Intent intent = super.getActivityIntent();
            intent.putExtra(MainActivity.EXTRA_SKIP_FIRST_TIME, true);
            return intent;
        }
    };

    public void testCreateMyKey() {

        mActivity.getActivity();

        // Clicks create my key
        onView(withId(R.id.create_key_create_key_button))
                .perform(click());

        // Clicks next with empty name
        onView(withId(R.id.create_key_next_button))
                .perform(click());
        onView(withId(R.id.create_key_name))
                .check(matches(withError(R.string.create_key_empty)));

        // Types name and clicks next
        onView(withId(R.id.create_key_name))
                .perform(typeText(SAMPLE_NAME));
        onView(withId(R.id.create_key_next_button))
                .perform(click());

        // Clicks next with empty email
        onView(withId(R.id.create_key_next_button))
                .perform(click());
        onView(withId(R.id.create_key_email))
                .check(matches(withError(R.string.create_key_empty)));

        // Types email
        onView(withId(R.id.create_key_email))
                .perform(typeText(SAMPLE_EMAIL));

        // Adds same email as additional email and dismisses the snackbar
        onView(withId(R.id.create_key_add_email))
                .perform(click());
        onView(withId(R.id.add_email_address))
                .perform(typeText(SAMPLE_EMAIL));
        onView(withText(android.R.string.ok))
                .inRoot(isDialog())
                .perform(click());
        onView(allOf(withId(R.id.sb__text), withText(R.string.create_key_email_already_exists_text)))
                .check(matches(isDisplayed()));
        onView(allOf(withId(R.id.sb__text), withText(R.string.create_key_email_already_exists_text)))
                .perform(swipeLeft());

        // Adds additional email
        onView(withId(R.id.create_key_add_email))
                .perform(click());
        onView(withId(R.id.add_email_address))
                .perform(typeText(SAMPLE_ADDITIONAL_EMAIL));
        onView(withText(android.R.string.ok))
                .inRoot(isDialog())
                .perform(click());
        onView(withId(R.id.create_key_emails))
                .check(matches(hasDescendant(allOf(withId(R.id.create_key_email_item_email), withText(SAMPLE_ADDITIONAL_EMAIL)))));

        // Removes additional email and clicks next
        onView(allOf(withId(R.id.create_key_email_item_delete_button), hasSibling(allOf(withId(R.id.create_key_email_item_email), withText(SAMPLE_ADDITIONAL_EMAIL)))))
                .perform(click())
                .check(doesNotExist());
        onView(withId(R.id.create_key_next_button))
                .perform(click(click()));

        // Clicks next with empty password
        onView(withId(R.id.create_key_next_button))
                .perform(click());
        onView(withId(R.id.create_key_passphrase))
                .check(matches(withError(R.string.create_key_empty)));

        // Types password
        onView(withId(R.id.create_key_passphrase))
                .perform(typeText(SAMPLE_PASSWORD));

        // Clicks next with empty confirm password
        onView(withId(R.id.create_key_next_button))
                .perform(click());
        onView(withId(R.id.create_key_passphrase_again))
                .check(matches(withError(R.string.create_key_passphrases_not_equal)));

        // Types confirm password
        onView(withId(R.id.create_key_passphrase_again))
                .perform(typeText(SAMPLE_PASSWORD));

        // Clicks show password twice and clicks next
        onView(withId(R.id.create_key_show_passphrase))
                .perform(click());
        onView(withId(R.id.create_key_passphrase))
                .check(matches(withTransformationMethod(HideReturnsTransformationMethod.class)));
        onView(withId(R.id.create_key_passphrase_again))
                .check(matches(withTransformationMethod(HideReturnsTransformationMethod.class)));
        onView(withId(R.id.create_key_show_passphrase))
                .perform(click());
        onView(withId(R.id.create_key_passphrase))
                .check(matches(withTransformationMethod(PasswordTransformationMethod.class)));
        onView(withId(R.id.create_key_passphrase_again))
                .check(matches(withTransformationMethod(PasswordTransformationMethod.class)));
        onView(withId(R.id.create_key_next_button))
                .perform(click());

        // Verifies name and email
        onView(withId(R.id.name))
                .check(matches(withText(SAMPLE_NAME)));
        onView(withId(R.id.email))
                .check(matches(withText(SAMPLE_EMAIL)));

        // Verifies backstack
        onView(withId(R.id.create_key_back_button))
                .perform(click());
        onView(withId(R.id.create_key_back_button))
                .perform(click());
        onView(withId(R.id.create_key_back_button))
                .perform(click());

        onView(withId(R.id.create_key_name))
                .check(matches(withText(SAMPLE_NAME)));
        onView(withId(R.id.create_key_next_button))
                .perform(click());

        onView(withId(R.id.create_key_email))
                .check(matches(withText(SAMPLE_EMAIL)));
        onView(withId(R.id.create_key_next_button))
                .perform(click());

        // TODO: Uncomment when fixed in main
//        onView(withId(R.id.create_key_passphrase))
//                .check(matches(withText(SAMPLE_PASSWORD)));
//        onView(withId(R.id.create_key_passphrase_again))
//                .check(matches(withText(SAMPLE_PASSWORD)));
        onView(withId(R.id.create_key_next_button))
                .perform(click());

        onView(withId(R.id.name))
                .check(matches(withText(SAMPLE_NAME)));
        onView(withId(R.id.email))
                .check(matches(withText(SAMPLE_EMAIL)));

        // Clicks create key
        onView(withId(R.id.create_key_next_button))
                .perform(click());
    }

}