1 /* 2 * Copyright (C) 2020 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.os.Looper; 23 import android.provider.Settings; 24 25 import androidx.preference.PreferenceManager; 26 import androidx.preference.PreferenceScreen; 27 import androidx.preference.SwitchPreference; 28 import androidx.test.core.app.ApplicationProvider; 29 import androidx.test.ext.junit.runners.AndroidJUnit4; 30 31 import com.android.settings.core.BasePreferenceController; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 /** 38 * Tests for {@link HighTextContrastPreferenceController}. 39 */ 40 @RunWith(AndroidJUnit4.class) 41 public class HighTextContrastPreferenceControllerTest { 42 43 private static final String PREF_KEY = "text_contrast"; 44 private static final int ON = 1; 45 private static final int OFF = 0; 46 private static final int UNKNOWN = -1; 47 48 private Context mContext; 49 private SwitchPreference mPreference; 50 private HighTextContrastPreferenceController mController; 51 private PreferenceScreen mScreen; 52 53 @Before setUp()54 public void setUp() { 55 mContext = ApplicationProvider.getApplicationContext(); 56 if (Looper.myLooper() == null) { 57 Looper.prepare(); 58 } 59 final PreferenceManager preferenceManager = new PreferenceManager(mContext); 60 mScreen = preferenceManager.createPreferenceScreen(mContext); 61 mPreference = new SwitchPreference(mContext); 62 mPreference.setKey(PREF_KEY); 63 mScreen.addPreference(mPreference); 64 mController = new HighTextContrastPreferenceController(mContext, PREF_KEY); 65 } 66 67 @Test getAvailabilityStatus_byDefault_shouldReturnAvailable()68 public void getAvailabilityStatus_byDefault_shouldReturnAvailable() { 69 assertThat(mController.getAvailabilityStatus()).isEqualTo( 70 BasePreferenceController.AVAILABLE); 71 } 72 73 @Test isChecked_enabledTextContrast_shouldReturnTrue()74 public void isChecked_enabledTextContrast_shouldReturnTrue() { 75 Settings.Secure.putInt(mContext.getContentResolver(), 76 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, ON); 77 78 mController.updateState(mPreference); 79 80 assertThat(mController.isChecked()).isTrue(); 81 assertThat(mPreference.isChecked()).isTrue(); 82 } 83 84 @Test isChecked_disabledTextContrast_shouldReturnFalse()85 public void isChecked_disabledTextContrast_shouldReturnFalse() { 86 Settings.Secure.putInt(mContext.getContentResolver(), 87 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, OFF); 88 89 mController.updateState(mPreference); 90 91 assertThat(mController.isChecked()).isFalse(); 92 assertThat(mPreference.isChecked()).isFalse(); 93 } 94 95 @Test setChecked_setTrue_shouldEnableTextContrast()96 public void setChecked_setTrue_shouldEnableTextContrast() { 97 mController.setChecked(true); 98 99 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 100 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, UNKNOWN)).isEqualTo(ON); 101 102 } 103 104 @Test setChecked_setFalse_shouldDisableTextContrast()105 public void setChecked_setFalse_shouldDisableTextContrast() { 106 mController.setChecked(false); 107 108 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 109 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, UNKNOWN)).isEqualTo(OFF); 110 } 111 112 @Test resetState_shouldDisableTextContrast()113 public void resetState_shouldDisableTextContrast() { 114 mController.displayPreference(mScreen); 115 mController.setChecked(true); 116 mPreference.setChecked(true); 117 118 mController.resetState(); 119 120 assertThat(mPreference.isChecked()).isFalse(); 121 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 122 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, UNKNOWN)).isEqualTo(OFF); 123 } 124 } 125