Added test cases to ClockPickerViewModelTest

Test: Unit test passes
Bug: 395647577
Flag: com.android.systemui.shared.new_customization_picker_ui
Change-Id: I47276ee84ad184544c7375fe2b08db120cff3aec
diff --git a/tests/robotests/src/com/android/customization/picker/clock/data/repository/FakeClockPickerRepository.kt b/tests/robotests/src/com/android/customization/picker/clock/data/repository/FakeClockPickerRepository.kt
index 3055f1f..7728218 100644
--- a/tests/robotests/src/com/android/customization/picker/clock/data/repository/FakeClockPickerRepository.kt
+++ b/tests/robotests/src/com/android/customization/picker/clock/data/repository/FakeClockPickerRepository.kt
@@ -32,7 +32,6 @@
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.asStateFlow
 import kotlinx.coroutines.flow.combine
-import kotlinx.coroutines.flow.update
 
 /** By default [FakeClockPickerRepository] uses [fakeClocks]. */
 open class FakeClockPickerRepository(clocks: List<ClockMetadataModel> = fakeClocks) :
@@ -44,15 +43,16 @@
     private val colorTone: MutableStateFlow<Int> =
         MutableStateFlow(ClockMetadataModel.DEFAULT_COLOR_TONE_PROGRESS)
     @ColorInt private val seedColor: MutableStateFlow<Int?> = MutableStateFlow(null)
-    private val axisPresetConfig: MutableStateFlow<AxisPresetConfig?> = MutableStateFlow(null)
+    private val clockListFlow: MutableStateFlow<List<ClockMetadataModel>> =
+        MutableStateFlow(fakeClocks)
     override val selectedClock: Flow<ClockMetadataModel> =
-        combine(selectedClockId, selectedColorId, colorTone, seedColor, axisPresetConfig) {
+        combine(clockListFlow, selectedClockId, selectedColorId, colorTone, seedColor) {
+            clockList,
             selectedClockId,
             selectedColor,
             colorTone,
-            seedColor,
-            axisPresetConfig ->
-            val selectedClock = fakeClocks.find { clock -> clock.clockId == selectedClockId }
+            seedColor ->
+            val selectedClock = clockList.find { clock -> clock.clockId == selectedClockId }
             checkNotNull(selectedClock)
             ClockMetadataModel(
                 clockId = selectedClock.clockId,
@@ -60,7 +60,7 @@
                 description = "description",
                 thumbnail = ColorDrawable(0),
                 isReactiveToTone = selectedClock.isReactiveToTone,
-                axisPresetConfig = axisPresetConfig,
+                axisPresetConfig = selectedClock.axisPresetConfig,
                 selectedColorId = selectedColor,
                 colorToneProgress = colorTone,
                 seedColor = seedColor,
@@ -89,29 +89,30 @@
     }
 
     override suspend fun setClockAxisStyle(axisStyle: ClockAxisStyle) {
-        val config = axisPresetConfig.value
-        if (config == null) {
-            axisPresetConfig.value =
-                AxisPresetConfig(
-                    groups =
-                        listOf(
-                            Group(presets = listOf(axisStyle), icon = ColorDrawable(Color.YELLOW))
-                        ),
-                    current = AxisPresetConfig.IndexedStyle(0, 0, axisStyle),
-                )
-        } else {
-            axisPresetConfig.update { axisPresetConfig ->
-                axisPresetConfig?.let { it.copy(current = it.findStyle(axisStyle)) }
+        val clockList = clockListFlow.value
+        val newClockList = clockList.toMutableList()
+        for ((index, clock) in clockList.withIndex()) {
+            val presetConfig = clock.axisPresetConfig
+            val style = presetConfig?.findStyle(axisStyle)
+            if (presetConfig != null && style != null) {
+                newClockList[index] =
+                    clock.copy(axisPresetConfig = presetConfig.copy(current = style))
             }
         }
+
+        clockListFlow.value = newClockList.toList()
     }
 
     override fun isReactiveToTone(clockId: ClockId): Boolean? = true
 
     companion object {
-        fun buildFakeAxis(i: Int): ClockFontAxis {
+        fun buildFakeClockAxisStyle(i: Int): ClockAxisStyle {
+            return ClockAxisStyle(listOf(buildFakeAxis(i)))
+        }
+
+        private fun buildFakeAxis(i: Int): ClockFontAxis {
             return ClockFontAxis(
-                key = "key",
+                key = "key#$i",
                 type = AxisType.Float,
                 maxValue = 0f,
                 minValue = 1000f,
@@ -121,10 +122,36 @@
             )
         }
 
+        private val fakeClockAxisStyle0 = ClockAxisStyle(listOf(buildFakeAxis(0)))
+        val fakeClockAxisStyle1 = ClockAxisStyle(listOf(buildFakeAxis(1)))
+        private val fakeClockAxisStyle2 = ClockAxisStyle(listOf(buildFakeAxis(2)))
+        private val fakeClockAxisStyle3 = ClockAxisStyle(listOf(buildFakeAxis(3)))
+        private val fakeAxisPresetConfig: AxisPresetConfig =
+            AxisPresetConfig(
+                groups =
+                    listOf(
+                        AxisPresetConfig.Group(
+                            presets = listOf(fakeClockAxisStyle0, fakeClockAxisStyle1),
+                            icon = ColorDrawable(Color.BLUE),
+                        ),
+                        AxisPresetConfig.Group(
+                            presets = listOf(fakeClockAxisStyle2, fakeClockAxisStyle3),
+                            icon = ColorDrawable(Color.YELLOW),
+                        ),
+                    ),
+                current =
+                    AxisPresetConfig.IndexedStyle(
+                        groupIndex = 0,
+                        presetIndex = 0,
+                        style = fakeClockAxisStyle0,
+                    ),
+            )
+
         const val CLOCK_ID_0 = "clock0"
         const val CLOCK_ID_1 = "clock1"
         const val CLOCK_ID_2 = "clock2"
         const val CLOCK_ID_3 = "clock3"
+
         val fakeClocks =
             listOf(
                 ClockMetadataModel(
@@ -133,7 +160,7 @@
                     "description0",
                     ColorDrawable(0),
                     true,
-                    null,
+                    fakeAxisPresetConfig,
                     null,
                     50,
                     null,
diff --git a/tests/robotests/src/com/android/customization/picker/clock/domain/interactor/ClockPickerInteractorTest.kt b/tests/robotests/src/com/android/customization/picker/clock/domain/interactor/ClockPickerInteractorTest.kt
index 484d810..67c2e46 100644
--- a/tests/robotests/src/com/android/customization/picker/clock/domain/interactor/ClockPickerInteractorTest.kt
+++ b/tests/robotests/src/com/android/customization/picker/clock/domain/interactor/ClockPickerInteractorTest.kt
@@ -3,7 +3,6 @@
 import androidx.test.filters.SmallTest
 import com.android.customization.picker.clock.data.repository.FakeClockPickerRepository
 import com.android.customization.picker.clock.shared.ClockSize
-import com.android.systemui.plugins.clocks.ClockAxisStyle
 import com.android.systemui.shared.customization.data.content.FakeCustomizationProviderClient
 import com.android.wallpaper.picker.customization.data.repository.CustomizationRuntimeValuesRepository
 import com.android.wallpaper.testing.FakeSnapshotStore
@@ -89,7 +88,7 @@
     @Test
     fun setFontAxisSettings() = runTest {
         val axisSettings = collectLastValue(underTest.axisSettings)
-        val fakeSettings = ClockAxisStyle(listOf(FakeClockPickerRepository.buildFakeAxis(10)))
+        val fakeSettings = FakeClockPickerRepository.fakeClockAxisStyle1
 
         underTest.setClockFontAxes(fakeSettings)
 
diff --git a/tests/robotests/src/com/android/wallpaper/customization/ui/viewmodel/ClockPickerViewModelTest.kt b/tests/robotests/src/com/android/wallpaper/customization/ui/viewmodel/ClockPickerViewModelTest.kt
index 315f071..2b71342 100644
--- a/tests/robotests/src/com/android/wallpaper/customization/ui/viewmodel/ClockPickerViewModelTest.kt
+++ b/tests/robotests/src/com/android/wallpaper/customization/ui/viewmodel/ClockPickerViewModelTest.kt
@@ -28,6 +28,7 @@
 import com.android.customization.picker.clock.ui.viewmodel.ClockSettingsViewModel
 import com.android.customization.picker.color.data.repository.FakeColorPickerRepository2
 import com.android.customization.picker.color.domain.interactor.ColorPickerInteractor2
+import com.android.systemui.plugins.clocks.AxisPresetConfig.IndexedStyle
 import com.android.systemui.shared.customization.data.content.FakeCustomizationProviderClient
 import com.android.themepicker.R
 import com.android.wallpaper.customization.ui.viewmodel.ClockPickerViewModel.Tab
@@ -114,26 +115,7 @@
         Dispatchers.resetMain()
     }
 
-    @Test
-    fun selectedTab_whenClickOnTabStyle() = runTest {
-        val tabs = collectLastValue(underTest.tabs)
-        val selectedTab = collectLastValue(underTest.selectedTab)
-
-        tabs()?.get(0)?.onClick?.invoke()
-
-        assertThat(selectedTab()).isEqualTo(Tab.STYLE)
-    }
-
-    @Test
-    fun selectedTab_whenClickOnTabColor() = runTest {
-        val tabs = collectLastValue(underTest.tabs)
-        val selectedTab = collectLastValue(underTest.selectedTab)
-
-        tabs()?.get(1)?.onClick?.invoke()
-
-        assertThat(selectedTab()).isEqualTo(Tab.COLOR)
-    }
-
+    //// Tabs
     @Test
     fun tabs_whenInitialState() = runTest {
         val tabs = collectLastValue(underTest.tabs)
@@ -204,6 +186,37 @@
     }
 
     @Test
+    fun selectedTab_whenClickOnTabStyle() = runTest {
+        val tabs = collectLastValue(underTest.tabs)
+        val selectedTab = collectLastValue(underTest.selectedTab)
+
+        tabs()?.get(0)?.onClick?.invoke()
+
+        assertThat(selectedTab()).isEqualTo(Tab.STYLE)
+    }
+
+    @Test
+    fun selectedTab_whenClickOnTabColor() = runTest {
+        val tabs = collectLastValue(underTest.tabs)
+        val selectedTab = collectLastValue(underTest.selectedTab)
+
+        tabs()?.get(1)?.onClick?.invoke()
+
+        assertThat(selectedTab()).isEqualTo(Tab.COLOR)
+    }
+
+    @Test
+    fun selectedTab_whenClickOnTabSize() = runTest {
+        val tabs = collectLastValue(underTest.tabs)
+        val selectedTab = collectLastValue(underTest.selectedTab)
+
+        tabs()?.get(2)?.onClick?.invoke()
+
+        assertThat(selectedTab()).isEqualTo(Tab.SIZE)
+    }
+
+    //// Clock style
+    @Test
     fun selectedClock_whenClickOnStyleOptions() = runTest {
         val selectedClock = collectLastValue(underTest.selectedClock)
 
@@ -249,6 +262,94 @@
         assertThat(option1OnClicked()).isNull()
     }
 
+    //// Clock font
+    @Test
+    fun previewingClockPresetIndexedStyle_whenInitialState() = runTest {
+        val previewingClockPresetIndexedStyle =
+            collectLastValue(underTest.previewingClockPresetIndexedStyle)
+
+        assertThat(previewingClockPresetIndexedStyle())
+            .isEqualTo(FakeClockPickerRepository.fakeClocks[0].axisPresetConfig?.current)
+    }
+
+    @Test
+    fun previewingClockPresetIndexedStyle_whenClickOnSliderStep() = runTest {
+        val previewingClockPresetIndexedStyle =
+            collectLastValue(underTest.previewingClockPresetIndexedStyle)
+        val axisPresetsSliderViewModel = collectLastValue(underTest.axisPresetsSliderViewModel)
+        val onSlierStopTrackingTouch = axisPresetsSliderViewModel()?.onSliderStopTrackingTouch
+        checkNotNull(onSlierStopTrackingTouch)
+
+        onSlierStopTrackingTouch.invoke(1F)
+
+        assertThat(previewingClockPresetIndexedStyle())
+            .isEqualTo(
+                IndexedStyle(
+                    groupIndex = 0,
+                    presetIndex = 1,
+                    style = FakeClockPickerRepository.fakeClockAxisStyle1,
+                )
+            )
+    }
+
+    @Test
+    fun shouldShowPresetSlider_true_whenDefault() = runTest {
+        val shouldShowPresetSlider = collectLastValue(underTest.shouldShowPresetSlider)
+
+        assertThat(shouldShowPresetSlider()).isTrue()
+    }
+
+    @Test
+    fun shouldShowPresetSlider_false_whenSelectClockWithNullAxisPresetConfig() = runTest {
+        val shouldShowPresetSlider = collectLastValue(underTest.shouldShowPresetSlider)
+        val clockStyleOptions = collectLastValue(underTest.clockStyleOptions)
+        // Advance CLOCKS_EVENT_UPDATE_DELAY_MILLIS since there is a delay from clockStyleOptions
+        advanceTimeBy(ClockPickerViewModel.CLOCKS_EVENT_UPDATE_DELAY_MILLIS)
+        val onClockOption1Clicked =
+            clockStyleOptions()?.get(1)?.onClicked?.let { collectLastValue(it) }
+        checkNotNull(onClockOption1Clicked)
+
+        onClockOption1Clicked()?.invoke()
+
+        assertThat(shouldShowPresetSlider()).isFalse()
+    }
+
+    @Test
+    fun axisPresetsSliderViewModel_initialState() = runTest {
+        val axisPresetsSliderViewModel = collectLastValue(underTest.axisPresetsSliderViewModel)
+
+        val expectedPresetSize =
+            FakeClockPickerRepository.fakeClocks[0].axisPresetConfig?.groups?.get(0)?.presets?.size
+        checkNotNull(expectedPresetSize)
+        val resultAxisPresetsSliderViewModel = axisPresetsSliderViewModel()
+        assertThat(resultAxisPresetsSliderViewModel?.valueFrom).isEqualTo(0F)
+        assertThat(resultAxisPresetsSliderViewModel?.valueTo)
+            .isEqualTo((expectedPresetSize - 1).toFloat())
+        assertThat(resultAxisPresetsSliderViewModel?.stepSize).isEqualTo(1F)
+    }
+
+    @Test
+    fun axisPresetsSliderSelectedValue_update_whenClickOnSliderStep() = runTest {
+        val axisPresetsSliderSelectedValue =
+            collectLastValue(underTest.axisPresetsSliderSelectedValue)
+        val axisPresetsSliderViewModel = collectLastValue(underTest.axisPresetsSliderViewModel)
+        val onSlierStopTrackingTouch = axisPresetsSliderViewModel()?.onSliderStopTrackingTouch
+        checkNotNull(onSlierStopTrackingTouch)
+
+        onSlierStopTrackingTouch.invoke(1F)
+
+        val expectedResult =
+            FakeClockPickerRepository.fakeClocks[0]
+                .axisPresetConfig
+                ?.groups
+                ?.get(0)
+                ?.presets
+                ?.get(1)
+        checkNotNull(expectedResult)
+        assertThat(axisPresetsSliderSelectedValue()).isEqualTo(1F)
+    }
+
+    //// Clock size
     @Test
     fun previewingClockSize_whenCallingOnClockSizeSwitchChecked() = runTest {
         val previewingClockSize = collectLastValue(underTest.previewingClockSize)
@@ -262,6 +363,7 @@
         assertThat(previewingClockSize()).isEqualTo(ClockSize.SMALL)
     }
 
+    //// Clock color
     @Test
     fun sliderProgress_whenOnSliderProgressChanged() = runTest {
         val sliderProgress = collectLastValue(underTest.previewingSliderProgress)