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.junit.Assert.assertEquals;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.DatePickerDialog;
28 import android.app.time.Capabilities;
29 import android.app.time.TimeCapabilities;
30 import android.app.time.TimeCapabilitiesAndConfig;
31 import android.app.time.TimeConfiguration;
32 import android.app.time.TimeManager;
33 import android.app.timedetector.TimeDetector;
34 import android.app.timedetector.TimeDetectorHelper;
35 import android.content.Context;
36 import android.os.UserHandle;
37 
38 import com.android.settingslib.RestrictedPreference;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.annotation.Config;
48 
49 import java.util.Calendar;
50 import java.util.GregorianCalendar;
51 
52 @RunWith(RobolectricTestRunner.class)
53 @Config(shadows = {
54         com.android.settings.testutils.shadow.ShadowFragment.class,
55 })
56 public class DatePreferenceControllerTest {
57 
58     @Mock
59     private Context mContext;
60     @Mock
61     private DatePreferenceController.DatePreferenceHost mHost;
62     @Mock
63     private TimeManager mTimeManager;
64     @Mock
65     private TimeDetector mTimeDetector;
66 
67     private RestrictedPreference mPreference;
68     private DatePreferenceController mController;
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73         mContext = spy(RuntimeEnvironment.application);
74         when(mContext.getSystemService(TimeDetector.class)).thenReturn(mTimeDetector);
75         when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager);
76         mPreference = new RestrictedPreference(RuntimeEnvironment.application);
77         mController = new DatePreferenceController(mContext, "test_key");
78         mController.setHost(mHost);
79     }
80 
81     @Test
shouldHandleDateSetCallback()82     public void shouldHandleDateSetCallback() {
83         mController.onDateSet(null, 2016, 1, 1);
84         verify(mHost).updateTimeAndDateDisplay(mContext);
85     }
86 
87     @Test
updateState_autoTimeEnabled_shouldDisablePref()88     public void updateState_autoTimeEnabled_shouldDisablePref() {
89         // Make sure not disabled by admin.
90         mPreference.setDisabledByAdmin(null);
91 
92         TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
93                 /* suggestManualAllowed= */false);
94         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
95         mController.updateState(mPreference);
96 
97         assertThat(mPreference.isEnabled()).isFalse();
98     }
99 
100     @Test
updateState_autoTimeDisabled_shouldEnablePref()101     public void updateState_autoTimeDisabled_shouldEnablePref() {
102         // Make sure not disabled by admin.
103         mPreference.setDisabledByAdmin(null);
104 
105         TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
106                 /* suggestManualAllowed= */true);
107         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
108         mController.updateState(mPreference);
109 
110         assertThat(mPreference.isEnabled()).isTrue();
111     }
112 
113     @Test
clickPreference_showDatePicker()114     public void clickPreference_showDatePicker() {
115         // Click a preference that's not controlled by this controller.
116         mPreference.setKey("fake_key");
117         assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse();
118 
119         // Click a preference controlled by this controller.
120         mPreference.setKey(mController.getPreferenceKey());
121         mController.handlePreferenceTreeClick(mPreference);
122         // Should show date picker
123         verify(mHost).showDatePicker();
124     }
125 
126     @Test
testBuildDatePicker()127     public void testBuildDatePicker() {
128         TimeDetectorHelper timeDetectorHelper = mock(TimeDetectorHelper.class);
129         when(timeDetectorHelper.getManualDateSelectionYearMin()).thenReturn(2015);
130         when(timeDetectorHelper.getManualDateSelectionYearMax()).thenReturn(2020);
131 
132         Context context = RuntimeEnvironment.application;
133         DatePickerDialog dialog = mController.buildDatePicker(context, timeDetectorHelper);
134 
135         GregorianCalendar calendar = new GregorianCalendar();
136 
137         long minDate = dialog.getDatePicker().getMinDate();
138         calendar.setTimeInMillis(minDate);
139         assertEquals(2015, calendar.get(Calendar.YEAR));
140 
141         long maxDate = dialog.getDatePicker().getMaxDate();
142         calendar.setTimeInMillis(maxDate);
143         assertEquals(2020, calendar.get(Calendar.YEAR));
144     }
145 
createCapabilitiesAndConfig( boolean suggestManualAllowed)146     static TimeCapabilitiesAndConfig createCapabilitiesAndConfig(
147             boolean suggestManualAllowed) {
148         int suggestManualCapability = suggestManualAllowed ? Capabilities.CAPABILITY_POSSESSED
149                 : Capabilities.CAPABILITY_NOT_SUPPORTED;
150         TimeCapabilities capabilities = new TimeCapabilities.Builder(UserHandle.SYSTEM)
151                 .setConfigureAutoDetectionEnabledCapability(Capabilities.CAPABILITY_POSSESSED)
152                 .setSetManualTimeCapability(suggestManualCapability)
153                 .build();
154         TimeConfiguration config = new TimeConfiguration.Builder()
155                 .setAutoDetectionEnabled(!suggestManualAllowed)
156                 .build();
157         return new TimeCapabilitiesAndConfig(capabilities, config);
158     }
159 }
160