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

Commit

Permalink
[UPDATE/OPTIMIZE] Add tests for PlantDetailViewModel and adjust add()
Browse files Browse the repository at this point in the history
  • Loading branch information
XinyueZ committed Feb 23, 2019
1 parent f02de96 commit 629ce43
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,35 @@
package com.google.samples.apps.sunflower.viewmodels

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.viewModelScope
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.testPlant
import com.google.samples.apps.sunflower.utilities.testPlants
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.TimeUnit

class PlantDetailViewModelTest {

private lateinit var appDatabase: AppDatabase
private lateinit var viewModel: PlantDetailViewModel
private lateinit var plantRepo: PlantRepository
private lateinit var gardenPlantingRepo: GardenPlantingRepository

@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
Expand All @@ -43,9 +55,9 @@ class PlantDetailViewModelTest {
val context = InstrumentationRegistry.getTargetContext()
appDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()

val plantRepo = PlantRepository.getInstance(appDatabase.plantDao())
val gardenPlantingRepo = GardenPlantingRepository.getInstance(
appDatabase.gardenPlantingDao())
plantRepo = PlantRepository.getInstance(appDatabase.plantDao())
gardenPlantingRepo = GardenPlantingRepository.getInstance(appDatabase.gardenPlantingDao())
appDatabase.plantDao().insertAll(testPlants)
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, testPlant.plantId)
}

Expand All @@ -59,4 +71,54 @@ class PlantDetailViewModelTest {
fun testDefaultValues() {
assertFalse(getValue(viewModel.isPlanted))
}

@Test
fun shouldAddPlantToGarden() = runBlocking {
val plantToAdd = testPlants[1]
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, plantToAdd.plantId)

launch {
viewModel.addPlantToGarden()
// DB needs time to update itself.
delay(TimeUnit.SECONDS.toMillis(1))
}.join()

val plantAdded =
getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
assertNotNull(plantAdded)
}

@ExperimentalCoroutinesApi
@Test
fun shouldCancelAddingPlantToGarden() = runBlocking {
val plantToAdd = testPlants[2]
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, plantToAdd.plantId)

launch {
viewModel.addPlantToGarden().cancel()
}.join()

val plantJustAdded =
getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
assertNull(plantJustAdded)
}

@ExperimentalCoroutinesApi
@Test
fun shouldCancelAddingPlantToGardenByViewModel() = runBlocking {
val plantToAdd = testPlants[2]
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, plantToAdd.plantId)

launch {
viewModel.addPlantToGarden()
}

launch {
viewModel.viewModelScope.cancel()
}.join()

val plantJustAdded =
getValue(gardenPlantingRepo.getGardenPlantingForPlant(plantToAdd.plantId))
assertNull(plantJustAdded)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Google LLC
* 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.
Expand All @@ -24,6 +24,7 @@ import com.google.samples.apps.sunflower.PlantDetailFragment
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
import com.google.samples.apps.sunflower.data.Plant
import com.google.samples.apps.sunflower.data.PlantRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -61,9 +62,10 @@ class PlantDetailViewModel(
plant = plantRepository.getPlant(plantId)
}

fun addPlantToGarden() {
fun addPlantToGarden(): CoroutineScope {
viewModelScope.launch {
gardenPlantingRepository.createGardenPlanting(plantId)
}
return viewModelScope
}
}

0 comments on commit 629ce43

Please sign in to comment.