Skip to content

Commit

Permalink
[WIP] Remove "test account" account type
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Jan 7, 2025
1 parent 62dc374 commit 45d3819
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 282 deletions.
17 changes: 0 additions & 17 deletions app/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,4 @@
<uses-permission android:name="android.permission.GET_ACCOUNTS" android:maxSdkVersion="22"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" android:maxSdkVersion="22"/>

<application>

<!-- test account type (without associated sync adapters) -->
<service
android:name="at.bitfire.davdroid.sync.account.TestAccountAuthenticator"
android:exported="false">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>

<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/test_account_authenticator"/>
</service>

</application>

</manifest>
19 changes: 19 additions & 0 deletions app/src/androidTest/kotlin/at/bitfire/davdroid/TestModules.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
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 @@ -6,15 +6,13 @@ 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.vcard4android.Contact
import at.bitfire.vcard4android.GroupMethod
import at.bitfire.vcard4android.LabeledProperty
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
Expand All @@ -24,7 +22,7 @@ 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
Expand All @@ -36,16 +34,12 @@ import javax.inject.Inject
@HiltAndroidTest
class LocalAddressBookTest {

@Inject @ApplicationContext
lateinit var context: Context

@get:Rule
val hiltRule = HiltAndroidRule(this)

@Inject
lateinit var addressbookFactory: LocalTestAddressBook.Factory

@Inject
@ApplicationContext
lateinit var context: Context

val account = Account("Test Account", "Test Account Type")
lateinit var addressBook: LocalTestAddressBook

Expand All @@ -54,15 +48,13 @@ class LocalAddressBookTest {
fun setUp() {
hiltRule.inject()

addressBook = addressbookFactory.create(account, provider, GroupMethod.CATEGORIES)
LocalTestAddressBook.createAccount(context)
addressBook = LocalTestAddressBook.create(context, account, provider)
}

@After
fun tearDown() {
// remove address book
val accountManager = AccountManager.get(context)
accountManager.removeAccountExplicitly(addressBook.addressBookAccount)
addressBook.remove()
}


Expand All @@ -87,7 +79,7 @@ class LocalAddressBookTest {
// rename address book
val newName = "New Name"
addressBook.renameAccount(newName)
assertEquals(Account(newName, LocalTestAddressBook.ACCOUNT.type), addressBook.addressBookAccount)
assertEquals(newName, addressBook.addressBookAccount.name)

// check whether contact is still here (including data rows) and not dirty
val result = addressBook.findContactById(id)
Expand Down Expand Up @@ -115,8 +107,8 @@ class LocalAddressBookTest {

// rename address book
val newName = "New Name"
addressBook.renameAccount(newName)
assertEquals(Account(newName, LocalTestAddressBook.ACCOUNT.type), addressBook.addressBookAccount)
assertTrue(addressBook.renameAccount(newName))
assertEquals(newName, addressBook.addressBookAccount.name)

// check whether group is still here and not dirty
val result = addressBook.findGroupById(id)
Expand All @@ -126,6 +118,7 @@ class LocalAddressBookTest {
assertEquals("Test Group", group.displayName)
}


companion object {

@JvmField
Expand All @@ -137,9 +130,8 @@ class LocalAddressBookTest {
@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 @@ -21,6 +21,7 @@ import at.bitfire.vcard4android.GroupMethod
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.After
import org.junit.AfterClass
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand All @@ -37,39 +38,12 @@ import javax.inject.Inject
@HiltAndroidTest
class LocalGroupTest {

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 = InstrumentationRegistry.getInstrumentation().targetContext
provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!!
assertNotNull(provider)
}

@AfterClass
@JvmStatic
fun disconnect() {
provider.close()
}
}
@Inject @ApplicationContext
lateinit var context: Context

@get:Rule
val hiltRule = HiltAndroidRule(this)

@Inject
@ApplicationContext
lateinit var context: Context

@Inject
lateinit var addressbookFactory: LocalTestAddressBook.Factory

val account = Account("Test Account", "Test Account Type")
private lateinit var addressBookGroupsAsCategories: LocalTestAddressBook
private lateinit var addressBookGroupsAsVCards: LocalTestAddressBook
Expand All @@ -78,14 +52,19 @@ class LocalGroupTest {
fun setup() {
hiltRule.inject()

addressBookGroupsAsCategories = addressbookFactory.create(account, provider, GroupMethod.CATEGORIES)
addressBookGroupsAsVCards = addressbookFactory.create(account, provider, GroupMethod.GROUP_VCARDS)
addressBookGroupsAsCategories = LocalTestAddressBook.create(context, account, provider, GroupMethod.CATEGORIES)
addressBookGroupsAsVCards = LocalTestAddressBook.create(context, account, provider, GroupMethod.GROUP_VCARDS)

// clear contacts
addressBookGroupsAsCategories.clear()
addressBookGroupsAsVCards.clear()
}

@After
fun tearDown() {

}


@Test
fun testApplyPendingMemberships_addPendingMembership() {
Expand Down Expand Up @@ -279,4 +258,28 @@ class LocalGroupTest {
add()
}


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 = InstrumentationRegistry.getInstrumentation().targetContext
provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!!
assertNotNull(provider)
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.Context
import android.provider.ContactsContract
import at.bitfire.davdroid.R
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.repository.DavServiceRepository
import at.bitfire.davdroid.settings.AccountSettings
Expand All @@ -18,25 +19,30 @@ import at.bitfire.vcard4android.GroupMethod
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import org.junit.Assert.assertTrue
import java.io.FileNotFoundException
import java.util.Optional
import java.util.concurrent.atomic.AtomicInteger
import java.util.logging.Logger

class LocalTestAddressBook @AssistedInject constructor(
@Assisted account: Account,
@Assisted("addressBook") addressBookAccount: Account,
@Assisted provider: ContentProviderClient,
@Assisted override val groupMethod: GroupMethod,
accountSettingsFactory: AccountSettings.Factory,
collectionRepository: DavCollectionRepository,
@ApplicationContext context: Context,
@ApplicationContext private val context: Context,
logger: Logger,
serviceRepository: DavServiceRepository,
syncFramework: SyncFrameworkIntegration
): LocalAddressBook(
account = account,
_addressBookAccount = ACCOUNT,
_addressBookAccount = addressBookAccount,
provider = provider,
accountSettingsFactory = accountSettingsFactory,
collectionRepository = collectionRepository,
Expand All @@ -49,7 +55,7 @@ class LocalTestAddressBook @AssistedInject constructor(

@AssistedFactory
interface Factory {
fun create(account: Account, provider: ContentProviderClient, groupMethod: GroupMethod): LocalTestAddressBook
fun create(account: Account, @Assisted("addressBook") addressBookAccount: Account, provider: ContentProviderClient, groupMethod: GroupMethod): LocalTestAddressBook
}

override var readOnly: Boolean
Expand Down Expand Up @@ -97,14 +103,32 @@ class LocalTestAddressBook @AssistedInject constructor(
throw FileNotFoundException()
}

fun remove() {
val accountManager = AccountManager.get(context)
assertTrue(accountManager.removeAccountExplicitly(addressBookAccount))
}


companion object {

val ACCOUNT = Account("LocalTestAddressBook", "at.bitfire.davdroid.test")
@dagger.hilt.EntryPoint
@InstallIn(SingletonComponent::class)
interface EntryPoint {
fun localTestAddressBookFactory(): Factory
}

val counter = AtomicInteger()

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))
val accountManager = AccountManager.get(context)
assertTrue(accountManager.addAccountExplicitly(addressBookAccount, null, null))

fun createAccount(context: Context) {
val am = AccountManager.get(context)
assertTrue("Couldn't create account for local test address-book", am.addAccountExplicitly(ACCOUNT, null, null))
// return address book with this account
val entryPoint = EntryPointAccessors.fromApplication<EntryPoint>(context)
val factory = entryPoint.localTestAddressBookFactory()
return factory.create(account, addressBookAccount, provider, groupMethod)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class CachedGroupMembershipHandlerTest {
}


@Inject
lateinit var addressbookFactory: LocalTestAddressBook.Factory

@Inject
@ApplicationContext
lateinit var context: Context
Expand All @@ -76,7 +73,7 @@ class CachedGroupMembershipHandlerTest {

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

val contact = Contact()
val localContact = LocalContact(addressBook, contact, null, null, 0)
Expand Down
Loading

0 comments on commit 45d3819

Please sign in to comment.