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