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