Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2176 from philipwhiuk/messageProviderFixAndTest
MessageProvider related fixes and testing
- Loading branch information
Showing
4 changed files
with
181 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
k9mail/src/androidTest/java/com/fsck/k9/provider/MessageProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.fsck.k9.provider; | ||
|
||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import android.test.ProviderTestCase2; | ||
import android.test.mock.MockContentResolver; | ||
|
||
import com.fsck.k9.Account; | ||
import com.fsck.k9.K9; | ||
import com.fsck.k9.Preferences; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.List; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class MessageProviderTest extends ProviderTestCase2 { | ||
|
||
private MockContentResolver mMockResolver; | ||
private Cursor cursor; | ||
|
||
public MessageProviderTest() { | ||
super(MessageProvider.class, MessageProvider.AUTHORITY); | ||
} | ||
|
||
|
||
@Before | ||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
mMockResolver = getMockContentResolver(); | ||
mContext = K9.app; | ||
Preferences preferences = Preferences.getPreferences(getMockContext()); | ||
List<Account> accountList = preferences.getAccounts(); | ||
for (Account account: accountList) { | ||
preferences.deleteAccount(account); | ||
} | ||
} | ||
|
||
@After | ||
@Override | ||
public void tearDown() throws Exception { | ||
if (cursor != null) { | ||
cursor.close(); | ||
} | ||
super.tearDown(); | ||
} | ||
|
||
private void createAccount() { | ||
Preferences preferences = Preferences.getPreferences(getMockContext()); | ||
Account account = preferences.newAccount(); | ||
account.setDescription("TestAccount"); | ||
account.setChipColor(10); | ||
account.setStoreUri("imap://user@domain.com/"); | ||
account.save(preferences); | ||
} | ||
|
||
@Test | ||
public void query_forAccounts_withNoAccounts_returnsEmptyCursor() { | ||
cursor = mMockResolver.query( | ||
Uri.parse("content://" + MessageProvider.AUTHORITY + "/accounts/"), | ||
null, null, null, null); | ||
|
||
boolean isNotEmpty = cursor.moveToFirst(); | ||
|
||
assertFalse(isNotEmpty); | ||
} | ||
|
||
@Test | ||
public void query_forAccounts_withAccount_returnsCursorWithData() { | ||
createAccount(); | ||
cursor = mMockResolver.query( | ||
Uri.parse("content://" + MessageProvider.AUTHORITY + "/accounts/"), | ||
null, null, null, null); | ||
|
||
boolean isNotEmpty = cursor.moveToFirst(); | ||
|
||
assertTrue(isNotEmpty); | ||
} | ||
|
||
@Test | ||
public void query_forAccounts_withAccount_withNoProjection_returnsNumberAndName() { | ||
createAccount(); | ||
|
||
cursor = mMockResolver.query( | ||
Uri.parse("content://" + MessageProvider.AUTHORITY + "/accounts/"), | ||
null, null, null, null); | ||
cursor.moveToFirst(); | ||
|
||
assertEquals(2, cursor.getColumnCount()); | ||
assertEquals(0, cursor.getColumnIndex(MessageProvider.AccountColumns.ACCOUNT_NUMBER)); | ||
assertEquals(1, cursor.getColumnIndex(MessageProvider.AccountColumns.ACCOUNT_NAME)); | ||
assertEquals(0, cursor.getInt(0)); | ||
assertEquals("TestAccount", cursor.getString(1)); | ||
} | ||
|
||
|
||
@Test | ||
public void query_forInboxMessages_whenEmpty_returnsEmptyCursor() { | ||
cursor = mMockResolver.query( | ||
Uri.parse("content://" + MessageProvider.AUTHORITY + "/inbox_messages/"), | ||
null, null, null, null); | ||
|
||
boolean isNotEmpty = cursor.moveToFirst(); | ||
|
||
assertFalse(isNotEmpty); | ||
} | ||
|
||
@Test | ||
public void query_forAccountUnreadMessages_whenNoAccount_returnsEmptyCursor() { | ||
cursor = mMockResolver.query( | ||
Uri.parse("content://" + MessageProvider.AUTHORITY + "/account_unread/0"), | ||
null, null, null, null); | ||
|
||
boolean isNotEmpty = cursor.moveToFirst(); | ||
|
||
assertFalse(isNotEmpty); | ||
} | ||
|
||
@Test | ||
public void query_forAccountUnreadMessages_whenNoMessages_returns0Unread() { | ||
createAccount(); | ||
cursor = mMockResolver.query( | ||
Uri.parse("content://" + MessageProvider.AUTHORITY + "/account_unread/0"), | ||
null, null, null, null); | ||
cursor.moveToFirst(); | ||
|
||
assertEquals(2, cursor.getColumnCount()); | ||
assertEquals(1, cursor.getColumnIndex(MessageProvider.UnreadColumns.ACCOUNT_NAME)); | ||
assertEquals(0, cursor.getColumnIndex(MessageProvider.UnreadColumns.UNREAD)); | ||
assertEquals(0, cursor.getInt(0)); | ||
assertEquals("TestAccount", cursor.getString(1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters