Skip to content

Commit

Permalink
Merge branch 'main-ose' into 934-create-accounts-table-in-db
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Jan 9, 2025
2 parents 05a21f3 + 843013a commit 4d53360
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +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.davdroid.R
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 @@ -25,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 @@ -37,33 +34,27 @@ import javax.inject.Inject
@HiltAndroidTest
class LocalAddressBookTest {

@get:Rule
val hiltRule = HiltAndroidRule(this)

@Inject
lateinit var addressbookFactory: LocalTestAddressBook.Factory

@Inject @ApplicationContext
lateinit var context: Context
val targetContext by lazy { InstrumentationRegistry.getInstrumentation().targetContext }

val account by lazy { Account("Test Account", targetContext.getString(R.string.account_type)) }
@get:Rule
val hiltRule = HiltAndroidRule(this)

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


@Before
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 @@ -88,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 @@ -116,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 @@ -127,6 +118,7 @@ class LocalAddressBookTest {
assertEquals("Test Group", group.displayName)
}


companion object {

@JvmField
Expand All @@ -138,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 @@ -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 @@ -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,20 @@ 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() {
addressBookGroupsAsCategories.remove()
addressBookGroupsAsVCards.remove()
}


@Test
fun testApplyPendingMemberships_addPendingMembership() {
Expand Down Expand Up @@ -279,4 +259,27 @@ 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().context
provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!!
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.Context
import android.provider.ContactsContract
import androidx.test.platform.app.InstrumentationRegistry
import at.bitfire.davdroid.R
import at.bitfire.davdroid.repository.AccountRepository
import at.bitfire.davdroid.repository.DavCollectionRepository
Expand All @@ -21,26 +20,31 @@ 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,
accountRepository: AccountRepository,
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,
accountRepository = accountRepository,
accountSettingsFactory = accountSettingsFactory,
Expand All @@ -54,7 +58,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 @@ -102,15 +106,43 @@ class LocalTestAddressBook @AssistedInject constructor(
throw FileNotFoundException()
}

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


companion object {

val targetContext by lazy { InstrumentationRegistry.getInstrumentation().targetContext }
val ACCOUNT = Account("LocalTestAddressBook", targetContext.getString(R.string.account_type_address_book))
@dagger.hilt.EntryPoint
@InstallIn(SingletonComponent::class)
interface EntryPoint {
fun localTestAddressBookFactory(): Factory
}

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

fun createAccount(context: Context) {
val am = AccountManager.get(context)
assertTrue("Couldn't create account for local test address-book", am.addAccountExplicitly(ACCOUNT, null, null))
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)
}

}
Expand Down
Loading

0 comments on commit 4d53360

Please sign in to comment.