Home | History | Annotate | Download | only in setup
      1 /*
      2  * Copyright (C) 2009 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 import com.android.email.provider.ProviderTestUtils;
     22 import com.android.email.provider.EmailContent.Account;
     23 import com.android.email.provider.EmailContent.HostAuth;
     24 
     25 import android.content.Intent;
     26 import android.test.ActivityInstrumentationTestCase2;
     27 import android.test.UiThreadTest;
     28 import android.test.suitebuilder.annotation.MediumTest;
     29 import android.view.View;
     30 import android.widget.Button;
     31 import android.widget.CheckBox;
     32 import android.widget.EditText;
     33 
     34 import java.net.URI;
     35 import java.net.URISyntaxException;
     36 
     37 /**
     38  * Tests of the basic UI logic in the Account Setup Incoming (IMAP / POP3) screen.
     39  */
     40 @MediumTest
     41 public class AccountSetupExchangeTests extends
     42         ActivityInstrumentationTestCase2<AccountSetupExchange> {
     43     //EXCHANGE-REMOVE-SECTION-START
     44     private AccountSetupExchange mActivity;
     45     private EditText mServerView;
     46     private EditText mPasswordView;
     47     private Button mNextButton;
     48     private CheckBox mSslRequiredCheckbox;
     49     private CheckBox mTrustAllCertificatesCheckbox;
     50     //EXCHANGE-REMOVE-SECTION-END
     51 
     52     public AccountSetupExchangeTests() {
     53         super(AccountSetupExchange.class);
     54     }
     55 
     56     //EXCHANGE-REMOVE-SECTION-START
     57     /**
     58      * Common setup code for all tests.  Sets up a default launch intent, which some tests
     59      * will use (others will override).
     60      */
     61     @Override
     62     protected void setUp() throws Exception {
     63         super.setUp();
     64 
     65         // This sets up a default URI which can be used by any of the test methods below.
     66         // Individual test methods can replace this with a custom URI if they wish
     67         // (except those that run on the UI thread - for them, it's too late to change it.)
     68         Intent i = getTestIntent("eas://user:password (at) server.com");
     69         setActivityIntent(i);
     70     }
     71 
     72     /**
     73      * Test processing with a complete, good URI -> good fields
     74      */
     75     public void testGoodUri() {
     76         Intent i = getTestIntent("eas://user:password (at) server.com");
     77         setActivityIntent(i);
     78         getActivityAndFields();
     79         assertTrue(mNextButton.isEnabled());
     80     }
     81 
     82     // TODO Add tests for valid usernames in eas
     83     // They would be <name> or <name>\<domain> or <name>/<domain> or a valid email address
     84 
     85     /**
     86      * No user is not OK - not enabled
     87      */
     88     public void testBadUriNoUser() {
     89         Intent i = getTestIntent("eas://:password (at) server.com");
     90         setActivityIntent(i);
     91         getActivityAndFields();
     92         assertFalse(mNextButton.isEnabled());
     93     }
     94 
     95     /**
     96      * No password is not OK - not enabled
     97      */
     98     public void testBadUriNoPassword() {
     99         Intent i = getTestIntent("eas://user (at) server.com");
    100         setActivityIntent(i);
    101         getActivityAndFields();
    102         assertFalse(mNextButton.isEnabled());
    103     }
    104 
    105     /**
    106      * Test for non-standard but OK server names
    107      */
    108     @UiThreadTest
    109     public void testGoodServerVariants() {
    110         getActivityAndFields();
    111         assertTrue(mNextButton.isEnabled());
    112 
    113         mServerView.setText("  server.com  ");
    114         assertTrue(mNextButton.isEnabled());
    115     }
    116 
    117     /**
    118      * Test for non-empty but non-OK server names
    119      */
    120     @UiThreadTest
    121     public void testBadServerVariants() {
    122         getActivityAndFields();
    123         assertTrue(mNextButton.isEnabled());
    124 
    125         mServerView.setText("  ");
    126         assertFalse(mNextButton.isEnabled());
    127 
    128         mServerView.setText("serv$er.com");
    129         assertFalse(mNextButton.isEnabled());
    130     }
    131 
    132     /**
    133      * Test aspects of loadFields()
    134      *
    135      * TODO: More cases
    136      */
    137     @UiThreadTest
    138     public void testLoadFields() {
    139         // The default URI has no SSL and no "trust"
    140         getActivityAndFields();
    141         assertFalse(mSslRequiredCheckbox.isChecked());
    142         assertFalse(mTrustAllCertificatesCheckbox.isChecked());
    143         assertFalse(mTrustAllCertificatesCheckbox.getVisibility() == View.VISIBLE);
    144 
    145         // Setup host auth with variants of SSL enabled and check.  This also enables the
    146         // "trust certificates" checkbox (not checked, but visible now).
    147         Account account =
    148             ProviderTestUtils.setupAccount("account", false, mActivity.getBaseContext());
    149         account.mHostAuthRecv = ProviderTestUtils.setupHostAuth(
    150                 "eas", "hostauth", 1, false, mActivity.getBaseContext());
    151         account.mHostAuthRecv.mFlags |= HostAuth.FLAG_SSL;
    152         account.mHostAuthRecv.mFlags &= ~HostAuth.FLAG_TRUST_ALL_CERTIFICATES;
    153         mActivity.loadFields(account);
    154         assertTrue(mSslRequiredCheckbox.isChecked());
    155         assertFalse(mTrustAllCertificatesCheckbox.isChecked());
    156         assertTrue(mTrustAllCertificatesCheckbox.getVisibility() == View.VISIBLE);
    157 
    158         // Setup host auth with variants of SSL enabled and check.  This also enables the
    159         // "trust certificates" checkbox (not checked, but visible now).
    160         account.mHostAuthRecv.mFlags |= HostAuth.FLAG_TRUST_ALL_CERTIFICATES;
    161         mActivity.loadFields(account);
    162         assertTrue(mSslRequiredCheckbox.isChecked());
    163         assertTrue(mTrustAllCertificatesCheckbox.isChecked());
    164         assertTrue(mTrustAllCertificatesCheckbox.getVisibility() == View.VISIBLE);
    165     }
    166 
    167     /**
    168      * Test to confirm that passwords with leading or trailing spaces are accepted verbatim.
    169      */
    170     @UiThreadTest
    171     public void testPasswordNoTrim() throws URISyntaxException {
    172         getActivityAndFields();
    173 
    174         // Clear the password - should disable
    175         checkPassword(null, false);
    176 
    177         // Various combinations of spaces should be OK
    178         checkPassword(" leading", true);
    179         checkPassword("trailing ", true);
    180         checkPassword("em bedded", true);
    181         checkPassword(" ", true);
    182     }
    183 
    184     /**
    185      * Check password field for a given password.  Should be called in UI thread.  Confirms that
    186      * the password has not been trimmed.
    187      *
    188      * @param password the password to test with
    189      * @param expectNext true if expected that this password will enable the "next" button
    190      */
    191     private void checkPassword(String password, boolean expectNext) throws URISyntaxException {
    192         mPasswordView.setText(password);
    193         if (expectNext) {
    194             assertTrue(mNextButton.isEnabled());
    195             URI uri = mActivity.getUri();
    196             String actualUserInfo = uri.getUserInfo();
    197             String actualPassword = actualUserInfo.split(":", 2)[1];
    198             assertEquals(password, actualPassword);
    199         } else {
    200             assertFalse(mNextButton.isEnabled());
    201         }
    202     }
    203 
    204     /**
    205      * TODO: Directly test validateFields() checking boolean result
    206      */
    207 
    208     /**
    209      * Get the activity (which causes it to be started, using our intent) and get the UI fields
    210      */
    211     private void getActivityAndFields() {
    212         mActivity = getActivity();
    213         mServerView = (EditText) mActivity.findViewById(R.id.account_server);
    214         mPasswordView = (EditText) mActivity.findViewById(R.id.account_password);
    215         mNextButton = (Button) mActivity.findViewById(R.id.next);
    216         mSslRequiredCheckbox = (CheckBox) mActivity.findViewById(R.id.account_ssl);
    217         mTrustAllCertificatesCheckbox =
    218             (CheckBox) mActivity.findViewById(R.id.account_trust_certificates);
    219     }
    220 
    221     /**
    222      * Create an intent with the Account in it
    223      */
    224     private Intent getTestIntent(String storeUriString) {
    225         EmailContent.Account account = new EmailContent.Account();
    226         account.setStoreUri(getInstrumentation().getTargetContext(), storeUriString);
    227         Intent i = new Intent(Intent.ACTION_MAIN);
    228         i.putExtra(AccountSetupExchange.EXTRA_ACCOUNT, account);
    229         i.putExtra(AccountSetupExchange.EXTRA_DISABLE_AUTO_DISCOVER, true);
    230         return i;
    231     }
    232     //EXCHANGE-REMOVE-SECTION-END
    233 }
    234