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.DISABLED_DEPENDENT_SETTING; 21 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 22 23 import static com.google.common.truth.Truth.assertThat; 24 import static org.mockito.Matchers.anyInt; 25 import static org.mockito.Mockito.when; 26 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.content.SharedPreferences; 30 import android.provider.Settings; 31 32 import com.android.internal.hardware.AmbientDisplayConfiguration; 33 import com.android.settings.dashboard.suggestions.SuggestionFeatureProviderImpl; 34 import com.android.settings.search.InlinePayload; 35 import com.android.settings.search.InlineSwitchPayload; 36 import com.android.settings.search.ResultPayload; 37 import com.android.settings.testutils.SettingsRobolectricTestRunner; 38 import com.android.settings.testutils.shadow.ShadowSecureSettings; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Answers; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.RuntimeEnvironment; 47 import org.robolectric.annotation.Config; 48 49 @RunWith(SettingsRobolectricTestRunner.class) 50 public class DoubleTapScreenPreferenceControllerTest { 51 52 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 53 private Context mContext; 54 @Mock 55 private AmbientDisplayConfiguration mAmbientDisplayConfiguration; 56 private DoubleTapScreenPreferenceController mController; 57 58 private static final String KEY_DOUBLE_TAP_SCREEN = "gesture_double_tap_screen"; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 mController = new DoubleTapScreenPreferenceController(mContext, KEY_DOUBLE_TAP_SCREEN); 64 mController.setConfig(mAmbientDisplayConfiguration); 65 } 66 67 @Test testIsChecked_configIsSet_shouldReturnTrue()68 public void testIsChecked_configIsSet_shouldReturnTrue() { 69 // Set the setting to be enabled. 70 when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(true); 71 72 assertThat(mController.isChecked()).isTrue(); 73 } 74 75 @Test testIsChecked_configIsNotSet_shouldReturnFalse()76 public void testIsChecked_configIsNotSet_shouldReturnFalse() { 77 when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(false); 78 79 assertThat(mController.isChecked()).isFalse(); 80 } 81 82 @Test testPreferenceController_ProperResultPayloadType()83 public void testPreferenceController_ProperResultPayloadType() { 84 final Context context = RuntimeEnvironment.application; 85 DoubleTapScreenPreferenceController controller = 86 new DoubleTapScreenPreferenceController(context, KEY_DOUBLE_TAP_SCREEN); 87 controller.setConfig(mAmbientDisplayConfiguration); 88 ResultPayload payload = controller.getResultPayload(); 89 assertThat(payload).isInstanceOf(InlineSwitchPayload.class); 90 } 91 92 @Test 93 @Config(shadows = ShadowSecureSettings.class) testSetValue_updatesCorrectly()94 public void testSetValue_updatesCorrectly() { 95 int newValue = 1; 96 ContentResolver resolver = mContext.getContentResolver(); 97 Settings.Secure.putInt(resolver, Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, 0); 98 99 ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue); 100 int updatedValue = Settings.Secure.getInt(resolver, 101 Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, -1); 102 103 assertThat(updatedValue).isEqualTo(newValue); 104 } 105 106 @Test 107 @Config(shadows = ShadowSecureSettings.class) testGetValue_correctValueReturned()108 public void testGetValue_correctValueReturned() { 109 int currentValue = 1; 110 Settings.Secure.putInt(mContext.getContentResolver(), 111 Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, currentValue); 112 113 int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext); 114 115 assertThat(newValue).isEqualTo(currentValue); 116 } 117 118 @Test isSuggestionCompleted_ambientDisplay_falseWhenNotVisited()119 public void isSuggestionCompleted_ambientDisplay_falseWhenNotVisited() { 120 when(mAmbientDisplayConfiguration.pulseOnDoubleTapAvailable()).thenReturn(true); 121 // No stored value in shared preferences if not visited yet. 122 final Context context = RuntimeEnvironment.application; 123 final SharedPreferences prefs = 124 new SuggestionFeatureProviderImpl(context).getSharedPrefs(context); 125 126 assertThat(DoubleTapScreenPreferenceController 127 .isSuggestionComplete(mAmbientDisplayConfiguration, prefs)).isFalse(); 128 } 129 130 @Test isSuggestionCompleted_ambientDisplay_trueWhenVisited()131 public void isSuggestionCompleted_ambientDisplay_trueWhenVisited() { 132 when(mAmbientDisplayConfiguration.pulseOnDoubleTapAvailable()).thenReturn(false); 133 final Context context = RuntimeEnvironment.application; 134 final SharedPreferences prefs = 135 new SuggestionFeatureProviderImpl(context).getSharedPrefs(context); 136 137 prefs.edit().putBoolean( 138 DoubleTapScreenSettings.PREF_KEY_SUGGESTION_COMPLETE, true).commit(); 139 140 assertThat(DoubleTapScreenPreferenceController 141 .isSuggestionComplete(mAmbientDisplayConfiguration, prefs)).isTrue(); 142 } 143 144 @Test canHandleClicks_falseWhenAlwaysOnEnabled()145 public void canHandleClicks_falseWhenAlwaysOnEnabled() { 146 when(mAmbientDisplayConfiguration.alwaysOnEnabled(anyInt())).thenReturn(true); 147 assertThat(mController.canHandleClicks()).isFalse(); 148 } 149 150 @Test canHandleClicks_trueWhenAlwaysOnDisabled()151 public void canHandleClicks_trueWhenAlwaysOnDisabled() { 152 when(mAmbientDisplayConfiguration.alwaysOnEnabled(anyInt())).thenReturn(false); 153 assertThat(mController.canHandleClicks()).isTrue(); 154 } 155 156 @Test getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE()157 public void getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE() { 158 when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(false); 159 when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(false); 160 final int availabilityStatus = mController.getAvailabilityStatus(); 161 162 assertThat(availabilityStatus).isEqualTo(UNSUPPORTED_ON_DEVICE); 163 } 164 165 @Test getAvailabilityStatus_aodOn_DISABLED_DEPENDENT_SETTING()166 public void getAvailabilityStatus_aodOn_DISABLED_DEPENDENT_SETTING() { 167 when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(true); 168 when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(false); 169 final int availabilityStatus = mController.getAvailabilityStatus(); 170 171 assertThat(availabilityStatus).isEqualTo(DISABLED_DEPENDENT_SETTING); 172 } 173 174 @Test getAvailabilityStatus_aodSupported_aodOff_AVAILABLE()175 public void getAvailabilityStatus_aodSupported_aodOff_AVAILABLE() { 176 when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(true); 177 when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(true); 178 final int availabilityStatus = mController.getAvailabilityStatus(); 179 180 assertThat(availabilityStatus).isEqualTo(AVAILABLE); 181 } 182 183 @Test isSliceableCorrectKey_returnsTrue()184 public void isSliceableCorrectKey_returnsTrue() { 185 final DoubleTapScreenPreferenceController controller = 186 new DoubleTapScreenPreferenceController(mContext,"gesture_double_tap_screen"); 187 assertThat(controller.isSliceable()).isTrue(); 188 } 189 190 @Test isSliceableIncorrectKey_returnsFalse()191 public void isSliceableIncorrectKey_returnsFalse() { 192 final DoubleTapScreenPreferenceController controller = 193 new DoubleTapScreenPreferenceController(mContext, "bad_key"); 194 assertThat(controller.isSliceable()).isFalse(); 195 } 196 } 197