Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Jan 7, 2025
1 parent 45d3819 commit be03406
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 170 deletions.
19 changes: 0 additions & 19 deletions app/src/androidTest/kotlin/at/bitfire/davdroid/TestModules.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
package at.bitfire.davdroid

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import at.bitfire.davdroid.push.PushRegistrationWorkerManager
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.startup.StartupPlugin
import at.bitfire.davdroid.startup.TasksAppWatcher
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import dagger.multibindings.Multibinds
import javax.inject.Provider
import javax.inject.Qualifier

/*@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class TargetContext
@Module
@InstallIn(SingletonComponent::class)
internal object TargetContextModule {
@Provides @TargetContext
fun targetContext(): Provider<Context> = object: Provider<Context> {
override fun get() = InstrumentationRegistry.getInstrumentation().targetContext
}
}*/

// remove PushRegistrationWorkerModule from Android tests
@Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class LocalCalendarTest {
}

@Test
// Flaky, Needs single or rec init of CalendarProvider (InitCalendarProviderRule)
// Needs InitCalendarProviderRule
fun testDeleteDirtyEventsWithoutInstances_Recurring_Instances() {
val event = Event().apply {
dtStart = DtStart("20220120T010203Z")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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 @@ -41,29 +40,6 @@ import java.util.UUID

class LocalEventTest {

companion object {

@JvmField
@ClassRule
val initCalendarProviderRule: TestRule = InitCalendarProviderRule.getInstance()

private lateinit var provider: ContentProviderClient

@BeforeClass
@JvmStatic
fun setUpClass() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
provider = context.contentResolver.acquireContentProviderClient(CalendarContract.AUTHORITY)!!
}

@AfterClass
@JvmStatic
fun tearDownClass() {
provider.closeCompat()
}

}

private val account = Account("LocalCalendarTest", ACCOUNT_TYPE_LOCAL)
private lateinit var calendar: LocalCalendar

Expand Down Expand Up @@ -482,4 +458,28 @@ class LocalEventTest {
}
}

}

companion object {

@JvmField
@ClassRule
val initCalendarProviderRule: TestRule = InitCalendarProviderRule.getInstance()

private lateinit var provider: ContentProviderClient

@BeforeClass
@JvmStatic
fun setUpClass() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
provider = context.contentResolver.acquireContentProviderClient(CalendarContract.AUTHORITY)!!
}

@AfterClass
@JvmStatic
fun tearDownClass() {
provider.closeCompat()
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class LocalGroupTest {

@After
fun tearDown() {

addressBookGroupsAsCategories.remove()
addressBookGroupsAsVCards.remove()
}


Expand Down Expand Up @@ -270,9 +271,8 @@ class LocalGroupTest {
@BeforeClass
@JvmStatic
fun connect() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val context = InstrumentationRegistry.getInstrumentation().context
provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!!
assertNotNull(provider)
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ class LocalTestAddressBook @AssistedInject constructor(

val counter = AtomicInteger()

/**
* Creates a [at.bitfire.davdroid.resource.LocalTestAddressBook].
*
* Make sure to delete it with [at.bitfire.davdroid.resource.LocalTestAddressBook.remove] or [removeAll] after use.
*/
fun create(context: Context, account: Account, provider: ContentProviderClient, groupMethod: GroupMethod = GroupMethod.GROUP_VCARDS): LocalTestAddressBook {
// create new address book account
val addressBookAccount = Account("Test Address Book ${counter.incrementAndGet()}", context.getString(R.string.account_type_address_book))
Expand All @@ -131,6 +136,12 @@ class LocalTestAddressBook @AssistedInject constructor(
return factory.create(account, addressBookAccount, provider, groupMethod)
}

fun removeAll(context: Context) {
val accountManager = AccountManager.get(context)
for (account in accountManager.getAccountsByType(context.getString(R.string.account_type_address_book)))
accountManager.removeAccountExplicitly(account)
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ import javax.inject.Inject
@HiltAndroidTest
class CachedGroupMembershipHandlerTest {

@Inject
@ApplicationContext
lateinit var context: Context

@get:Rule
val hiltRule = HiltAndroidRule(this)

val account = Account("Test Account", "Test Account Type")

@Before
fun inject() {
hiltRule.inject()
}


@Test
fun testMembership() {
val addressBook = LocalTestAddressBook.create(context, account, provider, GroupMethod.GROUP_VCARDS)
try {
val contact = Contact()
val localContact = LocalContact(addressBook, contact, null, null, 0)
CachedGroupMembershipHandler(localContact).handle(ContentValues().apply {
put(CachedGroupMembership.GROUP_ID, 123456)
put(CachedGroupMembership.RAW_CONTACT_ID, 789)
}, contact)
assertArrayEquals(arrayOf(123456L), localContact.cachedGroupMemberships.toArray())
} finally {
addressBook.remove()
}
}


companion object {

@JvmField
Expand All @@ -55,33 +87,4 @@ class CachedGroupMembershipHandlerTest {

}


@Inject
@ApplicationContext
lateinit var context: Context

@get:Rule
val hiltRule = HiltAndroidRule(this)

val account = Account("Test Account", "Test Account Type")

@Before
fun inject() {
hiltRule.inject()
}


@Test
fun testMembership() {
val addressBook = LocalTestAddressBook.create(context, account, provider, GroupMethod.GROUP_VCARDS)

val contact = Contact()
val localContact = LocalContact(addressBook, contact, null, null, 0)
CachedGroupMembershipHandler(localContact).handle(ContentValues().apply {
put(CachedGroupMembership.GROUP_ID, 123456)
put(CachedGroupMembership.RAW_CONTACT_ID, 789)
}, contact)
assertArrayEquals(arrayOf(123456L), localContact.cachedGroupMemberships.toArray())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ class GroupMembershipBuilderTest {
categories += "TEST GROUP"
}
val addressBookGroupsAsCategories = LocalTestAddressBook.create(context, account, provider, GroupMethod.CATEGORIES)
GroupMembershipBuilder(Uri.EMPTY, null, contact, addressBookGroupsAsCategories, false).build().also { result ->
assertEquals(1, result.size)
assertEquals(GroupMembership.CONTENT_ITEM_TYPE, result[0].values[GroupMembership.MIMETYPE])
assertEquals(addressBookGroupsAsCategories.findOrCreateGroup("TEST GROUP"), result[0].values[GroupMembership.GROUP_ROW_ID])
try {
GroupMembershipBuilder(Uri.EMPTY, null, contact, addressBookGroupsAsCategories, false).build().also { result ->
assertEquals(1, result.size)
assertEquals(GroupMembership.CONTENT_ITEM_TYPE, result[0].values[GroupMembership.MIMETYPE])
assertEquals(addressBookGroupsAsCategories.findOrCreateGroup("TEST GROUP"), result[0].values[GroupMembership.GROUP_ROW_ID])
}
} finally {
addressBookGroupsAsCategories.remove()
}
}

Expand All @@ -65,9 +69,13 @@ class GroupMembershipBuilderTest {
categories += "TEST GROUP"
}
val addressBookGroupsAsVCards = LocalTestAddressBook.create(context, account, provider, GroupMethod.GROUP_VCARDS)
GroupMembershipBuilder(Uri.EMPTY, null, contact, addressBookGroupsAsVCards, false).build().also { result ->
// group membership is constructed during post-processing
assertEquals(0, result.size)
try {
GroupMembershipBuilder(Uri.EMPTY, null, contact, addressBookGroupsAsVCards, false).build().also { result ->
// group membership is constructed during post-processing
assertEquals(0, result.size)
}
} finally {
addressBookGroupsAsVCards.remove()
}
}

Expand All @@ -83,7 +91,7 @@ class GroupMembershipBuilderTest {
@BeforeClass
@JvmStatic
fun connect() {
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
val context: Context = InstrumentationRegistry.getInstrumentation().context
provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,6 @@ import javax.inject.Inject
@HiltAndroidTest
class GroupMembershipHandlerTest {

companion object {

@JvmField
@ClassRule
val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!!

private lateinit var provider: ContentProviderClient

@BeforeClass
@JvmStatic
fun connect() {
val context: Context = InstrumentationRegistry.getInstrumentation().context
provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!!
Assert.assertNotNull(provider)
}

@AfterClass
@JvmStatic
fun disconnect() {
provider.close()
}

}

@Inject @ApplicationContext
lateinit var context: Context

Expand Down Expand Up @@ -109,4 +85,29 @@ class GroupMembershipHandlerTest {
}
}


companion object {

@JvmField
@ClassRule
val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!!

private lateinit var provider: ContentProviderClient

@BeforeClass
@JvmStatic
fun connect() {
val context: Context = InstrumentationRegistry.getInstrumentation().context
provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!!
Assert.assertNotNull(provider)
}

@AfterClass
@JvmStatic
fun disconnect() {
provider.close()
}

}

}
Loading

0 comments on commit be03406

Please sign in to comment.