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