1 /* 2 * Copyright (C) 2018 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.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.when; 22 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.pm.ResolveInfo; 27 import android.os.UserManager; 28 import android.provider.Settings; 29 30 import com.android.internal.R; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 import com.android.settings.testutils.shadow.SettingsShadowResources; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.Shadows; 42 import org.robolectric.annotation.Config; 43 import org.robolectric.shadows.ShadowPackageManager; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 @Config(shadows = SettingsShadowResources.class) 47 public class SwipeUpPreferenceControllerTest { 48 49 private Context mContext; 50 private ShadowPackageManager mPackageManager; 51 private SwipeUpPreferenceController mController; 52 53 private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE"; 54 private static final String KEY_SWIPE_UP = "gesture_swipe_up"; 55 56 @Before setUp()57 public void setUp() { 58 SettingsShadowResources.overrideResource(R.bool.config_swipe_up_gesture_setting_available, 59 true); 60 SettingsShadowResources.overrideResource(R.bool.config_swipe_up_gesture_default, true); 61 62 mContext = RuntimeEnvironment.application; 63 mPackageManager = Shadows.shadowOf(mContext.getPackageManager()); 64 mController = new SwipeUpPreferenceController(mContext, KEY_SWIPE_UP); 65 } 66 67 @After tearDown()68 public void tearDown() { 69 SettingsShadowResources.reset(); 70 } 71 72 @Test testIsGestureAvailable_matchingServiceExists_shouldReturnTrue()73 public void testIsGestureAvailable_matchingServiceExists_shouldReturnTrue() { 74 final ComponentName recentsComponentName = ComponentName.unflattenFromString( 75 mContext.getString(com.android.internal.R.string.config_recentsComponentName)); 76 final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP) 77 .setPackage(recentsComponentName.getPackageName()); 78 mPackageManager.addResolveInfoForIntent(quickStepIntent, new ResolveInfo()); 79 80 assertThat(SwipeUpPreferenceController.isGestureAvailable(mContext)).isTrue(); 81 } 82 83 @Test testIsGestureAvailable_overlayDisabled_matchingServiceExists_shouldReturnFalse()84 public void testIsGestureAvailable_overlayDisabled_matchingServiceExists_shouldReturnFalse() { 85 SettingsShadowResources.overrideResource(R.bool.config_swipe_up_gesture_setting_available, 86 false); 87 88 final ComponentName recentsComponentName = ComponentName.unflattenFromString( 89 mContext.getString(com.android.internal.R.string.config_recentsComponentName)); 90 final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP) 91 .setPackage(recentsComponentName.getPackageName()); 92 mPackageManager.addResolveInfoForIntent(quickStepIntent, new ResolveInfo()); 93 94 assertThat(SwipeUpPreferenceController.isGestureAvailable(mContext)).isFalse(); 95 } 96 97 @Test testIsGestureAvailable_noMatchingServiceExists_shouldReturnFalse()98 public void testIsGestureAvailable_noMatchingServiceExists_shouldReturnFalse() { 99 assertThat(SwipeUpPreferenceController.isGestureAvailable(mContext)).isFalse(); 100 } 101 102 @Test testIsChecked_defaultIsTrue_shouldReturnTrue()103 public void testIsChecked_defaultIsTrue_shouldReturnTrue() { 104 assertThat(mController.isChecked()).isTrue(); 105 } 106 107 @Test testIsChecked_defaultIsFalse_shouldReturnFalse()108 public void testIsChecked_defaultIsFalse_shouldReturnFalse() { 109 SettingsShadowResources.overrideResource(R.bool.config_swipe_up_gesture_default, false); 110 assertThat(mController.isChecked()).isFalse(); 111 } 112 113 @Test testIsChecked_setCheckedTrue_shouldReturnTrue()114 public void testIsChecked_setCheckedTrue_shouldReturnTrue() { 115 // Set the setting to be enabled. 116 mController.setChecked(true); 117 assertThat(mController.isChecked()).isTrue(); 118 } 119 120 @Test testIsChecked_setCheckedFalse_shouldReturnFalse()121 public void testIsChecked_setCheckedFalse_shouldReturnFalse() { 122 // Set the setting to be disabled. 123 mController.setChecked(false); 124 assertThat(mController.isChecked()).isFalse(); 125 } 126 127 @Test isSliceableCorrectKey_returnsTrue()128 public void isSliceableCorrectKey_returnsTrue() { 129 final SwipeUpPreferenceController controller = 130 new SwipeUpPreferenceController(mContext,"gesture_swipe_up"); 131 assertThat(controller.isSliceable()).isTrue(); 132 } 133 134 @Test isSliceableIncorrectKey_returnsFalse()135 public void isSliceableIncorrectKey_returnsFalse() { 136 final SwipeUpPreferenceController controller = 137 new SwipeUpPreferenceController(mContext, "bad_key"); 138 assertThat(controller.isSliceable()).isFalse(); 139 } 140 } 141