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