1 /* 2 * Copyright 2025 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.inputmethod; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.verify; 24 25 import android.app.settings.SettingsEnums; 26 import android.content.Context; 27 import android.hardware.input.InputSettings; 28 import android.os.UserHandle; 29 import android.provider.Settings; 30 31 import androidx.test.core.app.ApplicationProvider; 32 33 import com.android.settings.core.BasePreferenceController; 34 import com.android.settings.testutils.FakeFeatureFactory; 35 36 import org.junit.Before; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.junit.MockitoJUnit; 41 import org.mockito.junit.MockitoRule; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.annotation.Config; 44 45 /** Tests for {@link MousePointerSpeedPreferenceController} */ 46 @RunWith(RobolectricTestRunner.class) 47 @Config(shadows = { 48 com.android.settings.testutils.shadow.ShadowSystemSettings.class, 49 }) 50 public class MousePointerSpeedPreferenceControllerTest { 51 @Rule 52 public MockitoRule rule = MockitoJUnit.rule(); 53 54 private static final String PREFERENCE_KEY = "pointer_speed"; 55 private static final String SETTING_KEY = Settings.System.POINTER_SPEED; 56 57 private MousePointerSpeedPreferenceController mController; 58 private int mDefaultSpeed; 59 private FakeFeatureFactory mFeatureFactory; 60 61 @Before setUp()62 public void setUp() { 63 Context context = ApplicationProvider.getApplicationContext(); 64 mFeatureFactory = FakeFeatureFactory.setupForTest(); 65 mController = new MousePointerSpeedPreferenceController(context, PREFERENCE_KEY); 66 mDefaultSpeed = Settings.System.getIntForUser( 67 context.getContentResolver(), 68 SETTING_KEY, 69 InputSettings.DEFAULT_POINTER_SPEED, 70 UserHandle.USER_CURRENT); 71 } 72 73 @Test setSliderPosition_speedValue1_shouldReturnTrue()74 public void setSliderPosition_speedValue1_shouldReturnTrue() { 75 int inputSpeed = 1; 76 77 boolean result = mController.setSliderPosition(inputSpeed); 78 79 assertThat(result).isTrue(); 80 assertThat(mController.getSliderPosition()).isEqualTo(inputSpeed); 81 verify(mFeatureFactory.metricsFeatureProvider).action( 82 any(), 83 eq(SettingsEnums.ACTION_GESTURE_POINTER_SPEED_CHANGED), 84 eq(1)); 85 } 86 87 @Test setSliderPosition_speedValueOverMaxValue_shouldReturnFalse()88 public void setSliderPosition_speedValueOverMaxValue_shouldReturnFalse() { 89 int inputSpeed = InputSettings.MAX_POINTER_SPEED + 1; 90 91 boolean result = mController.setSliderPosition(inputSpeed); 92 93 assertThat(result).isFalse(); 94 assertThat(mController.getSliderPosition()).isEqualTo(mDefaultSpeed); 95 } 96 97 @Test setSliderPosition_speedValueOverMinValue_shouldReturnFalse()98 public void setSliderPosition_speedValueOverMinValue_shouldReturnFalse() { 99 int inputSpeed = InputSettings.MIN_POINTER_SPEED - 1; 100 101 boolean result = mController.setSliderPosition(inputSpeed); 102 103 assertThat(result).isFalse(); 104 assertThat(mController.getSliderPosition()).isEqualTo(mDefaultSpeed); 105 } 106 } 107