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