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