Yokan / rebuild
Course 1 8624561

AppIconViewModelTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/settings/appearance/AppIconViewModelTest.kt

test / fixture143 lines
1package eu.friedcutlet.yokan.ui.screens.settings.appearance23import androidx.lifecycle.ViewModelProvider4import androidx.lifecycle.ViewModelStore5import androidx.lifecycle.viewmodel.initializer6import androidx.lifecycle.viewmodel.viewModelFactory7import eu.friedcutlet.yokan.services.appicons.*8import kotlinx.coroutines.CompletableDeferred9import kotlinx.coroutines.Dispatchers10import kotlinx.coroutines.ExperimentalCoroutinesApi11import kotlinx.coroutines.test.StandardTestDispatcher12import kotlinx.coroutines.test.advanceUntilIdle13import kotlinx.coroutines.test.resetMain14import kotlinx.coroutines.test.runCurrent15import kotlinx.coroutines.test.runTest16import kotlinx.coroutines.test.setMain17import kotlin.test.Test18import kotlin.test.assertEquals19import kotlin.test.assertFalse20import kotlin.test.assertNull21import kotlin.test.assertTrue2223@OptIn(ExperimentalCoroutinesApi::class)24class AppIconViewModelTest {25    @Test fun draftCancelApplyResetAndRestartReadInstallationState() = runTest {26        Dispatchers.setMain(StandardTestDispatcher(testScheduler))27        val stores = mutableListOf<ViewModelStore>()28        try {29            val service = FakeIcons()30            val vm = model(service, stores)31            advanceUntilIdle()32            assertEquals(AppIconSelection.Default, vm.state.value.selection)33            vm.openPicker(); vm.chooseDesign(AppIconDesign.Torii); vm.chooseAppearance(ExplicitAppIcons[2])34            assertEquals(ExplicitAppIcons[2], vm.state.value.draft)35            vm.dismissPicker()36            assertEquals(AppIconSelection.Default, service.icon)37            assertEquals(0, service.writes)38            vm.openPicker()39            assertEquals(AppIconSelection.Default, vm.state.value.draft)40            vm.chooseDesign(AppIconDesign.Torii); vm.chooseAppearance(ExplicitAppIcons[2]); vm.applyDraft()41            advanceUntilIdle()42            assertEquals(ExplicitAppIcons[2], service.icon)43            assertFalse(vm.state.value.pickerOpen)44            val restarted = model(service, stores)45            advanceUntilIdle()46            assertEquals(ExplicitAppIcons[2], restarted.state.value.selection)47            restarted.openPicker(); restarted.chooseDesign(AppIconDesign.Default)48            assertEquals(AppIconSelection.Default, restarted.state.value.draft)49            restarted.select(AppIconSelection.Default); advanceUntilIdle()50            assertEquals(AppIconSelection.Default, service.icon)51            assertFalse(restarted.state.value.failed)52        } finally { stores.forEach { it.clear() }; advanceUntilIdle(); Dispatchers.resetMain() }53    }5455    @Test fun failedOrPartialOsChangeIsReadBackAndRetryRepairsUnknownState() = runTest {56        Dispatchers.setMain(StandardTestDispatcher(testScheduler))57        val stores = mutableListOf<ViewModelStore>()58        try {59            val service = FakeIcons()60            val vm = model(service, stores); advanceUntilIdle()61            vm.openPicker()62            service.reject = true63            vm.select(ExplicitAppIcons[2]); advanceUntilIdle()64            assertTrue(vm.state.value.failed)65            assertTrue(vm.state.value.pickerOpen)66            assertEquals(AppIconSelection.Default, vm.state.value.selection)67            // An older Android launcher switch may partially apply before a failure.68            service.icon = null69            vm.refresh(); advanceUntilIdle()70            assertNull(vm.state.value.selection)71            assertTrue(vm.state.value.failed)72            service.reject = false73            vm.select(AppIconSelection.Default); advanceUntilIdle()74            assertFalse(vm.state.value.failed)75            assertEquals(AppIconSelection.Default, vm.state.value.selection)76            service.ignoreChange = true77            vm.select(ExplicitAppIcons[1]); advanceUntilIdle()78            assertTrue(vm.state.value.failed) // Never claim success when the OS kept the previous icon.79            assertEquals(AppIconSelection.Default, vm.state.value.selection)80        } finally { stores.forEach { it.clear() }; advanceUntilIdle(); Dispatchers.resetMain() }81    }8283    @Test fun unsupportedChoicesAreRejectedAndSystemAppearanceIsCapabilityGated() = runTest {84        Dispatchers.setMain(StandardTestDispatcher(testScheduler))85        val stores = mutableListOf<ViewModelStore>()86        try {87            val android = FakeIcons()88            val vm = model(android, stores); advanceUntilIdle()89            vm.select(SystemAppIcon); advanceUntilIdle()90            assertEquals(0, android.writes)91            vm.openPicker(); vm.chooseDesign(AppIconDesign.Torii); vm.chooseAppearance(SystemAppIcon)92            assertEquals(ExplicitAppIcons[1], vm.state.value.draft)93            val ios = FakeIcons(AppIconCapabilities(AppIconPlatform.Ios, ExplicitAppIcons + SystemAppIcon))94            val iosVm = model(ios, stores); advanceUntilIdle()95            iosVm.openPicker(); iosVm.chooseDesign(AppIconDesign.Torii); iosVm.chooseAppearance(SystemAppIcon); iosVm.applyDraft()96            advanceUntilIdle(); assertEquals(SystemAppIcon, iosVm.state.value.selection)97            val static = model(StaticAppIconService(), stores); advanceUntilIdle()98            assertFalse(static.state.value.capabilities.canChange)99            static.select(ExplicitAppIcons[1]); advanceUntilIdle()100            assertEquals(AppIconSelection.Default, static.state.value.selection)101        } finally { stores.forEach { it.clear() }; advanceUntilIdle(); Dispatchers.resetMain() }102    }103104    @Test fun changesSerializeAndFinishWhenSettingsRouteCloses() = runTest {105        Dispatchers.setMain(StandardTestDispatcher(testScheduler))106        val stores = mutableListOf<ViewModelStore>()107        try {108            val service = FakeIcons()109            val vm = model(service, stores); advanceUntilIdle()110            service.pending = CompletableDeferred()111            vm.openPicker(); vm.select(ExplicitAppIcons[2]); runCurrent()112            assertTrue(vm.state.value.busy)113            assertEquals(AppIconSelection.Default, vm.state.value.selection)114            vm.select(ExplicitAppIcons[1]); vm.refresh()115            stores.single().clear()116            service.pending!!.complete(Unit); advanceUntilIdle()117            assertEquals(1, service.writes)118            assertEquals(ExplicitAppIcons[2], service.icon)119            val next = model(service, stores); advanceUntilIdle()120            assertEquals(ExplicitAppIcons[2], next.state.value.selection)121            assertFalse(next.state.value.busy)122        } finally { stores.forEach { it.clear() }; advanceUntilIdle(); Dispatchers.resetMain() }123    }124125    private fun model(service: AppIconService, stores: MutableList<ViewModelStore>): AppIconViewModel {126        val store = ViewModelStore().also(stores::add)127        return ViewModelProvider.create(store, viewModelFactory { initializer { AppIconViewModel(service) } })[AppIconViewModel::class]128    }129    private class FakeIcons(override val capabilities: AppIconCapabilities = AppIconCapabilities(AppIconPlatform.Android, ExplicitAppIcons)) : AppIconService {130        var icon: AppIconSelection? = AppIconSelection.Default131        var writes = 0132        var reject = false133        var ignoreChange = false134        var pending: CompletableDeferred<Unit>? = null135        override suspend fun current() = icon136        override suspend fun apply(selection: AppIconSelection) {137            writes++138            pending?.await()139            if (reject) error("OS rejected change")140            if (!ignoreChange) icon = selection141        }142    }143}144

Search chapters, reference and source filenames.