From 4272e923762ee2ab27009d32e50deb66c4bd0439 Mon Sep 17 00:00:00 2001 From: XinyueZ Date: Sat, 23 Feb 2019 17:49:08 +0100 Subject: [PATCH] [UPDATE/OPTIMIZE] Add tests for PlantDetailViewModel and adjust add() --- .../viewmodels/PlantDetailViewModelTest.kt | 67 ++++++++++++++++++- .../viewmodels/PlantDetailViewModel.kt | 7 +- 2 files changed, 68 insertions(+), 6 deletions(-) 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..1eaa7de9a 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 @@ -17,6 +17,7 @@ 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 @@ -24,16 +25,27 @@ 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() @@ -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) } @@ -59,4 +71,53 @@ 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) + } + + @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) + } } \ No newline at end of file diff --git a/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt b/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt index bbe6e6cf6..310cfc2ba 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt @@ -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. @@ -25,6 +25,7 @@ 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.ExperimentalCoroutinesApi +import kotlinx.coroutines.Job import kotlinx.coroutines.cancel import kotlinx.coroutines.launch @@ -61,8 +62,8 @@ class PlantDetailViewModel( plant = plantRepository.getPlant(plantId) } - fun addPlantToGarden() { - viewModelScope.launch { + fun addPlantToGarden(): Job { + return viewModelScope.launch { gardenPlantingRepository.createGardenPlanting(plantId) } }