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.email.provider.EmailContent;
21
22import android.content.Intent;
23import android.test.ActivityInstrumentationTestCase2;
24import android.test.UiThreadTest;
25import android.test.suitebuilder.annotation.MediumTest;
26import android.widget.Button;
27import android.widget.EditText;
28
29/**
30 * Tests of the basic UI logic in the Account Setup Incoming (IMAP / POP3) screen.
31 */
32@MediumTest
33public class AccountSetupIncomingTests extends
34        ActivityInstrumentationTestCase2<AccountSetupIncoming> {
35
36    // borrowed from AccountSetupIncoming
37    private static final String EXTRA_ACCOUNT = "account";
38
39    private AccountSetupIncoming mActivity;
40    private EditText mServerView;
41    private Button mNextButton;
42
43    public AccountSetupIncomingTests() {
44        super(AccountSetupIncoming.class);
45    }
46
47    /**
48     * Common setup code for all tests.  Sets up a default launch intent, which some tests
49     * will use (others will override).
50     */
51    @Override
52    protected void setUp() throws Exception {
53        super.setUp();
54
55        // This sets up a default URI which can be used by any of the test methods below.
56        // Individual test methods can replace this with a custom URI if they wish
57        // (except those that run on the UI thread - for them, it's too late to change it.)
58        Intent i = getTestIntent("imap://user:password@server.com:999");
59        setActivityIntent(i);
60    }
61
62    /**
63     * Test processing with a complete, good URI -> good fields
64     */
65    public void testGoodUri() {
66        Intent i = getTestIntent("imap://user:password@server.com:999");
67        setActivityIntent(i);
68        getActivityAndFields();
69        assertTrue(mNextButton.isEnabled());
70    }
71
72    /**
73     * No user is not OK - not enabled
74     */
75    public void testBadUriNoUser() {
76        Intent i = getTestIntent("imap://:password@server.com:999");
77        setActivityIntent(i);
78        getActivityAndFields();
79        assertFalse(mNextButton.isEnabled());
80    }
81
82    /**
83     * No password is not OK - not enabled
84     */
85    public void testBadUriNoPassword() {
86        Intent i = getTestIntent("imap://user@server.com:999");
87        setActivityIntent(i);
88        getActivityAndFields();
89        assertFalse(mNextButton.isEnabled());
90    }
91
92    /**
93     * No port is OK - still enabled
94     */
95    public void testGoodUriNoPort() {
96        Intent i = getTestIntent("imap://user:password@server.com");
97        setActivityIntent(i);
98        getActivityAndFields();
99        assertTrue(mNextButton.isEnabled());
100    }
101
102    /**
103     * Test for non-standard but OK server names
104     */
105    @UiThreadTest
106    public void testGoodServerVariants() {
107        getActivityAndFields();
108        assertTrue(mNextButton.isEnabled());
109
110        mServerView.setText("  server.com  ");
111        assertTrue(mNextButton.isEnabled());
112    }
113
114    /**
115     * Test for non-empty but non-OK server names
116     */
117    @UiThreadTest
118    public void testBadServerVariants() {
119        getActivityAndFields();
120        assertTrue(mNextButton.isEnabled());
121
122        mServerView.setText("  ");
123        assertFalse(mNextButton.isEnabled());
124
125        mServerView.setText("serv$er.com");
126        assertFalse(mNextButton.isEnabled());
127    }
128
129    /**
130     * TODO:  A series of tests to explore the logic around security models & ports
131     * TODO:  A series of tests exploring differences between IMAP and POP3
132     */
133
134    /**
135     * Get the activity (which causes it to be started, using our intent) and get the UI fields
136     */
137    private void getActivityAndFields() {
138        mActivity = getActivity();
139        mServerView = (EditText) mActivity.findViewById(R.id.account_server);
140        mNextButton = (Button) mActivity.findViewById(R.id.next);
141    }
142
143    /**
144     * Create an intent with the Account in it
145     */
146    private Intent getTestIntent(String storeUriString) {
147        EmailContent.Account account = new EmailContent.Account();
148        account.setStoreUri(getInstrumentation().getTargetContext(), storeUriString);
149        Intent i = new Intent(Intent.ACTION_MAIN);
150        i.putExtra(EXTRA_ACCOUNT, account);
151        return i;
152    }
153
154}
155