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