1 /* 2 * Copyright (C) 2018 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.MagnificationPreferenceFragment.OFF; 20 import static com.android.settings.accessibility.MagnificationPreferenceFragment.ON; 21 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.Mockito.spy; 26 27 import android.content.Context; 28 import android.content.res.Resources; 29 import android.provider.Settings; 30 31 import androidx.preference.Preference; 32 33 import com.android.settings.R; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 import org.robolectric.annotation.Config; 43 import org.robolectric.annotation.Implementation; 44 import org.robolectric.annotation.Implements; 45 import org.robolectric.annotation.Resetter; 46 47 @RunWith(RobolectricTestRunner.class) 48 public class MagnificationNavbarPreferenceControllerTest { 49 50 private Context mContext; 51 private MagnificationNavbarPreferenceController mController; 52 private Preference mPreference; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 mContext = spy(RuntimeEnvironment.application); 58 mController = new MagnificationNavbarPreferenceController(mContext, "test_key"); 59 mPreference = new Preference(mContext); 60 mController.updateState(mPreference); 61 } 62 63 @After tearDown()64 public void tearDown() { 65 ShadowMagnificationPreferenceFragment.reset(); 66 } 67 68 @Test 69 @Config(shadows = ShadowMagnificationPreferenceFragment.class) isAvailable_unsupported_shouldNotBeAvailable()70 public void isAvailable_unsupported_shouldNotBeAvailable() { 71 ShadowMagnificationPreferenceFragment.setApplicable(false); 72 73 assertThat(mController.getAvailabilityStatus()) 74 .isNotEqualTo(AVAILABLE); 75 } 76 77 @Test 78 @Config(shadows = ShadowMagnificationPreferenceFragment.class) isAvailable_supported_shouldBeAvailable()79 public void isAvailable_supported_shouldBeAvailable() { 80 ShadowMagnificationPreferenceFragment.setApplicable(true); 81 82 assertThat(mController.getAvailabilityStatus()) 83 .isEqualTo(AVAILABLE); 84 } 85 86 @Test updateState_shouldRefreshSummary()87 public void updateState_shouldRefreshSummary() { 88 Settings.Secure.putInt(mContext.getContentResolver(), 89 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, ON); 90 mController.updateState(mPreference); 91 assertThat(mPreference.getSummary()) 92 .isEqualTo(mContext.getText(R.string.accessibility_feature_state_on)); 93 94 Settings.Secure.putInt(mContext.getContentResolver(), 95 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, OFF); 96 mController.updateState(mPreference); 97 assertThat(mPreference.getSummary()) 98 .isEqualTo(mContext.getText(R.string.accessibility_feature_state_off)); 99 } 100 101 @Test updateState_shouldRefreshSummarySuw()102 public void updateState_shouldRefreshSummarySuw() { 103 mController.setIsFromSUW(true); 104 mController.updateState(mPreference); 105 assertThat(mPreference.getSummary()) 106 .isEqualTo(mContext.getString(R.string. 107 accessibility_screen_magnification_navbar_short_summary)); 108 } 109 110 @Test isChecked_enabled()111 public void isChecked_enabled() { 112 Settings.Secure.putInt(mContext.getContentResolver(), 113 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, ON); 114 115 assertThat(mController.isChecked()).isTrue(); 116 } 117 118 @Test isChecked_disabled()119 public void isChecked_disabled() { 120 Settings.Secure.putInt(mContext.getContentResolver(), 121 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, OFF); 122 123 assertThat(mController.isChecked()).isFalse(); 124 } 125 126 @Test setChecked_enabled()127 public void setChecked_enabled() { 128 mController.setChecked(true); 129 130 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 131 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, -1)) 132 .isEqualTo(ON); 133 } 134 135 @Test setChecked_disabled()136 public void setChecked_disabled() { 137 mController.setChecked(false); 138 139 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 140 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, -1)) 141 .isEqualTo(OFF); 142 } 143 144 @Implements(MagnificationPreferenceFragment.class) 145 public static class ShadowMagnificationPreferenceFragment { 146 private static boolean sIsApplicable; 147 148 @Resetter reset()149 static void reset() { 150 sIsApplicable = false; 151 } 152 153 @Implementation isApplicable(Resources res)154 protected static boolean isApplicable(Resources res) { 155 return sIsApplicable; 156 } 157 setApplicable(boolean applicable)158 static void setApplicable(boolean applicable) { 159 sIsApplicable = applicable; 160 } 161 } 162 163 @Test isSliceableCorrectKey_returnsTrue()164 public void isSliceableCorrectKey_returnsTrue() { 165 final MagnificationNavbarPreferenceController controller = 166 new MagnificationNavbarPreferenceController(mContext, 167 "screen_magnification_navbar_preference_screen"); 168 assertThat(controller.isSliceable()).isTrue(); 169 } 170 171 @Test isSliceableIncorrectKey_returnsFalse()172 public void isSliceableIncorrectKey_returnsFalse() { 173 final MagnificationNavbarPreferenceController controller = 174 new MagnificationNavbarPreferenceController(mContext, "bad_key"); 175 assertThat(controller.isSliceable()).isFalse(); 176 } 177 178 @Test isPublicSlice_returnTrue()179 public void isPublicSlice_returnTrue() { 180 assertThat(mController.isPublicSlice()).isTrue(); 181 } 182 } 183