1 /*
2  * Copyright (C) 2023 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 TrackpadPointerSpeedPreferenceController} */
46 @RunWith(RobolectricTestRunner.class)
47 @Config(shadows = {
48         com.android.settings.testutils.shadow.ShadowSystemSettings.class,
49 })
50 public class TrackpadPointerSpeedPreferenceControllerTest {
51     @Rule
52     public MockitoRule rule = MockitoJUnit.rule();
53 
54     private static final String PREFERENCE_KEY = "trackpad_pointer_speed";
55     private static final String SETTING_KEY = Settings.System.TOUCHPAD_POINTER_SPEED;
56 
57     private Context mContext;
58     private TrackpadPointerSpeedPreferenceController mController;
59     private int mDefaultSpeed;
60     private FakeFeatureFactory mFeatureFactory;
61 
62     @Before
setUp()63     public void setUp() {
64         mContext = ApplicationProvider.getApplicationContext();
65         mFeatureFactory = FakeFeatureFactory.setupForTest();
66         mController = new TrackpadPointerSpeedPreferenceController(mContext, PREFERENCE_KEY);
67         mDefaultSpeed = Settings.System.getIntForUser(
68                 mContext.getContentResolver(),
69                 SETTING_KEY,
70                 InputSettings.DEFAULT_POINTER_SPEED,
71                 UserHandle.USER_CURRENT);
72     }
73 
74     @Test
getAvailabilityStatus_expected()75     public void getAvailabilityStatus_expected() {
76         assertThat(mController.getAvailabilityStatus())
77                 .isEqualTo(BasePreferenceController.AVAILABLE);
78     }
79 
80     @Test
getMin_expected()81     public void getMin_expected() {
82         assertThat(mController.getMin()).isEqualTo(InputSettings.MIN_POINTER_SPEED);
83     }
84 
85     @Test
getMax_expected()86     public void getMax_expected() {
87         assertThat(mController.getMax()).isEqualTo(InputSettings.MAX_POINTER_SPEED);
88     }
89 
90     @Test
getSliderPosition_defaultSpeed_return0()91     public void getSliderPosition_defaultSpeed_return0() {
92         int result = mController.getSliderPosition();
93 
94         assertThat(result).isEqualTo(0);
95     }
96 
97     @Test
setSliderPosition_speedValue1_shouldReturnTrue()98     public void setSliderPosition_speedValue1_shouldReturnTrue() {
99         int inputSpeed = 1;
100 
101         boolean result = mController.setSliderPosition(inputSpeed);
102 
103         assertThat(result).isTrue();
104         assertThat(mController.getSliderPosition()).isEqualTo(inputSpeed);
105         verify(mFeatureFactory.metricsFeatureProvider).action(
106                 any(),
107                 eq(SettingsEnums.ACTION_GESTURE_POINTER_SPEED_CHANGED),
108                 eq(1));
109     }
110 
111     @Test
setSliderPosition_speedValueOverMaxValue_shouldReturnFalse()112     public void setSliderPosition_speedValueOverMaxValue_shouldReturnFalse() {
113         int inputSpeed = InputSettings.MAX_POINTER_SPEED + 1;
114 
115         boolean result = mController.setSliderPosition(inputSpeed);
116 
117         assertThat(result).isFalse();
118         assertThat(mController.getSliderPosition()).isEqualTo(mDefaultSpeed);
119     }
120 
121     @Test
setSliderPosition_speedValueOverMinValue_shouldReturnFalse()122     public void setSliderPosition_speedValueOverMinValue_shouldReturnFalse() {
123         int inputSpeed = InputSettings.MIN_POINTER_SPEED - 1;
124 
125         boolean result = mController.setSliderPosition(inputSpeed);
126 
127         assertThat(result).isFalse();
128         assertThat(mController.getSliderPosition()).isEqualTo(mDefaultSpeed);
129     }
130 }
131