1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.customization.model.picker.settings.data.repository
18 
19 import android.app.UiModeManager.ContrastUtils
20 import androidx.test.filters.SmallTest
21 import com.android.customization.picker.settings.data.repository.ColorContrastSectionRepository
22 import com.android.wallpaper.testing.FakeUiModeManager
23 import com.google.common.truth.Truth.assertThat
24 import dagger.hilt.android.testing.HiltAndroidRule
25 import dagger.hilt.android.testing.HiltAndroidTest
26 import javax.inject.Inject
27 import kotlinx.coroutines.ExperimentalCoroutinesApi
28 import kotlinx.coroutines.flow.toList
29 import kotlinx.coroutines.launch
30 import kotlinx.coroutines.test.TestScope
31 import kotlinx.coroutines.test.UnconfinedTestDispatcher
32 import kotlinx.coroutines.test.runTest
33 import org.junit.Before
34 import org.junit.Rule
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.robolectric.RobolectricTestRunner
38 
39 @HiltAndroidTest
40 @SmallTest
41 @RunWith(RobolectricTestRunner::class)
42 @OptIn(ExperimentalCoroutinesApi::class)
43 class ColorContrastSectionRepositoryTest {
44     @get:Rule var hiltRule = HiltAndroidRule(this)
45 
46     @Inject lateinit var uiModeManager: FakeUiModeManager
47     @Inject lateinit var underTest: ColorContrastSectionRepository
48     @Inject lateinit var testScope: TestScope
49 
50     @Before
setUpnull51     fun setUp() {
52         hiltRule.inject()
53     }
54 
55     @Test
creationSucceedsnull56     fun creationSucceeds() {
57         assertThat(underTest).isNotNull()
58     }
59 
60     @OptIn(ExperimentalCoroutinesApi::class)
61     @Test
contrastFlowEmitsValuesnull62     fun contrastFlowEmitsValues() =
63         testScope.runTest {
64             val nextContrastValues = listOf(0.5f, 0.7f, 0.8f)
65             val expectedContrastValues =
66                 nextContrastValues.map { ContrastUtils.toContrastLevel(it) }
67             // Set up a flow to collect all contrast values
68             val flowCollector = mutableListOf<Int>()
69             // Start collecting values from the flow, using an unconfined dispatcher to start
70             // collecting from the flow right away (rather than explicitly calling `runCurrent`)
71             // See https://developer.android.com/kotlin/flow/test#continuous-collection
72             backgroundScope.launch(UnconfinedTestDispatcher()) {
73                 underTest.contrast.toList(flowCollector)
74             }
75 
76             nextContrastValues.forEach { uiModeManager.setContrast(it) }
77 
78             // Ignore the first contrast value from constructing the repository
79             val collectedValues = flowCollector.drop(1)
80             assertThat(collectedValues).containsExactlyElementsIn(expectedContrastValues)
81         }
82 }
83