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.settings.accessibility; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.ArgumentMatchers.any; 25 import static org.mockito.ArgumentMatchers.eq; 26 import static org.mockito.Mockito.never; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.content.ContentResolver; 32 import android.content.Context; 33 import android.platform.test.annotations.DisableFlags; 34 import android.platform.test.annotations.EnableFlags; 35 import android.platform.test.flag.junit.SetFlagsRule; 36 import android.provider.Settings; 37 38 import androidx.preference.PreferenceScreen; 39 import androidx.test.core.app.ApplicationProvider; 40 41 import com.android.server.accessibility.Flags; 42 import com.android.settings.widget.SeekBarPreference; 43 44 import org.junit.After; 45 import org.junit.Before; 46 import org.junit.Rule; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.mockito.Mock; 50 import org.mockito.MockitoAnnotations; 51 import org.robolectric.RobolectricTestRunner; 52 53 /** Tests for {@link DaltonizerSaturationSeekbarPreferenceController}. */ 54 @RunWith(RobolectricTestRunner.class) 55 public class DaltonizerSaturationSeekbarPreferenceControllerTest { 56 57 private ContentResolver mContentResolver; 58 private DaltonizerSaturationSeekbarPreferenceController mController; 59 60 private int mOriginalSaturationLevel = -1; 61 62 private PreferenceScreen mScreen; 63 64 @Mock 65 private SeekBarPreference mPreference; 66 67 @Rule 68 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 69 70 @Before setup()71 public void setup() { 72 MockitoAnnotations.initMocks(this); 73 Context context = ApplicationProvider.getApplicationContext(); 74 mContentResolver = context.getContentResolver(); 75 mOriginalSaturationLevel = Settings.Secure.getInt( 76 mContentResolver, 77 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL, 78 7); 79 80 mScreen = spy(new PreferenceScreen(context, /* attrs= */ null)); 81 when(mScreen.findPreference(ToggleDaltonizerPreferenceFragment.KEY_SATURATION)) 82 .thenReturn(mPreference); 83 84 mController = new DaltonizerSaturationSeekbarPreferenceController( 85 context, 86 ToggleDaltonizerPreferenceFragment.KEY_SATURATION); 87 } 88 89 @After cleanup()90 public void cleanup() { 91 Settings.Secure.putInt( 92 mContentResolver, 93 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL, 94 mOriginalSaturationLevel); 95 } 96 97 98 @Test 99 @DisableFlags(Flags.FLAG_ENABLE_COLOR_CORRECTION_SATURATION) getAvailabilityStatus_flagDisabled_unavailable()100 public void getAvailabilityStatus_flagDisabled_unavailable() { 101 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 102 } 103 104 @Test 105 @EnableFlags(Flags.FLAG_ENABLE_COLOR_CORRECTION_SATURATION) getAvailabilityStatus_flagEnabled_available()106 public void getAvailabilityStatus_flagEnabled_available() { 107 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 108 } 109 110 @Test constructor_defaultValuesMatch()111 public void constructor_defaultValuesMatch() { 112 assertThat(mController.getSliderPosition()).isEqualTo(7); 113 assertThat(mController.getMax()).isEqualTo(10); 114 assertThat(mController.getMin()).isEqualTo(0); 115 } 116 117 @Test 118 @EnableFlags(Flags.FLAG_ENABLE_COLOR_CORRECTION_SATURATION) displayPreference_enabled_visible()119 public void displayPreference_enabled_visible() { 120 mController.displayPreference(mScreen); 121 122 verify(mPreference).setMax(eq(10)); 123 verify(mPreference).setMin(eq(0)); 124 verify(mPreference).setProgress(eq(7)); 125 verify(mPreference).setContinuousUpdates(eq(true)); 126 verify(mPreference).setOnPreferenceChangeListener(eq(mController)); 127 verify(mPreference).setVisible(eq(true)); 128 } 129 130 @Test 131 @DisableFlags(Flags.FLAG_ENABLE_COLOR_CORRECTION_SATURATION) displayPreference_disabled_notVisible()132 public void displayPreference_disabled_notVisible() { 133 mController.displayPreference(mScreen); 134 135 verify(mPreference).setMax(eq(10)); 136 verify(mPreference).setMin(eq(0)); 137 verify(mPreference).setProgress(eq(7)); 138 verify(mPreference).setContinuousUpdates(eq(true)); 139 verify(mPreference, never()).setOnPreferenceChangeListener(any()); 140 verify(mPreference).setVisible(eq(false)); 141 } 142 143 @Test setSliderPosition_inRange_secureSettingsUpdated()144 public void setSliderPosition_inRange_secureSettingsUpdated() { 145 var isSliderSet = mController.setSliderPosition(9); 146 147 assertThat(isSliderSet).isTrue(); 148 assertThat(Settings.Secure.getInt( 149 mContentResolver, 150 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL, 151 7)).isEqualTo(9); 152 } 153 154 @Test setSliderPosition_min_secureSettingsUpdated()155 public void setSliderPosition_min_secureSettingsUpdated() { 156 var isSliderSet = mController.setSliderPosition(0); 157 158 assertThat(isSliderSet).isTrue(); 159 assertThat(Settings.Secure.getInt( 160 mContentResolver, 161 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL, 162 7)).isEqualTo(0); 163 } 164 165 @Test setSliderPosition_max_secureSettingsUpdated()166 public void setSliderPosition_max_secureSettingsUpdated() { 167 var isSliderSet = mController.setSliderPosition(10); 168 169 assertThat(isSliderSet).isTrue(); 170 assertThat(Settings.Secure.getInt( 171 mContentResolver, 172 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL, 173 7)).isEqualTo(10); 174 } 175 176 @Test setSliderPosition_tooLarge_secureSettingsNotUpdated()177 public void setSliderPosition_tooLarge_secureSettingsNotUpdated() { 178 var isSliderSet = mController.setSliderPosition(11); 179 180 assertThat(isSliderSet).isFalse(); 181 assertThat(Settings.Secure.getInt( 182 mContentResolver, 183 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL, 184 7)).isEqualTo(7); 185 } 186 187 @Test setSliderPosition_tooSmall_secureSettingsNotUpdated()188 public void setSliderPosition_tooSmall_secureSettingsNotUpdated() { 189 var isSliderSet = mController.setSliderPosition(-1); 190 191 assertThat(isSliderSet).isFalse(); 192 assertThat(Settings.Secure.getInt( 193 mContentResolver, 194 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL, 195 7)).isEqualTo(7); 196 } 197 } 198