1/*
2 * Copyright (C) 2009 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.suitebuilder.annotation.MediumTest;
23import android.view.View;
24import android.widget.CheckBox;
25import android.widget.Spinner;
26import android.widget.SpinnerAdapter;
27
28import com.android.email.R;
29import com.android.emailcommon.provider.Account;
30import com.android.emailcommon.provider.HostAuth;
31
32import java.net.URISyntaxException;
33
34/**
35 * Tests of basic UI logic in the AccountSetupOptions screen.
36 * You can run this entire test case with:
37 *   runtest -c com.android.email.activity.setup.AccountSetupOptionsTests email
38 */
39@MediumTest
40public class AccountSetupOptionsTests
41        extends ActivityInstrumentationTestCase2<AccountSetupOptions> {
42
43    private AccountSetupOptions mActivity;
44    private Spinner mCheckFrequencyView;
45    private CheckBox mBackgroundAttachmentsView;
46
47    public AccountSetupOptionsTests() {
48        super(AccountSetupOptions.class);
49    }
50
51    /**
52     * Test that POP accounts aren't displayed with a push option
53     */
54    public void testPushOptionPOP()
55            throws URISyntaxException {
56        Intent i = getTestIntent("Name", "pop3://user:password@server.com");
57        this.setActivityIntent(i);
58
59        getActivityAndFields();
60
61        boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
62        assertFalse(hasPush);
63    }
64
65    /**
66     * Test that IMAP accounts aren't displayed with a push option
67     */
68    public void testPushOptionIMAP()
69            throws URISyntaxException {
70        Intent i = getTestIntent("Name", "imap://user:password@server.com");
71        this.setActivityIntent(i);
72
73        getActivityAndFields();
74
75        boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
76        assertFalse(hasPush);
77    }
78
79    /**
80     * Test that EAS accounts are displayed with a push option
81     */
82    public void testPushOptionEAS()
83            throws URISyntaxException {
84        Intent i = getTestIntent("Name", "eas://user:password@server.com");
85        this.setActivityIntent(i);
86
87        getActivityAndFields();
88
89        boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
90        assertTrue(hasPush);
91    }
92
93    /**
94     * Test that POP3 accounts don't have a "background attachments" checkbox
95     */
96    public void testBackgroundAttachmentsPop()
97            throws URISyntaxException {
98        checkBackgroundAttachments("pop3://user:password@server.com", false);
99    }
100
101    /**
102     * Test that IMAP accounts have a "background attachments" checkbox
103     */
104    public void testBackgroundAttachmentsImap()
105            throws URISyntaxException {
106        checkBackgroundAttachments("imap://user:password@server.com", true);
107    }
108
109    /**
110     * Test that EAS accounts have a "background attachments" checkbox
111     */
112    public void testBackgroundAttachmentsEas()
113            throws URISyntaxException {
114        checkBackgroundAttachments("eas://user:password@server.com", true);
115    }
116
117    /**
118     * Common code to check that the "background attachments" checkbox is shown/hidden properly
119     */
120    private void checkBackgroundAttachments(String storeUri, boolean expectVisible)
121            throws URISyntaxException {
122        Intent i = getTestIntent("Name", storeUri);
123        this.setActivityIntent(i);
124        getActivityAndFields();
125
126        boolean isNull = mBackgroundAttachmentsView == null;
127        boolean isVisible = !isNull && (mBackgroundAttachmentsView.getVisibility() == View.VISIBLE);
128
129        if (!expectVisible) {
130            assertTrue(!isVisible);
131        } else {
132            assertTrue(!isNull);
133            assertTrue(isVisible);
134        }
135    }
136
137    /**
138     * Get the activity (which causes it to be started, using our intent) and get the UI fields
139     */
140    private void getActivityAndFields() {
141        mActivity = getActivity();
142        mCheckFrequencyView = (Spinner) mActivity.findViewById(R.id.account_check_frequency);
143        mBackgroundAttachmentsView = (CheckBox) mActivity.findViewById(
144                R.id.account_background_attachments);
145    }
146
147    /**
148     * Test the frequency values list for a particular value
149     */
150    private boolean frequencySpinnerHasValue(int value) {
151        SpinnerAdapter sa = mCheckFrequencyView.getAdapter();
152
153        for (int i = 0; i < sa.getCount(); ++i) {
154            SpinnerOption so = (SpinnerOption) sa.getItem(i);
155            if (so != null && ((Integer)so.value).intValue() == value) {
156                return true;
157            }
158        }
159        return false;
160    }
161
162    /**
163     * Create an intent with the Account in it
164     */
165    private Intent getTestIntent(String name, String storeUri)
166            throws URISyntaxException {
167        Account account = new Account();
168        account.setSenderName(name);
169        Context context = getInstrumentation().getTargetContext();
170        HostAuth auth = account.getOrCreateHostAuthRecv(context);
171        HostAuth.setHostAuthFromString(auth, storeUri);
172        SetupData.init(SetupData.FLOW_MODE_NORMAL, account);
173        return new Intent(Intent.ACTION_MAIN);
174    }
175
176}
177