1 /* 2 * Copyright (C) 2021 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 android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU; 20 import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR; 21 22 import static com.android.settings.accessibility.FloatingMenuTransparencyPreferenceController.DEFAULT_TRANSPARENCY; 23 import static com.android.settings.accessibility.FloatingMenuTransparencyPreferenceController.MAXIMUM_TRANSPARENCY; 24 import static com.android.settings.accessibility.FloatingMenuTransparencyPreferenceController.PRECISION; 25 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 26 import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING; 27 28 import static com.google.common.truth.Truth.assertThat; 29 30 import static org.mockito.Mockito.verify; 31 import static org.mockito.Mockito.when; 32 33 import android.content.ContentResolver; 34 import android.content.Context; 35 import android.provider.Settings; 36 37 import androidx.test.core.app.ApplicationProvider; 38 39 import com.android.settings.widget.SeekBarPreference; 40 41 import org.junit.Before; 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.Spy; 47 import org.mockito.junit.MockitoJUnit; 48 import org.mockito.junit.MockitoRule; 49 import org.robolectric.RobolectricTestRunner; 50 51 /** Tests for {@link FloatingMenuTransparencyPreferenceController}. */ 52 @RunWith(RobolectricTestRunner.class) 53 public class FloatingMenuTransparencyPreferenceControllerTest { 54 55 @Rule 56 public MockitoRule mocks = MockitoJUnit.rule(); 57 58 @Spy 59 private final Context mContext = ApplicationProvider.getApplicationContext(); 60 @Mock 61 private ContentResolver mContentResolver; 62 private FloatingMenuTransparencyPreferenceController mController; 63 64 @Before setUp()65 public void setUp() { 66 when(mContext.getContentResolver()).thenReturn(mContentResolver); 67 mController = new FloatingMenuTransparencyPreferenceController(mContext, "test_key"); 68 } 69 70 @Test getAvailabilityStatus_a11yBtnModeFloatingMenu_returnAvailable()71 public void getAvailabilityStatus_a11yBtnModeFloatingMenu_returnAvailable() { 72 Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE, 73 ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU); 74 75 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 76 } 77 78 @Test getAvailabilityStatus_a11yBtnModeNavigationBar_returnDisabledDependentSetting()79 public void getAvailabilityStatus_a11yBtnModeNavigationBar_returnDisabledDependentSetting() { 80 Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE, 81 ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR); 82 83 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 84 } 85 86 @Test onChange_a11yBtnModeChangeToNavigationBar_preferenceDisabled()87 public void onChange_a11yBtnModeChangeToNavigationBar_preferenceDisabled() { 88 mController.mPreference = new SeekBarPreference(mContext); 89 Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE, 90 ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR); 91 92 mController.mContentObserver.onChange(false); 93 94 assertThat(mController.mPreference.isEnabled()).isFalse(); 95 } 96 97 @Test getSliderPosition_putNormalOpacityValue_expectedValue()98 public void getSliderPosition_putNormalOpacityValue_expectedValue() { 99 final float transparencyValue = 0.65f; 100 Settings.Secure.putFloat(mContext.getContentResolver(), 101 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_OPACITY, 102 (MAXIMUM_TRANSPARENCY - transparencyValue)); 103 104 assertThat(mController.getSliderPosition()).isEqualTo((int) (transparencyValue * 100)); 105 } 106 107 @Test getSliderPosition_putOutOfBoundOpacityValue_defaultValue()108 public void getSliderPosition_putOutOfBoundOpacityValue_defaultValue() { 109 Settings.Secure.putFloat(mContext.getContentResolver(), 110 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_OPACITY, 0.01f); 111 112 final int defaultValue = Math.round(DEFAULT_TRANSPARENCY * PRECISION); 113 assertThat(mController.getSliderPosition()).isEqualTo(defaultValue); 114 } 115 116 @Test setSliderPosition_expectedValue()117 public void setSliderPosition_expectedValue() { 118 final float transparencyValue = 0.27f; 119 mController.setSliderPosition((int) (transparencyValue * 100)); 120 121 final float value = Settings.Secure.getFloat(mContext.getContentResolver(), 122 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_OPACITY, -1); 123 assertThat(value).isEqualTo((MAXIMUM_TRANSPARENCY - transparencyValue)); 124 } 125 126 @Test onResume_registerSpecificContentObserver()127 public void onResume_registerSpecificContentObserver() { 128 mController.onResume(); 129 130 verify(mContentResolver).registerContentObserver( 131 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_BUTTON_MODE), false, 132 mController.mContentObserver); 133 verify(mContentResolver).registerContentObserver( 134 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED), 135 false, 136 mController.mContentObserver); 137 } 138 139 @Test onPause_unregisterContentObserver()140 public void onPause_unregisterContentObserver() { 141 mController.onPause(); 142 143 verify(mContentResolver).unregisterContentObserver(mController.mContentObserver); 144 } 145 } 146