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