1 /* 2 * Copyright (C) 2017 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.development; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.os.RemoteException; 25 import android.os.StrictMode; 26 import android.os.SystemProperties; 27 import android.view.IWindowManager; 28 29 import androidx.preference.PreferenceScreen; 30 import androidx.preference.SwitchPreference; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 import org.robolectric.util.ReflectionHelpers; 40 41 @RunWith(RobolectricTestRunner.class) 42 public class StrictModePreferenceControllerTest { 43 44 @Mock 45 private IWindowManager mWindowManager; 46 @Mock 47 private PreferenceScreen mScreen; 48 @Mock 49 private SwitchPreference mPreference; 50 51 private StrictModePreferenceController mController; 52 53 @Before setUp()54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 mController = new StrictModePreferenceController(RuntimeEnvironment.application); 57 ReflectionHelpers.setField(mController, "mWindowManager", mWindowManager); 58 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 59 mController.displayPreference(mScreen); 60 } 61 62 @Test onPreferenceChange_settingEnabled_shouldTurnOnStrictMode()63 public void onPreferenceChange_settingEnabled_shouldTurnOnStrictMode() throws RemoteException { 64 mController.onPreferenceChange(mPreference, true /* new value */); 65 66 verify(mWindowManager).setStrictModeVisualIndicatorPreference( 67 StrictModePreferenceController.STRICT_MODE_ENABLED); 68 } 69 70 @Test onPreferenceChange_settingDisabled_shouldTurnOffStrictMode()71 public void onPreferenceChange_settingDisabled_shouldTurnOffStrictMode() 72 throws RemoteException { 73 mController.onPreferenceChange(mPreference, false /* new value */); 74 75 verify(mWindowManager).setStrictModeVisualIndicatorPreference( 76 StrictModePreferenceController.STRICT_MODE_DISABLED); 77 } 78 79 @Test updateState_settingEnabled_preferenceShouldBeChecked()80 public void updateState_settingEnabled_preferenceShouldBeChecked() { 81 SystemProperties.set(StrictMode.VISUAL_PROPERTY, Boolean.toString(false)); 82 mController.updateState(mPreference); 83 84 verify(mPreference).setChecked(false); 85 } 86 87 @Test updateState_settingDisabled_preferenceShouldNotBeChecked()88 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 89 SystemProperties.set(StrictMode.VISUAL_PROPERTY, Boolean.toString(true)); 90 mController.updateState(mPreference); 91 92 verify(mPreference).setChecked(true); 93 } 94 95 @Test onDeveloperOptionsSwitchDisabled_shouldTurnOffPreference()96 public void onDeveloperOptionsSwitchDisabled_shouldTurnOffPreference() { 97 mController.onDeveloperOptionsSwitchDisabled(); 98 final boolean isEnabled = SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, 99 false /* default */); 100 101 assertThat(isEnabled).isFalse(); 102 verify(mPreference).setChecked(false); 103 verify(mPreference).setEnabled(false); 104 } 105 } 106