|
| 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.systemui.accessibility.data.repository |
| 18 | + |
| 19 | +import android.hardware.display.ColorDisplayManager |
| 20 | +import android.hardware.display.NightDisplayListener |
| 21 | +import android.os.UserHandle |
| 22 | +import android.provider.Settings |
| 23 | +import android.testing.LeakCheck |
| 24 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 25 | +import androidx.test.filters.SmallTest |
| 26 | +import com.android.systemui.SysuiTestCase |
| 27 | +import com.android.systemui.coroutines.collectLastValue |
| 28 | +import com.android.systemui.dagger.NightDisplayListenerModule |
| 29 | +import com.android.systemui.kosmos.Kosmos |
| 30 | +import com.android.systemui.user.utils.UserScopedService |
| 31 | +import com.android.systemui.util.mockito.argumentCaptor |
| 32 | +import com.android.systemui.util.mockito.eq |
| 33 | +import com.android.systemui.util.mockito.mock |
| 34 | +import com.android.systemui.util.mockito.whenever |
| 35 | +import com.android.systemui.util.settings.fakeGlobalSettings |
| 36 | +import com.android.systemui.util.settings.fakeSettings |
| 37 | +import com.android.systemui.utils.leaks.FakeLocationController |
| 38 | +import com.google.common.truth.Truth.assertThat |
| 39 | +import java.time.LocalTime |
| 40 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 41 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 42 | +import kotlinx.coroutines.test.TestScope |
| 43 | +import kotlinx.coroutines.test.runCurrent |
| 44 | +import kotlinx.coroutines.test.runTest |
| 45 | +import org.junit.Before |
| 46 | +import org.junit.Test |
| 47 | +import org.junit.runner.RunWith |
| 48 | +import org.mockito.ArgumentMatchers |
| 49 | +import org.mockito.Mockito.verify |
| 50 | + |
| 51 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 52 | +@SmallTest |
| 53 | +@RunWith(AndroidJUnit4::class) |
| 54 | +class NightDisplayRepositoryTest : SysuiTestCase() { |
| 55 | + private val kosmos = Kosmos() |
| 56 | + private val testUser = UserHandle.of(1)!! |
| 57 | + private val testStartTime = LocalTime.MIDNIGHT |
| 58 | + private val testEndTime = LocalTime.NOON |
| 59 | + private val colorDisplayManager = |
| 60 | + mock<ColorDisplayManager> { |
| 61 | + whenever(nightDisplayAutoMode).thenReturn(ColorDisplayManager.AUTO_MODE_DISABLED) |
| 62 | + whenever(isNightDisplayActivated).thenReturn(false) |
| 63 | + whenever(nightDisplayCustomStartTime).thenReturn(testStartTime) |
| 64 | + whenever(nightDisplayCustomEndTime).thenReturn(testEndTime) |
| 65 | + } |
| 66 | + private val locationController = FakeLocationController(LeakCheck()) |
| 67 | + private val nightDisplayListener = mock<NightDisplayListener>() |
| 68 | + private val listenerBuilder = |
| 69 | + mock<NightDisplayListenerModule.Builder> { |
| 70 | + whenever(setUser(ArgumentMatchers.anyInt())).thenReturn(this) |
| 71 | + whenever(build()).thenReturn(nightDisplayListener) |
| 72 | + } |
| 73 | + private val globalSettings = kosmos.fakeGlobalSettings |
| 74 | + private val secureSettings = kosmos.fakeSettings |
| 75 | + private val testDispatcher = StandardTestDispatcher() |
| 76 | + private val scope = TestScope(testDispatcher) |
| 77 | + private val userScopedColorDisplayManager = |
| 78 | + mock<UserScopedService<ColorDisplayManager>> { |
| 79 | + whenever(forUser(eq(testUser))).thenReturn(colorDisplayManager) |
| 80 | + } |
| 81 | + |
| 82 | + private val underTest = |
| 83 | + NightDisplayRepository( |
| 84 | + testDispatcher, |
| 85 | + scope.backgroundScope, |
| 86 | + globalSettings, |
| 87 | + secureSettings, |
| 88 | + listenerBuilder, |
| 89 | + userScopedColorDisplayManager, |
| 90 | + locationController, |
| 91 | + ) |
| 92 | + |
| 93 | + @Before |
| 94 | + fun setup() { |
| 95 | + enrollInForcedNightDisplayAutoMode(INITIALLY_FORCE_AUTO_MODE, testUser) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun nightDisplayState_matchesAutoMode() = |
| 100 | + scope.runTest { |
| 101 | + enrollInForcedNightDisplayAutoMode(INITIALLY_FORCE_AUTO_MODE, testUser) |
| 102 | + val callbackCaptor = argumentCaptor<NightDisplayListener.Callback>() |
| 103 | + val lastState by collectLastValue(underTest.nightDisplayState(testUser)) |
| 104 | + |
| 105 | + runCurrent() |
| 106 | + |
| 107 | + verify(nightDisplayListener).setCallback(callbackCaptor.capture()) |
| 108 | + val callback = callbackCaptor.value |
| 109 | + |
| 110 | + assertThat(lastState!!.autoMode).isEqualTo(ColorDisplayManager.AUTO_MODE_DISABLED) |
| 111 | + |
| 112 | + callback.onAutoModeChanged(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME) |
| 113 | + assertThat(lastState!!.autoMode).isEqualTo(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME) |
| 114 | + |
| 115 | + callback.onCustomStartTimeChanged(testStartTime) |
| 116 | + assertThat(lastState!!.startTime).isEqualTo(testStartTime) |
| 117 | + |
| 118 | + callback.onCustomEndTimeChanged(testEndTime) |
| 119 | + assertThat(lastState!!.endTime).isEqualTo(testEndTime) |
| 120 | + |
| 121 | + callback.onAutoModeChanged(ColorDisplayManager.AUTO_MODE_TWILIGHT) |
| 122 | + |
| 123 | + assertThat(lastState!!.autoMode).isEqualTo(ColorDisplayManager.AUTO_MODE_TWILIGHT) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun nightDisplayState_matchesIsNightDisplayActivated() = |
| 128 | + scope.runTest { |
| 129 | + val callbackCaptor = argumentCaptor<NightDisplayListener.Callback>() |
| 130 | + |
| 131 | + val lastState by collectLastValue(underTest.nightDisplayState(testUser)) |
| 132 | + runCurrent() |
| 133 | + |
| 134 | + verify(nightDisplayListener).setCallback(callbackCaptor.capture()) |
| 135 | + val callback = callbackCaptor.value |
| 136 | + assertThat(lastState!!.isActivated) |
| 137 | + .isEqualTo(colorDisplayManager.isNightDisplayActivated) |
| 138 | + |
| 139 | + callback.onActivated(true) |
| 140 | + assertThat(lastState!!.isActivated).isTrue() |
| 141 | + |
| 142 | + callback.onActivated(false) |
| 143 | + assertThat(lastState!!.isActivated).isFalse() |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + fun nightDisplayState_matchesController_initiallyCustomAutoMode() = |
| 148 | + scope.runTest { |
| 149 | + whenever(colorDisplayManager.nightDisplayAutoMode) |
| 150 | + .thenReturn(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME) |
| 151 | + |
| 152 | + val lastState by collectLastValue(underTest.nightDisplayState(testUser)) |
| 153 | + runCurrent() |
| 154 | + |
| 155 | + assertThat(lastState!!.autoMode).isEqualTo(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME) |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + fun nightDisplayState_matchesController_initiallyTwilightAutoMode() = |
| 160 | + scope.runTest { |
| 161 | + whenever(colorDisplayManager.nightDisplayAutoMode) |
| 162 | + .thenReturn(ColorDisplayManager.AUTO_MODE_TWILIGHT) |
| 163 | + |
| 164 | + val lastState by collectLastValue(underTest.nightDisplayState(testUser)) |
| 165 | + runCurrent() |
| 166 | + |
| 167 | + assertThat(lastState!!.autoMode).isEqualTo(ColorDisplayManager.AUTO_MODE_TWILIGHT) |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + fun nightDisplayState_matchesForceAutoMode() = |
| 172 | + scope.runTest { |
| 173 | + enrollInForcedNightDisplayAutoMode(false, testUser) |
| 174 | + val lastState by collectLastValue(underTest.nightDisplayState(testUser)) |
| 175 | + runCurrent() |
| 176 | + |
| 177 | + assertThat(lastState!!.shouldForceAutoMode).isEqualTo(false) |
| 178 | + |
| 179 | + enrollInForcedNightDisplayAutoMode(true, testUser) |
| 180 | + assertThat(lastState!!.shouldForceAutoMode).isEqualTo(true) |
| 181 | + } |
| 182 | + |
| 183 | + private fun enrollInForcedNightDisplayAutoMode(enroll: Boolean, userHandle: UserHandle) { |
| 184 | + globalSettings.putString( |
| 185 | + Settings.Global.NIGHT_DISPLAY_FORCED_AUTO_MODE_AVAILABLE, |
| 186 | + if (enroll) NIGHT_DISPLAY_FORCED_AUTO_MODE_AVAILABLE |
| 187 | + else NIGHT_DISPLAY_FORCED_AUTO_MODE_UNAVAILABLE |
| 188 | + ) |
| 189 | + secureSettings.putIntForUser( |
| 190 | + Settings.Secure.NIGHT_DISPLAY_AUTO_MODE, |
| 191 | + if (enroll) NIGHT_DISPLAY_AUTO_MODE_RAW_NOT_SET else NIGHT_DISPLAY_AUTO_MODE_RAW_SET, |
| 192 | + userHandle.identifier |
| 193 | + ) |
| 194 | + } |
| 195 | + |
| 196 | + private companion object { |
| 197 | + const val INITIALLY_FORCE_AUTO_MODE = false |
| 198 | + const val NIGHT_DISPLAY_FORCED_AUTO_MODE_AVAILABLE = "1" |
| 199 | + const val NIGHT_DISPLAY_FORCED_AUTO_MODE_UNAVAILABLE = "0" |
| 200 | + const val NIGHT_DISPLAY_AUTO_MODE_RAW_NOT_SET = -1 |
| 201 | + const val NIGHT_DISPLAY_AUTO_MODE_RAW_SET = 0 |
| 202 | + } |
| 203 | +} |
0 commit comments