• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.gestures;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.content.res.Resources;
27 import android.provider.Settings;
28 
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.robolectric.RobolectricTestRunner;
35 
36 @RunWith(RobolectricTestRunner.class)
37 public class PowerMenuSettingsUtilsTest {
38 
39     private Context mContext;
40     private Resources mResources;
41 
42     @Before
setUp()43     public void setUp() {
44         mContext = spy(ApplicationProvider.getApplicationContext());
45         mResources = mock(Resources.class);
46         when(mContext.getResources()).thenReturn(mResources);
47 
48         when(mResources.getBoolean(
49                         com.android.internal.R.bool
50                                 .config_longPressOnPowerForAssistantSettingAvailable))
51                 .thenReturn(true);
52     }
53 
54     @Test
isLongPressPowerForAssistantEnabled_valueSetToAssistant_returnsTrue()55     public void isLongPressPowerForAssistantEnabled_valueSetToAssistant_returnsTrue() {
56         Settings.Global.putInt(
57                 mContext.getContentResolver(), Settings.Global.POWER_BUTTON_LONG_PRESS, 5);
58         assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext)).isTrue();
59     }
60 
61     @Test
isLongPressPowerForAssistantEnabled_valueNotSetToAssistant_returnsFalse()62     public void isLongPressPowerForAssistantEnabled_valueNotSetToAssistant_returnsFalse() {
63         Settings.Global.putInt(
64                 mContext.getContentResolver(), Settings.Global.POWER_BUTTON_LONG_PRESS, 3);
65         assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext)).isFalse();
66     }
67 
68     @Test
isLongPressPowerForAssistantEnabled_valueNotSet_defaultToAssistant_returnsTrue()69     public void isLongPressPowerForAssistantEnabled_valueNotSet_defaultToAssistant_returnsTrue() {
70         when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior))
71                 .thenReturn(5);
72 
73         assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext)).isTrue();
74     }
75 
76     @Test
isLongPressPowerForAssistantEnabled_valueNotSet_defaultToPowerMenu_returnsFalse()77     public void isLongPressPowerForAssistantEnabled_valueNotSet_defaultToPowerMenu_returnsFalse() {
78         when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior))
79                 .thenReturn(1);
80 
81         assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext)).isFalse();
82     }
83 
84     @Test
isLongPressPowerSettingAvailable_defaultToAssistant_returnsTrue()85     public void isLongPressPowerSettingAvailable_defaultToAssistant_returnsTrue() {
86         when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior))
87                 .thenReturn(5);
88         assertThat(PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)).isTrue();
89     }
90 
91     @Test
isLongPressPowerSettingAvailable_defaultToPowerMenu_returnsTrue()92     public void isLongPressPowerSettingAvailable_defaultToPowerMenu_returnsTrue() {
93         when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior))
94                 .thenReturn(1);
95         assertThat(PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)).isTrue();
96     }
97 
98     @Test
isLongPressPowerSettingAvailable_defaultToPowerOff_returnsFalse()99     public void isLongPressPowerSettingAvailable_defaultToPowerOff_returnsFalse() {
100         // Power off is the unsupported option in long press power settings
101         when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior))
102                 .thenReturn(3);
103         assertThat(PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)).isFalse();
104     }
105 
106     @Test
isLongPressPowerSettingAvailable_settingDisabled_returnsFalse()107     public void isLongPressPowerSettingAvailable_settingDisabled_returnsFalse() {
108         // Disable the setting
109         when(mResources.getBoolean(
110                         com.android.internal.R.bool
111                                 .config_longPressOnPowerForAssistantSettingAvailable))
112                 .thenReturn(false);
113         when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior))
114                 .thenReturn(1);
115 
116         assertThat(PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)).isFalse();
117     }
118 
119     @Test
setLongPressPowerForAssistant_updatesValue()120     public void setLongPressPowerForAssistant_updatesValue() throws Exception {
121         boolean result = PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);
122 
123         assertThat(result).isTrue();
124         assertThat(
125                         Settings.Global.getInt(
126                                 mContext.getContentResolver(),
127                                 Settings.Global.POWER_BUTTON_LONG_PRESS))
128                 .isEqualTo(5);
129     }
130 
131     @Test
setLongPressPowerForAssistant_updatesKeyChordValueToPowerMenu()132     public void setLongPressPowerForAssistant_updatesKeyChordValueToPowerMenu() throws Exception {
133         PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);
134         assertThat(
135                         Settings.Global.getInt(
136                                 mContext.getContentResolver(),
137                                 Settings.Global.KEY_CHORD_POWER_VOLUME_UP))
138                 .isEqualTo(2);
139     }
140 
141     @Test
setLongPressPowerForPowerMenu_updatesValue()142     public void setLongPressPowerForPowerMenu_updatesValue() throws Exception {
143         boolean result = PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext);
144 
145         assertThat(result).isTrue();
146         assertThat(
147                         Settings.Global.getInt(
148                                 mContext.getContentResolver(),
149                                 Settings.Global.POWER_BUTTON_LONG_PRESS))
150                 .isEqualTo(1);
151     }
152 
153     @Test
setLongPressPowerForPowerMenu_updatesKeyChordValueToDefault()154     public void setLongPressPowerForPowerMenu_updatesKeyChordValueToDefault() throws Exception {
155         when(mResources.getInteger(com.android.internal.R.integer.config_keyChordPowerVolumeUp))
156                 .thenReturn(1);
157 
158         PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext);
159 
160         assertThat(
161                         Settings.Global.getInt(
162                                 mContext.getContentResolver(),
163                                 Settings.Global.KEY_CHORD_POWER_VOLUME_UP))
164                 .isEqualTo(1);
165     }
166 }
167