1 /* 2 * Copyright (C) 2022 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 android.view.flags.Flags.enableVectorCursorA11ySettings; 20 21 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assume.assumeTrue; 25 import static org.mockito.ArgumentMatchers.any; 26 import static org.mockito.ArgumentMatchers.anyString; 27 import static org.mockito.ArgumentMatchers.eq; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.app.settings.SettingsEnums; 32 import android.content.Context; 33 import android.os.UserHandle; 34 import android.provider.Settings; 35 import android.widget.SeekBar; 36 37 import androidx.lifecycle.Lifecycle; 38 import androidx.lifecycle.LifecycleEventObserver; 39 import androidx.lifecycle.LifecycleOwner; 40 import androidx.preference.PreferenceScreen; 41 42 import com.android.settings.testutils.FakeFeatureFactory; 43 import com.android.settings.testutils.shadow.ShadowSystemSettings; 44 import com.android.settings.widget.LabeledSeekBarPreference; 45 46 import org.junit.Before; 47 import org.junit.Rule; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.mockito.Mock; 51 import org.mockito.junit.MockitoJUnit; 52 import org.mockito.junit.MockitoRule; 53 import org.robolectric.RobolectricTestRunner; 54 import org.robolectric.RuntimeEnvironment; 55 import org.robolectric.annotation.Config; 56 57 /** Tests for {@link PointerScaleSeekBarController} */ 58 @RunWith(RobolectricTestRunner.class) 59 @Config(shadows = { 60 ShadowSystemSettings.class, 61 }) 62 public class PointerScaleSeekBarControllerTest { 63 64 private static final String PREFERENCE_KEY = "pointer_scale"; 65 66 @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule(); 67 68 @Mock private PreferenceScreen mPreferenceScreen; 69 @Mock private LifecycleOwner mLifecycleOwner; 70 71 private Context mContext; 72 private LabeledSeekBarPreference mPreference; 73 private PointerScaleSeekBarController mController; 74 private FakeFeatureFactory mFeatureFactory; 75 76 @Before setUp()77 public void setUp() { 78 mContext = RuntimeEnvironment.application; 79 mFeatureFactory = FakeFeatureFactory.setupForTest(); 80 mPreference = new LabeledSeekBarPreference(mContext, null); 81 mController = new PointerScaleSeekBarController(mContext, PREFERENCE_KEY); 82 } 83 84 @Test getAvailabilityStatus_flagEnabled()85 public void getAvailabilityStatus_flagEnabled() { 86 assumeTrue(enableVectorCursorA11ySettings()); 87 88 assertEquals(mController.getAvailabilityStatus(), AVAILABLE); 89 } 90 91 @Test onProgressChanged_changeListenerUpdatesSetting()92 public void onProgressChanged_changeListenerUpdatesSetting() { 93 when(mPreferenceScreen.findPreference(anyString())).thenReturn(mPreference); 94 mController.displayPreference(mPreferenceScreen); 95 SeekBar seekBar = mPreference.getSeekbar(); 96 int sliderValue = 1; 97 98 mPreference.onProgressChanged(seekBar, sliderValue, false); 99 100 float expectedScale = 1.5f; 101 float currentScale = Settings.System.getFloatForUser(mContext.getContentResolver(), 102 Settings.System.POINTER_SCALE, -1, UserHandle.USER_CURRENT); 103 assertEquals(expectedScale, currentScale, /* delta= */ 0.001f); 104 } 105 106 @Test onPause_logCurrentScaleValue()107 public void onPause_logCurrentScaleValue() { 108 float scale = 1.5f; 109 Settings.System.putFloatForUser(mContext.getContentResolver(), 110 Settings.System.POINTER_SCALE, scale, UserHandle.USER_CURRENT); 111 112 mController.onStateChanged(mLifecycleOwner, Lifecycle.Event.ON_PAUSE); 113 114 verify(mFeatureFactory.metricsFeatureProvider).action( 115 any(), eq(SettingsEnums.ACTION_POINTER_ICON_SCALE_CHANGED), 116 eq(Float.toString(scale))); 117 } 118 } 119