1 /*
2  * Copyright (C) 2016 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 android.provider.Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.content.SharedPreferences;
27 import android.content.pm.PackageManager;
28 import android.hardware.fingerprint.FingerprintManager;
29 import android.provider.Settings;
30 
31 import com.android.settings.dashboard.suggestions.SuggestionFeatureProviderImpl;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Answers;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class SwipeToNotificationPreferenceControllerTest {
44 
45     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
46     private Context mContext;
47     @Mock
48     private PackageManager mPackageManager;
49     @Mock
50     private FingerprintManager mFingerprintManager;
51 
52     private SwipeToNotificationPreferenceController mController;
53     private static final String KEY_SWIPE_DOWN = "gesture_swipe_down_fingerprint";
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58         mController = new SwipeToNotificationPreferenceController(mContext, KEY_SWIPE_DOWN);
59         when(mContext.getPackageManager()).thenReturn(mPackageManager);
60         when(mContext.getSystemService(Context.FINGERPRINT_SERVICE))
61                 .thenReturn(mFingerprintManager);
62     }
63 
64     @Test
isAvailable_hardwareNotAvailable_shouldReturnFalse()65     public void isAvailable_hardwareNotAvailable_shouldReturnFalse() {
66         stubFingerprintSupported(true);
67         when(mFingerprintManager.isHardwareDetected()).thenReturn(false);
68         when(mContext.getResources().
69                 getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
70                 .thenReturn(true);
71 
72         assertThat(mController.isAvailable()).isFalse();
73     }
74 
75     @Test
isAvailable_configIsTrue_shouldReturnTrue()76     public void isAvailable_configIsTrue_shouldReturnTrue() {
77         stubFingerprintSupported(true);
78         when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
79         when(mContext.getResources().
80                 getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
81                 .thenReturn(true);
82 
83         assertThat(mController.isAvailable()).isTrue();
84     }
85 
86     @Test
isAvailable_configIsFalse_shouldReturnFalse()87     public void isAvailable_configIsFalse_shouldReturnFalse() {
88         stubFingerprintSupported(true);
89         when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
90         when(mContext.getResources().
91                 getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
92                 .thenReturn(false);
93 
94         assertThat(mController.isAvailable()).isFalse();
95     }
96 
97     @Test
testIsChecked_configIsSet_shouldReturnTrue()98     public void testIsChecked_configIsSet_shouldReturnTrue() {
99         stubFingerprintSupported(true);
100         when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
101         // Set the setting to be enabled.
102         final Context context = RuntimeEnvironment.application;
103         Settings.Secure.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 1);
104         mController = new SwipeToNotificationPreferenceController(context, KEY_SWIPE_DOWN);
105 
106         assertThat(mController.isChecked()).isTrue();
107     }
108 
109     @Test
testIsChecked_configIsNotSet_shouldReturnFalse()110     public void testIsChecked_configIsNotSet_shouldReturnFalse() {
111         stubFingerprintSupported(true);
112         when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
113         // Set the setting to be disabled.
114         final Context context = RuntimeEnvironment.application;
115         Settings.Secure.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 0);
116         mController = new SwipeToNotificationPreferenceController(context, KEY_SWIPE_DOWN);
117 
118         assertThat(mController.isChecked()).isFalse();
119     }
120 
121     @Test
isSuggestionCompleted_configDisabled_shouldReturnTrue()122     public void isSuggestionCompleted_configDisabled_shouldReturnTrue() {
123         stubFingerprintSupported(true);
124         when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
125         when(mContext.getResources().
126                 getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
127                 .thenReturn(false);
128 
129         assertThat(SwipeToNotificationPreferenceController.isSuggestionComplete(
130                 mContext, null /* prefs */))
131                 .isTrue();
132     }
133 
134     @Test
isSuggestionCompleted_notVisited_shouldReturnFalse()135     public void isSuggestionCompleted_notVisited_shouldReturnFalse() {
136         stubFingerprintSupported(true);
137         when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
138         when(mContext.getResources().
139                 getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
140                 .thenReturn(true);
141         // No stored value in shared preferences if not visited yet.
142         final Context context = RuntimeEnvironment.application;
143         final SharedPreferences prefs = new SuggestionFeatureProviderImpl(context)
144                 .getSharedPrefs(context);
145 
146         assertThat(SwipeToNotificationPreferenceController.isSuggestionComplete(mContext, prefs))
147                 .isFalse();
148     }
149 
150     @Test
isSuggestionCompleted_visited_shouldReturnTrue()151     public void isSuggestionCompleted_visited_shouldReturnTrue() {
152         stubFingerprintSupported(true);
153         when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
154         when(mContext.getResources().
155                 getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
156                 .thenReturn(true);
157         // No stored value in shared preferences if not visited yet.
158         final Context context = RuntimeEnvironment.application;
159         final SharedPreferences prefs = new SuggestionFeatureProviderImpl(context)
160                 .getSharedPrefs(context);
161         prefs.edit()
162                 .putBoolean(SwipeToNotificationSettings.PREF_KEY_SUGGESTION_COMPLETE, true)
163                 .commit();
164 
165         assertThat(SwipeToNotificationPreferenceController.isSuggestionComplete(mContext, prefs))
166                 .isTrue();
167     }
168 
stubFingerprintSupported(boolean enabled)169     private void stubFingerprintSupported(boolean enabled) {
170         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT))
171                 .thenReturn(enabled);
172     }
173 
174     @Test
isSliceableCorrectKey_returnsTrue()175     public void isSliceableCorrectKey_returnsTrue() {
176         final SwipeToNotificationPreferenceController controller = new
177                 SwipeToNotificationPreferenceController(mContext,"gesture_swipe_down_fingerprint");
178         assertThat(controller.isSliceable()).isTrue();
179     }
180 
181     @Test
isSliceableIncorrectKey_returnsFalse()182     public void isSliceableIncorrectKey_returnsFalse() {
183         final SwipeToNotificationPreferenceController controller =
184                 new SwipeToNotificationPreferenceController(mContext, "bad_key");
185         assertThat(controller.isSliceable()).isFalse();
186     }
187 
188     @Test
isPublicSlice_returnTrue()189     public void isPublicSlice_returnTrue() {
190         assertThat(mController.isPublicSlice()).isTrue();
191     }
192 }
193