1 /*
2  * Copyright (C) 2023 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.android.settings.accessibility.AccessibilityUtil.State.OFF;
20 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
21 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.Mockito.reset;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 
29 import android.content.Context;
30 import android.provider.Settings;
31 
32 import androidx.preference.PreferenceManager;
33 import androidx.preference.PreferenceScreen;
34 import androidx.preference.SwitchPreference;
35 import androidx.test.core.app.ApplicationProvider;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.robolectric.RobolectricTestRunner;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class MagnificationJoystickPreferenceControllerTest {
44 
45     private static final String KEY_JOYSTICK =
46             Settings.Secure.ACCESSIBILITY_MAGNIFICATION_JOYSTICK_ENABLED;
47 
48     private final Context mContext = ApplicationProvider.getApplicationContext();
49     private final SwitchPreference mSwitchPreference = spy(new SwitchPreference(mContext));
50     private final MagnificationJoystickPreferenceController mController =
51             new MagnificationJoystickPreferenceController(mContext,
52                     MagnificationJoystickPreferenceController.PREF_KEY);
53 
54     @Before
setUp()55     public void setUp() {
56         final PreferenceManager preferenceManager = new PreferenceManager(mContext);
57         final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
58         mSwitchPreference.setKey(MagnificationJoystickPreferenceController.PREF_KEY);
59         screen.addPreference(mSwitchPreference);
60         mController.displayPreference(screen);
61 
62         mController.updateState(mSwitchPreference);
63         reset(mSwitchPreference);
64     }
65 
66     @Test
performClick_switchDefaultStateForJoystick_shouldReturnTrue()67     public void performClick_switchDefaultStateForJoystick_shouldReturnTrue() {
68         mSwitchPreference.performClick();
69 
70         verify(mSwitchPreference).setChecked(true);
71         assertThat(mController.isChecked()).isTrue();
72         assertThat(mSwitchPreference.isChecked()).isTrue();
73     }
74 
75     @Test
updateState_disableJoystick_shouldReturnFalse()76     public void updateState_disableJoystick_shouldReturnFalse() {
77         Settings.Secure.putInt(mContext.getContentResolver(), KEY_JOYSTICK, OFF);
78 
79         mController.updateState(mSwitchPreference);
80 
81         verify(mSwitchPreference).setChecked(false);
82         assertThat(mController.isChecked()).isFalse();
83         assertThat(mSwitchPreference.isChecked()).isFalse();
84     }
85 
86     @Test
getAvailableStatus_notInSetupWizard_returnAvailable()87     public void getAvailableStatus_notInSetupWizard_returnAvailable() {
88         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
89     }
90 
91     @Test
getAvailableStatus_inSetupWizard_returnConditionallyUnavailable()92     public void getAvailableStatus_inSetupWizard_returnConditionallyUnavailable() {
93         mController.setInSetupWizard(true);
94         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
95     }
96 }
97