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 static org.mockito.Mockito.atLeastOnce; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.provider.Settings; 29 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settingslib.core.lifecycle.Lifecycle; 34 import com.android.settingslib.widget.SelectorWithWidgetPreference; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.RuntimeEnvironment; 43 44 @RunWith(RobolectricTestRunner.class) 45 public class DaltonizerRadioButtonPreferenceControllerTest implements 46 DaltonizerRadioButtonPreferenceController.OnChangeListener { 47 private static final String PREF_KEY = "daltonizer_mode_protanomaly"; 48 private static final String PREF_VALUE = "11"; 49 private static final String PREF_FAKE_VALUE = "-1"; 50 51 private DaltonizerRadioButtonPreferenceController mController; 52 53 @Mock 54 private SelectorWithWidgetPreference mMockPref; 55 private Context mContext; 56 57 @Mock 58 private PreferenceScreen mScreen; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 mContext = RuntimeEnvironment.application; 64 mController = new DaltonizerRadioButtonPreferenceController(mContext, mock(Lifecycle.class), 65 PREF_KEY); 66 mController.setOnChangeListener(this); 67 68 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mMockPref); 69 when(mMockPref.getKey()).thenReturn(PREF_KEY); 70 mController.displayPreference(mScreen); 71 } 72 73 @Override onCheckedChanged(Preference preference)74 public void onCheckedChanged(Preference preference) { 75 mController.updateState(preference); 76 } 77 78 @Test isAvailable()79 public void isAvailable() { 80 assertThat(mController.isAvailable()).isTrue(); 81 } 82 83 @Test updateState_notChecked()84 public void updateState_notChecked() { 85 Settings.Secure.putString(mContext.getContentResolver(), 86 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, PREF_FAKE_VALUE); 87 88 mController.updateState(mMockPref); 89 90 // the first checked state is set to false by control 91 verify(mMockPref, atLeastOnce()).setChecked(false); 92 verify(mMockPref, never()).setChecked(true); 93 } 94 95 @Test updateState_checked()96 public void updateState_checked() { 97 Settings.Secure.putString(mContext.getContentResolver(), 98 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, PREF_VALUE); 99 100 mController.updateState(mMockPref); 101 102 // the first checked state is set to false by control 103 verify(mMockPref, atLeastOnce()).setChecked(false); 104 verify(mMockPref, atLeastOnce()).setChecked(true); 105 } 106 107 @Test onRadioButtonClick_shouldReturnDaltonizerValue()108 public void onRadioButtonClick_shouldReturnDaltonizerValue() { 109 mController.onRadioButtonClicked(mMockPref); 110 final String accessibilityDaltonizerValue = Settings.Secure.getString( 111 mContext.getContentResolver(), 112 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER); 113 114 assertThat(accessibilityDaltonizerValue).isEqualTo(PREF_VALUE); 115 } 116 } 117