Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4cbdc3f5 authored by Menghan Li's avatar Menghan Li
Browse files

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
parent 99c5bf8b
Loading
Loading
Loading
Loading
+117 −2
Original line number Diff line number Diff line
@@ -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
    @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
    public void isAvailable_configuredRbcUnAvailable_shouldReturnFalse() {
    @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());
    }