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.development; 18 19 import static com.android.settings.development.ShowFirstCrashDialogPreferenceController.SETTING_VALUE_OFF; 20 import static com.android.settings.development.ShowFirstCrashDialogPreferenceController.SETTING_VALUE_ON; 21 import static com.google.common.truth.Truth.assertThat; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.provider.Settings; 26 import android.support.v14.preference.SwitchPreference; 27 import android.support.v7.preference.PreferenceScreen; 28 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RuntimeEnvironment; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 public class ShowFirstCrashDialogPreferenceControllerTest { 40 41 @Mock 42 private PreferenceScreen mPreferenceScreen; 43 44 private Context mContext; 45 private SwitchPreference mPreference; 46 private ShowFirstCrashDialogPreferenceController mController; 47 48 @Before setup()49 public void setup() { 50 MockitoAnnotations.initMocks(this); 51 mContext = RuntimeEnvironment.application; 52 mPreference = new SwitchPreference(mContext); 53 mController = new ShowFirstCrashDialogPreferenceController(mContext); 54 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 55 .thenReturn(mPreference); 56 mController.displayPreference(mPreferenceScreen); 57 } 58 59 @Test onPreferenceChange_settingEnabled_showFirstCrashDialogShouldBeOn()60 public void onPreferenceChange_settingEnabled_showFirstCrashDialogShouldBeOn() { 61 mController.onPreferenceChange(mPreference, true /* new value */); 62 63 final int mode = Settings.Secure.getInt(mContext.getContentResolver(), 64 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, -1 /* default */); 65 66 assertThat(mode).isEqualTo(SETTING_VALUE_ON); 67 } 68 69 @Test onPreferenceChange_settingDisabled_showFirstCrashDialogShouldBeOff()70 public void onPreferenceChange_settingDisabled_showFirstCrashDialogShouldBeOff() { 71 mController.onPreferenceChange(mPreference, false /* new value */); 72 73 final int mode = Settings.Secure.getInt(mContext.getContentResolver(), 74 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, -1 /* default */); 75 76 assertThat(mode).isEqualTo(SETTING_VALUE_OFF); 77 } 78 79 @Test updateState_settingDisabled_preferenceShouldNotBeChecked()80 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 81 Settings.Secure.putInt(mContext.getContentResolver(), 82 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, SETTING_VALUE_OFF); 83 mController.updateState(mPreference); 84 85 assertThat(mPreference.isChecked()).isFalse(); 86 } 87 88 @Test updateState_settingEnabled_preferenceShouldBeChecked()89 public void updateState_settingEnabled_preferenceShouldBeChecked() { 90 Settings.Secure.putInt(mContext.getContentResolver(), 91 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, SETTING_VALUE_ON); 92 mController.updateState(mPreference); 93 94 assertThat(mPreference.isChecked()).isTrue(); 95 } 96 97 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()98 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 99 mController.onDeveloperOptionsSwitchDisabled(); 100 101 final int mode = Settings.Secure.getInt(mContext.getContentResolver(), 102 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, -1 /* default */); 103 104 assertThat(mode).isEqualTo(SETTING_VALUE_OFF); 105 assertThat(mPreference.isChecked()).isFalse(); 106 assertThat(mPreference.isEnabled()).isFalse(); 107 } 108 109 @Test onShowFirstCrashDialogGlobalOff_shouldEnablePreference()110 public void onShowFirstCrashDialogGlobalOff_shouldEnablePreference() { 111 Settings.Global.putInt(mContext.getContentResolver(), 112 Settings.Global.SHOW_FIRST_CRASH_DIALOG, SETTING_VALUE_OFF); 113 114 mController.displayPreference(mPreferenceScreen); 115 116 assertThat(mController.isAvailable()).isTrue(); 117 assertThat(mPreference.isVisible()).isTrue(); 118 } 119 120 @Test onShowFirstCrashDialogGlobalOn_shouldDisablePreference()121 public void onShowFirstCrashDialogGlobalOn_shouldDisablePreference() { 122 Settings.Global.putInt(mContext.getContentResolver(), 123 Settings.Global.SHOW_FIRST_CRASH_DIALOG, SETTING_VALUE_ON); 124 125 mController.displayPreference(mPreferenceScreen); 126 127 assertThat(mController.isAvailable()).isFalse(); 128 assertThat(mPreference.isVisible()).isFalse(); 129 } 130 } 131