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.provider.EmailContent; 21 22 import android.content.ContentUris; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.net.Uri; 26 import android.test.ActivityInstrumentationTestCase2; 27 import android.test.suitebuilder.annotation.MediumTest; 28 import android.widget.Button; 29 30 /** 31 * Tests of basic UI logic in the AccountSetupNamesTest screen. 32 */ 33 @MediumTest 34 public class AccountSetupNamesTests extends ActivityInstrumentationTestCase2<AccountSetupNames> { 35 36 // borrowed from AccountSetupNames 37 private static final String EXTRA_ACCOUNT_ID = "accountId"; 38 39 private long mAccountId; 40 private EmailContent.Account mAccount; 41 42 private Context mContext; 43 private AccountSetupNames mActivity; 44 private Button mDoneButton; 45 AccountSetupNamesTests()46 public AccountSetupNamesTests() { 47 super("com.android.email", AccountSetupNames.class); 48 } 49 50 /** 51 * Common setup code for all tests. 52 */ 53 @Override setUp()54 protected void setUp() throws Exception { 55 super.setUp(); 56 57 mContext = this.getInstrumentation().getTargetContext(); 58 } 59 60 /** 61 * Delete any dummy accounts we set up for this test 62 */ 63 @Override tearDown()64 protected void tearDown() throws Exception { 65 if (mAccount != null) { 66 Uri uri = ContentUris.withAppendedId( 67 EmailContent.Account.CONTENT_URI, mAccountId); 68 mContext.getContentResolver().delete(uri, null, null); 69 } 70 71 // must call last because it scrubs member variables 72 super.tearDown(); 73 } 74 75 /** 76 * Test a "good" account name (enables the button) 77 */ testGoodAccountName()78 public void testGoodAccountName() { 79 Intent i = getTestIntent("GoodName"); 80 this.setActivityIntent(i); 81 82 getActivityAndFields(); 83 84 assertTrue(mDoneButton.isEnabled()); 85 } 86 87 /** 88 * Test a "bad" account name (disables the button) 89 */ testBadAccountName()90 public void testBadAccountName() { 91 Intent i = getTestIntent(""); 92 this.setActivityIntent(i); 93 94 getActivityAndFields(); 95 96 assertFalse(mDoneButton.isEnabled()); 97 } 98 99 /** 100 * Get the activity (which causes it to be started, using our intent) and get the UI fields 101 */ getActivityAndFields()102 private void getActivityAndFields() { 103 mActivity = getActivity(); 104 mDoneButton = (Button) mActivity.findViewById(R.id.done); 105 } 106 107 /** 108 * Create an intent with the Account in it 109 */ getTestIntent(String name)110 private Intent getTestIntent(String name) { 111 mAccount = new EmailContent.Account(); 112 mAccount.setSenderName(name); 113 mAccount.save(mContext); 114 mAccountId = mAccount.mId; 115 116 Intent i = new Intent(Intent.ACTION_MAIN); 117 i.putExtra(EXTRA_ACCOUNT_ID, mAccountId); 118 return i; 119 } 120 121 } 122