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 com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.Mockito.doReturn; 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.when; 28 29 import android.app.admin.DevicePolicyManager; 30 import android.content.Context; 31 import android.content.SharedPreferences; 32 import android.hardware.display.AmbientDisplayConfiguration; 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 DoubleTapScreenPreferenceControllerTest { 47 48 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 49 private Context mContext; 50 @Mock 51 private AmbientDisplayConfiguration mAmbientDisplayConfiguration; 52 private DoubleTapScreenPreferenceController mController; 53 54 private static final String KEY_DOUBLE_TAP_SCREEN = "gesture_double_tap_screen"; 55 56 @Before setUp()57 public void setUp() { 58 MockitoAnnotations.initMocks(this); 59 doReturn(mock(DevicePolicyManager.class)).when(mContext) 60 .getSystemService(Context.DEVICE_POLICY_SERVICE); 61 mController = new DoubleTapScreenPreferenceController(mContext, KEY_DOUBLE_TAP_SCREEN); 62 mController.setConfig(mAmbientDisplayConfiguration); 63 } 64 65 @Test testIsChecked_configIsSet_shouldReturnTrue()66 public void testIsChecked_configIsSet_shouldReturnTrue() { 67 // Set the setting to be enabled. 68 when(mAmbientDisplayConfiguration.doubleTapGestureEnabled(anyInt())).thenReturn(true); 69 70 assertThat(mController.isChecked()).isTrue(); 71 } 72 73 @Test testIsChecked_configIsNotSet_shouldReturnFalse()74 public void testIsChecked_configIsNotSet_shouldReturnFalse() { 75 when(mAmbientDisplayConfiguration.doubleTapGestureEnabled(anyInt())).thenReturn(false); 76 77 assertThat(mController.isChecked()).isFalse(); 78 } 79 80 @Test isSuggestionCompleted_ambientDisplay_falseWhenNotVisited()81 public void isSuggestionCompleted_ambientDisplay_falseWhenNotVisited() { 82 when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(true); 83 // No stored value in shared preferences if not visited yet. 84 final Context context = RuntimeEnvironment.application; 85 final SharedPreferences prefs = 86 new SuggestionFeatureProviderImpl(context).getSharedPrefs(context); 87 88 assertThat(DoubleTapScreenPreferenceController 89 .isSuggestionComplete(mAmbientDisplayConfiguration, prefs)).isFalse(); 90 } 91 92 @Test isSuggestionCompleted_ambientDisplay_trueWhenVisited()93 public void isSuggestionCompleted_ambientDisplay_trueWhenVisited() { 94 when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(false); 95 final Context context = RuntimeEnvironment.application; 96 final SharedPreferences prefs = 97 new SuggestionFeatureProviderImpl(context).getSharedPrefs(context); 98 99 prefs.edit().putBoolean( 100 DoubleTapScreenSettings.PREF_KEY_SUGGESTION_COMPLETE, true).commit(); 101 102 assertThat(DoubleTapScreenPreferenceController 103 .isSuggestionComplete(mAmbientDisplayConfiguration, prefs)).isTrue(); 104 } 105 106 @Test getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE()107 public void getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE() { 108 when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(false); 109 when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(false); 110 final int availabilityStatus = mController.getAvailabilityStatus(); 111 112 assertThat(availabilityStatus).isEqualTo(UNSUPPORTED_ON_DEVICE); 113 } 114 115 @Test getAvailabilityStatus_aodSupported_aodOff_AVAILABLE()116 public void getAvailabilityStatus_aodSupported_aodOff_AVAILABLE() { 117 when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(true); 118 when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(true); 119 final int availabilityStatus = mController.getAvailabilityStatus(); 120 121 assertThat(availabilityStatus).isEqualTo(AVAILABLE); 122 } 123 124 @Test isSliceableCorrectKey_returnsTrue()125 public void isSliceableCorrectKey_returnsTrue() { 126 final DoubleTapScreenPreferenceController controller = 127 new DoubleTapScreenPreferenceController(mContext, "gesture_double_tap_screen"); 128 assertThat(controller.isSliceable()).isTrue(); 129 } 130 131 @Test isSliceableIncorrectKey_returnsFalse()132 public void isSliceableIncorrectKey_returnsFalse() { 133 final DoubleTapScreenPreferenceController controller = 134 new DoubleTapScreenPreferenceController(mContext, "bad_key"); 135 assertThat(controller.isSliceable()).isFalse(); 136 } 137 138 @Test isPublicSlice_returnTrue()139 public void isPublicSlice_returnTrue() { 140 assertThat(mController.isPublicSlice()).isTrue(); 141 } 142 } 143