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