Skip to content

Commit

Permalink
Add tests for SimpleScanViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
naivekook committed Sep 21, 2021
1 parent 1296cad commit 1912d8c
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 4 deletions.
Expand Up @@ -9,8 +9,10 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runBlockingTest
import openfoodfacts.github.scrachx.openfood.analytics.AnalyticsEvent
import openfoodfacts.github.scrachx.openfood.analytics.MatomoAnalytics
import openfoodfacts.github.scrachx.openfood.features.compare.ProductCompareViewModel.*
import openfoodfacts.github.scrachx.openfood.models.Product
import openfoodfacts.github.scrachx.openfood.models.entities.additive.AdditiveName
import openfoodfacts.github.scrachx.openfood.network.OpenFoodAPIClient
import openfoodfacts.github.scrachx.openfood.repositories.ProductRepository
import openfoodfacts.github.scrachx.openfood.utils.CoroutineDispatchersTest
import openfoodfacts.github.scrachx.openfood.utils.LocaleManager
Expand All @@ -29,13 +31,14 @@ class ProductCompareViewModelTest {
private val localeManager: LocaleManager = mock()
private val matomoAnalytics: MatomoAnalytics = mock()
private val coroutineDispatcher = CoroutineDispatchersTest()
private val openFoodAPIClient: OpenFoodAPIClient = mock()

private lateinit var viewModel: ProductCompareViewModel

@Before
fun setup() {
whenever(localeManager.getLanguage()).doReturn("en")
viewModel = ProductCompareViewModel(productRepository, localeManager, matomoAnalytics, coroutineDispatcher)
viewModel = ProductCompareViewModel(productRepository, localeManager, matomoAnalytics, coroutineDispatcher, openFoodAPIClient)
}

@Test
Expand All @@ -52,9 +55,9 @@ class ProductCompareViewModelTest {
whenever(productRepository.getAdditiveByTagAndLanguageCode("qwerty", "en"))
.doReturn(Single.just(AdditiveName("test-name")))

val flowItems = mutableListOf<Unit>()
val flowItems = mutableListOf<SideEffect>()
val job = launch {
viewModel.alreadyExistFlow.toList(flowItems)
viewModel.sideEffectFlow.toList(flowItems)
}

// WHEN
Expand All @@ -63,6 +66,7 @@ class ProductCompareViewModelTest {

// THEN
assertThat(flowItems.size).isEqualTo(1)
assertThat(flowItems[0] is SideEffect.ProductAlreadyAdded).isTrue()
job.cancel()
}

Expand All @@ -85,7 +89,7 @@ class ProductCompareViewModelTest {
whenever(productRepository.getAdditiveByTagAndLanguageCode("qwerty3", "en"))
.doReturn(Single.just(AdditiveName("test-name3")))

val flowItems = mutableListOf<List<ProductCompareViewModel.CompareProduct>>()
val flowItems = mutableListOf<List<CompareProduct>>()
val job = launch {
viewModel.productsFlow.toList(flowItems)
}
Expand Down
@@ -0,0 +1,142 @@
package openfoodfacts.github.scrachx.openfood.features.simplescan

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runBlockingTest
import openfoodfacts.github.scrachx.openfood.features.simplescan.SimpleScanViewModel.SideEffect
import openfoodfacts.github.scrachx.openfood.models.CameraState
import openfoodfacts.github.scrachx.openfood.repositories.ScannerPreferencesRepository
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.kotlin.*

@ExperimentalCoroutinesApi
class SimpleScanViewModelTest {

@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()

private val prefsRepository: ScannerPreferencesRepository = mock()

private lateinit var viewModel: SimpleScanViewModel

@Before
fun setup() {
whenever(prefsRepository.getAutoFocusPref()).doReturn(true)
whenever(prefsRepository.getFlashPref()).doReturn(true)
whenever(prefsRepository.isMlScannerEnabled()).doReturn(false)
whenever(prefsRepository.getCameraPref()).doReturn(CameraState.Back)

viewModel = SimpleScanViewModel(prefsRepository)
}

@Test
fun onInit_shouldEmitDefaultCameraOptions() = runBlockingTest {
// GIVEN
val flowItems = mutableListOf<SimpleScanScannerOptions>()
val job = launch {
viewModel.scannerOptionsFlow.toList(flowItems)
}

// THEN
assertThat(flowItems.size).isEqualTo(1)
assertThat(flowItems[0].autoFocusEnabled).isTrue()
assertThat(flowItems[0].flashEnabled).isTrue()
assertThat(flowItems[0].mlScannerEnabled).isFalse()
assertThat(flowItems[0].cameraState).isEqualTo(CameraState.Back)
job.cancel()
}

@Test
fun changeCameraAutoFocus_shouldChangeAutoFocus() = runBlockingTest {
// GIVEN
val flowItems = mutableListOf<SimpleScanScannerOptions>()
val job = launch {
viewModel.scannerOptionsFlow.toList(flowItems)
}

// WHEN
viewModel.changeCameraAutoFocus()

// THEN
verify(prefsRepository).saveAutoFocusPref(eq(false))
assertThat(flowItems.size).isEqualTo(2)
assertThat(flowItems[1].autoFocusEnabled).isFalse()
job.cancel()
}

@Test
fun changeCameraFlash_shouldChangeFlash() = runBlockingTest {
// GIVEN
val flowItems = mutableListOf<SimpleScanScannerOptions>()
val job = launch {
viewModel.scannerOptionsFlow.toList(flowItems)
}

// WHEN
viewModel.changeCameraFlash()

// THEN
verify(prefsRepository).saveFlashPref(eq(false))
assertThat(flowItems.size).isEqualTo(2)
assertThat(flowItems[1].flashEnabled).isFalse()
job.cancel()
}

@Test
fun changeCameraState_shouldCameraState() = runBlockingTest {
// GIVEN
val flowItems = mutableListOf<SimpleScanScannerOptions>()
val job = launch {
viewModel.scannerOptionsFlow.toList(flowItems)
}

// WHEN
viewModel.changeCameraState()

// THEN
verify(prefsRepository).saveCameraPref(eq(CameraState.Front))
assertThat(flowItems.size).isEqualTo(2)
assertThat(flowItems[1].cameraState).isEqualTo(CameraState.Front)
job.cancel()
}

@Test
fun barcodeDetected_shouldEmitRightEffect() = runBlockingTest {
// GIVEN
val givenBarcode = "qwerty"
val flowItems = mutableListOf<SideEffect>()
val job = launch {
viewModel.sideEffectsFlow.toList(flowItems)
}

// WHEN
viewModel.barcodeDetected(givenBarcode)

// THEN
assertThat(flowItems.size).isEqualTo(1)
assertThat((flowItems[0] as SideEffect.BarcodeDetected).barcode).isEqualTo(givenBarcode)
job.cancel()
}

@Test
fun troubleScanningPressed_shouldEmitRightEffect() = runBlockingTest {
// GIVEN
val flowItems = mutableListOf<SideEffect>()
val job = launch {
viewModel.sideEffectsFlow.toList(flowItems)
}

// WHEN
viewModel.troubleScanningPressed()

// THEN
assertThat(flowItems.size).isEqualTo(1)
assertThat(flowItems[0] is SideEffect.ScanTrouble).isTrue()
job.cancel()
}
}

0 comments on commit 1912d8c

Please sign in to comment.