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 
49     @Test
longPressBehaviourValuePresent_returnsValue()50     public void longPressBehaviourValuePresent_returnsValue() {
51         when(mResources.getInteger(
52                 com.android.internal.R.integer.config_longPressOnPowerBehavior))
53                 .thenReturn(0);
54         Settings.Global.putInt(mContext.getContentResolver(),
55                 Settings.Global.POWER_BUTTON_LONG_PRESS, 1);
56 
57         assertThat(PowerMenuSettingsUtils.getPowerButtonSettingValue(mContext)).isEqualTo(1);
58     }
59 
60     @Test
longPressBehaviourValueNotPresent_returnsDefault()61     public void longPressBehaviourValueNotPresent_returnsDefault() {
62         when(mResources.getInteger(
63                 com.android.internal.R.integer.config_longPressOnPowerBehavior))
64                 .thenReturn(2);
65 
66         assertThat(PowerMenuSettingsUtils.getPowerButtonSettingValue(mContext)).isEqualTo(2);
67     }
68 
69     @Test
longPressBehaviourValueSetToAssistant_isAssistEnabledReturnsTrue()70     public void longPressBehaviourValueSetToAssistant_isAssistEnabledReturnsTrue() {
71         Settings.Global.putInt(mContext.getContentResolver(),
72                 Settings.Global.POWER_BUTTON_LONG_PRESS, 5);
73         assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext)).isTrue();
74     }
75 
76     @Test
longPressBehaviourValueNotSetToAssistant_isAssistEnabledReturnsFalse()77     public void longPressBehaviourValueNotSetToAssistant_isAssistEnabledReturnsFalse() {
78         Settings.Global.putInt(mContext.getContentResolver(),
79                 Settings.Global.POWER_BUTTON_LONG_PRESS, 3);
80         assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext)).isFalse();
81     }
82 
83     @Test
longPressBehaviourDefaultSetToAssistant_isAssistEnabledReturnsFalse()84     public void longPressBehaviourDefaultSetToAssistant_isAssistEnabledReturnsFalse() {
85         when(mResources.getInteger(
86                 com.android.internal.R.integer.config_longPressOnPowerBehavior))
87                 .thenReturn(3);
88 
89         assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext)).isFalse();
90     }
91 }
92