1 /*
2  * Copyright (C) 2019 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 
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settings.testutils.shadow.ShadowKeyCharacterMap;
28 import com.android.settings.testutils.shadow.ShadowUtils;
29 
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.robolectric.RobolectricTestRunner;
35 import org.robolectric.RuntimeEnvironment;
36 import org.robolectric.annotation.Config;
37 
38 @RunWith(RobolectricTestRunner.class)
39 @Config(shadows = {ShadowUtils.class, ShadowKeyCharacterMap.class})
40 public class PowerButtonEndsCallPreferenceControllerTest {
41 
42     private static final int UNKNOWN = -1;
43 
44     private Context mContext;
45     private SwitchPreference mPreference;
46     private PowerButtonEndsCallPreferenceController mController;
47 
48     @Before
setUp()49     public void setUp() {
50         mContext = RuntimeEnvironment.application;
51         mPreference = new SwitchPreference(mContext);
52         mController = new PowerButtonEndsCallPreferenceController(mContext, "power_button");
53     }
54 
55     @After
tearDown()56     public void tearDown() {
57         ShadowUtils.reset();
58         ShadowKeyCharacterMap.reset();
59     }
60 
61     @Test
getAvailabilityStatus_hasPowerKeyAndVoiceCapable_shouldReturnAvailable()62     public void getAvailabilityStatus_hasPowerKeyAndVoiceCapable_shouldReturnAvailable() {
63         ShadowKeyCharacterMap.setDevicehasKey(true);
64         ShadowUtils.setIsVoiceCapable(true);
65 
66         assertThat(mController.getAvailabilityStatus())
67                 .isEqualTo(BasePreferenceController.AVAILABLE);
68     }
69 
70     @Test
getAvailabilityStatus_noVoiceCapable_shouldReturnUnsupportedOnDevice()71     public void getAvailabilityStatus_noVoiceCapable_shouldReturnUnsupportedOnDevice() {
72         ShadowKeyCharacterMap.setDevicehasKey(true);
73         ShadowUtils.setIsVoiceCapable(false);
74 
75         assertThat(mController.getAvailabilityStatus())
76                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
77     }
78 
79     @Test
getAvailabilityStatus_noPowerKey_shouldReturnUnsupportedOnDevice()80     public void getAvailabilityStatus_noPowerKey_shouldReturnUnsupportedOnDevice() {
81         ShadowKeyCharacterMap.setDevicehasKey(false);
82         ShadowUtils.setIsVoiceCapable(true);
83 
84         assertThat(mController.getAvailabilityStatus())
85                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
86     }
87 
88     @Test
isChecked_enabledHangUp_shouldReturnTrue()89     public void isChecked_enabledHangUp_shouldReturnTrue() {
90         Settings.Secure.putInt(mContext.getContentResolver(),
91                 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR,
92                 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP);
93 
94         mController.updateState(mPreference);
95 
96         assertThat(mController.isChecked()).isTrue();
97         assertThat(mPreference.isChecked()).isTrue();
98     }
99 
100     @Test
isChecked_disabledHangUp_shouldReturnFalse()101     public void isChecked_disabledHangUp_shouldReturnFalse() {
102         Settings.Secure.putInt(mContext.getContentResolver(),
103                 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR,
104                 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_SCREEN_OFF);
105 
106         mController.updateState(mPreference);
107 
108         assertThat(mController.isChecked()).isFalse();
109         assertThat(mPreference.isChecked()).isFalse();
110     }
111 
112     @Test
setChecked_enabled_shouldEnableHangUp()113     public void setChecked_enabled_shouldEnableHangUp() {
114         mController.setChecked(true);
115 
116         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
117                 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR, UNKNOWN))
118                 .isEqualTo(Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP);
119     }
120 
121     @Test
setChecked_disabled_shouldDisableHangUp()122     public void setChecked_disabled_shouldDisableHangUp() {
123         mController.setChecked(false);
124 
125         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
126                 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR, UNKNOWN))
127                 .isEqualTo(Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_SCREEN_OFF);
128     }
129 }
130