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 import android.view.InputDevice; 30 31 import androidx.test.core.app.ApplicationProvider; 32 33 import com.android.settings.R; 34 import com.android.settings.core.BasePreferenceController; 35 import com.android.settings.testutils.FakeFeatureFactory; 36 import com.android.settings.testutils.shadow.ShadowInputDevice; 37 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.junit.MockitoJUnit; 43 import org.mockito.junit.MockitoRule; 44 import org.robolectric.RobolectricTestRunner; 45 import org.robolectric.annotation.Config; 46 47 /** Tests for {@link TouchpadReverseScrollingPreferenceController} */ 48 @RunWith(RobolectricTestRunner.class) 49 @Config(shadows = { 50 com.android.settings.testutils.shadow.ShadowSystemSettings.class, 51 com.android.settings.testutils.shadow.ShadowInputDevice.class, 52 }) 53 public class TouchpadReverseScrollingPreferenceControllerTest { 54 @Rule 55 public MockitoRule rule = MockitoJUnit.rule(); 56 57 private static final String PREFERENCE_KEY = "touchpad_reverse_scrolling"; 58 private static final String SETTING_KEY = Settings.System.TOUCHPAD_NATURAL_SCROLLING; 59 60 private Context mContext; 61 private TouchpadReverseScrollingPreferenceController mController; 62 private FakeFeatureFactory mFeatureFactory; 63 64 @Before setUp()65 public void setUp() { 66 mContext = ApplicationProvider.getApplicationContext(); 67 mFeatureFactory = FakeFeatureFactory.setupForTest(); 68 mController = new TouchpadReverseScrollingPreferenceController(mContext, PREFERENCE_KEY); 69 } 70 71 @Test getAvailabilityStatus_expected()72 public void getAvailabilityStatus_expected() { 73 int deviceId = 1; 74 ShadowInputDevice.sDeviceIds = new int[]{deviceId}; 75 InputDevice device = ShadowInputDevice.makeInputDevicebyIdWithSources(deviceId, 76 InputDevice.SOURCE_TOUCHPAD); 77 ShadowInputDevice.addDevice(deviceId, device); 78 79 assertThat(mController.getAvailabilityStatus()) 80 .isEqualTo(BasePreferenceController.AVAILABLE); 81 } 82 83 @Test getSliceHighlightMenuRes_expected()84 public void getSliceHighlightMenuRes_expected() { 85 assertThat(mController.getSliceHighlightMenuRes()).isEqualTo(R.string.menu_key_system); 86 } 87 88 @Test setChecked_true_shouldReturn0()89 public void setChecked_true_shouldReturn0() { 90 mController.setChecked(true); 91 92 int result = Settings.System.getIntForUser( 93 mContext.getContentResolver(), 94 SETTING_KEY, 95 0, 96 UserHandle.USER_CURRENT); 97 98 assertThat(result).isEqualTo(0); 99 verify(mFeatureFactory.metricsFeatureProvider).action( 100 any(), 101 eq(SettingsEnums.ACTION_GESTURE_REVERSE_SCROLLING_CHANGED), 102 eq(true)); 103 } 104 105 @Test setChecked_false_shouldReturn1()106 public void setChecked_false_shouldReturn1() { 107 mController.setChecked(false); 108 109 int result = Settings.System.getIntForUser( 110 mContext.getContentResolver(), 111 SETTING_KEY, 112 0, 113 UserHandle.USER_CURRENT); 114 115 assertThat(result).isEqualTo(1); 116 verify(mFeatureFactory.metricsFeatureProvider).action( 117 any(), 118 eq(SettingsEnums.ACTION_GESTURE_REVERSE_SCROLLING_CHANGED), 119 eq(false)); 120 } 121 122 @Test isChecked_providerPutInt1_returnFalse()123 public void isChecked_providerPutInt1_returnFalse() { 124 Settings.System.putIntForUser( 125 mContext.getContentResolver(), 126 SETTING_KEY, 127 1, 128 UserHandle.USER_CURRENT); 129 130 boolean result = mController.isChecked(); 131 132 assertThat(result).isFalse(); 133 } 134 135 @Test isChecked_providerPutInt0_returnTrue()136 public void isChecked_providerPutInt0_returnTrue() { 137 Settings.System.putIntForUser( 138 mContext.getContentResolver(), 139 SETTING_KEY, 140 0, 141 UserHandle.USER_CURRENT); 142 143 boolean result = mController.isChecked(); 144 145 assertThat(result).isTrue(); 146 } 147 } 148