From fefb44752683ddaea10fd079c3af6c1cefaeed9f Mon Sep 17 00:00:00 2001 From: sallyyuen Date: Tue, 13 Apr 2021 17:41:52 -0700 Subject: [PATCH] Call ColorDisplayManager for Extra Dim/RBC state - Move junit test into unit test folder - Settings resources ids are different from the junit test ids so we need to find the correct resource id - ColorDisplayManager will not update in time before the controller is checked, and it can't be mocked since it's a final class Bug: 170970675 Test: manual, atest ReduceBrightColorsPreferenceControllerTest Change-Id: I57bfdd8294c5b6e147b4e11ae97b6b56f6121343 --- ...educeBrightColorsPreferenceController.java | 11 +-- ...eReduceBrightColorsPreferenceFragment.java | 12 +-- ...eBrightColorsPreferenceControllerTest.java | 58 ----------- ...eBrightColorsPreferenceControllerTest.java | 98 +++++++++++++++++++ 4 files changed, 106 insertions(+), 73 deletions(-) delete mode 100644 tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java create mode 100644 tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java diff --git a/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java b/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java index ddf00e5a846..d886a595d9c 100644 --- a/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java +++ b/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java @@ -42,6 +42,7 @@ public class ReduceBrightColorsPreferenceController extends TogglePreferenceCont private ContentObserver mSettingsContentObserver; private PrimarySwitchPreference mPreference; private final Context mContext; + private final ColorDisplayManager mColorDisplayManager; public ReduceBrightColorsPreferenceController(Context context, String preferenceKey) { @@ -56,21 +57,17 @@ public class ReduceBrightColorsPreferenceController extends TogglePreferenceCont } } }; + mColorDisplayManager = mContext.getSystemService(ColorDisplayManager.class); } @Override public boolean isChecked() { - return Settings.Secure.getIntForUser(mContext.getContentResolver(), - Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, - 0, - UserHandle.USER_CURRENT) == 1; + return mColorDisplayManager.isReduceBrightColorsActivated(); } @Override public boolean setChecked(boolean isChecked) { - return Settings.Secure.putIntForUser(mContext.getContentResolver(), - Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, isChecked ? 1 : 0, - UserHandle.USER_CURRENT); + return mColorDisplayManager.setReduceBrightColorsActivated(isChecked); } @Override diff --git a/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java index ad995565dd9..7a4ae62befa 100644 --- a/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java +++ b/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java @@ -16,9 +16,6 @@ package com.android.settings.accessibility; -import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; -import static com.android.settings.accessibility.AccessibilityUtil.State.ON; - import android.app.settings.SettingsEnums; import android.content.Context; import android.hardware.display.ColorDisplayManager; @@ -56,6 +53,7 @@ public class ToggleReduceBrightColorsPreferenceFragment extends ToggleFeaturePre private SettingsContentObserver mSettingsContentObserver; private ReduceBrightColorsIntensityPreferenceController mRbcIntensityPreferenceController; private ReduceBrightColorsPersistencePreferenceController mRbcPersistencePreferenceController; + private ColorDisplayManager mColorDisplayManager; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -79,7 +77,7 @@ public class ToggleReduceBrightColorsPreferenceFragment extends ToggleFeaturePre updateSwitchBarToggleSwitch(); } }; - + mColorDisplayManager = getContext().getSystemService(ColorDisplayManager.class); final View view = super.onCreateView(inflater, container, savedInstanceState); // Parent sets the title when creating the view, so set it after calling super mToggleServiceSwitchPreference.setTitle(R.string.reduce_bright_colors_switch_title); @@ -137,8 +135,7 @@ public class ToggleReduceBrightColorsPreferenceFragment extends ToggleFeaturePre @Override protected void onPreferenceToggled(String preferenceKey, boolean enabled) { AccessibilityStatsLogUtils.logAccessibilityServiceEnabled(mComponentName, enabled); - Settings.Secure.putInt(getContentResolver(), - REDUCE_BRIGHT_COLORS_ACTIVATED_KEY, enabled ? ON : OFF); + mColorDisplayManager.setReduceBrightColorsActivated(enabled); } @Override @@ -161,8 +158,7 @@ public class ToggleReduceBrightColorsPreferenceFragment extends ToggleFeaturePre @Override protected void updateSwitchBarToggleSwitch() { - final boolean checked = Settings.Secure.getInt(getContentResolver(), - REDUCE_BRIGHT_COLORS_ACTIVATED_KEY, OFF) == ON; + final boolean checked = mColorDisplayManager.isReduceBrightColorsActivated(); mRbcIntensityPreferenceController.updateState(getPreferenceScreen() .findPreference(KEY_INTENSITY)); mRbcPersistencePreferenceController.updateState(getPreferenceScreen() diff --git a/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java deleted file mode 100644 index 325e6a84b33..00000000000 --- a/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2020 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.accessibility; - -import static com.google.common.truth.Truth.assertThat; - -import android.content.Context; -import android.provider.Settings; - -import androidx.test.core.app.ApplicationProvider; -import androidx.test.ext.junit.runners.AndroidJUnit4; - -import com.android.settings.R; - -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class ReduceBrightColorsPreferenceControllerTest { - private static final String PREF_KEY = "rbc_preference"; - private final Context mContext = ApplicationProvider.getApplicationContext(); - private final ReduceBrightColorsPreferenceController mController = - new ReduceBrightColorsPreferenceController(mContext, PREF_KEY); - - @Test - public void getSummary_returnSummary() { - assertThat(mController.getSummary().toString().contains( - mContext.getText(R.string.reduce_bright_colors_preference_summary))).isTrue(); - } - - @Test - public void isChecked_reduceBrightColorsIsActivated_returnTrue() { - Settings.Secure.putInt(mContext.getContentResolver(), - Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1); - assertThat(mController.isChecked()).isTrue(); - } - - @Test - public void isChecked_reduceBrightColorsIsNotActivated_returnFalse() { - Settings.Secure.putInt(mContext.getContentResolver(), - Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 0); - assertThat(mController.isChecked()).isFalse(); - } -} diff --git a/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java b/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java new file mode 100644 index 00000000000..e1c0277c7e9 --- /dev/null +++ b/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2021 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.accessibility; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.res.Resources; +import android.provider.Settings; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.internal.R; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class ReduceBrightColorsPreferenceControllerTest { + private static final String PREF_KEY = "rbc_preference"; + + private Context mContext; + private Resources mResources;; + private ReduceBrightColorsPreferenceController mController; + + @Before + public void setUp() { + mContext = spy(ApplicationProvider.getApplicationContext()); + mResources = spy(mContext.getResources()); + when(mContext.getResources()).thenReturn(mResources); + mController = new ReduceBrightColorsPreferenceController(mContext, + PREF_KEY); + } + + @Test + public void getSummary_returnSummary() { + assertThat(mController.getSummary().toString().contains( + resourceString("reduce_bright_colors_preference_summary"))).isTrue(); + } + + @Ignore("ColorDisplayManager runs in a different thread which results in a race condition") + @Test + public void isChecked_reduceBrightColorsIsActivated_returnTrue() { + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1); + assertThat(mController.isChecked()).isTrue(); + } + + @Ignore("ColorDisplayManager runs in a different thread which results in a race condition") + @Test + public void isChecked_reduceBrightColorsIsNotActivated_returnFalse() { + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 0); + assertThat(mController.isChecked()).isFalse(); + } + + @Test + public void isAvailable_configuredRbcAvailable_shouldReturnTrue() { + doReturn(true).when(mResources).getBoolean( + R.bool.config_reduceBrightColorsAvailable); + assertThat(mController.isAvailable()).isTrue(); + } + @Test + public void isAvailable_configuredRbcUnAvailable_shouldReturnFalse() { + 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()); + } + + private String resourceString(String name) { + return mContext.getResources().getString(resourceId("string", name)); + } +}