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.UserHandle;
23 import android.provider.Settings;
24 
25 import androidx.preference.SwitchPreference;
26 import androidx.test.core.app.ApplicationProvider;
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 
29 import com.android.settings.core.BasePreferenceController;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 @RunWith(AndroidJUnit4.class)
36 public class PrimaryMonoPreferenceControllerTest {
37 
38     private static final int ON = 1;
39     private static final int OFF = 0;
40     private static final int UNKNOWN = -1;
41 
42     private Context mContext;
43     private SwitchPreference mPreference;
44     private PrimaryMonoPreferenceController mController;
45 
46     @Before
setUp()47     public void setUp() {
48         mContext = ApplicationProvider.getApplicationContext();
49         mPreference = new SwitchPreference(mContext);
50         mController = new PrimaryMonoPreferenceController(mContext, "test_key");
51     }
52 
53     @Test
getAvailabilityStatus_byDefault_shouldReturnAvailable()54     public void getAvailabilityStatus_byDefault_shouldReturnAvailable() {
55         assertThat(mController.getAvailabilityStatus()).isEqualTo(
56                 BasePreferenceController.AVAILABLE);
57     }
58 
59     @Test
isChecked_enabledMonoAudio_shouldReturnTrue()60     public void isChecked_enabledMonoAudio_shouldReturnTrue() {
61         Settings.System.putIntForUser(mContext.getContentResolver(),
62                 Settings.System.MASTER_MONO, ON, UserHandle.USER_CURRENT);
63 
64         mController.updateState(mPreference);
65 
66         assertThat(mController.isChecked()).isTrue();
67         assertThat(mPreference.isChecked()).isTrue();
68     }
69 
70     @Test
isChecked_disabledMonoAudio_shouldReturnFalse()71     public void isChecked_disabledMonoAudio_shouldReturnFalse() {
72         Settings.System.putIntForUser(mContext.getContentResolver(),
73                 Settings.System.MASTER_MONO, OFF, UserHandle.USER_CURRENT);
74 
75         mController.updateState(mPreference);
76 
77         assertThat(mController.isChecked()).isFalse();
78         assertThat(mPreference.isChecked()).isFalse();
79     }
80 
81     @Test
setChecked_setTrue_shouldEnableMonoAudio()82     public void setChecked_setTrue_shouldEnableMonoAudio() {
83         mController.setChecked(true);
84 
85         assertThat(Settings.System.getIntForUser(mContext.getContentResolver(),
86                 Settings.System.MASTER_MONO, UNKNOWN, UserHandle.USER_CURRENT)).isEqualTo(ON);
87     }
88 
89     @Test
setChecked_setFalse_shouldDisableMonoAudio()90     public void setChecked_setFalse_shouldDisableMonoAudio() {
91         mController.setChecked(false);
92 
93         assertThat(Settings.System.getIntForUser(mContext.getContentResolver(),
94                 Settings.System.MASTER_MONO, UNKNOWN, UserHandle.USER_CURRENT)).isEqualTo(OFF);
95     }
96 }
97