AccountSetupOutgoingTests.java revision 951eb080f98e86d50cb549ee52fb72d8f07196c3
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 android.content.Context;
20import android.content.Intent;
21import android.test.ActivityInstrumentationTestCase2;
22import android.test.UiThreadTest;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.widget.CheckBox;
25import android.widget.EditText;
26
27import com.android.email.R;
28import com.android.emailcommon.provider.Account;
29import com.android.emailcommon.provider.HostAuth;
30
31import java.net.URISyntaxException;
32
33/**
34 * Tests of the basic UI logic in the Account Setup Outgoing (SMTP) screen.
35 * You can run this entire test case with:
36 *   runtest -c com.android.email.activity.setup.AccountSetupOutgoingTests email
37 */
38@MediumTest
39public class AccountSetupOutgoingTests extends
40        ActivityInstrumentationTestCase2<AccountSetupFinal> {
41
42    private AccountSetupFinal mActivity;
43    private EditText mServerView;
44    private AuthenticationView mAuthenticationView;
45
46    public AccountSetupOutgoingTests() {
47        super(AccountSetupFinal.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("smtp://user:password@server.com:999");
62        setActivityIntent(i);
63    }
64
65    private boolean isNextButtonEnabled() {
66        return mActivity.mNextButton.isEnabled();
67    }
68
69    /**
70     * Test processing with a complete, good URI -> good fields
71     */
72    @UiThreadTest
73    public void testGoodUri() {
74        getActivityAndFields();
75        assertTrue(isNextButtonEnabled());
76    }
77
78    /**
79     * No user is not OK - not enabled
80     */
81    public void testBadUriNoUser()
82            throws Throwable {
83        Intent i = getTestIntent("smtp://:password@server.com:999");
84        setActivityIntent(i);
85        getActivityAndFields();
86        runTestOnUiThread(new Runnable() {
87            @Override
88            public void run() {
89                final CheckBox requireLoginView = (CheckBox)
90                        mActivity.findViewById(R.id.account_require_login);
91                requireLoginView.setChecked(true);
92            }
93        });
94        assertFalse(isNextButtonEnabled());
95    }
96
97    /**
98     * No password is not OK - not enabled
99     */
100    public void testBadUriNoPassword()
101            throws URISyntaxException {
102        Intent i = getTestIntent("smtp://user@server.com:999");
103        setActivityIntent(i);
104        getActivityAndFields();
105        assertFalse(isNextButtonEnabled());
106    }
107
108    /**
109     * No port is OK - still enabled
110     */
111    public void testGoodUriNoPort()
112            throws URISyntaxException {
113        Intent i = getTestIntent("smtp://user:password@server.com");
114        setActivityIntent(i);
115        getActivityAndFields();
116        assertTrue(isNextButtonEnabled());
117    }
118
119    /**
120     * Test for non-standard but OK server names
121     */
122    @UiThreadTest
123    public void testGoodServerVariants() {
124        getActivityAndFields();
125        assertTrue(isNextButtonEnabled());
126
127        mServerView.setText("  server.com  ");
128        assertTrue(isNextButtonEnabled());
129    }
130
131    /**
132     * Test for non-empty but non-OK server names
133     */
134    @UiThreadTest
135    public void testBadServerVariants() {
136        getActivityAndFields();
137        assertTrue(isNextButtonEnabled());
138
139        mServerView.setText("  ");
140        assertFalse(isNextButtonEnabled());
141
142        mServerView.setText("serv$er.com");
143        assertFalse(isNextButtonEnabled());
144    }
145
146    /**
147     * Test to confirm that passwords with leading or trailing spaces are accepted verbatim.
148     */
149    @UiThreadTest
150    public void brokentestPasswordNoTrim() throws URISyntaxException {
151        getActivityAndFields();
152
153        // Clear the password - should disable
154        checkPassword(null, false);
155
156        // Various combinations of spaces should be OK
157        checkPassword(" leading", true);
158        checkPassword("trailing ", true);
159// TODO: need to fix this part of the test
160//        checkPassword("em bedded", true);
161//        checkPassword(" ", true);
162    }
163
164    /**
165     * Check password field for a given password.  Should be called in UI thread.  Confirms that
166     * the password has not been trimmed.
167     *
168     * @param password the password to test with
169     * @param expectNext true if expected that this password will enable the "next" button
170     */
171    private void checkPassword(String password, boolean expectNext) throws URISyntaxException {
172        mAuthenticationView.setPassword(password);
173        if (expectNext) {
174            assertTrue(isNextButtonEnabled());
175        } else {
176            assertFalse(isNextButtonEnabled());
177        }
178    }
179
180    /**
181     * TODO:  A series of tests to explore the logic around security models & ports
182     */
183
184    /**
185     * Get the activity (which causes it to be started, using our intent) and get the UI fields
186     */
187    private void getActivityAndFields() {
188        mActivity = getActivity();
189        mServerView = (EditText) mActivity.findViewById(R.id.account_server);
190        mAuthenticationView = (AuthenticationView) mActivity.findViewById(R.id.authentication_view);
191    }
192
193    /**
194     * Create an intent with the Account in it
195     */
196    private Intent getTestIntent(String senderUriString)
197            throws URISyntaxException {
198        final Account account = new Account();
199        final Context context = getInstrumentation().getTargetContext();
200        final HostAuth auth = account.getOrCreateHostAuthSend(context);
201        auth.setHostAuthFromString(senderUriString);
202        final SetupDataFragment setupDataFragment =
203                new SetupDataFragment(SetupDataFragment.FLOW_MODE_NORMAL, account);
204        final Intent i = new Intent(AccountSetupFinal.ACTION_JUMP_TO_OUTGOING);
205        i.putExtra(SetupDataFragment.EXTRA_SETUP_DATA, setupDataFragment);
206        return i;
207    }
208
209}
210