• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.android.settings.gestures.OneHandedSettingsUtils.ONE_HANDED_MODE_TARGET_NAME;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.content.Context;
24 import android.os.UserHandle;
25 import android.provider.Settings;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.robolectric.RobolectricTestRunner;
31 import org.robolectric.RuntimeEnvironment;
32 
33 @RunWith(RobolectricTestRunner.class)
34 public class OneHandedSettingsUtilsTest {
35     private static final int OFF = 0;
36     private static final int ON = 1;
37 
38     private Context mContext;
39 
40     private int mCurrentUserId;
41 
42     @Before
setUp()43     public void setUp() {
44         mContext = RuntimeEnvironment.application;
45         mCurrentUserId = UserHandle.myUserId();
46         OneHandedSettingsUtils.setUserId(mCurrentUserId);
47     }
48 
49     @Test
setOneHandedModeEnabled_setEnable_shouldReturnEnabled()50     public void setOneHandedModeEnabled_setEnable_shouldReturnEnabled() {
51         OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, true);
52 
53         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
54                 Settings.Secure.ONE_HANDED_MODE_ENABLED, OFF, mCurrentUserId)).isEqualTo(ON);
55     }
56 
57     @Test
setOneHandedModeEnabled_setDisable_shouldReturnDisabled()58     public void setOneHandedModeEnabled_setDisable_shouldReturnDisabled() {
59         OneHandedSettingsUtils.setOneHandedModeEnabled(mContext, false);
60 
61         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
62                 Settings.Secure.ONE_HANDED_MODE_ENABLED, OFF, mCurrentUserId)).isEqualTo(OFF);
63     }
64 
65     @Test
setTapsAppToExitEnabled_setEnable_shouldReturnEnabled()66     public void setTapsAppToExitEnabled_setEnable_shouldReturnEnabled() {
67         OneHandedSettingsUtils.setTapsAppToExitEnabled(mContext, true);
68 
69         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
70                 Settings.Secure.TAPS_APP_TO_EXIT, OFF, mCurrentUserId)).isEqualTo(ON);
71     }
72 
73     @Test
setTapsAppToExitEnabled_setDisable_shouldReturnDisabled()74     public void setTapsAppToExitEnabled_setDisable_shouldReturnDisabled() {
75         OneHandedSettingsUtils.setTapsAppToExitEnabled(mContext, false);
76 
77         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
78                 Settings.Secure.TAPS_APP_TO_EXIT, OFF, mCurrentUserId)).isEqualTo(OFF);
79     }
80 
81     @Test
setTimeout_setNever_shouldReturnNeverValue()82     public void setTimeout_setNever_shouldReturnNeverValue() {
83         OneHandedSettingsUtils.setTimeoutValue(mContext,
84                 OneHandedSettingsUtils.OneHandedTimeout.NEVER.getValue());
85 
86         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
87                 Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
88                 OneHandedSettingsUtils.OneHandedTimeout.NEVER.getValue(), mCurrentUserId))
89                 .isEqualTo(0);
90     }
91 
92     @Test
setTimeout_setShort_shouldReturnShortValue()93     public void setTimeout_setShort_shouldReturnShortValue() {
94         OneHandedSettingsUtils.setTimeoutValue(mContext,
95                 OneHandedSettingsUtils.OneHandedTimeout.SHORT.getValue());
96 
97         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
98                 Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
99                 OneHandedSettingsUtils.OneHandedTimeout.SHORT.getValue(), mCurrentUserId))
100                 .isEqualTo(4);
101     }
102 
103     @Test
setTimeout_setMedium_shouldReturnMediumValue()104     public void setTimeout_setMedium_shouldReturnMediumValue() {
105         OneHandedSettingsUtils.setTimeoutValue(mContext,
106                 OneHandedSettingsUtils.OneHandedTimeout.MEDIUM.getValue());
107 
108         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
109                 Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
110                 OneHandedSettingsUtils.OneHandedTimeout.MEDIUM.getValue(), mCurrentUserId))
111                 .isEqualTo(8);
112     }
113 
114     @Test
setTimeout_setLong_shouldReturnLongValue()115     public void setTimeout_setLong_shouldReturnLongValue() {
116         OneHandedSettingsUtils.setTimeoutValue(mContext,
117                 OneHandedSettingsUtils.OneHandedTimeout.LONG.getValue());
118 
119         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
120                 Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
121                 OneHandedSettingsUtils.OneHandedTimeout.LONG.getValue(), mCurrentUserId))
122                 .isEqualTo(12);
123     }
124 
125     @Test
getShortcutEnabled_a11yButtonVolumeKeysShortcutEnabled_returnTrue()126     public void getShortcutEnabled_a11yButtonVolumeKeysShortcutEnabled_returnTrue() {
127         setupShortcuts(
128                 /* enableFab= */ true, /* enableVolumeKeys= */ true, /* enableQs=*/ false);
129 
130         assertThat(OneHandedSettingsUtils.getShortcutEnabled(mContext)).isTrue();
131     }
132 
133     @Test
getShortcutEnabled_a11yButtonShortcutEnabled_returnTrue()134     public void getShortcutEnabled_a11yButtonShortcutEnabled_returnTrue() {
135         setupShortcuts(
136                 /* enableFab= */ true, /* enableVolumeKeys= */ false, /* enableQs=*/ false);
137 
138         assertThat(OneHandedSettingsUtils.getShortcutEnabled(mContext)).isTrue();
139     }
140 
141     @Test
getShortcutEnabled_volumeKeysShortcutEnabled_returnTrue()142     public void getShortcutEnabled_volumeKeysShortcutEnabled_returnTrue() {
143         setupShortcuts(
144                 /* enableFab= */ false, /* enableVolumeKeys= */ true, /* enableQs=*/ false);
145 
146         assertThat(OneHandedSettingsUtils.getShortcutEnabled(mContext)).isTrue();
147     }
148 
149     @Test
getShortcutEnabled_noShortcutsEnabled_returnFalse()150     public void getShortcutEnabled_noShortcutsEnabled_returnFalse() {
151         setupShortcuts(
152                 /* enableFab= */ false, /* enableVolumeKeys= */ false, /* enableQs=*/ false);
153 
154         assertThat(OneHandedSettingsUtils.getShortcutEnabled(mContext)).isFalse();
155     }
156 
157     @Test
getShortcutEnabled_qsShortcutEnabled_returnTrue()158     public void getShortcutEnabled_qsShortcutEnabled_returnTrue() {
159         setupShortcuts(
160                 /* enableFab= */ false, /* enableVolumeKeys= */ false, /* enableQs=*/ true);
161 
162         assertThat(OneHandedSettingsUtils.getShortcutEnabled(mContext)).isTrue();
163     }
164 
setupShortcuts(boolean enableFab, boolean enableVolumeKeys, boolean enableQs)165     private void setupShortcuts(boolean enableFab, boolean enableVolumeKeys, boolean enableQs) {
166         setupShortcut(Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS, enableFab);
167         setupShortcut(Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE, enableVolumeKeys);
168         setupShortcut(Settings.Secure.ACCESSIBILITY_QS_TARGETS, enableQs);
169     }
170 
setupShortcut(String shortcutSettingKey, boolean enabled)171     private void setupShortcut(String shortcutSettingKey, boolean enabled) {
172         final String targetName = enabled ? ONE_HANDED_MODE_TARGET_NAME : "";
173         Settings.Secure.putStringForUser(
174                 mContext.getContentResolver(), shortcutSettingKey, targetName, mCurrentUserId);
175     }
176 }
177