Skip to content

Commit

Permalink
Tests: don't create account as long as Hilt is not ready (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 authored Jul 24, 2024
1 parent d4c05b9 commit 8e7d289
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import android.content.Context
import android.provider.CalendarContract
import android.provider.ContactsContract
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import androidx.work.Configuration
import androidx.work.ListenableWorker
import androidx.work.WorkManager
Expand All @@ -25,76 +24,56 @@ import at.bitfire.davdroid.TestUtils.workScheduledOrRunning
import at.bitfire.davdroid.db.Credentials
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.sync.account.AccountUtils
import dagger.assisted.AssistedFactory
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.junit4.MockKRule
import io.mockk.mockkObject
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.junit.AfterClass
import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject

@HiltAndroidTest
class PeriodicSyncWorkerTest {

companion object {

private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext

private val accountManager = AccountManager.get(context)
private val account = Account("Test Account", context.getString(R.string.account_type))
private val fakeCredentials = Credentials("test", "test")

@BeforeClass
@JvmStatic
fun setUp() {
// Initialize WorkManager for instrumentation tests.
val config = Configuration.Builder()
.setMinimumLoggingLevel(Log.DEBUG)
.build()
WorkManagerTestInitHelper.initializeTestWorkManager(context, config)

assertTrue(AccountUtils.createAccount(context, account, AccountSettings.initialUserData(fakeCredentials)))
ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 1)
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1)
}

@AfterClass
@JvmStatic
fun removeAccount() {
accountManager.removeAccountExplicitly(account)
}

}

@AssistedFactory
interface PeriodicSyncWorkerFactory {
fun create(appContext: Context, workerParams: WorkerParameters): PeriodicSyncWorker
}

@Inject
@ApplicationContext
lateinit var context: Context

@Inject
lateinit var syncWorkerFactory: PeriodicSyncWorkerFactory
lateinit var syncWorkerFactory: PeriodicSyncWorker.Factory

@get:Rule
val hiltRule = HiltAndroidRule(this)
@get:Rule
val mockkRule = MockKRule(this)

private val accountManager by lazy { AccountManager.get(context) }
private val account by lazy { Account("Test Account", context.getString(R.string.account_type)) }
private val fakeCredentials = Credentials("test", "test")

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

// Initialize WorkManager for instrumentation tests.
val config = Configuration.Builder()
.setMinimumLoggingLevel(Log.DEBUG)
.build()
WorkManagerTestInitHelper.initializeTestWorkManager(context, config)

// create test account
assertTrue(AccountUtils.createAccount(context, account, AccountSettings.initialUserData(fakeCredentials)))
ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 1)
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1)
}

@After
fun teardown() {
accountManager.removeAccountExplicitly(account)
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package at.bitfire.davdroid.sync.worker
import android.accounts.Account
import android.content.Context
import android.provider.CalendarContract
import androidx.annotation.VisibleForTesting
import androidx.hilt.work.HiltWorker
import androidx.work.Constraints
import androidx.work.Data
Expand All @@ -18,6 +19,7 @@ import androidx.work.WorkManager
import androidx.work.WorkerParameters
import at.bitfire.davdroid.sync.SyncDispatcher
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -107,4 +109,10 @@ class PeriodicSyncWorker @AssistedInject constructor(

}

@AssistedFactory
@VisibleForTesting
interface Factory {
fun create(appContext: Context, workerParams: WorkerParameters): PeriodicSyncWorker
}

}

0 comments on commit 8e7d289

Please sign in to comment.