Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.

Commit 85d03c1

Browse files
committed
[UPDATE/OPTIMIZE] Add tests for PlantDetailViewModel and adjust add()
1 parent f02de96 commit 85d03c1

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,35 @@
1717
package com.google.samples.apps.sunflower.viewmodels
1818

1919
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
20+
import androidx.lifecycle.viewModelScope
2021
import androidx.room.Room
2122
import androidx.test.InstrumentationRegistry
2223
import com.google.samples.apps.sunflower.data.AppDatabase
2324
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
2425
import com.google.samples.apps.sunflower.data.PlantRepository
2526
import com.google.samples.apps.sunflower.utilities.getValue
2627
import 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
2734
import org.junit.After
2835
import org.junit.Assert.assertFalse
36+
import org.junit.Assert.assertNotNull
37+
import org.junit.Assert.assertNull
2938
import org.junit.Before
3039
import org.junit.Rule
3140
import org.junit.Test
41+
import java.util.concurrent.TimeUnit
3242

3343
class 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
}

app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Google LLC
2+
* Copyright 2019 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import com.google.samples.apps.sunflower.data.GardenPlantingRepository
2525
import com.google.samples.apps.sunflower.data.Plant
2626
import com.google.samples.apps.sunflower.data.PlantRepository
2727
import kotlinx.coroutines.ExperimentalCoroutinesApi
28+
import kotlinx.coroutines.Job
2829
import kotlinx.coroutines.cancel
2930
import kotlinx.coroutines.launch
3031

@@ -61,8 +62,8 @@ class PlantDetailViewModel(
6162
plant = plantRepository.getPlant(plantId)
6263
}
6364

64-
fun addPlantToGarden() {
65-
viewModelScope.launch {
65+
fun addPlantToGarden(): Job {
66+
return viewModelScope.launch {
6667
gardenPlantingRepository.createGardenPlanting(plantId)
6768
}
6869
}

0 commit comments

Comments
 (0)