Skip to content

Commit

Permalink
Drop mainAccount method
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Sep 16, 2024
1 parent 4c596d3 commit 67d0409
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@ package at.bitfire.davdroid.repository
import android.accounts.Account
import android.accounts.AccountManager
import android.content.Context
import android.os.Bundle
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.resource.LocalAddressBook.Companion.USER_DATA_COLLECTION_ID
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.sync.TasksAppManager
import dagger.Lazy
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.every
import io.mockk.mockk
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.logging.Logger
import javax.inject.Inject

Expand Down Expand Up @@ -69,42 +60,4 @@ class AccountRepositoryTest {
accountManager.removeAccountExplicitly(addressBookAccount)
}

@Test(expected = IllegalArgumentException::class)
fun testMainAccount_OtherAccount() {
accountRepository.mainAccount(Account("Other Account", "com.example"))
}

@Test
fun testMainAccount_AddressBookAccount_WithMainAccount() {
// create address book account
assertTrue(accountManager.addAccountExplicitly(addressBookAccount, null, Bundle(1).apply {
putString(USER_DATA_COLLECTION_ID, "0")
}))
assertEquals("0", accountManager.getUserData(addressBookAccount, USER_DATA_COLLECTION_ID))

// mock main account
val mainAccount = Account("Main Account", context.getString(R.string.account_type))

// mock repository calls
every { collectionRepository.get(0L) } returns mockk<Collection>(relaxed = true) {
every { id } returns 0L
every { serviceId } returns 0L
}
every { serviceRepository.get(0L) } returns mockk<Service>(relaxed = true) {
every { accountName } returns mainAccount.name
}

// check mainAccount(); should find main account of address book account
assertEquals(mainAccount, accountRepository.mainAccount(addressBookAccount))
}

@Test
fun testMainAccount_AddressBookAccount_WithoutMainAccount() {
// create address book account
assertTrue(accountManager.addAccountExplicitly(addressBookAccount, null, Bundle.EMPTY))

// check mainAccount(); should fail because there's no main account
assertNull(accountRepository.mainAccount(addressBookAccount))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import at.bitfire.davdroid.db.Credentials
import at.bitfire.davdroid.db.HomeSet
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.resource.LocalAddressBook
import at.bitfire.davdroid.resource.LocalAddressBook.Companion.USER_DATA_COLLECTION_ID
import at.bitfire.davdroid.resource.LocalTaskList
import at.bitfire.davdroid.servicedetection.DavResourceFinder
import at.bitfire.davdroid.servicedetection.RefreshCollectionsWorker
Expand Down Expand Up @@ -172,28 +171,6 @@ class AccountRepository @Inject constructor(

fun getAll(): Array<Account> = accountManager.getAccountsByType(accountType)

/**
* Finds and returns the main account of the given address book's account
*
* @param account the address book account to find the main account for
*
* @return the associated main account, `null` if none can be found (e.g. when main account has been deleted)
*
* @throws IllegalArgumentException when [account] is not an address book account
*/
fun mainAccount(account: Account): Account? =
if (account.type == context.getString(R.string.account_type_address_book)) {
val manager = AccountManager.get(context)
manager.getUserData(account, USER_DATA_COLLECTION_ID)?.toLongOrNull()?.let { collectionId ->
collectionRepository.get(collectionId)?.let { collection ->
serviceRepository.get(collection.serviceId)?.let { service ->
Account(service.accountName, accountType)
}
}
}
} else
throw IllegalArgumentException("$account is not an address book account")

fun getAllFlow() = callbackFlow<Set<Account>> {
val listener = OnAccountsUpdateListener { accounts ->
trySend(accounts.filter { it.type == accountType }.toSet())
Expand Down

0 comments on commit 67d0409

Please sign in to comment.