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