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