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 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.robolectric.RobolectricTestRunner;
31 import org.robolectric.RuntimeEnvironment;
32 
33 @RunWith(RobolectricTestRunner.class)
34 public class MagnificationEnablePreferenceControllerTest {
35     private static final String PREF_KEY = "screen_magnification_enable";
36     private static final String KEY_ENABLE = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE;
37     private static final int UNKNOWN = -1;
38     private Context mContext;
39     private SwitchPreference mPreference;
40     private MagnificationEnablePreferenceController mController;
41 
42     @Before
setUp()43     public void setUp() {
44         mContext = RuntimeEnvironment.application;
45         mPreference = new SwitchPreference(mContext);
46         mController = new MagnificationEnablePreferenceController(mContext, PREF_KEY);
47     }
48 
49     @Test
isChecked_enabledFullscreenMagnificationMode_shouldReturnTrue()50     public void isChecked_enabledFullscreenMagnificationMode_shouldReturnTrue() {
51         Settings.Secure.putIntForUser(mContext.getContentResolver(),
52                 KEY_ENABLE, Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN,
53                 UserHandle.USER_CURRENT);
54 
55         mController.updateState(mPreference);
56 
57         assertThat(mController.isChecked()).isTrue();
58         assertThat(mPreference.isChecked()).isTrue();
59     }
60 
61     @Test
isChecked_enabledWindowMagnificationMode_shouldReturnFalse()62     public void isChecked_enabledWindowMagnificationMode_shouldReturnFalse() {
63         Settings.Secure.putIntForUser(mContext.getContentResolver(),
64                 KEY_ENABLE, Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW,
65                 UserHandle.USER_CURRENT);
66 
67         mController.updateState(mPreference);
68 
69         assertThat(mController.isChecked()).isFalse();
70         assertThat(mPreference.isChecked()).isFalse();
71     }
72 
73 
74     @Test
setChecked_setTrue_shouldEnableFullscreenMagnificationMode()75     public void setChecked_setTrue_shouldEnableFullscreenMagnificationMode() {
76         mController.setChecked(true);
77 
78         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
79                 KEY_ENABLE, UNKNOWN,
80                 UserHandle.USER_CURRENT)).isEqualTo(
81                 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN);
82     }
83 
84     @Test
setChecked_setFalse_shouldEnableWindowMagnificationMode()85     public void setChecked_setFalse_shouldEnableWindowMagnificationMode() {
86         mController.setChecked(false);
87 
88         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
89                 KEY_ENABLE, UNKNOWN,
90                 UserHandle.USER_CURRENT)).isEqualTo(
91                 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW);
92     }
93 }
94