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 android.content.Context;
     20 import android.content.Intent;
     21 import android.test.ActivityInstrumentationTestCase2;
     22 import android.test.suitebuilder.annotation.MediumTest;
     23 import android.test.suitebuilder.annotation.Suppress;
     24 import android.view.View;
     25 import android.widget.CheckBox;
     26 import android.widget.Spinner;
     27 import android.widget.SpinnerAdapter;
     28 
     29 import com.android.email.R;
     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 basic UI logic in the AccountSetupOptions screen.
     37  * You can run this entire test case with:
     38  *   runtest -c com.android.email.activity.setup.AccountSetupOptionsTests email
     39  */
     40 @Suppress
     41 @MediumTest
     42 public class AccountSetupOptionsTests
     43         extends ActivityInstrumentationTestCase2<AccountSetupFinal> {
     44 
     45     private AccountSetupFinal mActivity;
     46     private Spinner mCheckFrequencyView;
     47     private CheckBox mBackgroundAttachmentsView;
     48 
     49     public AccountSetupOptionsTests() {
     50         super(AccountSetupFinal.class);
     51     }
     52 
     53     /**
     54      * Test that POP accounts aren't displayed with a push option
     55      */
     56     public void testPushOptionPOP()
     57             throws URISyntaxException {
     58         Intent i = getTestIntent("Name", "pop3://user:password (at) server.com");
     59         this.setActivityIntent(i);
     60 
     61         getActivityAndFields();
     62 
     63         boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
     64         assertFalse(hasPush);
     65     }
     66 
     67     /**
     68      * Test that IMAP accounts aren't displayed with a push option
     69      */
     70     public void testPushOptionIMAP()
     71             throws URISyntaxException {
     72         Intent i = getTestIntent("Name", "imap://user:password (at) server.com");
     73         this.setActivityIntent(i);
     74 
     75         getActivityAndFields();
     76 
     77         boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
     78         assertFalse(hasPush);
     79     }
     80 
     81     /**
     82      * Test that EAS accounts are displayed with a push option
     83      */
     84     public void testPushOptionEAS()
     85             throws URISyntaxException {
     86         Intent i = getTestIntent("Name", "eas://user:password (at) server.com");
     87         this.setActivityIntent(i);
     88 
     89         getActivityAndFields();
     90 
     91         boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
     92         assertTrue(hasPush);
     93     }
     94 
     95     /**
     96      * Test that POP3 accounts don't have a "background attachments" checkbox
     97      */
     98     public void testBackgroundAttachmentsPop()
     99             throws URISyntaxException {
    100         checkBackgroundAttachments("pop3://user:password (at) server.com", false);
    101     }
    102 
    103     /**
    104      * Test that IMAP accounts have a "background attachments" checkbox
    105      */
    106     public void testBackgroundAttachmentsImap()
    107             throws URISyntaxException {
    108         checkBackgroundAttachments("imap://user:password (at) server.com", true);
    109     }
    110 
    111     /**
    112      * Test that EAS accounts have a "background attachments" checkbox
    113      */
    114     public void testBackgroundAttachmentsEas()
    115             throws URISyntaxException {
    116         checkBackgroundAttachments("eas://user:password (at) server.com", true);
    117     }
    118 
    119     /**
    120      * Common code to check that the "background attachments" checkbox is shown/hidden properly
    121      */
    122     private void checkBackgroundAttachments(String storeUri, boolean expectVisible)
    123             throws URISyntaxException {
    124         Intent i = getTestIntent("Name", storeUri);
    125         this.setActivityIntent(i);
    126         getActivityAndFields();
    127 
    128         boolean isNull = mBackgroundAttachmentsView == null;
    129         boolean isVisible = !isNull && (mBackgroundAttachmentsView.getVisibility() == View.VISIBLE);
    130 
    131         if (!expectVisible) {
    132             assertTrue(!isVisible);
    133         } else {
    134             assertTrue(!isNull);
    135             assertTrue(isVisible);
    136         }
    137     }
    138 
    139     /**
    140      * Get the activity (which causes it to be started, using our intent) and get the UI fields
    141      */
    142     private void getActivityAndFields() {
    143         mActivity = getActivity();
    144         mCheckFrequencyView = (Spinner) mActivity.findViewById(R.id.account_check_frequency);
    145         mBackgroundAttachmentsView = (CheckBox) mActivity.findViewById(
    146                 R.id.account_background_attachments);
    147     }
    148 
    149     /**
    150      * Test the frequency values list for a particular value
    151      */
    152     private boolean frequencySpinnerHasValue(int value) {
    153         SpinnerAdapter sa = mCheckFrequencyView.getAdapter();
    154 
    155         for (int i = 0; i < sa.getCount(); ++i) {
    156             SpinnerOption so = (SpinnerOption) sa.getItem(i);
    157             if (so != null && ((Integer)so.value) == value) {
    158                 return true;
    159             }
    160         }
    161         return false;
    162     }
    163 
    164     /**
    165      * Create an intent with the Account in it
    166      */
    167     private Intent getTestIntent(String name, String storeUri)
    168             throws URISyntaxException {
    169         final Account account = new Account();
    170         account.setSenderName(name);
    171         final Context context = getInstrumentation().getTargetContext();
    172         final HostAuth auth = account.getOrCreateHostAuthRecv(context);
    173         auth.setHostAuthFromString(storeUri);
    174         final SetupDataFragment setupDataFragment =
    175                 new SetupDataFragment();
    176         setupDataFragment.setFlowMode(SetupDataFragment.FLOW_MODE_NORMAL);
    177         setupDataFragment.setAccount(account);
    178         final Intent i = new Intent(AccountSetupFinal.ACTION_JUMP_TO_OPTIONS);
    179         i.putExtra(SetupDataFragment.EXTRA_SETUP_DATA, setupDataFragment);
    180         return i;
    181     }
    182 
    183 }
    184