-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
package at.bitfire.davdroid.resource | ||
|
||
import android.accounts.Account | ||
import android.content.ContentProviderClient | ||
import android.content.Context | ||
import android.provider.ContactsContract | ||
import at.bitfire.davdroid.R | ||
import at.bitfire.davdroid.db.Collection | ||
import at.bitfire.davdroid.db.Service | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.InjectMockKs | ||
import io.mockk.impl.annotations.SpyK | ||
import io.mockk.junit4.MockKRule | ||
import io.mockk.just | ||
import io.mockk.mockk | ||
import io.mockk.mockkObject | ||
import io.mockk.runs | ||
import io.mockk.verify | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
|
@@ -19,13 +29,29 @@ class LocalAddressBookStoreTest { | |
@get:Rule | ||
val mockkRule = MockKRule(this) | ||
|
||
val context: Context = mockk(relaxed = true) { | ||
every { getString(R.string.account_type_address_book) } returns "com.bitfire.davdroid.addressbook" | ||
} | ||
// val account = Account("[email protected]", "com.bitfire.davdroid.addressbook") | ||
val account: Account = mockk(relaxed = true) { | ||
// every { name } returns "[email protected]" | ||
// every { type } returns "com.bitfire.davdroid.addressbook" | ||
} | ||
val provider = mockk<ContentProviderClient>(relaxed = true) | ||
val addressBook: LocalAddressBook = mockk(relaxed = true) { | ||
every { updateSyncFrameworkSettings() } just runs | ||
every { addressBookAccount } returns account | ||
every { settings } returns LocalAddressBookStore.contactsProviderSettings | ||
} | ||
|
||
@SpyK | ||
@InjectMockKs | ||
var localAddressBookStore = LocalAddressBookStore( | ||
addressBookFactory = mockk(relaxed = true), | ||
collectionRepository = mockk(relaxed = true), | ||
context = mockk(relaxed = true), | ||
localAddressBookFactory = mockk(relaxed = true), | ||
context = context, | ||
localAddressBookFactory = mockk(relaxed = true) { | ||
every { create(account, provider) } returns addressBook | ||
}, | ||
logger = mockk(relaxed = true), | ||
serviceRepository = mockk(relaxed = true) { | ||
every { get(any<Long>()) } returns null | ||
|
@@ -62,7 +88,7 @@ class LocalAddressBookStoreTest { | |
|
||
@Test | ||
fun test_accountName_missingDisplayNameAndService() { | ||
val collection = mockk<Collection> { | ||
val collection = mockk<Collection>(relaxed = true) { | ||
every { id } returns 1 | ||
every { url } returns "https://example.com/addressbook/funnyfriends".toHttpUrl() | ||
every { displayName } returns null | ||
|
@@ -72,6 +98,37 @@ class LocalAddressBookStoreTest { | |
} | ||
|
||
|
||
@Test | ||
fun test_create_createAccountReturnsNull() { | ||
val collection = mockk<Collection>(relaxed = true) { | ||
every { id } returns 1 | ||
every { url } returns "https://example.com/addressbook/funnyfriends".toHttpUrl() | ||
} | ||
every { localAddressBookStore.createAccount(any(), any(), any()) } returns null | ||
assertEquals(null, localAddressBookStore.create(provider, collection)) | ||
} | ||
|
||
@Test | ||
fun test_create_createAccountReturnsAccount() { | ||
val collection = mockk<Collection>(relaxed = true) { | ||
every { id } returns 1 | ||
every { url } returns "https://example.com/addressbook/funnyfriends".toHttpUrl() | ||
} | ||
every { localAddressBookStore.createAccount(any(), any(), any()) } returns account | ||
every { addressBook.readOnly } returns true | ||
val addrBook = localAddressBookStore.create(provider, collection)!! | ||
|
||
verify(exactly = 1) { addressBook.updateSyncFrameworkSettings() } | ||
assertEquals(account, addrBook.addressBookAccount) | ||
assertEquals(LocalAddressBookStore.contactsProviderSettings, addrBook.settings) | ||
assertEquals(true, addrBook.readOnly) | ||
|
||
every { addressBook.readOnly } returns false | ||
val addrBook2 = localAddressBookStore.create(provider, collection)!! | ||
assertEquals(false, addrBook2.readOnly) | ||
} | ||
|
||
|
||
/** | ||
* Tests the calculation of read only state is correct | ||
*/ | ||
|