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