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.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 @RunWith(AndroidJUnit4.class)
35 public class HighTextContrastPreferenceControllerTest {
36 
37     private static final int ON = 1;
38     private static final int OFF = 0;
39     private static final int UNKNOWN = -1;
40 
41     private Context mContext;
42     private SwitchPreference mPreference;
43     private HighTextContrastPreferenceController mController;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = ApplicationProvider.getApplicationContext();
48         mPreference = new SwitchPreference(mContext);
49         mController = new HighTextContrastPreferenceController(mContext, "text_contrast");
50     }
51 
52     @Test
getAvailabilityStatus_byDefault_shouldReturnAvailable()53     public void getAvailabilityStatus_byDefault_shouldReturnAvailable() {
54         assertThat(mController.getAvailabilityStatus()).isEqualTo(
55                 BasePreferenceController.AVAILABLE);
56     }
57 
58     @Test
isChecked_enabledTextContrast_shouldReturnTrue()59     public void isChecked_enabledTextContrast_shouldReturnTrue() {
60         Settings.Secure.putInt(mContext.getContentResolver(),
61                 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, ON);
62 
63         mController.updateState(mPreference);
64 
65         assertThat(mController.isChecked()).isTrue();
66         assertThat(mPreference.isChecked()).isTrue();
67     }
68 
69     @Test
isChecked_disabledTextContrast_shouldReturnFalse()70     public void isChecked_disabledTextContrast_shouldReturnFalse() {
71         Settings.Secure.putInt(mContext.getContentResolver(),
72                 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, OFF);
73 
74         mController.updateState(mPreference);
75 
76         assertThat(mController.isChecked()).isFalse();
77         assertThat(mPreference.isChecked()).isFalse();
78     }
79 
80     @Test
setChecked_setTrue_shouldEnableTextContrast()81     public void setChecked_setTrue_shouldEnableTextContrast() {
82         mController.setChecked(true);
83 
84         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
85                 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, UNKNOWN)).isEqualTo(ON);
86 
87     }
88 
89     @Test
setChecked_setFalse_shouldDisableTextContrast()90     public void setChecked_setFalse_shouldDisableTextContrast() {
91         mController.setChecked(false);
92 
93         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
94                 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, UNKNOWN)).isEqualTo(OFF);
95     }
96 }
97