From 4cbdc3f598a68a226a5c896ab7e89a35dbd95133 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Wed, 8 Jan 2025 20:43:51 +0000 Subject: [PATCH] fix(ReduceBrightColorsPreferenceController) Different feature configs This change updates the tests for ReduceBrightColorsPreferenceController to account for different combinations of feature flags and configurations, specifically related to the "Even Dimmer" flag and the availability of Reduce Bright Colors. The updated tests ensure that the preference controller correctly determines its availability based on these factors, improving test coverage and reliability. This addresses inconsistencies between the code and test cases, ensuring accurate behavior across various device configurations. Bug: 387071233 Test: atest ReduceBrightColorsPreferenceControllerTest Flag: com.android.server.display.feature.flags.even_dimmer Change-Id: Ie565ce996c7d4f49e194b119b32bf01a6508393e --- ...eBrightColorsPreferenceControllerTest.java | 119 +++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) diff --git a/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java b/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java index e1c0277c7e9..9cc118f6bc2 100644 --- a/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java +++ b/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java @@ -24,20 +24,30 @@ import static org.mockito.Mockito.when; import android.content.Context; import android.content.res.Resources; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.annotations.EnableFlags; +import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.android.internal.R; +import com.android.server.display.feature.flags.Flags; import org.junit.Before; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +/** Tests for {@link ReduceBrightColorsPreferenceController} */ @RunWith(AndroidJUnit4.class) public class ReduceBrightColorsPreferenceControllerTest { + + @Rule + public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); + private static final String PREF_KEY = "rbc_preference"; private Context mContext; @@ -76,18 +86,123 @@ public class ReduceBrightColorsPreferenceControllerTest { } @Test - public void isAvailable_configuredRbcAvailable_shouldReturnTrue() { + @DisableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOffAndDisabled_RbcOnAndAvailable_returnTrue() { + doReturn(false).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1); doReturn(true).when(mResources).getBoolean( R.bool.config_reduceBrightColorsAvailable); + assertThat(mController.isAvailable()).isTrue(); } + @Test - public void isAvailable_configuredRbcUnAvailable_shouldReturnFalse() { + @DisableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOffAndDisabled_RbcOffAndAvailable_returnTrue() { + doReturn(false).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 0); + doReturn(true).when(mResources).getBoolean( + R.bool.config_reduceBrightColorsAvailable); + + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + @DisableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOffAndDisabled_RbcOnAndUnavailable_returnFalse() { + doReturn(false).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1); doReturn(false).when(mResources).getBoolean( R.bool.config_reduceBrightColorsAvailable); + assertThat(mController.isAvailable()).isFalse(); } + @Test + @EnableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOnAndDisabled_RbcOnAndAvailable_returnTrue() { + doReturn(false).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1); + doReturn(true).when(mResources).getBoolean( + R.bool.config_reduceBrightColorsAvailable); + + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOnAndDisabled_RbcOffAndAvailable_returnTrue() { + doReturn(false).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 0); + doReturn(true).when(mResources).getBoolean( + R.bool.config_reduceBrightColorsAvailable); + + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOnAndDisabled_RbcOnAndUnavailable_returnFalse() { + doReturn(false).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1); + doReturn(false).when(mResources).getBoolean( + R.bool.config_reduceBrightColorsAvailable); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOnAndEnabled_RbcOnAndAvailable_returnFalse() { + doReturn(true).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1); + doReturn(true).when(mResources).getBoolean( + R.bool.config_reduceBrightColorsAvailable); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOnAndEnabled_RbcOffAndAvailable_returnFalse() { + doReturn(true).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 0); + doReturn(true).when(mResources).getBoolean( + R.bool.config_reduceBrightColorsAvailable); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_EVEN_DIMMER) + public void isAvailable_whenEvenDimmerOnAndEnabled_RbcOnAndUnavailable_returnFalse() { + doReturn(true).when(mResources).getBoolean( + com.android.internal.R.bool.config_evenDimmerEnabled); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1); + doReturn(false).when(mResources).getBoolean( + R.bool.config_reduceBrightColorsAvailable); + + assertThat(mController.isAvailable()).isFalse(); + } + + private int resourceId(String type, String name) { return mContext.getResources().getIdentifier(name, type, mContext.getPackageName()); }