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.provider.Settings; 23 24 import androidx.preference.SwitchPreference; 25 import androidx.test.core.app.ApplicationProvider; 26 import androidx.test.ext.junit.runners.AndroidJUnit4; 27 28 import com.android.settings.core.BasePreferenceController; 29 30 import org.junit.After; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 /** 36 * Tests for {@link FontWeightAdjustmentPreferenceController}. 37 */ 38 @RunWith(AndroidJUnit4.class) 39 public class FontWeightAdjustmentPreferenceControllerTest { 40 private static final int ON = FontWeightAdjustmentPreferenceController.BOLD_TEXT_ADJUSTMENT; 41 private static final int OFF = 0; 42 43 private Context mContext; 44 private SwitchPreference mPreference; 45 private FontWeightAdjustmentPreferenceController mController; 46 47 @Before setUp()48 public void setUp() { 49 mContext = ApplicationProvider.getApplicationContext(); 50 mPreference = new SwitchPreference(mContext); 51 mController = new FontWeightAdjustmentPreferenceController( 52 mContext, "font_weight_adjustment"); 53 } 54 55 @After teardown()56 public void teardown() { 57 Settings.Secure.resetToDefaults(mContext.getContentResolver(), /* tag= */ null); 58 } 59 60 @Test getAvailabilityStatus_byDefault_shouldReturnAvailable()61 public void getAvailabilityStatus_byDefault_shouldReturnAvailable() { 62 assertThat(mController.getAvailabilityStatus()).isEqualTo( 63 BasePreferenceController.AVAILABLE); 64 } 65 66 @Test isChecked_enabledBoldText_shouldReturnTrue()67 public void isChecked_enabledBoldText_shouldReturnTrue() { 68 Settings.Secure.putInt(mContext.getContentResolver(), 69 Settings.Secure.FONT_WEIGHT_ADJUSTMENT, ON); 70 71 mController.updateState(mPreference); 72 73 assertThat(mController.isChecked()).isTrue(); 74 assertThat(mPreference.isChecked()).isTrue(); 75 } 76 77 @Test isChecked_disabledBoldText_shouldReturnFalse()78 public void isChecked_disabledBoldText_shouldReturnFalse() { 79 Settings.Secure.putInt(mContext.getContentResolver(), 80 Settings.Secure.FONT_WEIGHT_ADJUSTMENT, OFF); 81 82 mController.updateState(mPreference); 83 84 assertThat(mController.isChecked()).isFalse(); 85 assertThat(mPreference.isChecked()).isFalse(); 86 } 87 88 @Test setChecked_setTrue_shouldEnableBoldText()89 public void setChecked_setTrue_shouldEnableBoldText() { 90 mController.setChecked(true); 91 92 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 93 Settings.Secure.FONT_WEIGHT_ADJUSTMENT, OFF)).isEqualTo(ON); 94 } 95 96 @Test setChecked_setFalse_shouldDisableBoldText()97 public void setChecked_setFalse_shouldDisableBoldText() { 98 mController.setChecked(false); 99 100 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 101 Settings.Secure.FONT_WEIGHT_ADJUSTMENT, OFF)).isEqualTo(OFF); 102 } 103 104 @Test resetState_shouldDisableBoldText()105 public void resetState_shouldDisableBoldText() { 106 mController.setChecked(true); 107 108 mController.resetState(); 109 110 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 111 Settings.Secure.FONT_WEIGHT_ADJUSTMENT, OFF)).isEqualTo(OFF); 112 } 113 } 114