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 android.content.Context;
22 import android.os.SystemProperties;
23 import android.os.UserHandle;
24 
25 import com.android.settings.core.BasePreferenceController;
26 import com.android.settingslib.widget.SelectorWithWidgetPreference;
27 
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.robolectric.RobolectricTestRunner;
33 import org.robolectric.RuntimeEnvironment;
34 
35 @RunWith(RobolectricTestRunner.class)
36 public class OneHandedActionShowNotificationPrefControllerTest {
37 
38     private static final String KEY = "gesture_one_handed_action_show_notification";
39 
40     private Context mContext;
41     private OneHandedSettingsUtils mUtils;
42     private OneHandedActionShowNotificationPrefController mController;
43     private SelectorWithWidgetPreference mPreference;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = RuntimeEnvironment.application;
48         mUtils = new OneHandedSettingsUtils(mContext);
49         mController = new OneHandedActionShowNotificationPrefController(mContext, KEY);
50         mPreference = new SelectorWithWidgetPreference(mContext);
51         OneHandedSettingsUtils.setUserId(UserHandle.myUserId());
52     }
53 
54     @Test
updateState_setGesturalMode_shouldEnabled()55     public void updateState_setGesturalMode_shouldEnabled() {
56         OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, true);
57         mUtils.setNavigationBarMode(mContext, "2" /* fully gestural */);
58 
59         mController.updateState(mPreference);
60         assertThat(mPreference.isEnabled()).isTrue();
61     }
62 
63     @Test
updateState_ShowNotificationEnabled_shouldChecked()64     public void updateState_ShowNotificationEnabled_shouldChecked() {
65         OneHandedSettingsUtils.setSwipeDownNotificationEnabled(mContext, true);
66 
67         mController.updateState(mPreference);
68         assertThat(mPreference.isChecked()).isTrue();
69     }
70 
71     @Test
updateState_showNotificationDisabled_shouldUnchecked()72     public void updateState_showNotificationDisabled_shouldUnchecked() {
73         OneHandedSettingsUtils.setSwipeDownNotificationEnabled(mContext, false);
74 
75         mController.updateState(mPreference);
76         assertThat(mPreference.isChecked()).isFalse();
77     }
78 
79     @Test
getAvailabilityStatus_setOneHandedModeDisabled_shouldDisabled()80     public void getAvailabilityStatus_setOneHandedModeDisabled_shouldDisabled() {
81         SystemProperties.set(OneHandedSettingsUtils.SUPPORT_ONE_HANDED_MODE, "true");
82         OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, false);
83         mUtils.setNavigationBarMode(mContext, "2" /* fully gestural */);
84 
85         assertThat(mController.getAvailabilityStatus())
86                 .isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING);
87     }
88 
89     @Ignore("b/313541907")
90     @Test
getAvailabilityStatus_setNavi3ButtonMode_shouldDisabled()91     public void getAvailabilityStatus_setNavi3ButtonMode_shouldDisabled() {
92         SystemProperties.set(OneHandedSettingsUtils.SUPPORT_ONE_HANDED_MODE, "true");
93         OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, true);
94         mUtils.setNavigationBarMode(mContext, "0" /* 3 button */);
95 
96         assertThat(mController.getAvailabilityStatus())
97                 .isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING);
98     }
99 
100     @Test
getAvailabilityStatus_unsetSupportOneHandedModeProperty_shouldDisabled()101     public void getAvailabilityStatus_unsetSupportOneHandedModeProperty_shouldDisabled() {
102         SystemProperties.set(OneHandedSettingsUtils.SUPPORT_ONE_HANDED_MODE, "false");
103         OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, true);
104         mUtils.setNavigationBarMode(mContext, "2" /* fully gestural */);
105 
106         assertThat(mController.getAvailabilityStatus())
107                 .isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING);
108     }
109 
110     @Test
getAvailabilityStatus_setShortcutEnabled_shouldEnabled()111     public void getAvailabilityStatus_setShortcutEnabled_shouldEnabled() {
112         SystemProperties.set(OneHandedSettingsUtils.SUPPORT_ONE_HANDED_MODE, "true");
113         OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, false);
114         mUtils.setNavigationBarMode(mContext, "0" /* 3-button mode */);
115         mUtils.setShortcutEnabled(mContext, true);
116 
117         assertThat(mController.getAvailabilityStatus())
118                 .isEqualTo(BasePreferenceController.AVAILABLE);
119     }
120 
121     @Test
getAvailabilityStatus_setShortcutDisabled_shouldDisabled()122     public void getAvailabilityStatus_setShortcutDisabled_shouldDisabled() {
123         SystemProperties.set(OneHandedSettingsUtils.SUPPORT_ONE_HANDED_MODE, "true");
124         OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, false);
125         mUtils.setNavigationBarMode(mContext, "0" /* 3-button mode */);
126         mUtils.setShortcutEnabled(mContext, false);
127 
128         assertThat(mController.getAvailabilityStatus())
129                 .isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING);
130     }
131 }
132