1 /* 2 * Copyright (C) 2020 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 com.android.settings.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 21 import static com.android.settings.accessibility.AccessibilityUtil.UserShortcutType; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import android.content.ComponentName; 26 import android.content.Context; 27 import android.provider.Settings; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.robolectric.RobolectricTestRunner; 33 import org.robolectric.RuntimeEnvironment; 34 35 @RunWith(RobolectricTestRunner.class) 36 public class ToggleScreenMagnificationPreferenceFragmentTest { 37 38 private static final String DUMMY_PACKAGE_NAME = "com.dummy.example"; 39 private static final String DUMMY_CLASS_NAME = DUMMY_PACKAGE_NAME + ".dummy_a11y_service"; 40 private static final ComponentName DUMMY_COMPONENT_NAME = new ComponentName(DUMMY_PACKAGE_NAME, 41 DUMMY_CLASS_NAME); 42 private static final String SOFTWARE_SHORTCUT_KEY = 43 Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS; 44 private static final String HARDWARE_SHORTCUT_KEY = 45 Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE; 46 private static final String TRIPLETAP_SHORTCUT_KEY = 47 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED; 48 private static final String MAGNIFICATION_CONTROLLER_NAME = 49 "com.android.server.accessibility.MagnificationController"; 50 51 private Context mContext; 52 53 @Before setUp()54 public void setUp() { 55 mContext = RuntimeEnvironment.application; 56 } 57 58 @Test hasValueInSettings_putValue_hasValue()59 public void hasValueInSettings_putValue_hasValue() { 60 setMagnificationTripleTapEnabled(/* enabled= */ true); 61 62 assertThat(ToggleScreenMagnificationPreferenceFragment.hasMagnificationValuesInSettings( 63 mContext, UserShortcutType.TRIPLETAP)).isTrue(); 64 } 65 66 @Test optInAllValuesToSettings_optInValue_haveMatchString()67 public void optInAllValuesToSettings_optInValue_haveMatchString() { 68 int shortcutTypes = UserShortcutType.SOFTWARE | UserShortcutType.TRIPLETAP; 69 70 ToggleScreenMagnificationPreferenceFragment.optInAllMagnificationValuesToSettings(mContext, 71 shortcutTypes); 72 73 assertThat(getStringFromSettings(SOFTWARE_SHORTCUT_KEY)).isEqualTo( 74 MAGNIFICATION_CONTROLLER_NAME); 75 assertThat(getMagnificationTripleTapStatus()).isTrue(); 76 77 } 78 79 @Test optInAllValuesToSettings_existOtherValue_optInValue_haveMatchString()80 public void optInAllValuesToSettings_existOtherValue_optInValue_haveMatchString() { 81 putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, DUMMY_COMPONENT_NAME.flattenToString()); 82 83 ToggleScreenMagnificationPreferenceFragment.optInAllMagnificationValuesToSettings(mContext, 84 UserShortcutType.SOFTWARE); 85 86 assertThat(getStringFromSettings(SOFTWARE_SHORTCUT_KEY)).isEqualTo( 87 DUMMY_COMPONENT_NAME.flattenToString() + ":" + MAGNIFICATION_CONTROLLER_NAME); 88 89 } 90 91 @Test optOutAllValuesToSettings_optOutValue_emptyString()92 public void optOutAllValuesToSettings_optOutValue_emptyString() { 93 putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, MAGNIFICATION_CONTROLLER_NAME); 94 putStringIntoSettings(HARDWARE_SHORTCUT_KEY, MAGNIFICATION_CONTROLLER_NAME); 95 setMagnificationTripleTapEnabled(/* enabled= */ true); 96 int shortcutTypes = 97 UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE | UserShortcutType.TRIPLETAP; 98 99 ToggleScreenMagnificationPreferenceFragment.optOutAllMagnificationValuesFromSettings( 100 mContext, shortcutTypes); 101 102 assertThat(getStringFromSettings(SOFTWARE_SHORTCUT_KEY)).isEmpty(); 103 assertThat(getStringFromSettings(HARDWARE_SHORTCUT_KEY)).isEmpty(); 104 assertThat(getMagnificationTripleTapStatus()).isFalse(); 105 } 106 107 @Test optOutValueFromSettings_existOtherValue_optOutValue_haveMatchString()108 public void optOutValueFromSettings_existOtherValue_optOutValue_haveMatchString() { 109 putStringIntoSettings(SOFTWARE_SHORTCUT_KEY, 110 DUMMY_COMPONENT_NAME.flattenToString() + ":" + MAGNIFICATION_CONTROLLER_NAME); 111 putStringIntoSettings(HARDWARE_SHORTCUT_KEY, 112 DUMMY_COMPONENT_NAME.flattenToString() + ":" + MAGNIFICATION_CONTROLLER_NAME); 113 int shortcutTypes = UserShortcutType.SOFTWARE | UserShortcutType.HARDWARE; 114 115 ToggleScreenMagnificationPreferenceFragment.optOutAllMagnificationValuesFromSettings( 116 mContext, shortcutTypes); 117 118 assertThat(getStringFromSettings(SOFTWARE_SHORTCUT_KEY)).isEqualTo( 119 DUMMY_COMPONENT_NAME.flattenToString()); 120 assertThat(getStringFromSettings(HARDWARE_SHORTCUT_KEY)).isEqualTo( 121 DUMMY_COMPONENT_NAME.flattenToString()); 122 } 123 putStringIntoSettings(String key, String componentName)124 private void putStringIntoSettings(String key, String componentName) { 125 Settings.Secure.putString(mContext.getContentResolver(), key, componentName); 126 } 127 setMagnificationTripleTapEnabled(boolean enabled)128 private void setMagnificationTripleTapEnabled(boolean enabled) { 129 Settings.Secure.putInt(mContext.getContentResolver(), 130 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, enabled ? ON : OFF); 131 } 132 getStringFromSettings(String key)133 private String getStringFromSettings(String key) { 134 return Settings.Secure.getString(mContext.getContentResolver(), key); 135 } 136 getMagnificationTripleTapStatus()137 private boolean getMagnificationTripleTapStatus() { 138 return Settings.Secure.getInt(mContext.getContentResolver(), 139 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, OFF) == ON; 140 } 141 } 142