• 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 android.app.time.Capabilities.CAPABILITY_POSSESSED;
20 import static android.app.time.DetectorStatusTypes.DETECTION_ALGORITHM_STATUS_RUNNING;
21 import static android.app.time.DetectorStatusTypes.DETECTOR_STATUS_RUNNING;
22 import static android.app.time.LocationTimeZoneAlgorithmStatus.PROVIDER_STATUS_NOT_PRESENT;
23 import static android.app.time.LocationTimeZoneAlgorithmStatus.PROVIDER_STATUS_NOT_READY;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.when;
30 
31 import android.app.time.Capabilities;
32 import android.app.time.LocationTimeZoneAlgorithmStatus;
33 import android.app.time.TelephonyTimeZoneAlgorithmStatus;
34 import android.app.time.TimeManager;
35 import android.app.time.TimeZoneCapabilities;
36 import android.app.time.TimeZoneCapabilitiesAndConfig;
37 import android.app.time.TimeZoneConfiguration;
38 import android.app.time.TimeZoneDetectorStatus;
39 import android.content.Context;
40 import android.os.UserHandle;
41 
42 import com.android.settingslib.RestrictedPreference;
43 
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.robolectric.RobolectricTestRunner;
50 import org.robolectric.RuntimeEnvironment;
51 
52 @RunWith(RobolectricTestRunner.class)
53 public class TimeZonePreferenceControllerTest {
54 
55     @Mock
56     private TimeManager mTimeManager;
57     private Context mContext;
58     private TimeZonePreferenceController mController;
59     private RestrictedPreference mPreference;
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64 
65         mContext = spy(RuntimeEnvironment.application);
66         doReturn(mTimeManager).when(mContext).getSystemService(TimeManager.class);
67 
68         mPreference = new RestrictedPreference(mContext);
69 
70         mController = spy(new TimeZonePreferenceController(mContext, "test_key"));
71         doReturn("test timezone").when(mController).getTimeZoneOffsetAndName();
72     }
73 
74     @Test
updateState_suggestManualNotAllowed_shouldDisablePref()75     public void updateState_suggestManualNotAllowed_shouldDisablePref() {
76         // Make sure not disabled by admin.
77         mPreference.setDisabledByAdmin(null);
78 
79         TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
80             /* suggestManualAllowed= */false);
81         when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
82 
83         mController.updateState(mPreference);
84 
85         assertThat(mPreference.isEnabled()).isFalse();
86     }
87 
88     @Test
updateState_suggestManualAllowed_shouldEnablePref()89     public void updateState_suggestManualAllowed_shouldEnablePref() {
90         // Make sure not disabled by admin.
91         mPreference.setDisabledByAdmin(null);
92 
93         TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
94             /* suggestManualAllowed= */true);
95         when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
96 
97         mController.updateState(mPreference);
98 
99         assertThat(mPreference.isEnabled()).isTrue();
100     }
101 
createCapabilitiesAndConfig( boolean suggestManualAllowed)102     private static TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig(
103             boolean suggestManualAllowed) {
104         TimeZoneDetectorStatus status = new TimeZoneDetectorStatus(DETECTOR_STATUS_RUNNING,
105                 new TelephonyTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING),
106                 new LocationTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING,
107                         PROVIDER_STATUS_NOT_READY, null,
108                         PROVIDER_STATUS_NOT_PRESENT, null));
109         int suggestManualCapability = suggestManualAllowed ? Capabilities.CAPABILITY_POSSESSED
110                 : Capabilities.CAPABILITY_NOT_SUPPORTED;
111         boolean useLocationEnabled = true;
112         TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(UserHandle.SYSTEM)
113                 .setConfigureAutoDetectionEnabledCapability(Capabilities.CAPABILITY_POSSESSED)
114                 .setUseLocationEnabled(useLocationEnabled)
115                 .setConfigureGeoDetectionEnabledCapability(Capabilities.CAPABILITY_NOT_SUPPORTED)
116                 .setSetManualTimeZoneCapability(suggestManualCapability)
117                 .setConfigureNotificationsEnabledCapability(CAPABILITY_POSSESSED)
118                 .build();
119         TimeZoneConfiguration config = new TimeZoneConfiguration.Builder()
120                 .setAutoDetectionEnabled(!suggestManualAllowed)
121                 .setGeoDetectionEnabled(false)
122                 .setNotificationsEnabled(true)
123                 .build();
124         return new TimeZoneCapabilitiesAndConfig(status, capabilities, config);
125     }
126 }
127