1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.activity.setup;
18
19import com.android.email.R;
20import com.android.emailcommon.provider.Account;
21import com.android.emailcommon.provider.HostAuth;
22
23import android.content.Context;
24import android.content.Intent;
25import android.test.ActivityInstrumentationTestCase2;
26import android.test.UiThreadTest;
27import android.test.suitebuilder.annotation.MediumTest;
28import android.widget.EditText;
29
30import java.net.URISyntaxException;
31
32/**
33 * Tests of the basic UI logic in the Account Setup Incoming (IMAP / POP3) screen.
34 * You can run this entire test case with:
35 *   runtest -c com.android.email.activity.setup.AccountSetupIncomingTests email
36 */
37@MediumTest
38public class AccountSetupIncomingTests extends
39        ActivityInstrumentationTestCase2<AccountSetupIncoming> {
40
41    private AccountSetupIncoming mActivity;
42    private AccountSetupIncomingFragment mFragment;
43    private EditText mServerView;
44    private EditText mPasswordView;
45
46    public AccountSetupIncomingTests() {
47        super(AccountSetupIncoming.class);
48    }
49
50    /**
51     * Common setup code for all tests.  Sets up a default launch intent, which some tests
52     * will use (others will override).
53     */
54    @Override
55    protected void setUp() throws Exception {
56        super.setUp();
57
58        // This sets up a default URI which can be used by any of the test methods below.
59        // Individual test methods can replace this with a custom URI if they wish
60        // (except those that run on the UI thread - for them, it's too late to change it.)
61        Intent i = getTestIntent("imap://user:password@server.com:999");
62        setActivityIntent(i);
63    }
64
65    /**
66     * Test processing with a complete, good URI -> good fields
67     */
68    public void testGoodUri()
69            throws URISyntaxException {
70        Intent i = getTestIntent("imap://user:password@server.com:999");
71        setActivityIntent(i);
72        getActivityAndFields();
73        assertTrue(mActivity.mNextButtonEnabled);
74    }
75
76    /**
77     * No user is not OK - not enabled
78     */
79    public void testBadUriNoUser()
80            throws URISyntaxException {
81        Intent i = getTestIntent("imap://:password@server.com:999");
82        setActivityIntent(i);
83        getActivityAndFields();
84        assertFalse(mActivity.mNextButtonEnabled);
85    }
86
87    /**
88     * No password is not OK - not enabled
89     */
90    public void testBadUriNoPassword()
91            throws URISyntaxException {
92        Intent i = getTestIntent("imap://user@server.com:999");
93        setActivityIntent(i);
94        getActivityAndFields();
95        assertFalse(mActivity.mNextButtonEnabled);
96    }
97
98    /**
99     * No port is OK - still enabled
100     */
101    public void testGoodUriNoPort()
102            throws URISyntaxException {
103        Intent i = getTestIntent("imap://user:password@server.com");
104        setActivityIntent(i);
105        getActivityAndFields();
106        assertTrue(mActivity.mNextButtonEnabled);
107    }
108
109    /**
110     * Test for non-standard but OK server names
111     */
112    @UiThreadTest
113    public void testGoodServerVariants() {
114        getActivityAndFields();
115        assertTrue(mActivity.mNextButtonEnabled);
116
117        mServerView.setText("  server.com  ");
118        assertTrue(mActivity.mNextButtonEnabled);
119    }
120
121    /**
122     * Test for non-empty but non-OK server names
123     */
124    @UiThreadTest
125    public void testBadServerVariants() {
126        getActivityAndFields();
127        assertTrue(mActivity.mNextButtonEnabled);
128
129        mServerView.setText("  ");
130        assertFalse(mActivity.mNextButtonEnabled);
131
132        mServerView.setText("serv$er.com");
133        assertFalse(mActivity.mNextButtonEnabled);
134    }
135
136    /**
137     * Test to confirm that passwords with leading or trailing spaces are accepted verbatim.
138     */
139    @UiThreadTest
140    public void testPasswordNoTrim() throws URISyntaxException {
141        getActivityAndFields();
142
143        // Clear the password - should disable
144        checkPassword(null, false);
145
146        // Various combinations of spaces should be OK
147        checkPassword(" leading", true);
148        checkPassword("trailing ", true);
149        checkPassword("em bedded", true);
150        checkPassword(" ", true);
151    }
152
153    /**
154     * Check password field for a given password.  Should be called in UI thread.  Confirms that
155     * the password has not been trimmed.
156     *
157     * @param password the password to test with
158     * @param expectNext true if expected that this password will enable the "next" button
159     */
160    private void checkPassword(String password, boolean expectNext) throws URISyntaxException {
161        mPasswordView.setText(password);
162        if (expectNext) {
163            assertTrue(mActivity.mNextButtonEnabled);
164        } else {
165            assertFalse(mActivity.mNextButtonEnabled);
166        }
167    }
168
169    /**
170     * TODO:  A series of tests to explore the logic around security models & ports
171     * TODO:  A series of tests exploring differences between IMAP and POP3
172     */
173
174    /**
175     * Get the activity (which causes it to be started, using our intent) and get the UI fields
176     */
177    private void getActivityAndFields() {
178        mActivity = getActivity();
179        mFragment = mActivity.mFragment;
180        mServerView = (EditText) mActivity.findViewById(R.id.account_server);
181        mPasswordView = (EditText) mActivity.findViewById(R.id.account_password);
182    }
183
184    /**
185     * Create an intent with the Account in it
186     */
187    private Intent getTestIntent(String storeUriString)
188            throws URISyntaxException {
189        Account account = new Account();
190        Context context = getInstrumentation().getTargetContext();
191        HostAuth auth = account.getOrCreateHostAuthRecv(context);
192        HostAuth.setHostAuthFromString(auth, storeUriString);
193        SetupData.init(SetupData.FLOW_MODE_NORMAL, account);
194        return new Intent(Intent.ACTION_MAIN);
195    }
196
197}
198