Fix sync tests

Change-Id: I4c62fcfa59793bef35eda2892f4f75acaeb86ca6
pull/2/head
Adetunji Dahunsi 3 years ago committed by Don Turner
parent b4a636c42e
commit b5d7832549

@ -21,7 +21,14 @@ plugins {
id 'nowinandroid.spotless'
}
android {
defaultConfig {
testInstrumentationRunner "com.google.samples.apps.nowinandroid.core.testing.NiaTestRunner"
}
}
dependencies {
implementation project(':core-common')
implementation project(':core-model')
implementation project(':core-domain')
implementation project(':core-datastore')
@ -41,6 +48,9 @@ dependencies {
androidTestImplementation libs.androidx.work.testing
kaptAndroidTest libs.hilt.compiler
kaptAndroidTest libs.hilt.ext.compiler
configurations.configureEach {
resolutionStrategy {
// Temporary workaround for https://issuetracker.google.com/174733673

@ -19,19 +19,23 @@ package com.google.samples.apps.nowinandroid.sync.workers
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import androidx.work.Configuration
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkInfo
import androidx.work.WorkManager
import androidx.work.testing.SynchronousExecutor
import androidx.work.testing.WorkManagerTestInitHelper
import androidx.work.workDataOf
import java.util.concurrent.TimeUnit
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@HiltAndroidTest
class SyncWorkerTest {
@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)
private val context get() = InstrumentationRegistry.getInstrumentation().context
@Before
@ -46,26 +50,26 @@ class SyncWorkerTest {
}
@Test
fun testSyncPeriodicWork() {
// Define input data
val input = workDataOf()
fun testSyncWork() {
// Create request
val request = PeriodicWorkRequestBuilder<SyncWorker>(15, TimeUnit.MINUTES)
.setInputData(input)
.build()
val request = SyncWorker.startUpSyncWork()
val workManager = WorkManager.getInstance(context)
val testDriver = WorkManagerTestInitHelper.getTestDriver(context)!!
// Enqueue and wait for result.
workManager.enqueue(request).result.get()
// Tells the testing framework the period delay is met
testDriver.setPeriodDelayMet(request.id)
// Get WorkInfo and outputData
val workInfo = workManager.getWorkInfoById(request.id).get()
val preRunWorkInfo = workManager.getWorkInfoById(request.id).get()
// Assert
assertEquals(workInfo.state, WorkInfo.State.ENQUEUED)
assertEquals(WorkInfo.State.ENQUEUED, preRunWorkInfo.state)
// Tells the testing framework that the constraints have been met
testDriver.setAllConstraintsMet(request.id)
val postRequirementWorkInfo = workManager.getWorkInfoById(request.id).get()
assertEquals(WorkInfo.State.RUNNING, postRequirementWorkInfo.state)
}
}

@ -29,13 +29,16 @@ import com.google.samples.apps.nowinandroid.core.domain.Synchronizer
import com.google.samples.apps.nowinandroid.core.domain.repository.AuthorsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.TopicsRepository
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.sync.initializers.SyncConstraints
import com.google.samples.apps.nowinandroid.sync.initializers.syncForegroundInfo
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
/**
* Syncs the data layer by delegating to the appropriate repository instances with
@ -49,12 +52,13 @@ class SyncWorker @AssistedInject constructor(
private val topicRepository: TopicsRepository,
private val newsRepository: NewsRepository,
private val authorsRepository: AuthorsRepository,
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
) : CoroutineWorker(appContext, workerParams), Synchronizer {
override suspend fun getForegroundInfo(): ForegroundInfo =
appContext.syncForegroundInfo()
override suspend fun doWork(): Result = coroutineScope {
override suspend fun doWork(): Result = withContext(ioDispatcher) {
// First sync the repositories in parallel
val syncedSuccessfully = awaitAll(
async { topicRepository.sync() },

Loading…
Cancel
Save