Skip to content

Commit

Permalink
Use TestAccount correctly again (was not merged correctly)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Jan 9, 2025
1 parent 4d53360 commit 4ca0ef6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
package at.bitfire.davdroid.repository

import android.content.Context
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.sync.account.TestAccountAuthenticator
import at.bitfire.davdroid.sync.account.TestAccount
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
Expand Down Expand Up @@ -41,7 +40,7 @@ class AccountRepositoryTest {

@Test
fun testRemoveOrphanedInDb() {
TestAccountAuthenticator.provide { systemAccount ->
TestAccount.provide { systemAccount ->
val dao = db.accountDao()
dao.insertOrIgnore(DbAccount(id = 1, name = systemAccount.name))
dao.insertOrIgnore(DbAccount(id = 2, name = "no-corresponding-system-account"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.sync.account.TestAccountAuthenticator
import at.bitfire.davdroid.sync.account.TestAccount
import dagger.Lazy
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
Expand Down Expand Up @@ -51,19 +51,17 @@ class DavCollectionRepositoryTest {
fun setUp() {
hiltRule.inject()

account = TestAccountAuthenticator.create()
account = TestAccount.create()
db.accountDao().insertOrIgnore(DbAccount(name = account.name))

val service = Service(id=0, accountName=account.name, type= Service.TYPE_CALDAV, principal = null)
serviceId = serviceRepository.insertOrReplace(service)
}

@After
fun cleanUp() {
db.close()

fun tearDown() {
serviceRepository.deleteAll()
TestAccountAuthenticator.remove(account)
TestAccount.remove(account)
}


Expand Down Expand Up @@ -102,7 +100,4 @@ class DavCollectionRepositoryTest {
}
}


// Test helpers and dependencies

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.accounts.Account
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.HomeSet
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.sync.account.TestAccountAuthenticator
import at.bitfire.davdroid.sync.account.TestAccount
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import okhttp3.HttpUrl.Companion.toHttpUrl
Expand Down Expand Up @@ -43,7 +43,7 @@ class DavHomeSetRepositoryTest {
fun setUp() {
hiltRule.inject()

account = TestAccountAuthenticator.create()
account = TestAccount.create()
db.accountDao().insertOrIgnore(DbAccount(name = account.name))

val service = Service(id=0, accountName=account.name, type= Service.TYPE_CALDAV, principal = null)
Expand All @@ -52,7 +52,7 @@ class DavHomeSetRepositoryTest {

@After
fun tearDown() {
TestAccountAuthenticator.remove(account)
TestAccount.remove(account)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.settings.Settings
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.sync.account.TestAccountAuthenticator
import at.bitfire.davdroid.sync.account.TestAccount
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
Expand Down Expand Up @@ -81,7 +81,7 @@ class CollectionListRefresherTest {
fun setup() {
hiltRule.inject()

account = TestAccountAuthenticator.create()
account = TestAccount.create()
db.accountDao().insertOrIgnore(DbAccount(name = account.name))

// Start mock web server
Expand All @@ -96,7 +96,7 @@ class CollectionListRefresherTest {
@After
fun teardown() {
mockServer.shutdown()
TestAccountAuthenticator.remove(account)
TestAccount.remove(account)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AccountsCleanupWorkerTest {
hiltRule.inject()
TestUtils.setUpWorkManager(context, workerFactory)

account = TestAccountAuthenticator.create()
account = TestAccount.create()
db.accountDao().insertOrIgnore(DbAccount(name = account.name))

// Prepare test account
Expand All @@ -74,7 +74,7 @@ class AccountsCleanupWorkerTest {
// Remove the account here in any case; Nice to have when the test fails
accountManager.removeAccountExplicitly(addressBookAccount)

TestAccountAuthenticator.remove(account)
TestAccount.remove(account)
}


Expand All @@ -98,26 +98,24 @@ class AccountsCleanupWorkerTest {

@Test
fun testCleanUpAddressBooks_keepsAddressBookWithAccount() {
TestAccountAuthenticator.provide { account ->
// Create address book account _with_ corresponding account and verify
val userData = Bundle(2).apply {
putString(LocalAddressBook.USER_DATA_ACCOUNT_NAME, account.name)
putString(LocalAddressBook.USER_DATA_ACCOUNT_TYPE, account.type)
}
assertTrue(accountManager.addAccountExplicitly(addressBookAccount, null, userData))

val addressBookAccounts = accountManager.getAccountsByType(addressBookAccountType)
assertEquals(addressBookAccount, addressBookAccounts.firstOrNull())

// Create worker and run the method
val worker = TestListenableWorkerBuilder<AccountsCleanupWorker>(context)
.setWorkerFactory(workerFactory)
.build()
worker.cleanUpAddressBooks()

// Verify account was _not_ deleted
assertEquals(addressBookAccount, addressBookAccounts.firstOrNull())
// Create address book account _with_ corresponding account and verify
val userData = Bundle(2).apply {
putString(LocalAddressBook.USER_DATA_ACCOUNT_NAME, account.name)
putString(LocalAddressBook.USER_DATA_ACCOUNT_TYPE, account.type)
}
assertTrue(accountManager.addAccountExplicitly(addressBookAccount, null, userData))

val addressBookAccounts = accountManager.getAccountsByType(addressBookAccountType)
assertEquals(addressBookAccount, addressBookAccounts.firstOrNull())

// Create worker and run the method
val worker = TestListenableWorkerBuilder<AccountsCleanupWorker>(context)
.setWorkerFactory(workerFactory)
.build()
worker.cleanUpAddressBooks()

// Verify account was _not_ deleted
assertEquals(addressBookAccount, addressBookAccounts.firstOrNull())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import org.junit.Assert.assertTrue

object TestAccount {

private val context by lazy { InstrumentationRegistry.getInstrumentation().context }
private val targetContext by lazy { InstrumentationRegistry.getInstrumentation().targetContext }

val accountManager by lazy { AccountManager.get(context) }

/**
* Creates a test account, usually in the `Before` setUp of a test.
*
Expand All @@ -23,9 +26,10 @@ object TestAccount {
val accountType = targetContext.getString(R.string.account_type)
val account = Account("Test Account", accountType)

val initialData = AccountSettings.initialUserData(null)
initialData.putString(AccountSettings.KEY_SETTINGS_VERSION, version.toString())
assertTrue(SystemAccountUtils.createAccount(targetContext, account, initialData))
val initialData = AccountSettings.initialUserData(null).apply {
putString(AccountSettings.KEY_SETTINGS_VERSION, version.toString())
}
assertTrue(SystemAccountUtils.createAccount(context, account, initialData))

return account
}
Expand All @@ -34,8 +38,7 @@ object TestAccount {
* Removes a test account, usually in the `@After` tearDown of a test.
*/
fun remove(account: Account) {
val am = AccountManager.get(targetContext)
assertTrue(am.removeAccountExplicitly(account))
assertTrue(accountManager.removeAccountExplicitly(account))
}

/**
Expand Down

This file was deleted.

0 comments on commit 4ca0ef6

Please sign in to comment.