1 package com.android.settings.display; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import static org.mockito.Mockito.doReturn; 6 import static org.mockito.Mockito.mock; 7 import static org.mockito.Mockito.reset; 8 import static org.mockito.Mockito.spy; 9 import static org.mockito.Mockito.verify; 10 import static org.mockito.Mockito.when; 11 12 import android.app.admin.DevicePolicyManager; 13 import android.content.ContentResolver; 14 import android.content.Context; 15 import android.hardware.display.ColorDisplayManager; 16 import android.provider.Settings; 17 18 import androidx.preference.Preference; 19 import androidx.preference.PreferenceScreen; 20 21 import com.android.settings.testutils.shadow.SettingsShadowResources; 22 23 import org.junit.After; 24 import org.junit.Before; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.mockito.Answers; 28 import org.mockito.Mock; 29 import org.mockito.MockitoAnnotations; 30 import org.robolectric.RobolectricTestRunner; 31 import org.robolectric.RuntimeEnvironment; 32 import org.robolectric.annotation.Config; 33 34 @RunWith(RobolectricTestRunner.class) 35 @Config(shadows = { 36 SettingsShadowResources.class 37 }) 38 public class DisplayWhiteBalancePreferenceControllerTest { 39 40 private DisplayWhiteBalancePreferenceController mController; 41 42 @Mock 43 private ColorDisplayManager mColorDisplayManager; 44 private ContentResolver mContentResolver; 45 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 46 private Context mContext; 47 @Mock 48 private PreferenceScreen mScreen; 49 @Mock 50 private Preference mPreference; 51 52 private final String PREFERENCE_KEY = "display_white_balance"; 53 54 @After tearDown()55 public void tearDown() { 56 SettingsShadowResources.reset(); 57 } 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 doReturn(mock(DevicePolicyManager.class)).when(mContext) 63 .getSystemService(Context.DEVICE_POLICY_SERVICE); 64 65 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 66 when(mContext.getContentResolver()).thenReturn(mContentResolver); 67 when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources()); 68 when(mScreen.findPreference(PREFERENCE_KEY)).thenReturn(mPreference); 69 70 mController = spy(new DisplayWhiteBalancePreferenceController(mContext, PREFERENCE_KEY)); 71 doReturn(mColorDisplayManager).when(mController).getColorDisplayManager(); 72 } 73 74 @Test isAvailable()75 public void isAvailable() { 76 SettingsShadowResources.overrideResource( 77 com.android.internal.R.bool.config_displayWhiteBalanceAvailable, true); 78 79 assertThat(mController.isAvailable()).isTrue(); 80 } 81 82 @Test isAvailable_configuredUnavailable()83 public void isAvailable_configuredUnavailable() { 84 SettingsShadowResources.overrideResource( 85 com.android.internal.R.bool.config_displayWhiteBalanceAvailable, false); 86 87 assertThat(mController.isAvailable()).isFalse(); 88 } 89 90 @Test setChecked_true_setSuccessfully()91 public void setChecked_true_setSuccessfully() { 92 when(mColorDisplayManager.setDisplayWhiteBalanceEnabled(true)).thenReturn(true); 93 assertThat(mController.setChecked(true)).isTrue(); 94 } 95 96 @Test setChecked_false_setSuccessfully()97 public void setChecked_false_setSuccessfully() { 98 when(mColorDisplayManager.setDisplayWhiteBalanceEnabled(false)).thenReturn(true); 99 assertThat(mController.setChecked(false)).isTrue(); 100 } 101 102 @Test isChecked_true()103 public void isChecked_true() { 104 when(mColorDisplayManager.isDisplayWhiteBalanceEnabled()).thenReturn(true); 105 assertThat(mController.isChecked()).isTrue(); 106 } 107 108 @Test isChecked_false()109 public void isChecked_false() { 110 when(mColorDisplayManager.isDisplayWhiteBalanceEnabled()).thenReturn(false); 111 assertThat(mController.isChecked()).isFalse(); 112 } 113 114 @Test onStart_configuredUnavailable()115 public void onStart_configuredUnavailable() { 116 SettingsShadowResources.overrideResource( 117 com.android.internal.R.bool.config_displayWhiteBalanceAvailable, false); 118 mController.displayPreference(mScreen); 119 mController.onStart(); 120 assertThat(mController.mContentObserver).isNull(); 121 } 122 123 @Test onStart_configuredAvailable()124 public void onStart_configuredAvailable() { 125 SettingsShadowResources.overrideResource( 126 com.android.internal.R.bool.config_displayWhiteBalanceAvailable, true); 127 when(mColorDisplayManager.getColorMode()) 128 .thenReturn(ColorDisplayManager.COLOR_MODE_NATURAL); 129 toggleAccessibilityInversion(false); 130 toggleAccessibilityDaltonizer(false); 131 132 mController.displayPreference(mScreen); 133 mController.onStart(); 134 assertThat(mController.mContentObserver).isNotNull(); 135 } 136 137 @Test visibility_configuredAvailableAccessibilityToggled()138 public void visibility_configuredAvailableAccessibilityToggled() { 139 SettingsShadowResources.overrideResource( 140 com.android.internal.R.bool.config_displayWhiteBalanceAvailable, true); 141 mController.displayPreference(mScreen); 142 143 // Accessibility features disabled 144 toggleAccessibilityInversion(false); 145 reset(mPreference); 146 mController.updateVisibility(); 147 verify(mPreference).setVisible(true); 148 149 toggleAccessibilityDaltonizer(false); 150 reset(mPreference); 151 mController.updateVisibility(); 152 verify(mPreference).setVisible(true); 153 154 // Accessibility features enabled one by one 155 toggleAccessibilityInversion(true); 156 mController.updateVisibility(); 157 verify(mPreference).setVisible(false); 158 159 toggleAccessibilityDaltonizer(true); 160 reset(mPreference); 161 mController.updateVisibility(); 162 verify(mPreference).setVisible(false); 163 164 // Accessibility features disabled one by one 165 toggleAccessibilityInversion(false); 166 reset(mPreference); 167 mController.updateVisibility(); 168 // Daltonizer is still enabled, so we expect the preference to still be invisible 169 verify(mPreference).setVisible(false); 170 171 // Now both a11y features are disabled, so we expect the preference to become visible 172 toggleAccessibilityDaltonizer(false); 173 mController.updateVisibility(); 174 verify(mPreference).setVisible(true); 175 } 176 177 @Test visibility_configuredAvailableColorModeChanged()178 public void visibility_configuredAvailableColorModeChanged() { 179 SettingsShadowResources.overrideResource( 180 com.android.internal.R.bool.config_displayWhiteBalanceAvailable, true); 181 mController.displayPreference(mScreen); 182 183 // Non-Saturated color mode selected 184 when(mColorDisplayManager.getColorMode()).thenReturn(ColorDisplayManager.COLOR_MODE_NATURAL); 185 reset(mPreference); 186 mController.updateVisibility(); 187 verify(mPreference).setVisible(true); 188 189 // Saturated color mode selected 190 when(mColorDisplayManager.getColorMode()).thenReturn(ColorDisplayManager.COLOR_MODE_SATURATED); 191 mController.updateVisibility(); 192 verify(mPreference).setVisible(false); 193 194 // Switch back to non-Saturated color mode 195 when(mColorDisplayManager.getColorMode()).thenReturn(ColorDisplayManager.COLOR_MODE_NATURAL); 196 reset(mPreference); 197 mController.updateVisibility(); 198 verify(mPreference).setVisible(true); 199 } 200 toggleAccessibilityInversion(boolean enable)201 private void toggleAccessibilityInversion(boolean enable) { 202 Settings.Secure.putInt(mContentResolver, 203 Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, enable ? 1 : 0); 204 } 205 toggleAccessibilityDaltonizer(boolean enable)206 private void toggleAccessibilityDaltonizer(boolean enable) { 207 Settings.Secure.putInt(mContentResolver, 208 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, enable ? 1 : 0); 209 } 210 } 211