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.mail.Store; 21 import com.android.email.provider.EmailContent; 22 import com.android.email.provider.EmailContent.Account; 23 24 import android.content.ContentUris; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.net.Uri; 28 import android.test.ActivityUnitTestCase; 29 import android.test.suitebuilder.annotation.SmallTest; 30 import android.view.View; 31 32 import java.util.HashSet; 33 34 /** 35 * This is a series of unit tests for the AccountSetupAccountType class. 36 */ 37 @SmallTest 38 public class AccountSetupAccountTypeUnitTests 39 extends ActivityUnitTestCase<AccountSetupAccountType> { 40 41 // Borrowed from AccountSetupAccountType 42 private static final String EXTRA_ACCOUNT = "account"; 43 44 Context mContext; 45 46 private HashSet<Account> mAccounts = new HashSet<Account>(); 47 48 public AccountSetupAccountTypeUnitTests() { 49 super(AccountSetupAccountType.class); 50 } 51 52 @Override 53 protected void setUp() throws Exception { 54 super.setUp(); 55 56 mContext = this.getInstrumentation().getTargetContext(); 57 } 58 59 /** 60 * Delete any dummy accounts we set up for this test 61 */ 62 @Override 63 protected void tearDown() throws Exception { 64 for (Account account : mAccounts) { 65 Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, account.mId); 66 mContext.getContentResolver().delete(uri, null, null); 67 } 68 69 // must call last because it scrubs member variables 70 super.tearDown(); 71 } 72 73 /** 74 * Test store type limit enforcement 75 */ 76 public void testStoreTypeLimits() { 77 EmailContent.Account acct1 = createTestAccount("scheme1"); 78 EmailContent.Account acct2 = createTestAccount("scheme1"); 79 EmailContent.Account acct3 = createTestAccount("scheme2"); 80 81 AccountSetupAccountType activity = startActivity(getTestIntent(acct1), null, null); 82 83 // Test with no limit 84 Store.StoreInfo info = new Store.StoreInfo(); 85 info.mAccountInstanceLimit = -1; 86 info.mScheme = "scheme1"; 87 assertTrue("no limit", activity.checkAccountInstanceLimit(info)); 88 89 // Test with limit, but not reached 90 info.mAccountInstanceLimit = 3; 91 assertTrue("limit, but not reached", activity.checkAccountInstanceLimit(info)); 92 93 // Test with limit, reached 94 info.mAccountInstanceLimit = 2; 95 assertFalse("limit, reached", activity.checkAccountInstanceLimit(info)); 96 } 97 98 /** 99 * Confirm that EAS is presented, when supported. 100 */ 101 public void testEasOffered() { 102 Account acct1 = createTestAccount("scheme1"); 103 AccountSetupAccountType activity = startActivity(getTestIntent(acct1), null, null); 104 View exchangeButton = activity.findViewById(R.id.exchange); 105 106 int expected = View.GONE; // Default is hidden 107 //EXCHANGE-REMOVE-SECTION-START 108 expected = View.VISIBLE; // Will be visible if supported. 109 //EXCHANGE-REMOVE-SECTION-END 110 111 assertEquals(expected, exchangeButton.getVisibility()); 112 } 113 114 /** 115 * Create a dummy account with minimal fields 116 */ 117 private Account createTestAccount(String scheme) { 118 Account account = new Account(); 119 account.setStoreUri(mContext, scheme + "://user:pass (at) server.com:123"); 120 account.save(mContext); 121 mAccounts.add(account); 122 return account; 123 } 124 125 /** 126 * Create an intent with the Account in it 127 */ 128 private Intent getTestIntent(EmailContent.Account account) { 129 Intent i = new Intent(Intent.ACTION_MAIN); 130 i.putExtra(EXTRA_ACCOUNT, account); 131 return i; 132 } 133 134 } 135