1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.accessibility; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.Context; 22 import android.provider.Settings; 23 24 import androidx.lifecycle.LifecycleOwner; 25 import androidx.preference.PreferenceManager; 26 import androidx.preference.PreferenceScreen; 27 import androidx.test.core.app.ApplicationProvider; 28 29 import com.android.settings.R; 30 import com.android.settingslib.core.lifecycle.Lifecycle; 31 import com.android.settingslib.widget.SelectorWithWidgetPreference; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.shadows.ShadowLooper; 39 40 /** 41 * Tests for {@link DaltonizerRadioButtonPreferenceController} 42 */ 43 @RunWith(RobolectricTestRunner.class) 44 public class DaltonizerRadioButtonPreferenceControllerTest { 45 private static final int DALTONIZER_MODE_INDEX = 0; 46 private static final String PREF_INVALID_VALUE = "-1"; 47 48 private final Context mContext = ApplicationProvider.getApplicationContext(); 49 private final String mPrefKey = 50 mContext.getResources() 51 .getStringArray(R.array.daltonizer_mode_keys)[DALTONIZER_MODE_INDEX]; 52 private final String mPrefValue = 53 String.valueOf(mContext.getResources() 54 .getIntArray(R.array.daltonizer_type_values)[DALTONIZER_MODE_INDEX]); 55 private DaltonizerRadioButtonPreferenceController mController; 56 private SelectorWithWidgetPreference mPreference; 57 private PreferenceScreen mScreen; 58 59 private LifecycleOwner mLifecycleOwner; 60 private Lifecycle mLifecycle; 61 62 @Before setUp()63 public void setUp() { 64 // initialize the value as unchecked 65 Settings.Secure.putString(mContext.getContentResolver(), 66 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, PREF_INVALID_VALUE); 67 mController = new DaltonizerRadioButtonPreferenceController(mContext, mPrefKey); 68 mPreference = new SelectorWithWidgetPreference(mContext); 69 mPreference.setKey(mPrefKey); 70 mScreen = new PreferenceManager(mContext).createPreferenceScreen(mContext); 71 mScreen.addPreference(mPreference); 72 mController.displayPreference(mScreen); 73 mLifecycleOwner = () -> mLifecycle; 74 mLifecycle = new Lifecycle(mLifecycleOwner); 75 } 76 77 @After cleanUp()78 public void cleanUp() { 79 mLifecycle.removeObserver(mController); 80 } 81 82 @Test isAvailable()83 public void isAvailable() { 84 assertThat(mController.isAvailable()).isTrue(); 85 } 86 87 @Test updateState_valueNotMatched_notChecked()88 public void updateState_valueNotMatched_notChecked() { 89 Settings.Secure.putString(mContext.getContentResolver(), 90 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, PREF_INVALID_VALUE); 91 92 mController.updateState(mPreference); 93 94 assertThat(mPreference.isChecked()).isFalse(); 95 } 96 97 @Test updateState_valueMatched_checked()98 public void updateState_valueMatched_checked() { 99 Settings.Secure.putString(mContext.getContentResolver(), 100 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, mPrefValue); 101 102 mController.updateState(mPreference); 103 104 assertThat(mPreference.isChecked()).isTrue(); 105 } 106 107 @Test onRadioButtonClick_shouldReturnDaltonizerValue()108 public void onRadioButtonClick_shouldReturnDaltonizerValue() { 109 mController.onRadioButtonClicked(mPreference); 110 111 final String accessibilityDaltonizerValue = Settings.Secure.getString( 112 mContext.getContentResolver(), 113 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER); 114 assertThat(accessibilityDaltonizerValue).isEqualTo(mPrefValue); 115 } 116 117 @Test onResume_settingsValueChangedToUnmatch_preferenceBecomesUnchecked()118 public void onResume_settingsValueChangedToUnmatch_preferenceBecomesUnchecked() { 119 Settings.Secure.putString(mContext.getContentResolver(), 120 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, mPrefValue); 121 mController.updateState(mPreference); 122 assertThat(mPreference.isChecked()).isTrue(); 123 mLifecycle.addObserver(mController); 124 125 mLifecycle.handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event.ON_RESUME); 126 Settings.Secure.putString(mContext.getContentResolver(), 127 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, PREF_INVALID_VALUE); 128 ShadowLooper.idleMainLooper(); 129 130 assertThat(mPreference.isChecked()).isFalse(); 131 } 132 133 @Test onPause_settingsValueChangedAndMatch_preferenceStateNotUpdated()134 public void onPause_settingsValueChangedAndMatch_preferenceStateNotUpdated() { 135 assertThat(mPreference.isChecked()).isFalse(); 136 mLifecycle.addObserver(mController); 137 mLifecycle.handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event.ON_RESUME); 138 139 mLifecycle.handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event.ON_PAUSE); 140 Settings.Secure.putString(mContext.getContentResolver(), 141 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, mPrefValue); 142 ShadowLooper.idleMainLooper(); 143 144 assertThat(mPreference.isChecked()).isFalse(); 145 } 146 } 147