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.Context; 20 import android.content.Intent; 21 import android.test.ActivityInstrumentationTestCase2; 22 import android.test.UiThreadTest; 23 import android.test.suitebuilder.annotation.MediumTest; 24 import android.view.View; 25 import android.widget.CheckBox; 26 import android.widget.EditText; 27 28 import com.android.email.R; 29 import com.android.email.provider.ProviderTestUtils; 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 the basic UI logic in the Account Setup Incoming (IMAP / POP3) screen. 37 * You can run this entire test case with: 38 * runtest -c com.android.email.activity.setup.AccountSetupExchangeTests email 39 */ 40 @MediumTest 41 public class AccountSetupExchangeTests extends 42 ActivityInstrumentationTestCase2<AccountSetupExchange> { 43 //EXCHANGE-REMOVE-SECTION-START 44 private AccountSetupExchange mActivity; 45 private AccountSetupExchangeFragment mFragment; 46 private EditText mServerView; 47 private EditText mPasswordView; 48 private CheckBox mSslRequiredCheckbox; 49 private CheckBox mTrustAllCertificatesCheckbox; 50 //EXCHANGE-REMOVE-SECTION-END 51 52 public AccountSetupExchangeTests() { 53 super(AccountSetupExchange.class); 54 } 55 56 //EXCHANGE-REMOVE-SECTION-START 57 /** 58 * Common setup code for all tests. Sets up a default launch intent, which some tests 59 * will use (others will override). 60 */ 61 @Override 62 protected void setUp() throws Exception { 63 super.setUp(); 64 65 // This sets up a default URI which can be used by any of the test methods below. 66 // Individual test methods can replace this with a custom URI if they wish 67 // (except those that run on the UI thread - for them, it's too late to change it.) 68 Intent i = getTestIntent("eas://user:password (at) server.com"); 69 setActivityIntent(i); 70 } 71 72 /** 73 * Test processing with a complete, good URI -> good fields 74 */ 75 public void testGoodUri() throws URISyntaxException { 76 Intent i = getTestIntent("eas://user:password (at) server.com"); 77 setActivityIntent(i); 78 getActivityAndFields(); 79 assertTrue(mActivity.mNextButtonEnabled); 80 } 81 82 // TODO Add tests for valid usernames in eas 83 // They would be <name> or <name>\<domain> or <name>/<domain> or a valid email address 84 85 /** 86 * No user is not OK - not enabled 87 */ 88 public void testBadUriNoUser() throws URISyntaxException { 89 Intent i = getTestIntent("eas://:password (at) server.com"); 90 setActivityIntent(i); 91 getActivityAndFields(); 92 assertFalse(mActivity.mNextButtonEnabled); 93 } 94 95 /** 96 * No password is not OK - not enabled 97 */ 98 public void testBadUriNoPassword() throws URISyntaxException { 99 Intent i = getTestIntent("eas://user (at) server.com"); 100 setActivityIntent(i); 101 getActivityAndFields(); 102 assertFalse(mActivity.mNextButtonEnabled); 103 } 104 105 /** 106 * Test for non-standard but OK server names 107 */ 108 @UiThreadTest 109 public void testGoodServerVariants() { 110 getActivityAndFields(); 111 assertTrue(mActivity.mNextButtonEnabled); 112 113 mServerView.setText(" server.com "); 114 assertTrue(mActivity.mNextButtonEnabled); 115 } 116 117 /** 118 * Test for non-empty but non-OK server names 119 */ 120 @UiThreadTest 121 public void testBadServerVariants() { 122 getActivityAndFields(); 123 assertTrue(mActivity.mNextButtonEnabled); 124 125 mServerView.setText(" "); 126 assertFalse(mActivity.mNextButtonEnabled); 127 128 mServerView.setText("serv$er.com"); 129 assertFalse(mActivity.mNextButtonEnabled); 130 } 131 132 /** 133 * Test to confirm that passwords with leading or trailing spaces are accepted verbatim. 134 */ 135 @UiThreadTest 136 public void testPasswordNoTrim() { 137 getActivityAndFields(); 138 139 // Clear the password - should disable 140 checkPassword(null, false); 141 142 // Various combinations of spaces should be OK 143 checkPassword(" leading", true); 144 checkPassword("trailing ", true); 145 checkPassword("em bedded", true); 146 checkPassword(" ", true); 147 } 148 149 /** 150 * Check password field for a given password. Should be called in UI thread. Confirms that 151 * the password has not been trimmed. 152 * 153 * @param password the password to test with 154 * @param expectNext true if expected that this password will enable the "next" button 155 */ 156 private void checkPassword(String password, boolean expectNext) { 157 mPasswordView.setText(password); 158 if (expectNext) { 159 assertTrue(mActivity.mNextButtonEnabled); 160 } else { 161 assertFalse(mActivity.mNextButtonEnabled); 162 } 163 } 164 165 /** 166 * Test aspects of loadSettings() 167 * 168 * TODO: More cases 169 */ 170 @UiThreadTest 171 public void testLoadSettings() { 172 // The default URI has no SSL and no "trust" 173 getActivityAndFields(); 174 assertFalse(mSslRequiredCheckbox.isChecked()); 175 assertFalse(mTrustAllCertificatesCheckbox.isChecked()); 176 assertFalse(mTrustAllCertificatesCheckbox.getVisibility() == View.VISIBLE); 177 178 // Setup host auth with variants of SSL enabled and check. This also enables the 179 // "trust certificates" checkbox (not checked, but visible now). 180 Account account = 181 ProviderTestUtils.setupAccount("account", false, mActivity.getBaseContext()); 182 account.mHostAuthRecv = ProviderTestUtils.setupHostAuth( 183 "eas", "hostauth", false, mActivity.getBaseContext()); 184 account.mHostAuthRecv.mFlags |= HostAuth.FLAG_SSL; 185 account.mHostAuthRecv.mFlags &= ~HostAuth.FLAG_TRUST_ALL; 186 mActivity.mFragment.mLoaded = false; 187 boolean loadResult = mActivity.mFragment.loadSettings(account); 188 assertTrue(loadResult); 189 assertTrue(mSslRequiredCheckbox.isChecked()); 190 assertFalse(mTrustAllCertificatesCheckbox.isChecked()); 191 assertTrue(mTrustAllCertificatesCheckbox.getVisibility() == View.VISIBLE); 192 193 // Setup host auth with variants of SSL enabled and check. This also enables the 194 // "trust certificates" checkbox (not checked, but visible now). 195 account.mHostAuthRecv.mFlags |= HostAuth.FLAG_TRUST_ALL; 196 mActivity.mFragment.mLoaded = false; 197 loadResult = mActivity.mFragment.loadSettings(account); 198 assertTrue(loadResult); 199 assertTrue(mSslRequiredCheckbox.isChecked()); 200 assertTrue(mTrustAllCertificatesCheckbox.isChecked()); 201 assertTrue(mTrustAllCertificatesCheckbox.getVisibility() == View.VISIBLE); 202 203 // A simple test of an incomplete account, which will fail validation 204 account.mHostAuthRecv.mPassword = ""; 205 mActivity.mFragment.mLoaded = false; 206 loadResult = mActivity.mFragment.loadSettings(account); 207 assertFalse(loadResult); 208 } 209 210 /** 211 * TODO: Directly test validateFields() checking boolean result 212 */ 213 214 /** 215 * Get the activity (which causes it to be started, using our intent) and get the UI fields 216 */ 217 private void getActivityAndFields() { 218 mActivity = getActivity(); 219 mFragment = mActivity.mFragment; 220 mServerView = (EditText) mActivity.findViewById(R.id.account_server); 221 mPasswordView = (EditText) mActivity.findViewById(R.id.account_password); 222 mSslRequiredCheckbox = (CheckBox) mActivity.findViewById(R.id.account_ssl); 223 mTrustAllCertificatesCheckbox = 224 (CheckBox) mActivity.findViewById(R.id.account_trust_certificates); 225 } 226 227 /** 228 * Create an intent with the Account in it 229 */ 230 private Intent getTestIntent(String storeUriString) throws URISyntaxException { 231 Account account = new Account(); 232 Context context = getInstrumentation().getTargetContext(); 233 HostAuth auth = account.getOrCreateHostAuthRecv(context); 234 HostAuth.setHostAuthFromString(auth, storeUriString); 235 Intent i = new Intent(Intent.ACTION_MAIN); 236 SetupData.init(SetupData.FLOW_MODE_NORMAL, account); 237 SetupData.setAllowAutodiscover(false); 238 return i; 239 } 240 //EXCHANGE-REMOVE-SECTION-END 241 } 242