1 /* 2 * Copyright 2025 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.accessibility; 18 19 import static androidx.lifecycle.Lifecycle.Event.ON_START; 20 import static androidx.lifecycle.Lifecycle.Event.ON_STOP; 21 22 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; 23 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 24 25 import static com.google.common.truth.Truth.assertThat; 26 27 import android.content.Context; 28 import android.platform.test.annotations.EnableFlags; 29 import android.platform.test.flag.junit.SetFlagsRule; 30 import android.provider.Settings; 31 32 import androidx.lifecycle.LifecycleOwner; 33 import androidx.test.core.app.ApplicationProvider; 34 35 import com.android.settings.core.BasePreferenceController; 36 import com.android.settingslib.core.lifecycle.Lifecycle; 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.robolectric.RobolectricTestRunner; 43 import org.robolectric.shadow.api.Shadow; 44 import org.robolectric.shadows.ShadowContentResolver; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class ToggleAutoclickMainSwitchPreferenceControllerTest { 48 49 private static final String PREFERENCE_KEY = "accessibility_autoclick_main_switch"; 50 51 @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 52 private final Context mContext = ApplicationProvider.getApplicationContext(); 53 54 private ShadowContentResolver mShadowContentResolver; 55 private ToggleAutoclickMainSwitchPreferenceController mController; 56 private LifecycleOwner mLifecycleOwner; 57 private Lifecycle mLifecycle; 58 59 @Before setUp()60 public void setUp() { 61 mShadowContentResolver = Shadow.extract(mContext.getContentResolver()); 62 63 mController = new ToggleAutoclickMainSwitchPreferenceController(mContext, PREFERENCE_KEY); 64 mLifecycleOwner = () -> mLifecycle; 65 mLifecycle = new Lifecycle(mLifecycleOwner); 66 mLifecycle.addObserver(mController); 67 } 68 69 @Test 70 @EnableFlags(com.android.server.accessibility.Flags.FLAG_ENABLE_AUTOCLICK_INDICATOR) getAvailabilityStatus_availableWhenFlagOn()71 public void getAvailabilityStatus_availableWhenFlagOn() { 72 assertThat(mController.getAvailabilityStatus()) 73 .isEqualTo(BasePreferenceController.AVAILABLE); 74 } 75 76 @Test setChecked_withTrue_shouldUpdateSetting()77 public void setChecked_withTrue_shouldUpdateSetting() { 78 Settings.Secure.putInt(mContext.getContentResolver(), 79 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF); 80 81 mController.setChecked(true); 82 83 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 84 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF)) 85 .isEqualTo(ON); 86 } 87 88 @Test setChecked_withFalse_shouldUpdateSetting()89 public void setChecked_withFalse_shouldUpdateSetting() { 90 Settings.Secure.putInt(mContext.getContentResolver(), 91 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, ON); 92 93 mController.setChecked(false); 94 95 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 96 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF)) 97 .isEqualTo(OFF); 98 } 99 100 @Test onStart_shouldRegisterContentObserver()101 public void onStart_shouldRegisterContentObserver() { 102 mLifecycle.handleLifecycleEvent(ON_START); 103 104 assertThat(mShadowContentResolver.getContentObservers( 105 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED))) 106 .hasSize(1); 107 } 108 109 @Test onStop_shouldUnregisterContentObserver()110 public void onStop_shouldUnregisterContentObserver() { 111 mLifecycle.handleLifecycleEvent(ON_START); 112 mLifecycle.handleLifecycleEvent(ON_STOP); 113 114 assertThat(mShadowContentResolver.getContentObservers( 115 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED))) 116 .isEmpty(); 117 } 118 } 119