Created
March 20, 2019 15:56
-
-
Save pfmaggi/fe2d069fb9c9b9c6ac3582b2e0a1e646 to your computer and use it in GitHub Desktop.
Sample implementation for testing CoroutineWorker from the WorkManager library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2019 Google LLC | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example.android.wmtest.main | |
import android.content.Context | |
import android.util.Log | |
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | |
import androidx.test.platform.app.InstrumentationRegistry | |
import androidx.work.OneTimeWorkRequestBuilder | |
import androidx.work.WorkInfo | |
import androidx.work.WorkManager | |
import androidx.work.WorkerParameters | |
import androidx.work.testing.SynchronousExecutor | |
import androidx.work.testing.WorkManagerTestInitHelper | |
import kotlinx.coroutines.asCoroutineDispatcher | |
import kotlinx.coroutines.runBlocking | |
import org.hamcrest.CoreMatchers.`is` | |
import org.junit.Assert.assertThat | |
import org.junit.Before | |
import org.junit.Rule | |
import org.junit.Test | |
import org.junit.runner.RunWith | |
import org.junit.runners.JUnit4 | |
import java.util.concurrent.TimeUnit | |
@RunWith(JUnit4::class) | |
class RefreshMainDataWorkTest { | |
private lateinit var targetContext: Context | |
private lateinit var configuration: Configuration | |
private lateinit var workManager: WorkManager | |
@get:Rule | |
var instantTaskExecutorRule = InstantTaskExecutorRule() | |
@Before | |
fun setup() { | |
targetContext = InstrumentationRegistry.getInstrumentation().targetContext | |
configuration = Configuration.Builder() | |
// Set log level to Log.DEBUG to make it easier to debug | |
.setMinimumLoggingLevel(Log.DEBUG) | |
// Use a SynchronousExecutor here to make it easier to write tests | |
.setExecutor(SynchronousExecutor()) | |
.build() | |
// Initialize WorkManager for instrumentation tests. | |
WorkManagerTestInitHelper.initializeTestWorkManager(targetContext, configuration) | |
workManager = WorkManager.getInstance() | |
} | |
@Test | |
@Throws(Exception::class) | |
fun testRefreshMainDataWork() { | |
// Create request | |
val request = OneTimeWorkRequestBuilder<MyWorkTestable>() | |
.build() | |
// Enqueue and wait for result. This also runs the Worker synchronously | |
// because we are using a SynchronousExecutor. | |
workManager.enqueue(request).result.get() | |
// Get WorkInfo | |
val workInfo = workManager.getWorkInfoById(request.id).get() | |
// Assert | |
assertThat(workInfo.state, `is`(WorkInfo.State.SUCCEEDED)) | |
} | |
} | |
class RefreshMainDataWorkTestable(context: Context, params: WorkerParameters) : | |
RefreshMainDataWork(context, params) { | |
override val coroutineContext = SynchronousExecutor().asCoroutineDispatcher() | |
override suspend fun doWork(): Result = runBlocking { | |
super.doWork() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment