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