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.content.Context; 24 import android.provider.Settings; 25 import android.support.v14.preference.SwitchPreference; 26 import android.support.v7.preference.PreferenceScreen; 27 28 import com.android.settings.testutils.SettingsRobolectricTestRunner; 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.RuntimeEnvironment; 36 37 @RunWith(SettingsRobolectricTestRunner.class) 38 public class DisableAutomaticUpdatesPreferenceControllerTest { 39 40 @Mock 41 private PreferenceScreen mPreferenceScreen; 42 @Mock 43 private SwitchPreference mPreference; 44 45 private Context mContext; 46 private DisableAutomaticUpdatesPreferenceController mController; 47 48 @Before setup()49 public void setup() { 50 MockitoAnnotations.initMocks(this); 51 mContext = RuntimeEnvironment.application; 52 mController = new DisableAutomaticUpdatesPreferenceController(mContext); 53 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( 54 mPreference); 55 mController.displayPreference(mPreferenceScreen); 56 } 57 58 @Test onPreferenceChanged_turnOnAutomaticUpdates()59 public void onPreferenceChanged_turnOnAutomaticUpdates() { 60 mController.onPreferenceChange(null, true); 61 62 final int mode = Settings.System.getInt(mContext.getContentResolver(), 63 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, -1); 64 65 assertThat(mode).isEqualTo( 66 DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING); 67 } 68 69 @Test onPreferenceChanged_turnOffAutomaticUpdates()70 public void onPreferenceChanged_turnOffAutomaticUpdates() { 71 mController.onPreferenceChange(null, false); 72 73 final int mode = Settings.System.getInt(mContext.getContentResolver(), 74 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, -1); 75 76 assertThat(mode).isEqualTo( 77 DisableAutomaticUpdatesPreferenceController.DISABLE_UPDATES_SETTING); 78 } 79 80 @Test updateState_preferenceShouldBeChecked()81 public void updateState_preferenceShouldBeChecked() { 82 Settings.System 83 .putInt(mContext.getContentResolver(), Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, 84 DisableAutomaticUpdatesPreferenceController.ENABLE_UPDATES_SETTING); 85 mController.updateState(mPreference); 86 87 verify(mPreference).setChecked(true); 88 } 89 90 @Test updateState_preferenceShouldNotBeChecked()91 public void updateState_preferenceShouldNotBeChecked() { 92 Settings.System 93 .putInt(mContext.getContentResolver(), Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, 94 DisableAutomaticUpdatesPreferenceController.DISABLE_UPDATES_SETTING); 95 mController.updateState(mPreference); 96 97 verify(mPreference).setChecked(false); 98 } 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