1717package com.google.samples.apps.sunflower.viewmodels
1818
1919import androidx.arch.core.executor.testing.InstantTaskExecutorRule
20+ import androidx.lifecycle.viewModelScope
2021import androidx.room.Room
2122import androidx.test.InstrumentationRegistry
2223import com.google.samples.apps.sunflower.data.AppDatabase
2324import com.google.samples.apps.sunflower.data.GardenPlantingRepository
2425import com.google.samples.apps.sunflower.data.PlantRepository
2526import com.google.samples.apps.sunflower.utilities.getValue
2627import com.google.samples.apps.sunflower.utilities.testPlant
28+ import com.google.samples.apps.sunflower.utilities.testPlants
29+ import kotlinx.coroutines.ExperimentalCoroutinesApi
30+ import kotlinx.coroutines.cancel
31+ import kotlinx.coroutines.delay
32+ import kotlinx.coroutines.launch
33+ import kotlinx.coroutines.runBlocking
2734import org.junit.After
2835import org.junit.Assert.assertFalse
36+ import org.junit.Assert.assertNotNull
37+ import org.junit.Assert.assertNull
2938import org.junit.Before
3039import org.junit.Rule
3140import org.junit.Test
41+ import java.util.concurrent.TimeUnit
3242
3343class PlantDetailViewModelTest {
3444
3545 private lateinit var appDatabase: AppDatabase
3646 private lateinit var viewModel: PlantDetailViewModel
47+ private lateinit var plantRepo: PlantRepository
48+ private lateinit var gardenPlantingRepo: GardenPlantingRepository
3749
3850 @get:Rule
3951 var instantTaskExecutorRule = InstantTaskExecutorRule ()
@@ -43,9 +55,9 @@ class PlantDetailViewModelTest {
4355 val context = InstrumentationRegistry .getTargetContext()
4456 appDatabase = Room .inMemoryDatabaseBuilder(context, AppDatabase ::class .java).build()
4557
46- val plantRepo = PlantRepository .getInstance(appDatabase.plantDao())
47- val gardenPlantingRepo = GardenPlantingRepository .getInstance(
48- appDatabase.gardenPlantingDao() )
58+ plantRepo = PlantRepository .getInstance(appDatabase.plantDao())
59+ gardenPlantingRepo = GardenPlantingRepository .getInstance(appDatabase.gardenPlantingDao())
60+ appDatabase.plantDao().insertAll(testPlants )
4961 viewModel = PlantDetailViewModel (plantRepo, gardenPlantingRepo, testPlant.plantId)
5062 }
5163
@@ -59,4 +71,59 @@ class PlantDetailViewModelTest {
5971 fun testDefaultValues () {
6072 assertFalse(getValue(viewModel.isPlanted))
6173 }
74+
75+ @Test
76+ fun shouldAddPlantToGarden () = runBlocking {
77+ val plantToAdd = testPlants[1 ]
78+ viewModel = PlantDetailViewModel (plantRepo, gardenPlantingRepo, plantToAdd.plantId)
79+
80+ launch {
81+ viewModel.addPlantToGarden()
82+ // DB needs time to update itself.
83+ delay(TimeUnit .SECONDS .toMillis(1 ))
84+ }.join()
85+
86+ val plantAdded =
87+ getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
88+ assertNotNull(plantAdded)
89+ }
90+
91+ @Test
92+ fun shouldCancelAddingPlantToGarden () = runBlocking {
93+ val plantToAdd = testPlants[2 ]
94+ viewModel = PlantDetailViewModel (plantRepo, gardenPlantingRepo, plantToAdd.plantId)
95+
96+ launch {
97+ viewModel.addPlantToGarden().cancel()
98+
99+ // Manually wait 1 sec, the adding should not happen due to call the cancel().
100+ delay(TimeUnit .SECONDS .toMillis(1 ))
101+ }.join()
102+
103+ val plantJustAdded =
104+ getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
105+ assertNull(plantJustAdded)
106+ }
107+
108+ @ExperimentalCoroutinesApi
109+ @Test
110+ fun shouldCancelAddingPlantToGardenByViewModel () = runBlocking {
111+ val plantToAdd = testPlants[2 ]
112+ viewModel = PlantDetailViewModel (plantRepo, gardenPlantingRepo, plantToAdd.plantId)
113+
114+ launch {
115+ viewModel.addPlantToGarden()
116+ }
117+
118+ launch {
119+ viewModel.viewModelScope.cancel()
120+
121+ // Manually wait 1 sec, the adding should not happen due to call the cancel().
122+ delay(TimeUnit .SECONDS .toMillis(1 ))
123+ }.join()
124+
125+ val plantJustAdded =
126+ getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
127+ assertNull(plantJustAdded)
128+ }
62129}
0 commit comments