From 1da29cb58b0aa5f2ecf3d6ac37784a145b5ed8e1 Mon Sep 17 00:00:00 2001 From: skydoves Date: Fri, 22 Mar 2019 15:17:27 +0900 Subject: [PATCH 1/2] Added TaskExecutorUtils --- .../sunflower/utilities/TaskExecutorUtils.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 app/src/androidTest/java/com/google/samples/apps/sunflower/utilities/TaskExecutorUtils.kt diff --git a/app/src/androidTest/java/com/google/samples/apps/sunflower/utilities/TaskExecutorUtils.kt b/app/src/androidTest/java/com/google/samples/apps/sunflower/utilities/TaskExecutorUtils.kt new file mode 100644 index 000000000..fd76472ea --- /dev/null +++ b/app/src/androidTest/java/com/google/samples/apps/sunflower/utilities/TaskExecutorUtils.kt @@ -0,0 +1,30 @@ +package com.google.samples.apps.sunflower.utilities + +import androidx.arch.core.executor.ArchTaskExecutor +import androidx.arch.core.executor.TaskExecutor + +/** + * Helper methods for serves as a central point to execute common tasks, from + * https://android.googlesource.com/platform/frameworks/support/+/refs/tags/android-p-preview-1/app-toolkit/runtime/src/main/java/android/arch/core/executor/ArchTaskExecutor.java + * + * Sets a delegate to handle task execution requests using TaskExecutor as the delegate. + * App Toolkit components will use a TaskExecutors. + */ +fun registerTaskExecutor() { + ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() { + override fun executeOnDiskIO(runnable: Runnable) = runnable.run() + override fun isMainThread() = true + override fun postToMainThread(runnable: Runnable) = runnable.run() + }) +} + +/** + * Helper methods for serves as a central point to execute common tasks, from + * https://android.googlesource.com/platform/frameworks/support/+/refs/tags/android-p-preview-1/app-toolkit/runtime/src/main/java/android/arch/core/executor/ArchTaskExecutor.java + * + * Sets the default delegate to handle task execution requests. + * App Toolkit components will use the default TaskExecutor. + */ +fun unRegisterTaskExecutor() { + ArchTaskExecutor.getInstance().setDelegate(null) +} \ No newline at end of file From db098f77c640c22e5a9aa0b429cf754c8257dcd8 Mon Sep 17 00:00:00 2001 From: skydoves Date: Fri, 22 Mar 2019 15:19:25 +0900 Subject: [PATCH 2/2] replaced InstantTaskExecutorRule with ArchTaskExecutor --- .../apps/sunflower/data/GardenPlantingDaoTest.kt | 11 ++++++----- .../samples/apps/sunflower/data/PlantDaoTest.kt | 11 ++++++----- .../sunflower/viewmodels/PlantDetailViewModelTest.kt | 11 ++++++----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt index 294ebaba8..8f11e4fee 100644 --- a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt +++ b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt @@ -16,20 +16,20 @@ package com.google.samples.apps.sunflower.data -import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.room.Room import androidx.test.InstrumentationRegistry import androidx.test.espresso.matcher.ViewMatchers.assertThat import com.google.samples.apps.sunflower.utilities.getValue +import com.google.samples.apps.sunflower.utilities.registerTaskExecutor import com.google.samples.apps.sunflower.utilities.testCalendar import com.google.samples.apps.sunflower.utilities.testGardenPlanting import com.google.samples.apps.sunflower.utilities.testPlant import com.google.samples.apps.sunflower.utilities.testPlants +import com.google.samples.apps.sunflower.utilities.unRegisterTaskExecutor import org.hamcrest.CoreMatchers.equalTo import org.junit.After import org.junit.Assert.assertNull import org.junit.Before -import org.junit.Rule import org.junit.Test class GardenPlantingDaoTest { @@ -37,9 +37,6 @@ class GardenPlantingDaoTest { private lateinit var gardenPlantingDao: GardenPlantingDao private var testGardenPlantingId: Long = 0 - @get:Rule - var instantTaskExecutorRule = InstantTaskExecutorRule() - @Before fun createDb() { val context = InstrumentationRegistry.getTargetContext() database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build() @@ -47,10 +44,14 @@ class GardenPlantingDaoTest { database.plantDao().insertAll(testPlants) testGardenPlantingId = gardenPlantingDao.insertGardenPlanting(testGardenPlanting) + + registerTaskExecutor() } @After fun closeDb() { database.close() + + unRegisterTaskExecutor() } @Test fun testGetGardenPlantings() { diff --git a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt index a0049120a..31211130e 100644 --- a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt +++ b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt @@ -16,16 +16,16 @@ package com.google.samples.apps.sunflower.data -import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.room.Room import androidx.test.InstrumentationRegistry import androidx.test.runner.AndroidJUnit4 import com.google.samples.apps.sunflower.utilities.getValue +import com.google.samples.apps.sunflower.utilities.registerTaskExecutor +import com.google.samples.apps.sunflower.utilities.unRegisterTaskExecutor import org.hamcrest.Matchers.equalTo import org.junit.After import org.junit.Assert.assertThat import org.junit.Before -import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -37,9 +37,6 @@ class PlantDaoTest { private val plantB = Plant("2", "B", "", 1, 1, "") private val plantC = Plant("3", "C", "", 2, 2, "") - @get:Rule - var instantTaskExecutorRule = InstantTaskExecutorRule() - @Before fun createDb() { val context = InstrumentationRegistry.getTargetContext() database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build() @@ -47,10 +44,14 @@ class PlantDaoTest { // Insert plants in non-alphabetical order to test that results are sorted by name plantDao.insertAll(listOf(plantB, plantC, plantA)) + + registerTaskExecutor() } @After fun closeDb() { database.close() + + unRegisterTaskExecutor() } @Test fun testGetPlants() { diff --git a/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt b/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt index 0ddff5150..5108377c2 100644 --- a/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt +++ b/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt @@ -16,18 +16,18 @@ package com.google.samples.apps.sunflower.viewmodels -import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.room.Room import androidx.test.InstrumentationRegistry import com.google.samples.apps.sunflower.data.AppDatabase import com.google.samples.apps.sunflower.data.GardenPlantingRepository import com.google.samples.apps.sunflower.data.PlantRepository import com.google.samples.apps.sunflower.utilities.getValue +import com.google.samples.apps.sunflower.utilities.registerTaskExecutor import com.google.samples.apps.sunflower.utilities.testPlant +import com.google.samples.apps.sunflower.utilities.unRegisterTaskExecutor import org.junit.After import org.junit.Assert.assertFalse import org.junit.Before -import org.junit.Rule import org.junit.Test class PlantDetailViewModelTest { @@ -35,9 +35,6 @@ class PlantDetailViewModelTest { private lateinit var appDatabase: AppDatabase private lateinit var viewModel: PlantDetailViewModel - @get:Rule - var instantTaskExecutorRule = InstantTaskExecutorRule() - @Before fun setUp() { val context = InstrumentationRegistry.getTargetContext() @@ -47,11 +44,15 @@ class PlantDetailViewModelTest { val gardenPlantingRepo = GardenPlantingRepository.getInstance( appDatabase.gardenPlantingDao()) viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, testPlant.plantId) + + registerTaskExecutor() } @After fun tearDown() { appDatabase.close() + + unRegisterTaskExecutor() } @Test