Skip to content

Commit

Permalink
Test create address book
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Nov 14, 2024
1 parent 90ca1a0 commit 2b9ea3c
Showing 1 changed file with 61 additions and 4 deletions.
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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand Down

0 comments on commit 2b9ea3c

Please sign in to comment.