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.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
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.time.Capabilities;
28 import android.app.time.TimeCapabilities;
29 import android.app.time.TimeCapabilitiesAndConfig;
30 import android.app.time.TimeConfiguration;
31 import android.app.time.TimeManager;
32 import android.content.Context;
33 import android.os.UserHandle;
34 
35 import androidx.preference.Preference;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class AutoTimePreferenceControllerTest {
48 
49     @Mock
50     private UpdateTimeAndDateCallback mCallback;
51     private Context mContext;
52     private AutoTimePreferenceController mController;
53     private Preference mPreference;
54     @Mock
55     private TimeManager mTimeManager;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mContext = spy(RuntimeEnvironment.application);
61         mPreference = new Preference(mContext);
62         when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager);
63 
64         mController = new AutoTimePreferenceController(mContext, "test_key");
65         mController.setDateAndTimeCallback(mCallback);
66     }
67 
68     @Test
autoTimeNotSupported_notAvailable()69     public void autoTimeNotSupported_notAvailable() {
70         TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
71                 /* autoSupported= */false, /* autoEnabled= */false);
72         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
73 
74         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
75     }
76 
77     @Test
autoTimeNotSupported_notEnable()78     public void autoTimeNotSupported_notEnable() {
79         TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
80                 /* autoSupported= */false, /* autoEnabled= */false);
81         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
82 
83         assertThat(mController.isEnabled()).isFalse();
84     }
85 
86     @Test
isEnabled_autoEnabledIsFalse_shouldReadFromTimeManagerConfig()87     public void isEnabled_autoEnabledIsFalse_shouldReadFromTimeManagerConfig() {
88         // Disabled
89         TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
90                 /* autoSupported= */true, /* autoEnabled= */false);
91         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
92 
93         assertThat(mController.isEnabled()).isFalse();
94     }
95 
96     @Test
isEnabled_autoEnabledIsTrue_shouldReadFromTimeManagerConfig()97     public void isEnabled_autoEnabledIsTrue_shouldReadFromTimeManagerConfig() {
98         // Enabled
99         TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
100                 /* autoSupported= */true, /* autoEnabled= */true);
101         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
102 
103         assertThat(mController.isEnabled()).isTrue();
104     }
105 
106     @Test
updatePreferenceChange_prefIsChecked_shouldUpdatePreferenceAndNotifyCallback()107     public void updatePreferenceChange_prefIsChecked_shouldUpdatePreferenceAndNotifyCallback() {
108         TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
109                 /* autoSupported= */true, /* autoEnabled= */false);
110         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
111         when(mTimeManager.updateTimeConfiguration(Mockito.any())).thenReturn(true);
112 
113         assertThat(mController.onPreferenceChange(mPreference, true)).isTrue();
114         verify(mCallback).updateTimeAndDateDisplay(mContext);
115 
116         // Check the service was asked to change the configuration correctly.
117         TimeConfiguration timeConfiguration = new TimeConfiguration.Builder()
118                 .setAutoDetectionEnabled(true)
119                 .build();
120         verify(mTimeManager).updateTimeConfiguration(timeConfiguration);
121 
122         // Update the mTimeManager mock so that it now returns the expected updated config.
123         TimeCapabilitiesAndConfig capabilitiesAndConfigAfterUpdate = createCapabilitiesAndConfig(
124                 /* autoSupported= */true, /* autoEnabled= */true);
125         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(
126                 capabilitiesAndConfigAfterUpdate);
127 
128         assertThat(mController.isEnabled()).isTrue();
129     }
130 
131     @Test
updatePreferenceChange_prefIsUnchecked_shouldUpdatePreferenceAndNotifyCallback()132     public void updatePreferenceChange_prefIsUnchecked_shouldUpdatePreferenceAndNotifyCallback() {
133         TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
134                 /* autoSupported= */true, /* autoEnabled= */true);
135         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
136         when(mTimeManager.updateTimeConfiguration(Mockito.any())).thenReturn(true);
137 
138         assertThat(mController.onPreferenceChange(mPreference, false)).isTrue();
139         verify(mCallback).updateTimeAndDateDisplay(mContext);
140 
141         // Check the service was asked to change the configuration correctly.
142         TimeConfiguration timeConfiguration = new TimeConfiguration.Builder()
143                 .setAutoDetectionEnabled(false)
144                 .build();
145         verify(mTimeManager).updateTimeConfiguration(timeConfiguration);
146 
147         // Update the mTimeManager mock so that it now returns the expected updated config.
148         TimeCapabilitiesAndConfig capabilitiesAndConfigAfterUpdate =
149                 createCapabilitiesAndConfig(/* autoSupported= */true, /* autoEnabled= */false);
150         when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(
151                 capabilitiesAndConfigAfterUpdate);
152 
153         assertThat(mController.isEnabled()).isFalse();
154     }
155 
createCapabilitiesAndConfig(boolean autoSupported, boolean autoEnabled)156     private static TimeCapabilitiesAndConfig createCapabilitiesAndConfig(boolean autoSupported,
157             boolean autoEnabled) {
158         int configureAutoDetectionEnabledCapability =
159                 autoSupported ? Capabilities.CAPABILITY_POSSESSED
160                         : Capabilities.CAPABILITY_NOT_SUPPORTED;
161         TimeCapabilities capabilities = new TimeCapabilities.Builder(UserHandle.SYSTEM)
162                 .setConfigureAutoDetectionEnabledCapability(configureAutoDetectionEnabledCapability)
163                 .setSetManualTimeCapability(Capabilities.CAPABILITY_POSSESSED)
164                 .build();
165         TimeConfiguration config = new TimeConfiguration.Builder()
166                 .setAutoDetectionEnabled(autoEnabled)
167                 .build();
168         return new TimeCapabilitiesAndConfig(capabilities, config);
169     }
170 }
171