1 package com.android.settings.development; 2 3 /* 4 * Copyright (C) 2017 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.provider.Settings; 27 import android.support.v14.preference.SwitchPreference; 28 import android.support.v7.preference.PreferenceScreen; 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 39 @RunWith(SettingsRobolectricTestRunner.class) 40 public class NotificationChannelWarningsPreferenceControllerTest { 41 42 @Mock 43 private SwitchPreference mPreference; 44 @Mock 45 private PreferenceScreen mScreen; 46 47 private Context mContext; 48 private NotificationChannelWarningsPreferenceController mController; 49 50 @Before 51 public void setup() { 52 MockitoAnnotations.initMocks(this); 53 mContext = RuntimeEnvironment.application; 54 mController = new NotificationChannelWarningsPreferenceController(mContext); 55 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 56 mController.displayPreference(mScreen); 57 } 58 59 @Test 60 public void onPreferenceChange_settingEnabled_shouldEnableNotificationChannelWarnings() { 61 mController.onPreferenceChange(mPreference, true /* new value */); 62 63 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 64 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, -1 /* default */); 65 66 assertThat(mode).isEqualTo( 67 NotificationChannelWarningsPreferenceController.SETTING_VALUE_ON); 68 } 69 70 @Test 71 public void onPreferenceChange_settingDisabled_shouldDisableNotificationChannelWarnings() { 72 mController.onPreferenceChange(mPreference, false /* new value */); 73 74 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 75 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, -1 /* default */); 76 77 assertThat(mode).isEqualTo( 78 NotificationChannelWarningsPreferenceController.SETTING_VALUE_OFF); 79 } 80 81 @Test 82 public void updateState_settingEnabled_preferenceShouldBeChecked() { 83 Settings.Global.putInt(mContext.getContentResolver(), 84 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, 85 NotificationChannelWarningsPreferenceController.SETTING_VALUE_ON); 86 87 mController.updateState(mPreference); 88 89 verify(mPreference).setChecked(true); 90 } 91 92 @Test 93 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 94 Settings.Global.putInt(mContext.getContentResolver(), 95 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, 96 NotificationChannelWarningsPreferenceController.SETTING_VALUE_OFF); 97 98 mController.updateState(mPreference); 99 100 verify(mPreference).setChecked(false); 101 } 102 103 @Test 104 public void updateState_settingUndefinedDebuggingEnabled_preferenceShouldBeChecked() { 105 mController = spy(mController); 106 doReturn(true).when(mController).isDebuggable(); 107 Settings.Global.putString(mContext.getContentResolver(), 108 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, "NotAnInteger"); 109 110 mController.updateState(mPreference); 111 112 verify(mPreference).setChecked(true); 113 } 114 115 @Test 116 public void updateState_settingUndefinedDebuggingDisabled_preferenceShouldNotBeChecked() { 117 mController = spy(mController); 118 doReturn(false).when(mController).isDebuggable(); 119 Settings.Global.putString(mContext.getContentResolver(), 120 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, "NotAnInteger"); 121 122 mController.updateState(mPreference); 123 124 verify(mPreference).setChecked(false); 125 } 126 127 @Test 128 public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeDisabled() { 129 mController.onDeveloperOptionsSwitchDisabled(); 130 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 131 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, -1 /* default */); 132 133 assertThat(mode).isEqualTo( 134 NotificationChannelWarningsPreferenceController.SETTING_VALUE_OFF); 135 verify(mPreference).setChecked(false); 136 verify(mPreference).setEnabled(false); 137 } 138 } 139