1 /* 2 * Copyright 2024 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 import static com.android.settings.inputmethod.PointerStrokeStylePreferenceController.KEY_POINTER_STROKE_STYLE; 23 24 import static org.junit.Assert.assertEquals; 25 import static org.junit.Assert.assertNotNull; 26 import static org.junit.Assume.assumeTrue; 27 import static org.mockito.ArgumentMatchers.any; 28 import static org.mockito.ArgumentMatchers.eq; 29 import static org.mockito.Mockito.verify; 30 import static org.mockito.Mockito.when; 31 32 import android.app.settings.SettingsEnums; 33 import android.content.Context; 34 import android.os.UserHandle; 35 import android.provider.Settings; 36 37 import androidx.lifecycle.Lifecycle; 38 import androidx.lifecycle.LifecycleEventObserver; 39 import androidx.lifecycle.LifecycleOwner; 40 import androidx.preference.Preference; 41 import androidx.preference.PreferenceScreen; 42 import androidx.test.core.app.ApplicationProvider; 43 44 import com.android.settings.testutils.FakeFeatureFactory; 45 import com.android.settings.testutils.shadow.ShadowSystemSettings; 46 47 import org.junit.Before; 48 import org.junit.Rule; 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 import org.mockito.Mock; 52 import org.mockito.junit.MockitoJUnit; 53 import org.mockito.junit.MockitoRule; 54 import org.robolectric.RobolectricTestRunner; 55 import org.robolectric.annotation.Config; 56 57 /** Tests for {@link PointerStrokeStylePreferenceController} */ 58 @RunWith(RobolectricTestRunner.class) 59 @Config(shadows = { 60 ShadowSystemSettings.class, 61 }) 62 public class PointerStrokeStylePreferenceControllerTest { 63 @Rule 64 public MockitoRule mMockitoRule = MockitoJUnit.rule(); 65 66 @Mock 67 PreferenceScreen mPreferenceScreen; 68 @Mock 69 LifecycleOwner mLifecycleOwner; 70 71 private Context mContext; 72 private PointerStrokeStylePreferenceController mController; 73 private FakeFeatureFactory mFeatureFactory; 74 75 @Before setUp()76 public void setUp() { 77 mContext = ApplicationProvider.getApplicationContext(); 78 mFeatureFactory = FakeFeatureFactory.setupForTest(); 79 mController = new PointerStrokeStylePreferenceController(mContext); 80 } 81 82 @Test displayPreference_initializeDataStore()83 public void displayPreference_initializeDataStore() { 84 Preference strokePreference = new Preference(mContext); 85 strokePreference.setKey(KEY_POINTER_STROKE_STYLE); 86 when(mPreferenceScreen.findPreference(eq(KEY_POINTER_STROKE_STYLE))).thenReturn( 87 strokePreference); 88 89 mController.displayPreference(mPreferenceScreen); 90 91 assertNotNull(strokePreference.getPreferenceDataStore()); 92 } 93 94 @Test getAvailabilityStatus_flagEnabled()95 public void getAvailabilityStatus_flagEnabled() { 96 assumeTrue(enableVectorCursorA11ySettings()); 97 98 assertEquals(mController.getAvailabilityStatus(), AVAILABLE); 99 } 100 101 @Test onPause_logCurrentStrokeValue()102 public void onPause_logCurrentStrokeValue() { 103 int strokeStyle = 1; 104 Settings.System.putIntForUser(mContext.getContentResolver(), 105 Settings.System.POINTER_STROKE_STYLE, strokeStyle, UserHandle.USER_CURRENT); 106 107 mController.onStateChanged(mLifecycleOwner, Lifecycle.Event.ON_PAUSE); 108 109 verify(mFeatureFactory.metricsFeatureProvider).action( 110 any(), eq(SettingsEnums.ACTION_POINTER_ICON_STROKE_STYLE_CHANGED), 111 eq(strokeStyle)); 112 } 113 } 114