From a9c2dc8731122c9503a447081b1df727f2bf01a7 Mon Sep 17 00:00:00 2001 From: Mike Scamell Date: Thu, 12 Sep 2024 15:11:32 +0200 Subject: [PATCH] WIP --- .../ShowOnAppLaunchActivity.kt | 2 + .../ShowOnAppLaunchViewModelTest.kt | 128 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 app/src/test/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchViewModelTest.kt diff --git a/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchActivity.kt b/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchActivity.kt index d666c257c284..25ef376a386f 100644 --- a/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchActivity.kt +++ b/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchActivity.kt @@ -32,6 +32,8 @@ import com.duckduckgo.common.ui.viewbinding.viewBinding import com.duckduckgo.di.scopes.ActivityScope import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import timber.log.Timber +import timber.log.Timber.Forest @InjectWith(ActivityScope::class) @ContributeToActivityStarter(ShowOnAppLaunchScreenNoParams::class) diff --git a/app/src/test/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchViewModelTest.kt b/app/src/test/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchViewModelTest.kt new file mode 100644 index 000000000000..e1ac0ec224da --- /dev/null +++ b/app/src/test/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchViewModelTest.kt @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2024 DuckDuckGo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.duckduckgo.app.generalsettings.showonapplaunch + +import app.cash.turbine.test +import com.duckduckgo.app.generalsettings.showonapplaunch.model.ShowOnAppLaunchOption.LastOpenedTab +import com.duckduckgo.app.generalsettings.showonapplaunch.model.ShowOnAppLaunchOption.NewTabPage +import com.duckduckgo.app.generalsettings.showonapplaunch.store.FakeShowOnAppLaunchOptionDataStore +import com.duckduckgo.common.test.CoroutineTestRule +import com.duckduckgo.common.utils.DispatcherProvider +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +class ShowOnAppLaunchViewModelTest { + + @get:Rule + val coroutineTestRule = CoroutineTestRule() + + private lateinit var testee: ShowOnAppLaunchViewModel + private lateinit var fakeDataStore: FakeShowOnAppLaunchOptionDataStore + private val dispatcherProvider: DispatcherProvider = coroutineTestRule.testDispatcherProvider + + @Before + fun setup() { + fakeDataStore = FakeShowOnAppLaunchOptionDataStore() + testee = ShowOnAppLaunchViewModel(dispatcherProvider, fakeDataStore, FakeUrlConverter()) + } + + @Test + fun whenViewModelInitializedThenInitialStateIsCorrect() = runTest { + fakeDataStore.setShowOnAppLaunchOption(LastOpenedTab) + + testee.viewState.test { + val initialState = awaitItem() + assertEquals(LastOpenedTab, initialState.selectedOption) + assertEquals("duckduckgo.com", initialState.specificPageUrl) + } + } + + @Test + fun whenShowOnAppLaunchOptionChangedThenStateIsUpdated() = runTest { + testee.onShowOnAppLaunchOptionChanged(NewTabPage) + testee.viewState.test { + val updatedState = awaitItem() + assertEquals(NewTabPage, updatedState.selectedOption) + } + } + + @Test + fun whenSpecificPageUrlSetThenStateIsUpdated() = runTest { + val newUrl = "https://example.com" + + testee.setSpecificPageUrl(newUrl) + testee.viewState.test { + val updatedState = awaitItem() + assertEquals(newUrl, updatedState.specificPageUrl) + } + } + + @Test + fun whenInvalidUrlSetThenStateIsNotUpdated() = runTest { + val invalidUrl = "invalid-url" + + testee.setSpecificPageUrl(invalidUrl) + testee.viewState.test { + val updatedState = awaitItem() + assertEquals("https://duckduckgo.com", updatedState.specificPageUrl) + } + } + + @Test + fun whenNoUrlProvidedThenStateIsNotUpdated() = runTest { + testee.setSpecificPageUrl("") + testee.viewState.test { + val updatedState = awaitItem() + assertEquals("https://duckduckgo.com", updatedState.specificPageUrl) + } + } + + @Test + fun whenMultipleOptionsChangedThenStateIsUpdatedCorrectly() = runTest { + testee.onShowOnAppLaunchOptionChanged(NewTabPage) + testee.onShowOnAppLaunchOptionChanged(LastOpenedTab) + testee.viewState.test { + val updatedState = awaitItem() + assertEquals(LastOpenedTab, updatedState.selectedOption) + } + } + + @Test + fun whenUrlConversionFailsThenStateIsNotUpdated() = runTest { + val failingUrlConverter = object : UrlConverter { + override fun convertUrl(url: String?): String { + throw IllegalArgumentException("Conversion failed") + } + } + testee = ShowOnAppLaunchViewModel(dispatcherProvider, fakeDataStore, failingUrlConverter) + + testee.setSpecificPageUrl("https://example.com") + testee.viewState.test { + val updatedState = awaitItem() + assertEquals("https://duckduckgo.com", updatedState.specificPageUrl) + } + } + + private class FakeUrlConverter : UrlConverter { + override fun convertUrl(url: String?): String { + return url ?: "https://duckduckgo.com" + } + } +}