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

Commit 5b189de9 authored by jeffreyhuang's avatar jeffreyhuang
Browse files

Introduce SimulateColorSpacePreferenceController

 - Create new SimulateColorSpacePreferenceController
 - Create controller inside the DashboardFragment
 - Port logic from DevelopmentSettings into the controller

Bug: 34203528
Test: make RunSettingsRoboTests -j40
Change-Id: I89044404b19c47a6e3ce4e9d86dd6806aadbd0b5
parent e2fc925b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -308,7 +308,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
        // debug non-rectangular clip operations
        controllers.add(new ForceMSAAPreferenceController(context));
        controllers.add(new HardwareOverlaysPreferenceController(context));
        // simulate color space
        controllers.add(new SimulateColorSpacePreferenceController(context));
        // set gpu renderer
        controllers.add(new UsbAudioRoutingPreferenceController(context));
        controllers.add(new StrictModePreferenceController(context));
+145 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.development;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.view.accessibility.AccessibilityManager;

import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;

public class SimulateColorSpacePreferenceController extends
        DeveloperOptionsPreferenceController implements
        Preference.OnPreferenceChangeListener, PreferenceControllerMixin {

    private static final String SIMULATE_COLOR_SPACE = "simulate_color_space";

    @VisibleForTesting
    static final int SETTING_VALUE_OFF = 0;
    @VisibleForTesting
    static final int SETTING_VALUE_ON = 1;

    private ListPreference mPreference;

    public SimulateColorSpacePreferenceController(Context context) {
        super(context);
    }

    @Override
    public String getPreferenceKey() {
        return SIMULATE_COLOR_SPACE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);

        mPreference = (ListPreference) screen.findPreference(getPreferenceKey());
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        writeSimulateColorSpace(newValue);
        return true;
    }

    @Override
    public void updateState(Preference preference) {
        updateSimulateColorSpace();
    }

    @Override
    protected void onDeveloperOptionsSwitchEnabled() {
        mPreference.setEnabled(true);
    }

    @Override
    public void onDeveloperOptionsDisabled() {
        if (usingDevelopmentColorSpace()) {
            writeSimulateColorSpace(-1);
        }
        mPreference.setEnabled(false);
    }

    private void updateSimulateColorSpace() {
        final ContentResolver cr = mContext.getContentResolver();
        final boolean enabled = Settings.Secure.getInt(
                cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_OFF)
                != SETTING_VALUE_OFF;
        if (enabled) {
            final String mode = Integer.toString(Settings.Secure.getInt(
                    cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
                    AccessibilityManager.DALTONIZER_DISABLED));
            mPreference.setValue(mode);
            final int index = mPreference.findIndexOfValue(mode);
            if (index < 0) {
                final Resources res = mContext.getResources();
                // We're using a mode controlled by accessibility preferences.
                mPreference.setSummary(res.getString(R.string.daltonizer_type_overridden,
                        res.getString(R.string.accessibility_display_daltonizer_preference_title)));
            } else {
                mPreference.setSummary("%s");
            }
        } else {
            mPreference.setValue(
                    Integer.toString(AccessibilityManager.DALTONIZER_DISABLED));
        }
    }

    private void writeSimulateColorSpace(Object value) {
        final ContentResolver cr = mContext.getContentResolver();
        final int newMode = Integer.parseInt(value.toString());
        if (newMode < 0) {
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
                    SETTING_VALUE_OFF);
        } else {
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
                    SETTING_VALUE_ON);
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, newMode);
        }
    }

    /**
     * @return <code>true</code> if the color space preference is currently
     * controlled by development settings
     */
    private boolean usingDevelopmentColorSpace() {
        final ContentResolver cr = mContext.getContentResolver();
        final boolean enabled = Settings.Secure.getInt(
                cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_OFF)
                != SETTING_VALUE_OFF;
        if (enabled) {
            final String mode = Integer.toString(Settings.Secure.getInt(
                    cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
                    AccessibilityManager.DALTONIZER_DISABLED));
            final int index = mPreference.findIndexOfValue(mode);
            if (index >= 0) {
                // We're using a mode controlled by developer preferences.
                return true;
            }
        }
        return false;
    }
}
+174 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.development;

import static com.android.settings.development.SimulateColorSpacePreferenceController
        .SETTING_VALUE_OFF;
import static com.android.settings.development.SimulateColorSpacePreferenceController
        .SETTING_VALUE_ON;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SimulateColorSpacePreferenceControllerTest {

    @Mock
    private ListPreference mPreference;
    @Mock
    private PreferenceScreen mScreen;

    /**
     * 0: Disabled
     * 1: Monochromacy
     * 2: Deuteranomaly (red-green)
     * 3: Protanomaly (red-green)
     * 4: Tritanomaly (blue-yellow)
     */
    private String[] mListValues;
    private Context mContext;
    private SimulateColorSpacePreferenceController mController;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mListValues = mContext.getResources().getStringArray(R.array.simulate_color_space_values);
        mController = new SimulateColorSpacePreferenceController(mContext);
        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
        mController.displayPreference(mScreen);
    }

    @Test
    public void onPreferenceChange_disabledSelected_shouldTurnOffPreference()
            throws Settings.SettingNotFoundException {
        mController.onPreferenceChange(mPreference, mListValues[0]);

        final int enabled = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED);

        assertThat(enabled).isEqualTo(SETTING_VALUE_OFF);
    }

    @Test
    public void onPreferenceChange_monochromacySelected_shouldEnableAndSelectPreference()
            throws Settings.SettingNotFoundException {
        mController.onPreferenceChange(mPreference, mListValues[1]);

        final int enabled = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED);
        final int settingValue = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER);

        assertThat(enabled).isEqualTo(SETTING_VALUE_ON);
        assertThat(settingValue).isEqualTo(Integer.valueOf(mListValues[1]));
    }

    @Test
    public void updateState_settingOff_shouldSetValueToDisabled() {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_OFF);

        mController.updateState(mPreference);

        verify(mPreference).setValue(mListValues[0]);
    }

    @Test
    public void updateState_settingOnMonochromacyEnabled_shouldSelectMonochromacy() {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_ON);
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, Integer.valueOf(mListValues[1]));

        mController.updateState(mPreference);

        verify(mPreference).setValue(mListValues[1]);
        verify(mPreference).setSummary("%s");
    }

    @Test
    public void updateState_settingOnControlledByAccessibility_shouldSetOverridedSummary() {
        Resources res = mContext.getResources();
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_ON);
        when(mPreference.findIndexOfValue(anyString())).thenReturn(-1);

        mController.updateState(mPreference);

        verify(mPreference).setSummary(res.getString(R.string.daltonizer_type_overridden,
                res.getString(R.string.accessibility_display_daltonizer_preference_title)));
    }

    @Test
    public void onDeveloperOptionsSwitchDisabled_notControlledByDevOptions_shouldDisableAndReset()
            throws Settings.SettingNotFoundException {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_ON);
        when(mPreference.findIndexOfValue(anyString())).thenReturn(-1);

        mController.onDeveloperOptionsDisabled();

        final int settingValue = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED);
        assertThat(settingValue).isEqualTo(SETTING_VALUE_ON);
        verify(mPreference).setEnabled(false);
    }

    @Test
    public void onDeveloperOptionsSwitchDisabled_controlledByDevOptions_shouldDisableAndNotReset()
            throws Settings.SettingNotFoundException {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_ON);

        mController.onDeveloperOptionsDisabled();

        final int settingValue = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED);
        assertThat(settingValue).isEqualTo(SETTING_VALUE_OFF);
        verify(mPreference).setEnabled(false);
    }

    @Test
    public void onDeveloperOptionsSwitchEnabled_shouldEnablePreference() {
        mController.onDeveloperOptionsSwitchEnabled();

        verify(mPreference).setEnabled(true);
    }
}