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.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED; 20 21 import static com.android.settings.gestures.DoubleTapPowerPreferenceController.OFF; 22 import static com.android.settings.gestures.DoubleTapPowerPreferenceController.ON; 23 import static com.android.settings.gestures.DoubleTapPowerPreferenceController.isSuggestionComplete; 24 25 import static com.google.common.truth.Truth.assertThat; 26 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.settings.dashboard.suggestions.SuggestionFeatureProviderImpl; 35 import com.android.settings.display.AmbientDisplayAlwaysOnPreferenceController; 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 import com.android.settings.testutils.shadow.SettingsShadowResources; 41 import com.android.settings.testutils.shadow.ShadowSecureSettings; 42 43 import org.junit.After; 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.robolectric.RuntimeEnvironment; 48 import org.robolectric.annotation.Config; 49 50 @RunWith(SettingsRobolectricTestRunner.class) 51 @Config(shadows = SettingsShadowResources.class) 52 public class DoubleTapPowerPreferenceControllerTest { 53 54 private Context mContext; 55 private ContentResolver mContentResolver; 56 private DoubleTapPowerPreferenceController mController; 57 private static final String KEY_DOUBLE_TAP_POWER = "gesture_double_tap_power"; 58 59 @Before setUp()60 public void setUp() { 61 mContext = RuntimeEnvironment.application; 62 mContentResolver = mContext.getContentResolver(); 63 mController = new DoubleTapPowerPreferenceController(mContext, KEY_DOUBLE_TAP_POWER); 64 } 65 66 @After tearDown()67 public void tearDown() { 68 SettingsShadowResources.reset(); 69 } 70 71 @Test isAvailable_configIsTrue_shouldReturnTrue()72 public void isAvailable_configIsTrue_shouldReturnTrue() { 73 SettingsShadowResources.overrideResource( 74 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled, 75 Boolean.TRUE); 76 77 assertThat(mController.isAvailable()).isTrue(); 78 } 79 80 @Test isAvailable_configIsTrue_shouldReturnFalse()81 public void isAvailable_configIsTrue_shouldReturnFalse() { 82 SettingsShadowResources.overrideResource( 83 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled, 84 Boolean.FALSE); 85 86 assertThat(mController.isAvailable()).isFalse(); 87 } 88 89 @Test testIsChecked_configIsNotSet_shouldReturnTrue()90 public void testIsChecked_configIsNotSet_shouldReturnTrue() { 91 // Set the setting to be enabled. 92 Settings.System.putInt(mContentResolver, CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, ON); 93 mController = new DoubleTapPowerPreferenceController(mContext, KEY_DOUBLE_TAP_POWER); 94 95 assertThat(mController.isChecked()).isTrue(); 96 } 97 98 @Test testIsChecked_configIsSet_shouldReturnFalse()99 public void testIsChecked_configIsSet_shouldReturnFalse() { 100 // Set the setting to be disabled. 101 Settings.System.putInt(mContentResolver, CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, OFF); 102 mController = new DoubleTapPowerPreferenceController(mContext, KEY_DOUBLE_TAP_POWER); 103 104 assertThat(mController.isChecked()).isFalse(); 105 } 106 107 @Test testPreferenceController_ProperResultPayloadType()108 public void testPreferenceController_ProperResultPayloadType() { 109 DoubleTapPowerPreferenceController controller = 110 new DoubleTapPowerPreferenceController(mContext, KEY_DOUBLE_TAP_POWER); 111 ResultPayload payload = controller.getResultPayload(); 112 assertThat(payload).isInstanceOf(InlineSwitchPayload.class); 113 } 114 115 @Test 116 @Config(shadows = ShadowSecureSettings.class) testSetValue_updatesCorrectly()117 public void testSetValue_updatesCorrectly() { 118 int newValue = 1; 119 Settings.Secure.putInt(mContentResolver, 120 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0); 121 122 InlinePayload payload = ((InlineSwitchPayload) mController.getResultPayload()); 123 payload.setValue(mContext, newValue); 124 int updatedValue = Settings.Secure.getInt(mContentResolver, 125 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, -1); 126 updatedValue = 1 - updatedValue; // DoubleTapPower is a non-standard switch 127 128 assertThat(updatedValue).isEqualTo(newValue); 129 } 130 131 @Test 132 @Config(shadows = ShadowSecureSettings.class) testGetValue_correctValueReturned()133 public void testGetValue_correctValueReturned() { 134 int currentValue = 1; 135 Settings.Secure.putInt(mContentResolver, 136 Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, currentValue); 137 138 int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext); 139 newValue = 1 - newValue; // DoubleTapPower is a non-standard switch 140 assertThat(newValue).isEqualTo(currentValue); 141 } 142 143 @Test isSuggestionCompleted_doubleTapPower_trueWhenNotAvailable()144 public void isSuggestionCompleted_doubleTapPower_trueWhenNotAvailable() { 145 SettingsShadowResources.overrideResource( 146 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled, false); 147 148 assertThat(isSuggestionComplete(mContext, null/* prefs */)).isTrue(); 149 } 150 151 @Test isSuggestionCompleted_doubleTapPower_falseWhenNotVisited()152 public void isSuggestionCompleted_doubleTapPower_falseWhenNotVisited() { 153 SettingsShadowResources.overrideResource( 154 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled, true); 155 // No stored value in shared preferences if not visited yet. 156 final SharedPreferences prefs = 157 new SuggestionFeatureProviderImpl(mContext).getSharedPrefs(mContext); 158 assertThat(isSuggestionComplete(mContext, prefs)).isFalse(); 159 } 160 161 @Test isSuggestionCompleted_doubleTapPower_trueWhenVisited()162 public void isSuggestionCompleted_doubleTapPower_trueWhenVisited() { 163 SettingsShadowResources.overrideResource( 164 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled, true); 165 // No stored value in shared preferences if not visited yet. 166 final SharedPreferences prefs = 167 new SuggestionFeatureProviderImpl(mContext).getSharedPrefs(mContext); 168 prefs.edit().putBoolean(DoubleTapPowerSettings.PREF_KEY_SUGGESTION_COMPLETE, true).commit(); 169 170 assertThat(isSuggestionComplete(mContext, prefs)).isTrue(); 171 } 172 173 @Test isSliceableCorrectKey_returnsTrue()174 public void isSliceableCorrectKey_returnsTrue() { 175 final DoubleTapPowerPreferenceController controller = 176 new DoubleTapPowerPreferenceController(mContext, "gesture_double_tap_power"); 177 assertThat(controller.isSliceable()).isTrue(); 178 } 179 180 @Test isSliceableIncorrectKey_returnsFalse()181 public void isSliceableIncorrectKey_returnsFalse() { 182 final DoubleTapPowerPreferenceController controller = 183 new DoubleTapPowerPreferenceController(mContext, "bad_key"); 184 assertThat(controller.isSliceable()).isFalse(); 185 } 186 } 187