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.mail.Store;
     20 import com.android.email.provider.EmailContent;
     21 import com.android.email.provider.EmailContent.Account;
     22 
     23 import android.content.ContentUris;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.net.Uri;
     27 import android.preference.ListPreference;
     28 import android.test.ActivityInstrumentationTestCase2;
     29 import android.test.suitebuilder.annotation.MediumTest;
     30 
     31 /**
     32  * Tests of basic UI logic in the AccountSettings screen.
     33  */
     34 @MediumTest
     35 public class AccountSettingsTests extends ActivityInstrumentationTestCase2<AccountSettings> {
     36 
     37     // Borrowed from AccountSettings
     38     private static final String EXTRA_ACCOUNT_ID = "account_id";
     39 
     40     private long mAccountId;
     41     private Account mAccount;
     42 
     43     private Context mContext;
     44     private AccountSettings mActivity;
     45     private ListPreference mCheckFrequency;
     46 
     47     private static final String PREFERENCE_FREQUENCY = "account_check_frequency";
     48 
     49     public AccountSettingsTests() {
     50         super(AccountSettings.class);
     51     }
     52 
     53     /**
     54      * Common setup code for all tests.
     55      */
     56     @Override
     57     protected void setUp() throws Exception {
     58         super.setUp();
     59 
     60         mContext = this.getInstrumentation().getTargetContext();
     61     }
     62 
     63     /**
     64      * Delete any dummy accounts we set up for this test
     65      */
     66     @Override
     67     protected void tearDown() throws Exception {
     68         if (mAccount != null) {
     69             Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, mAccountId);
     70             mContext.getContentResolver().delete(uri, null, null);
     71         }
     72 
     73         // must call last because it scrubs member variables
     74         super.tearDown();
     75     }
     76 
     77     /**
     78      * Test that POP accounts aren't displayed with a push option
     79      */
     80     public void testPushOptionPOP() {
     81         Intent i = getTestIntent("Name", "pop3://user:password (at) server.com",
     82                 "smtp://user:password (at) server.com");
     83         this.setActivityIntent(i);
     84 
     85         getActivityAndFields();
     86 
     87         boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
     88         assertFalse(hasPush);
     89     }
     90 
     91     /**
     92      * Test that IMAP accounts aren't displayed with a push option
     93      */
     94     public void testPushOptionIMAP() {
     95         Intent i = getTestIntent("Name", "imap://user:password (at) server.com",
     96                 "smtp://user:password (at) server.com");
     97         this.setActivityIntent(i);
     98 
     99         getActivityAndFields();
    100 
    101         boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
    102         assertFalse(hasPush);
    103     }
    104 
    105     /**
    106      * Test that EAS accounts are displayed with a push option
    107      */
    108     public void testPushOptionEAS() {
    109         // This test should only be run if EAS is supported
    110         if (Store.StoreInfo.getStoreInfo("eas", this.getInstrumentation().getTargetContext())
    111                 == null) {
    112             return;
    113         }
    114 
    115         Intent i = getTestIntent("Name", "eas://user:password (at) server.com",
    116                 "eas://user:password (at) server.com");
    117         this.setActivityIntent(i);
    118 
    119         getActivityAndFields();
    120 
    121         boolean hasPush = frequencySpinnerHasValue(Account.CHECK_INTERVAL_PUSH);
    122         assertTrue(hasPush);
    123     }
    124 
    125     /**
    126      * Get the activity (which causes it to be started, using our intent) and get the UI fields
    127      */
    128     private void getActivityAndFields() {
    129         mActivity = getActivity();
    130         mCheckFrequency = (ListPreference) mActivity.findPreference(PREFERENCE_FREQUENCY);
    131     }
    132 
    133     /**
    134      * Test the frequency values list for a particular value
    135      */
    136     private boolean frequencySpinnerHasValue(int value) {
    137         CharSequence[] values = mCheckFrequency.getEntryValues();
    138         for (CharSequence listValue : values) {
    139             if (listValue != null && Integer.parseInt(listValue.toString()) == value) {
    140                 return true;
    141             }
    142         }
    143         return false;
    144     }
    145 
    146     /**
    147      * Create an intent with the Account in it
    148      */
    149     private Intent getTestIntent(String name, String storeUri, String senderUri) {
    150         mAccount = new Account();
    151         mAccount.setSenderName(name);
    152         // For EAS, at least, email address is required
    153         mAccount.mEmailAddress = "user (at) server.com";
    154         mAccount.setStoreUri(mContext, storeUri);
    155         mAccount.setSenderUri(mContext, senderUri);
    156         mAccount.save(mContext);
    157         mAccountId = mAccount.mId;
    158 
    159         Intent i = new Intent(Intent.ACTION_MAIN);
    160         i.putExtra(EXTRA_ACCOUNT_ID, mAccountId);
    161         return i;
    162     }
    163 
    164 }
    165