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.app.AlarmManager;
20 import android.content.Context;
21 import android.support.v7.preference.Preference;
22 
23 import com.android.settings.SettingsRobolectricTestRunner;
24 import com.android.settings.TestConfig;
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 import static org.mockito.Mockito.when;
37 
38 @RunWith(SettingsRobolectricTestRunner.class)
39 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
40 public class DatePreferenceControllerTest {
41 
42     @Mock
43     private Context mContext;
44     @Mock
45     private AlarmManager mAlarmManager;
46     @Mock
47     private DatePreferenceController.DatePreferenceHost mHost;
48     @Mock
49     private AutoTimePreferenceController mAutoTimePreferenceController;
50 
51     private Preference mPreference;
52     private DatePreferenceController mController;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         when(mContext.getSystemService(Context.ALARM_SERVICE)).thenReturn(mAlarmManager);
58         mPreference = new Preference(ShadowApplication.getInstance().getApplicationContext());
59         mController = new DatePreferenceController(mContext, mHost, mAutoTimePreferenceController);
60     }
61 
62     @Test
isAlwaysAvailable()63     public void isAlwaysAvailable() {
64         assertThat(mController.isAvailable()).isTrue();
65     }
66 
67     @Test
shouldHandleDateSetCallback()68     public void shouldHandleDateSetCallback() {
69         mController.onDateSet(null, 2016, 1, 1);
70         verify(mHost).updateTimeAndDateDisplay(mContext);
71     }
72 
73     @Test
updateState_autoTimeEnabled_shouldDisablePref()74     public void updateState_autoTimeEnabled_shouldDisablePref() {
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         when(mAutoTimePreferenceController.isEnabled()).thenReturn(false);
84         mController.updateState(mPreference);
85 
86         assertThat(mPreference.isEnabled()).isTrue();
87     }
88 
89     @Test
clickPreference_showDatePicker()90     public void clickPreference_showDatePicker() {
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).showDatePicker();
100     }
101 }
102