1 /* 2 * Copyright (C) 2016 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.datetime; 18 19 import android.content.Context; 20 import android.provider.Settings; 21 22 import com.android.settings.SettingsRobolectricTestRunner; 23 import com.android.settings.TestConfig; 24 import com.android.settingslib.RestrictedSwitchPreference; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.mockito.Mock; 30 import org.mockito.MockitoAnnotations; 31 import org.robolectric.annotation.Config; 32 import org.robolectric.shadows.ShadowApplication; 33 34 import static com.google.common.truth.Truth.assertThat; 35 import static org.mockito.Mockito.verify; 36 37 @RunWith(SettingsRobolectricTestRunner.class) 38 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 39 public class AutoTimePreferenceControllerTest { 40 41 @Mock 42 private UpdateTimeAndDateCallback mCallback; 43 44 private Context mContext; 45 private RestrictedSwitchPreference mPreference; 46 private AutoTimePreferenceController mController; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 mContext = ShadowApplication.getInstance().getApplicationContext(); 52 mPreference = new RestrictedSwitchPreference(mContext); 53 mController = new AutoTimePreferenceController(mContext, mCallback); 54 } 55 56 @Test testIsEnabled_shouldReadFromSettingsProvider()57 public void testIsEnabled_shouldReadFromSettingsProvider() { 58 // Disabled 59 Settings.Global.putInt(mContext.getContentResolver(), 60 Settings.Global.AUTO_TIME, 0); 61 assertThat(mController.isEnabled()).isFalse(); 62 63 // Enabled 64 Settings.Global.putInt(mContext.getContentResolver(), 65 Settings.Global.AUTO_TIME, 1); 66 assertThat(mController.isEnabled()).isTrue(); 67 } 68 69 @Test updatePreferenceChange_prefIsChecked_shouldUpdatePreferenceAndNotifyCallback()70 public void updatePreferenceChange_prefIsChecked_shouldUpdatePreferenceAndNotifyCallback() { 71 mController.onPreferenceChange(mPreference, true); 72 73 assertThat(mController.isEnabled()).isTrue(); 74 verify(mCallback).updateTimeAndDateDisplay(mContext); 75 } 76 77 @Test updatePreferenceChange_prefIsUnchecked_shouldUpdatePreferenceAndNotifyCallback()78 public void updatePreferenceChange_prefIsUnchecked_shouldUpdatePreferenceAndNotifyCallback() { 79 mController.onPreferenceChange(mPreference, false); 80 81 assertThat(mController.isEnabled()).isFalse(); 82 verify(mCallback).updateTimeAndDateDisplay(mContext); 83 } 84 } 85