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.ContentUris;
20import android.content.Context;
21import android.content.Intent;
22import android.net.Uri;
23import android.preference.ListPreference;
24import android.preference.PreferenceFragment;
25import android.test.ActivityInstrumentationTestCase2;
26import android.test.suitebuilder.annotation.MediumTest;
27
28import com.android.emailcommon.provider.Account;
29import com.android.emailcommon.provider.HostAuth;
30
31import java.net.URISyntaxException;
32
33/**
34 * Tests of basic UI logic in the Account Settings fragment.
35 *
36 * TODO: This should use a local provider for the test "accounts", and not touch user data
37 * TODO: These cannot run in the single-pane mode, and need to be refactored into single-pane
38 *       and multi-pane versions.  Until then, they are all disabled.
39 *
40 * To execute:  runtest -c com.android.email.activity.setup.AccountSettingsTests email
41 */
42@MediumTest
43public class AccountSettingsTests extends ActivityInstrumentationTestCase2<AccountSettings> {
44
45    private long mAccountId;
46    private Account mAccount;
47
48    private Context mContext;
49    private ListPreference mCheckFrequency;
50
51    private static final String PREFERENCE_FREQUENCY = "account_check_frequency";
52
53    public AccountSettingsTests() {
54        super(AccountSettings.class);
55    }
56
57    /**
58     * Common setup code for all tests.
59     */
60    @Override
61    protected void setUp() throws Exception {
62        super.setUp();
63
64        mContext = getInstrumentation().getTargetContext();
65    }
66
67    /**
68     * Delete any dummy accounts we set up for this test
69     */
70    @Override
71    protected void tearDown() throws Exception {
72        if (mAccount != null) {
73            Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, mAccountId);
74            mContext.getContentResolver().delete(uri, null, null);
75        }
76
77        // must call last because it scrubs member variables
78        super.tearDown();
79    }
80
81    /**
82     * Test that POP accounts aren't displayed with a push option
83     */
84    public void disable_testPushOptionPOP() throws Throwable {
85        Intent i = getTestIntent("Name", "pop3://user:password@server.com",
86                "smtp://user:password@server.com");
87        setActivityIntent(i);
88
89        getActivityAndFields();
90
91        boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
92        assertFalse(hasPush);
93    }
94
95    /**
96     * Test that IMAP accounts aren't displayed with a push option
97     */
98    public void disable_testPushOptionIMAP() throws Throwable {
99        Intent i = getTestIntent("Name", "imap://user:password@server.com",
100                "smtp://user:password@server.com");
101        setActivityIntent(i);
102
103        getActivityAndFields();
104
105        boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
106        assertFalse(hasPush);
107    }
108
109    /**
110     * Test that EAS accounts are displayed with a push option
111     */
112    public void disable_testPushOptionEAS() throws Throwable {
113        Intent i = getTestIntent("Name", "eas://user:password@server.com",
114                "eas://user:password@server.com");
115        setActivityIntent(i);
116
117        getActivityAndFields();
118
119        boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
120        assertTrue(hasPush);
121    }
122
123    /**
124     * Get the activity (which causes it to be started, using our intent) and get the UI fields
125     */
126    private void getActivityAndFields() throws Throwable {
127        final AccountSettings theActivity = getActivity();
128
129        runTestOnUiThread(new Runnable() {
130            public void run() {
131                PreferenceFragment f = (PreferenceFragment) theActivity.mCurrentFragment;
132                mCheckFrequency =
133                    (ListPreference) f.findPreference(PREFERENCE_FREQUENCY);
134            }
135        });
136    }
137
138    /**
139     * Test the frequency values list for a particular value
140     */
141    private boolean frequencySpinnerHasValue(int value) {
142        CharSequence[] values = mCheckFrequency.getEntryValues();
143        for (CharSequence listValue : values) {
144            if (listValue != null && Integer.parseInt(listValue.toString()) == value) {
145                return true;
146            }
147        }
148        return false;
149    }
150
151    /**
152     * Create an intent with the Account in it
153     */
154    private Intent getTestIntent(String name, String storeUri, String senderUri)
155            throws URISyntaxException {
156        mAccount = new Account();
157        mAccount.setSenderName(name);
158        // For EAS, at least, email address is required
159        mAccount.mEmailAddress = "user@server.com";
160        HostAuth.setHostAuthFromString(mAccount.getOrCreateHostAuthRecv(mContext), storeUri);
161        HostAuth.setHostAuthFromString(mAccount.getOrCreateHostAuthSend(mContext), senderUri);
162        mAccount.save(mContext);
163        mAccountId = mAccount.mId;
164
165        return AccountSettings.createAccountSettingsIntent(mContext, mAccountId, null);
166    }
167
168}
169