1 /* 2 * Copyright (C) 2008 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.test.suitebuilder.annotation.Suppress; 25 import android.widget.EditText; 26 27 import com.android.email.R; 28 import com.android.email.activity.setup.AccountSetupIncoming; 29 import com.android.email.activity.setup.AccountSetupIncomingFragment; 30 import com.android.email.activity.setup.SetupData; 31 import com.android.emailcommon.provider.Account; 32 import com.android.emailcommon.provider.HostAuth; 33 34 import java.net.URISyntaxException; 35 36 /** 37 * Tests of the basic UI logic in the Account Setup Incoming (IMAP / POP3) screen. 38 * You can run this entire test case with: 39 * runtest -c com.android.email.activity.setup.AccountSetupIncomingTests email 40 */ 41 @Suppress 42 @MediumTest 43 public class AccountSetupIncomingTests extends 44 ActivityInstrumentationTestCase2<AccountSetupIncoming> { 45 46 private AccountSetupIncoming mActivity; 47 private AccountSetupIncomingFragment mFragment; 48 private EditText mServerView; 49 private EditText mPasswordView; 50 51 public AccountSetupIncomingTests() { 52 super(AccountSetupIncoming.class); 53 } 54 55 /** 56 * Common setup code for all tests. Sets up a default launch intent, which some tests 57 * will use (others will override). 58 */ 59 @Override 60 protected void setUp() throws Exception { 61 super.setUp(); 62 63 // This sets up a default URI which can be used by any of the test methods below. 64 // Individual test methods can replace this with a custom URI if they wish 65 // (except those that run on the UI thread - for them, it's too late to change it.) 66 Intent i = getTestIntent("imap://user:password (at) server.com:999"); 67 setActivityIntent(i); 68 } 69 70 /** 71 * Test processing with a complete, good URI -> good fields 72 */ 73 public void testGoodUri() 74 throws URISyntaxException { 75 Intent i = getTestIntent("imap://user:password (at) server.com:999"); 76 setActivityIntent(i); 77 getActivityAndFields(); 78 assertTrue(mActivity.mNextButtonEnabled); 79 } 80 81 /** 82 * No user is not OK - not enabled 83 */ 84 public void testBadUriNoUser() 85 throws URISyntaxException { 86 Intent i = getTestIntent("imap://:password (at) server.com:999"); 87 setActivityIntent(i); 88 getActivityAndFields(); 89 assertFalse(mActivity.mNextButtonEnabled); 90 } 91 92 /** 93 * No password is not OK - not enabled 94 */ 95 public void testBadUriNoPassword() 96 throws URISyntaxException { 97 Intent i = getTestIntent("imap://user (at) server.com:999"); 98 setActivityIntent(i); 99 getActivityAndFields(); 100 assertFalse(mActivity.mNextButtonEnabled); 101 } 102 103 /** 104 * No port is OK - still enabled 105 */ 106 public void testGoodUriNoPort() 107 throws URISyntaxException { 108 Intent i = getTestIntent("imap://user:password (at) server.com"); 109 setActivityIntent(i); 110 getActivityAndFields(); 111 assertTrue(mActivity.mNextButtonEnabled); 112 } 113 114 /** 115 * Test for non-standard but OK server names 116 */ 117 @UiThreadTest 118 public void testGoodServerVariants() { 119 getActivityAndFields(); 120 assertTrue(mActivity.mNextButtonEnabled); 121 122 mServerView.setText(" server.com "); 123 assertTrue(mActivity.mNextButtonEnabled); 124 } 125 126 /** 127 * Test for non-empty but non-OK server names 128 */ 129 @UiThreadTest 130 public void testBadServerVariants() { 131 getActivityAndFields(); 132 assertTrue(mActivity.mNextButtonEnabled); 133 134 mServerView.setText(" "); 135 assertFalse(mActivity.mNextButtonEnabled); 136 137 mServerView.setText("serv$er.com"); 138 assertFalse(mActivity.mNextButtonEnabled); 139 } 140 141 /** 142 * Test to confirm that passwords with leading or trailing spaces are accepted verbatim. 143 */ 144 @UiThreadTest 145 public void testPasswordNoTrim() throws URISyntaxException { 146 getActivityAndFields(); 147 148 // Clear the password - should disable 149 checkPassword(null, false); 150 151 // Various combinations of spaces should be OK 152 checkPassword(" leading", true); 153 checkPassword("trailing ", true); 154 checkPassword("em bedded", true); 155 checkPassword(" ", true); 156 } 157 158 /** 159 * Check password field for a given password. Should be called in UI thread. Confirms that 160 * the password has not been trimmed. 161 * 162 * @param password the password to test with 163 * @param expectNext true if expected that this password will enable the "next" button 164 */ 165 private void checkPassword(String password, boolean expectNext) throws URISyntaxException { 166 mPasswordView.setText(password); 167 if (expectNext) { 168 assertTrue(mActivity.mNextButtonEnabled); 169 } else { 170 assertFalse(mActivity.mNextButtonEnabled); 171 } 172 } 173 174 /** 175 * TODO: A series of tests to explore the logic around security models & ports 176 * TODO: A series of tests exploring differences between IMAP and POP3 177 */ 178 179 /** 180 * Get the activity (which causes it to be started, using our intent) and get the UI fields 181 */ 182 private void getActivityAndFields() { 183 mActivity = getActivity(); 184 mFragment = (AccountSetupIncomingFragment) mActivity.mFragment; 185 mServerView = (EditText) mActivity.findViewById(R.id.account_server); 186 mPasswordView = (EditText) mActivity.findViewById(R.id.account_password); 187 } 188 189 /** 190 * Create an intent with the Account in it 191 */ 192 private Intent getTestIntent(String storeUriString) 193 throws URISyntaxException { 194 Account account = new Account(); 195 Context context = getInstrumentation().getTargetContext(); 196 HostAuth auth = account.getOrCreateHostAuthRecv(context); 197 HostAuth.setHostAuthFromString(auth, storeUriString); 198 199 /*Bundle extras = new Bundle(); 200 extras.putParcelable(SetupData.EXTRA_SETUP_DATA, new SetupData(SetupData.FLOW_MODE_NORMAL, account));*/ 201 202 Intent intent = new Intent(Intent.ACTION_MAIN); 203 // intent.putExtras(extras); 204 205 return intent; 206 } 207 } 208