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 com.android.email.R;
     20 import com.android.email.provider.EmailContent;
     21 import com.android.email.provider.EmailContent.HostAuth;
     22 
     23 import android.content.ContentUris;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.net.Uri;
     27 import android.test.ActivityInstrumentationTestCase2;
     28 import android.test.suitebuilder.annotation.MediumTest;
     29 import android.widget.Button;
     30 
     31 /**
     32  * Tests of basic UI logic in the AccountSetupNamesTest screen.
     33  */
     34 @MediumTest
     35 public class AccountSetupNamesTests extends ActivityInstrumentationTestCase2<AccountSetupNames> {
     36 
     37     // borrowed from AccountSetupNames
     38     private static final String EXTRA_ACCOUNT_ID = "accountId";
     39 
     40     private long mAccountId;
     41     private EmailContent.Account mAccount;
     42 
     43     private Context mContext;
     44     private AccountSetupNames mActivity;
     45     private Button mDoneButton;
     46 
     47     public AccountSetupNamesTests() {
     48         super(AccountSetupNames.class);
     49     }
     50 
     51     /**
     52      * Common setup code for all tests.
     53      */
     54     @Override
     55     protected void setUp() throws Exception {
     56         super.setUp();
     57 
     58         mContext = this.getInstrumentation().getTargetContext();
     59     }
     60 
     61     /**
     62      * Delete any dummy accounts we set up for this test
     63      */
     64     @Override
     65     protected void tearDown() throws Exception {
     66         if (mAccount != null) {
     67             Uri uri = ContentUris.withAppendedId(
     68                     EmailContent.Account.CONTENT_URI, mAccountId);
     69             mContext.getContentResolver().delete(uri, null, null);
     70         }
     71 
     72         // must call last because it scrubs member variables
     73         super.tearDown();
     74     }
     75 
     76     /**
     77      * Test a "good" account name (enables the button)
     78      */
     79     public void testGoodAccountName() {
     80         Intent i = getTestIntent("imap", "GoodName");
     81         this.setActivityIntent(i);
     82 
     83         getActivityAndFields();
     84 
     85         assertTrue(mDoneButton.isEnabled());
     86     }
     87 
     88     /**
     89      * Test a "bad" account name (disables the button)
     90      */
     91     public void testBadAccountName() {
     92         Intent i = getTestIntent("imap", "");
     93         this.setActivityIntent(i);
     94 
     95         getActivityAndFields();
     96 
     97         assertFalse(mDoneButton.isEnabled());
     98     }
     99 
    100     /**
    101      * Test a "bad" account name (disables the button)
    102      */
    103     public void testEasAccountName() {
    104         Intent i = getTestIntent("eas", "");
    105         this.setActivityIntent(i);
    106 
    107         getActivityAndFields();
    108 
    109         assertTrue(mDoneButton.isEnabled());
    110     }
    111 
    112     /**
    113      * Get the activity (which causes it to be started, using our intent) and get the UI fields
    114      */
    115     private void getActivityAndFields() {
    116         mActivity = getActivity();
    117         mDoneButton = (Button) mActivity.findViewById(R.id.done);
    118     }
    119 
    120     /**
    121      * Create an intent with the Account in it, using protocol as the protocol and name as the
    122      * user's sender name
    123      */
    124     private Intent getTestIntent(String protocol, String name) {
    125         mAccount = new EmailContent.Account();
    126         mAccount.setSenderName(name);
    127         HostAuth hostAuth = new HostAuth();
    128         hostAuth.mProtocol = protocol;
    129         mAccount.mHostAuthRecv = hostAuth;
    130         mAccount.save(mContext);
    131         mAccountId = mAccount.mId;
    132 
    133         Intent i = new Intent(Intent.ACTION_MAIN);
    134         i.putExtra(EXTRA_ACCOUNT_ID, mAccountId);
    135         return i;
    136     }
    137 }
    138