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.content.Context; 25 import android.provider.Settings; 26 27 import androidx.preference.PreferenceScreen; 28 import androidx.preference.SwitchPreference; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RobolectricTestRunner; 36 import org.robolectric.RuntimeEnvironment; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class DisableAutomaticUpdatesPreferenceControllerTest { 40 41 @Mock 42 private PreferenceScreen mPreferenceScreen; 43 @Mock 44 private SwitchPreference mPreference; 45 46 private Context mContext; 47 private DisableAutomaticUpdatesPreferenceController mController; 48 49 @Before setup()50 public void setup() { 51 MockitoAnnotations.initMocks(this); 52 mContext = RuntimeEnvironment.application; 53 mController = new DisableAutomaticUpdatesPreferenceController(mContext); 54 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( 55 mPreference); 56 mController.displayPreference(mPreferenceScreen); 57 } 58 59 @Test onPreferenceChanged_turnOnAutomaticUpdates()60 public void onPreferenceChanged_turnOnAutomaticUpdates() { 61 mController.onPreferenceChange(null, true); 62 63 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 64 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, -1); 65 66 assertThat(mode).isEqualTo( 67 DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING); 68 } 69 70 @Test onPreferenceChanged_turnOffAutomaticUpdates()71 public void onPreferenceChanged_turnOffAutomaticUpdates() { 72 mController.onPreferenceChange(null, false); 73 74 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 75 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, -1); 76 77 assertThat(mode).isEqualTo( 78 DisableAutomaticUpdatesPreferenceController.DISABLE_UPDATES_SETTING); 79 } 80 81 @Test updateState_preferenceShouldBeChecked()82 public void updateState_preferenceShouldBeChecked() { 83 Settings.Global 84 .putInt(mContext.getContentResolver(), Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, 85 DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING); 86 mController.updateState(mPreference); 87 88 verify(mPreference).setChecked(true); 89 } 90 91 @Test updateState_preferenceShouldNotBeChecked()92 public void updateState_preferenceShouldNotBeChecked() { 93 Settings.Global 94 .putInt(mContext.getContentResolver(), Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, 95 DisableAutomaticUpdatesPreferenceController.DISABLE_UPDATES_SETTING); 96 mController.updateState(mPreference); 97 98 verify(mPreference).setChecked(false); 99 } 100 101 @Test onDeveloperOptionsDisabled_shouldDisablePreference()102 public void onDeveloperOptionsDisabled_shouldDisablePreference() { 103 mController.onDeveloperOptionsDisabled(); 104 105 verify(mPreference).setEnabled(false); 106 verify(mPreference).setChecked(false); 107 } 108 } 109