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 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.app.timedetector.TimeDetector; 25 import android.content.Context; 26 27 import com.android.settingslib.RestrictedPreference; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RobolectricTestRunner; 35 import org.robolectric.RuntimeEnvironment; 36 37 @RunWith(RobolectricTestRunner.class) 38 public class DatePreferenceControllerTest { 39 40 @Mock 41 private Context mContext; 42 @Mock 43 private TimeDetector mTimeDetector; 44 @Mock 45 private DatePreferenceController.DatePreferenceHost mHost; 46 @Mock 47 private AutoTimePreferenceController mAutoTimePreferenceController; 48 49 private RestrictedPreference mPreference; 50 private DatePreferenceController mController; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 when(mContext.getSystemService(TimeDetector.class)).thenReturn(mTimeDetector); 56 mPreference = new RestrictedPreference(RuntimeEnvironment.application); 57 mController = new DatePreferenceController(mContext, mHost, mAutoTimePreferenceController); 58 } 59 60 @Test isAlwaysAvailable()61 public void isAlwaysAvailable() { 62 assertThat(mController.isAvailable()).isTrue(); 63 } 64 65 @Test shouldHandleDateSetCallback()66 public void shouldHandleDateSetCallback() { 67 mController.onDateSet(null, 2016, 1, 1); 68 verify(mHost).updateTimeAndDateDisplay(mContext); 69 } 70 71 @Test updateState_autoTimeEnabled_shouldDisablePref()72 public void updateState_autoTimeEnabled_shouldDisablePref() { 73 // Make sure not disabled by admin. 74 mPreference.setDisabledByAdmin(null); 75 76 when(mAutoTimePreferenceController.isEnabled()).thenReturn(true); 77 mController.updateState(mPreference); 78 79 assertThat(mPreference.isEnabled()).isFalse(); 80 } 81 82 @Test updateState_autoTimeDisabled_shouldEnablePref()83 public void updateState_autoTimeDisabled_shouldEnablePref() { 84 // Make sure not disabled by admin. 85 mPreference.setDisabledByAdmin(null); 86 87 when(mAutoTimePreferenceController.isEnabled()).thenReturn(false); 88 mController.updateState(mPreference); 89 90 assertThat(mPreference.isEnabled()).isTrue(); 91 } 92 93 @Test clickPreference_showDatePicker()94 public void clickPreference_showDatePicker() { 95 // Click a preference that's not controlled by this controller. 96 mPreference.setKey("fake_key"); 97 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 98 99 // Click a preference controlled by this controller. 100 mPreference.setKey(mController.getPreferenceKey()); 101 mController.handlePreferenceTreeClick(mPreference); 102 // Should show date picker 103 verify(mHost).showDatePicker(); 104 } 105 } 106