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.test.suitebuilder.annotation.Suppress;
     25 import android.view.View;
     26 import android.widget.EditText;
     27 
     28 import com.android.email.R;
     29 import com.android.email.setup.AuthenticatorSetupIntentHelper;
     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.AccountSetupIncomingTests email
     39  */
     40 @Suppress
     41 @MediumTest
     42 public class AccountSetupIncomingTests extends
     43         ActivityInstrumentationTestCase2<AccountSetupFinal> {
     44 
     45     private AccountSetupFinal mActivity;
     46     private EditText mServerView;
     47     private AuthenticationView mAuthenticationView;
     48 
     49     public AccountSetupIncomingTests() {
     50         super(AccountSetupFinal.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("imap://user:password (at) server.com:999");
     65         setActivityIntent(i);
     66     }
     67 
     68     private boolean isNextButtonEnabled() {
     69         final View nextButton = mActivity.findViewById(R.id.next);
     70         return nextButton.isEnabled();
     71     }
     72 
     73     /**
     74      * Test processing with a complete, good URI -> good fields
     75      */
     76     public void testGoodUri()
     77             throws URISyntaxException {
     78         Intent i = getTestIntent("imap://user:password (at) server.com:999");
     79         setActivityIntent(i);
     80         getActivityAndFields();
     81         assertTrue(isNextButtonEnabled());
     82     }
     83 
     84     /**
     85      * No user is not OK - not enabled
     86      */
     87     public void testBadUriNoUser()
     88             throws URISyntaxException {
     89         Intent i = getTestIntent("imap://:password (at) server.com:999");
     90         setActivityIntent(i);
     91         getActivityAndFields();
     92         assertFalse(isNextButtonEnabled());
     93     }
     94 
     95     /**
     96      * No password is not OK - not enabled
     97      */
     98     public void testBadUriNoPassword()
     99             throws URISyntaxException {
    100         Intent i = getTestIntent("imap://user (at) server.com:999");
    101         setActivityIntent(i);
    102         getActivityAndFields();
    103         assertFalse(isNextButtonEnabled());
    104     }
    105 
    106     /**
    107      * No port is OK - still enabled
    108      */
    109     public void testGoodUriNoPort()
    110             throws URISyntaxException {
    111         Intent i = getTestIntent("imap://user:password (at) server.com");
    112         setActivityIntent(i);
    113         getActivityAndFields();
    114         assertTrue(isNextButtonEnabled());
    115     }
    116 
    117     /**
    118      * Test for non-standard but OK server names
    119      */
    120     @UiThreadTest
    121     public void testGoodServerVariants() {
    122         getActivityAndFields();
    123         assertTrue(isNextButtonEnabled());
    124 
    125         mServerView.setText("  server.com  ");
    126         assertTrue(isNextButtonEnabled());
    127     }
    128 
    129     /**
    130      * Test for non-empty but non-OK server names
    131      */
    132     @UiThreadTest
    133     public void testBadServerVariants() {
    134         getActivityAndFields();
    135         assertTrue(isNextButtonEnabled());
    136 
    137         mServerView.setText("  ");
    138         assertFalse(isNextButtonEnabled());
    139 
    140         mServerView.setText("serv$er.com");
    141         assertFalse(isNextButtonEnabled());
    142     }
    143 
    144     /**
    145      * Test to confirm that passwords with leading or trailing spaces are accepted verbatim.
    146      */
    147     @UiThreadTest
    148     public void testPasswordNoTrim() throws URISyntaxException {
    149         getActivityAndFields();
    150 
    151         // Clear the password - should disable
    152         checkPassword(null, false);
    153 
    154         // Various combinations of spaces should be OK
    155         checkPassword(" leading", true);
    156         checkPassword("trailing ", true);
    157         checkPassword("em bedded", true);
    158         checkPassword(" ", true);
    159     }
    160 
    161     /**
    162      * Check password field for a given password.  Should be called in UI thread.  Confirms that
    163      * the password has not been trimmed.
    164      *
    165      * @param password the password to test with
    166      * @param expectNext true if expected that this password will enable the "next" button
    167      */
    168     private void checkPassword(String password, boolean expectNext) throws URISyntaxException {
    169         mAuthenticationView.setPassword(password);
    170         if (expectNext) {
    171             assertTrue(isNextButtonEnabled());
    172         } else {
    173             assertFalse(isNextButtonEnabled());
    174         }
    175     }
    176 
    177     /**
    178      * TODO:  A series of tests to explore the logic around security models & ports
    179      * TODO:  A series of tests exploring differences between IMAP and POP3
    180      */
    181 
    182     /**
    183      * Get the activity (which causes it to be started, using our intent) and get the UI fields
    184      */
    185     private void getActivityAndFields() {
    186         mActivity = getActivity();
    187         mServerView = (EditText) mActivity.findViewById(R.id.account_server);
    188         mAuthenticationView = (AuthenticationView) mActivity.findViewById(R.id.authentication_view);
    189     }
    190 
    191     /**
    192      * Create an intent with the Account in it
    193      */
    194     private Intent getTestIntent(String storeUriString)
    195             throws URISyntaxException {
    196         final Account account = new Account();
    197         final Context context = getInstrumentation().getTargetContext();
    198         final HostAuth auth = account.getOrCreateHostAuthRecv(context);
    199         auth.setHostAuthFromString(storeUriString);
    200         final SetupDataFragment setupDataFragment =
    201                 new SetupDataFragment();
    202         setupDataFragment.setFlowMode(AuthenticatorSetupIntentHelper.FLOW_MODE_NORMAL);
    203         setupDataFragment.setAccount(account);
    204         final Intent i = new Intent(AccountSetupFinal.ACTION_JUMP_TO_INCOMING);
    205         i.putExtra(SetupDataFragment.EXTRA_SETUP_DATA, setupDataFragment);
    206         return i;
    207     }
    208 }
    209