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