1 /* 2 * Copyright (C) 2021 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 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ApplicationInfo; 24 import android.content.pm.ResolveInfo; 25 import android.content.pm.ServiceInfo; 26 import android.provider.Settings; 27 28 import androidx.test.core.app.ApplicationProvider; 29 30 import com.android.internal.R; 31 import com.android.settings.testutils.shadow.SettingsShadowResources; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.Shadows; 38 import org.robolectric.annotation.Config; 39 import org.robolectric.shadows.ShadowPackageManager; 40 41 @RunWith(RobolectricTestRunner.class) 42 @Config(shadows = SettingsShadowResources.class) 43 public class GestureNavigationSettingsAssistControllerTest { 44 45 private static final String KEY_SWIPE_FOR_ASSIST = "assistant_gesture_corner_swipe"; 46 private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE"; 47 private GestureNavigationSettingsAssistController mController; 48 49 private Context mContext; 50 51 @Before setUp()52 public void setUp() { 53 mContext = ApplicationProvider.getApplicationContext(); 54 55 // This sets up SystemNavigationPreferenceController.isGestureAvailable() so it returns true 56 SettingsShadowResources.overrideResource(R.bool.config_swipe_up_gesture_setting_available, 57 true); 58 final String recentsComponentPackageName = "recents.component"; 59 SettingsShadowResources.overrideResource(R.string.config_recentsComponentName, 60 recentsComponentPackageName + "/.ComponentName"); 61 final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP) 62 .setPackage(recentsComponentPackageName); 63 final ResolveInfo info = new ResolveInfo(); 64 info.serviceInfo = new ServiceInfo(); 65 info.resolvePackageName = recentsComponentPackageName; 66 info.serviceInfo.packageName = info.resolvePackageName; 67 info.serviceInfo.name = recentsComponentPackageName; 68 info.serviceInfo.applicationInfo = new ApplicationInfo(); 69 info.serviceInfo.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; 70 ShadowPackageManager shadowPackageManager = Shadows.shadowOf(mContext.getPackageManager()); 71 shadowPackageManager.addResolveInfoForIntent(quickStepIntent, info); 72 73 mController = new GestureNavigationSettingsAssistController(mContext, KEY_SWIPE_FOR_ASSIST); 74 } 75 76 @Test isAvailable_systemNavigationControllerReturnsTrue_shouldReturnTrue()77 public void isAvailable_systemNavigationControllerReturnsTrue_shouldReturnTrue() { 78 assertThat(mController.isAvailable()).isTrue(); 79 } 80 81 @Test isAvailable_systemNavigationControllerReturnsFalse_shouldReturnFalse()82 public void isAvailable_systemNavigationControllerReturnsFalse_shouldReturnFalse() { 83 SettingsShadowResources.overrideResource(R.bool.config_swipe_up_gesture_setting_available, 84 false); 85 assertThat(mController.isAvailable()).isFalse(); 86 } 87 88 @Test isChecked_noDefault_true()89 public void isChecked_noDefault_true() { 90 assertThat(mController.isChecked()).isTrue(); 91 } 92 93 @Test isChecked_valueFalse_shouldReturnFalse()94 public void isChecked_valueFalse_shouldReturnFalse() { 95 Settings.Secure.putInt(mContext.getContentResolver(), 96 Settings.Secure.ASSIST_TOUCH_GESTURE_ENABLED, 0); 97 assertThat(mController.isChecked()).isFalse(); 98 } 99 100 @Test isChecked_valueTrue_shouldReturnTrue()101 public void isChecked_valueTrue_shouldReturnTrue() { 102 Settings.Secure.putInt(mContext.getContentResolver(), 103 Settings.Secure.ASSIST_TOUCH_GESTURE_ENABLED, 1); 104 assertThat(mController.isChecked()).isTrue(); 105 } 106 107 @Test onPreferenceChange_preferenceChecked_valueTrue()108 public void onPreferenceChange_preferenceChecked_valueTrue() { 109 mController.onPreferenceChange(null, true); 110 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 111 Settings.Secure.ASSIST_TOUCH_GESTURE_ENABLED, -1)).isEqualTo(1); 112 } 113 114 @Test onPreferenceChange_preferenceUnchecked_valueFalse()115 public void onPreferenceChange_preferenceUnchecked_valueFalse() { 116 mController.onPreferenceChange(null, false); 117 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 118 Settings.Secure.ASSIST_TOUCH_GESTURE_ENABLED, -1)).isEqualTo(0); 119 } 120 } 121