Skip to content

Commit

Permalink
Merge branch 'refs/heads/main-ose' into 1077-edge-to-edge-not-working…
Browse files Browse the repository at this point in the history
…-properly
  • Loading branch information
ArnyminerZ committed Nov 18, 2024
2 parents baf099d + 0f4e48a commit 27bf1c0
Show file tree
Hide file tree
Showing 26 changed files with 923 additions and 581 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package at.bitfire.davdroid.resource

import android.accounts.Account
import android.content.ContentProviderClient
import android.content.Context
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.sync.account.SystemAccountUtils
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
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.Rule
import org.junit.Test

@HiltAndroidTest
class LocalAddressBookStoreTest {

@get:Rule
val hiltRule = HiltAndroidRule(this)

@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(
collectionRepository = 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
every { get(200) } returns mockk<Service> {
every { accountName } returns "[email protected]"
}
},
settings = mockk(relaxed = true)
)

@Test
fun test_accountName_missingService() {
val collection = mockk<Collection> {
every { id } returns 42
every { url } returns "https://example.com/addressbook/funnyfriends".toHttpUrl()
every { displayName } returns null
every { serviceId } returns 404
}
assertEquals("funnyfriends #42", localAddressBookStore.accountName(collection))
}

@Test
fun test_accountName_missingDisplayName() {
val collection = mockk<Collection> {
every { id } returns 42
every { url } returns "https://example.com/addressbook/funnyfriends".toHttpUrl()
every { displayName } returns null
every { serviceId } returns 200
}
val accountName = localAddressBookStore.accountName(collection)
assertEquals("funnyfriends ([email protected]) #42", accountName)
}

@Test
fun test_accountName_missingDisplayNameAndService() {
val collection = mockk<Collection>(relaxed = true) {
every { id } returns 1
every { url } returns "https://example.com/addressbook/funnyfriends".toHttpUrl()
every { displayName } returns null
every { serviceId } returns 404 // missing service
}
assertEquals("funnyfriends #1", localAddressBookStore.accountName(collection))
}


@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)
}

@Test
fun test_createAccount_succeeds() {
mockkObject(SystemAccountUtils)
every { SystemAccountUtils.createAccount(any(), any(), any()) } returns true
val account = Account("[email protected]", "com.bitfire.davdroid.addressbook")
val createdAccount: Account = localAddressBookStore.createAccount(
"[email protected]", 42, "https://example.com/addressbook/funnyfriends"
)!!
verify(exactly = 1) { SystemAccountUtils.createAccount(context, account, any()) }
assertEquals(account, createdAccount)
}


/**
* Tests the calculation of read only state is correct
*/
@Test
fun test_shouldBeReadOnly() {
val collectionReadOnly = mockk<Collection> { every { readOnly() } returns true }
assertTrue(LocalAddressBookStore.shouldBeReadOnly(collectionReadOnly, false))
assertTrue(LocalAddressBookStore.shouldBeReadOnly(collectionReadOnly, true))

val collectionNotReadOnly = mockk<Collection> { every { readOnly() } returns false }
assertFalse(LocalAddressBookStore.shouldBeReadOnly(collectionNotReadOnly, false))
assertTrue(LocalAddressBookStore.shouldBeReadOnly(collectionNotReadOnly, true))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,32 @@ package at.bitfire.davdroid.resource

import android.Manifest
import android.accounts.Account
import android.accounts.AccountManager
import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.Context
import android.provider.ContactsContract
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import at.bitfire.davdroid.db.Collection
import at.bitfire.vcard4android.Contact
import at.bitfire.vcard4android.GroupMethod
import at.bitfire.vcard4android.LabeledProperty
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import ezvcard.property.Telephone
import io.mockk.every
import io.mockk.mockk
import java.util.LinkedList
import javax.inject.Inject
import org.junit.After
import org.junit.AfterClass
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
import java.util.LinkedList
import javax.inject.Inject

@HiltAndroidTest
class LocalAddressBookTest {
Expand Down Expand Up @@ -63,7 +60,8 @@ class LocalAddressBookTest {
@After
fun tearDown() {
// remove address book
addressBook.deleteCollection()
val accountManager = AccountManager.get(context)
accountManager.removeAccountExplicitly(addressBook.addressBookAccount)
}


Expand Down Expand Up @@ -127,21 +125,6 @@ class LocalAddressBookTest {
assertEquals("Test Group", group.displayName)
}

/**
* Tests the calculation of read only state is correct
*/
@Test
fun test_shouldBeReadOnly() {
val collectionReadOnly = mockk<Collection> { every { readOnly() } returns true }
assertTrue(LocalAddressBook.shouldBeReadOnly(collectionReadOnly, false))
assertTrue(LocalAddressBook.shouldBeReadOnly(collectionReadOnly, true))

val collectionNotReadOnly = mockk<Collection> { every { readOnly() } returns false }
assertFalse(LocalAddressBook.shouldBeReadOnly(collectionNotReadOnly, false))
assertTrue(LocalAddressBook.shouldBeReadOnly(collectionNotReadOnly, true))
}


companion object {

@JvmField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class LocalCalendarTest {

@After
fun tearDown() {
calendar.deleteCollection()
calendar.delete()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.os.Build
import android.provider.CalendarContract
import android.provider.CalendarContract.ACCOUNT_TYPE_LOCAL
import android.provider.CalendarContract.Events
import androidx.datastore.dataStore
import androidx.test.platform.app.InstrumentationRegistry
import at.bitfire.davdroid.InitCalendarProviderRule
import at.bitfire.ical4android.AndroidCalendar
Expand Down Expand Up @@ -74,7 +75,7 @@ class LocalEventTest {

@After
fun removeCalendar() {
calendar.deleteCollection()
calendar.delete()
}


Expand Down Expand Up @@ -282,7 +283,7 @@ class LocalEventTest {
})
}
val localEvent = LocalEvent(calendar, event, "filename.ics", null, null, 0)
val uri = localEvent.add()
localEvent.add()

calendar.findById(localEvent.id!!)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class LocalTestCollection(
override val readOnly: Boolean
get() = throw NotImplementedError()

override fun deleteCollection(): Boolean = true

override fun findDeleted() = entries.filter { it.deleted }
override fun findDirty() = entries.filter { it.dirty }

Expand Down
Loading

0 comments on commit 27bf1c0

Please sign in to comment.