1 /* 2 * Copyright (C) 2010 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; 18 19 import com.android.email.DBTestHelper; 20 import com.android.email.FolderProperties; 21 import com.android.email.R; 22 import com.android.email.provider.ProviderTestUtils; 23 import com.android.emailcommon.provider.Account; 24 import com.android.emailcommon.provider.Mailbox; 25 26 import android.content.Context; 27 import android.content.Loader; 28 import android.database.Cursor; 29 import android.database.MatrixCursor; 30 import android.test.LoaderTestCase; 31 32 /** 33 * Tests for {@link AccountSelectorAdapter.AccountsLoader}. 34 * 35 * TODO add more tests. 36 */ 37 public class AccountSelectorAdapterTest extends LoaderTestCase { 38 private Context mProviderContext; 39 40 @Override 41 protected void setUp() throws Exception { 42 super.setUp(); 43 mProviderContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(mContext); 44 } 45 46 /** 47 * - Confirm that AccountsLoader adds the combined view row, iif there is more than 1 account. 48 * - Confirm that AccountsLoader doesn't add recent mailboxes. 49 * 50 * two-pane version. 51 * 52 * TODO add one-pane version 53 */ 54 public void testCombinedViewRow_twoPane() { 55 final Account a1 = ProviderTestUtils.setupAccount("a1", true, mProviderContext); 56 { 57 // Only 1 account -- no combined view row. 58 Loader<Cursor> l = new AccountSelectorAdapter.AccountsLoader(mProviderContext, 0L, 59 0L, true); 60 AccountSelectorAdapter.CursorWithExtras result = 61 (AccountSelectorAdapter.CursorWithExtras) getLoaderResultSynchronously(l); 62 assertEquals(1, result.getAccountCount()); 63 assertEquals(2, result.getCount()); // +1 as the cursor has the header row 64 assertEquals(0, result.getRecentMailboxCount()); // No recent on two-pane. 65 } 66 67 final Account a2 = ProviderTestUtils.setupAccount("a2", true, mProviderContext); 68 { 69 // 2 accounts -- with combined view row, so returns 3 account rows. 70 Loader<Cursor> l = new AccountSelectorAdapter.AccountsLoader(mProviderContext, 0L, 71 0L, true); 72 AccountSelectorAdapter.CursorWithExtras result = 73 (AccountSelectorAdapter.CursorWithExtras) getLoaderResultSynchronously(l); 74 assertEquals(3, result.getAccountCount()); 75 assertEquals(4, result.getCount()); // +1 as the cursor has the header row 76 assertEquals(0, result.getRecentMailboxCount()); // No recent on two-pane. 77 } 78 } 79 80 private static AccountSelectorAdapter.CursorWithExtras createCursorWithExtras() { 81 final MatrixCursor m = new MatrixCursorWithCachedColumns(new String[] {"column"}); 82 return new AccountSelectorAdapter.CursorWithExtras(m.getColumnNames(), m); 83 } 84 85 public void testCursorWithExtras_setAccountMailboxInfo() { 86 final Context context = mProviderContext; 87 final Account a1 = ProviderTestUtils.setupAccount("a1", true, context); 88 final Account a2 = ProviderTestUtils.setupAccount("a2", true, context); 89 final Mailbox m1 = ProviderTestUtils.setupMailbox("Inbox", a1.mId, true, context, 90 Mailbox.TYPE_INBOX); 91 final Mailbox m2 = ProviderTestUtils.setupMailbox("box2", a2.mId, true, context, 92 Mailbox.TYPE_MAIL); 93 addMessage(m1, true, false); 94 addMessage(m2, false, false); 95 addMessage(m2, false, false); 96 addMessage(m2, true, true); 97 98 // Account 1 - no mailbox 99 AccountSelectorAdapter.CursorWithExtras c = createCursorWithExtras(); 100 c.setAccountMailboxInfo(context, a1.mId, Mailbox.NO_MAILBOX); 101 102 assertTrue(c.accountExists()); 103 assertEquals(a1.mId, c.getAccountId()); 104 assertEquals("a1", c.getAccountDisplayName()); 105 assertEquals(Mailbox.NO_MAILBOX, c.getMailboxId()); 106 assertNull(c.getMailboxDisplayName()); 107 assertEquals(0, c.getMailboxMessageCount()); 108 109 // Account 1 - inbox 110 c = createCursorWithExtras(); 111 c.setAccountMailboxInfo(context, a1.mId, m1.mId); 112 113 assertTrue(c.accountExists()); 114 assertEquals(a1.mId, c.getAccountId()); 115 assertEquals("a1", c.getAccountDisplayName()); 116 assertEquals(m1.mId, c.getMailboxId()); 117 assertEquals("Inbox", c.getMailboxDisplayName()); 118 assertEquals(1, c.getMailboxMessageCount()); 119 120 // Account 2 - regular mailbox 121 c = createCursorWithExtras(); 122 c.setAccountMailboxInfo(context, a2.mId, m2.mId); 123 124 assertTrue(c.accountExists()); 125 assertEquals(a2.mId, c.getAccountId()); 126 assertEquals("a2", c.getAccountDisplayName()); 127 assertEquals(m2.mId, c.getMailboxId()); 128 assertEquals("box2", c.getMailboxDisplayName()); 129 assertEquals(2, c.getMailboxMessageCount()); 130 131 // combined - no mailbox 132 c = createCursorWithExtras(); 133 c.setAccountMailboxInfo(context, Account.ACCOUNT_ID_COMBINED_VIEW, Mailbox.NO_MAILBOX); 134 135 assertTrue(c.accountExists()); 136 assertEquals(Account.ACCOUNT_ID_COMBINED_VIEW, c.getAccountId()); 137 assertEquals(getContext().getString(R.string.mailbox_list_account_selector_combined_view), 138 c.getAccountDisplayName()); 139 assertEquals(Mailbox.NO_MAILBOX, c.getMailboxId()); 140 assertNull(c.getMailboxDisplayName()); 141 assertEquals(0, c.getMailboxMessageCount()); 142 143 // combined - all inbox 144 c = createCursorWithExtras(); 145 c.setAccountMailboxInfo(context, Account.ACCOUNT_ID_COMBINED_VIEW, 146 Mailbox.QUERY_ALL_INBOXES); 147 148 assertTrue(c.accountExists()); 149 assertEquals(Account.ACCOUNT_ID_COMBINED_VIEW, c.getAccountId()); 150 assertEquals(getContext().getString(R.string.mailbox_list_account_selector_combined_view), 151 c.getAccountDisplayName()); 152 assertEquals(Mailbox.QUERY_ALL_INBOXES, c.getMailboxId()); 153 assertEquals(getContext().getString(R.string.account_folder_list_summary_inbox), 154 c.getMailboxDisplayName()); 155 // (message count = 1, because account 2 doesn't have inbox) 156 157 // TODO For some reason getMailboxMessageCount returns 0 in tests. Investigate it. 158 // assertEquals(1, c.getMailboxMessageCount()); 159 160 // Account 1 - all starred 161 // Special case; it happens when you open "starred" on a normal account's mailbox list 162 // on two-pane. 163 c = createCursorWithExtras(); 164 c.setAccountMailboxInfo(context, a1.mId, Mailbox.QUERY_ALL_FAVORITES); 165 166 assertTrue(c.accountExists()); 167 assertEquals(a1.mId, c.getAccountId()); 168 assertEquals("a1", c.getAccountDisplayName()); 169 assertEquals(Mailbox.QUERY_ALL_FAVORITES, c.getMailboxId()); 170 assertEquals(getContext().getString(R.string.account_folder_list_summary_starred), 171 c.getMailboxDisplayName()); 172 // assertEquals(2, c.getMailboxMessageCount()); 173 174 // Invalid id 175 c = createCursorWithExtras(); 176 c.setAccountMailboxInfo(context, 123456, 1232456); // no such account / mailbox 177 178 assertFalse(c.accountExists()); 179 } 180 181 private void addMessage(Mailbox m, boolean starred, boolean read) { 182 ProviderTestUtils.setupMessage("a", m.mAccountKey, m.mId, false, true, mProviderContext, 183 starred, read); 184 } 185 } 186