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 
17 package com.android.email.activity.setup;
18 
19 import android.content.ContentUris;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.preference.ListPreference;
24 import android.test.ActivityInstrumentationTestCase2;
25 import android.test.suitebuilder.annotation.MediumTest;
26 import android.test.suitebuilder.annotation.Suppress;
27 
28 import com.android.emailcommon.provider.Account;
29 
30 import 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
43 public 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 
EmailPreferenceActivityTests()54     public EmailPreferenceActivityTests() {
55         super(EmailPreferenceActivity.class);
56     }
57 
58     /**
59      * Common setup code for all tests.
60      */
61     @Override
setUp()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
tearDown()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      */
testPushOptionPOP()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      */
testPushOptionIMAP()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      */
testPushOptionEAS()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      */
getActivityAndFields()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      */
frequencySpinnerHasValue(int value)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      */
getTestIntent(String name, String storeUri, String senderUri)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