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.os.UserHandle;
28 import android.provider.Settings;
29 
30 import androidx.test.core.app.ApplicationProvider;
31 
32 import com.android.settings.R;
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 TrackpadTapToClickPreferenceController} */
46 @RunWith(RobolectricTestRunner.class)
47 @Config(shadows = {
48         com.android.settings.testutils.shadow.ShadowSystemSettings.class,
49 })
50 public class TrackpadTapToClickPreferenceControllerTest {
51     @Rule
52     public MockitoRule rule = MockitoJUnit.rule();
53 
54     private static final String PREFERENCE_KEY = "trackpad_tap_to_click";
55     private static final String SETTING_KEY = Settings.System.TOUCHPAD_TAP_TO_CLICK;
56 
57     private Context mContext;
58     private TrackpadTapToClickPreferenceController mController;
59     private FakeFeatureFactory mFeatureFactory;
60 
61     @Before
setUp()62     public void setUp() {
63         mContext = ApplicationProvider.getApplicationContext();
64         mFeatureFactory = FakeFeatureFactory.setupForTest();
65         mController = new TrackpadTapToClickPreferenceController(mContext, PREFERENCE_KEY);
66     }
67 
68     @Test
getAvailabilityStatus_expected()69     public void getAvailabilityStatus_expected() {
70         assertThat(mController.getAvailabilityStatus())
71                 .isEqualTo(BasePreferenceController.AVAILABLE);
72     }
73 
74     @Test
getSliceHighlightMenuRes_expected()75     public void getSliceHighlightMenuRes_expected() {
76         assertThat(mController.getSliceHighlightMenuRes()).isEqualTo(R.string.menu_key_system);
77     }
78 
79     @Test
setChecked_true_shouldReturn1()80     public void setChecked_true_shouldReturn1() {
81         mController.setChecked(true);
82 
83         int result = Settings.System.getIntForUser(
84                 mContext.getContentResolver(),
85                 SETTING_KEY,
86                 0,
87                 UserHandle.USER_CURRENT);
88 
89         assertThat(result).isEqualTo(1);
90         verify(mFeatureFactory.metricsFeatureProvider).action(
91                 any(),
92                 eq(SettingsEnums.ACTION_GESTURE_TAP_TO_CLICK_CHANGED),
93                 eq(true));
94     }
95 
96     @Test
setChecked_false_shouldReturn0()97     public void setChecked_false_shouldReturn0() {
98         mController.setChecked(false);
99 
100         int result = Settings.System.getIntForUser(
101                 mContext.getContentResolver(),
102                 SETTING_KEY,
103                 0,
104                 UserHandle.USER_CURRENT);
105 
106         assertThat(result).isEqualTo(0);
107         verify(mFeatureFactory.metricsFeatureProvider).action(
108                 any(),
109                 eq(SettingsEnums.ACTION_GESTURE_TAP_TO_CLICK_CHANGED),
110                 eq(false));
111     }
112 
113     @Test
isChecked_providerPutInt1_returnTrue()114     public void isChecked_providerPutInt1_returnTrue() {
115         Settings.System.putIntForUser(
116                 mContext.getContentResolver(),
117                 SETTING_KEY,
118                 1,
119                 UserHandle.USER_CURRENT);
120 
121         boolean result = mController.isChecked();
122 
123         assertThat(result).isTrue();
124     }
125 
126     @Test
isChecked_providerPutInt0_returnFalse()127     public void isChecked_providerPutInt0_returnFalse() {
128         Settings.System.putIntForUser(
129                 mContext.getContentResolver(),
130                 SETTING_KEY,
131                 0,
132                 UserHandle.USER_CURRENT);
133 
134         boolean result = mController.isChecked();
135 
136         assertThat(result).isFalse();
137     }
138 }
139