• 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 
21 import static org.mockito.Mockito.verify;
22 import static org.robolectric.shadow.api.Shadow.extract;
23 
24 import android.content.Context;
25 import android.net.ConnectivityManager;
26 import android.provider.Settings;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.annotation.Config;
40 
41 @RunWith(RobolectricTestRunner.class)
42 @Config(shadows = ShadowConnectivityManager.class)
43 public class AutoTimeZonePreferenceControllerTest {
44 
45     @Mock
46     private UpdateTimeAndDateCallback mCallback;
47 
48     private Context mContext;
49     private AutoTimeZonePreferenceController mController;
50     private Preference mPreference;
51     private ShadowConnectivityManager connectivityManager;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56         mContext = RuntimeEnvironment.application;
57         mPreference = new Preference(mContext);
58         connectivityManager = extract(mContext.getSystemService(ConnectivityManager.class));
59         connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, true);
60     }
61 
62     @Test
isFromSUW_notAvailable()63     public void isFromSUW_notAvailable() {
64         mController = new AutoTimeZonePreferenceController(
65                 mContext, null /* callback */, true /* isFromSUW */);
66 
67         assertThat(mController.isAvailable()).isFalse();
68     }
69 
70     @Test
notFromSUW_isAvailable()71     public void notFromSUW_isAvailable() {
72         mController = new AutoTimeZonePreferenceController(
73                 mContext, null /* callback */, false /* isFromSUW */);
74 
75         assertThat(mController.isAvailable()).isTrue();
76     }
77 
78     @Test
isWifiOnly_notAvailable()79     public void isWifiOnly_notAvailable() {
80         connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, false);
81 
82         mController = new AutoTimeZonePreferenceController(
83                 mContext, null /* callback */, false /* fromSUW */);
84 
85         assertThat(mController.isAvailable()).isFalse();
86     }
87 
88     @Test
isFromSUW_notEnable()89     public void isFromSUW_notEnable() {
90         mController =
91             new AutoTimeZonePreferenceController(mContext, null /* callback */, true /* fromSUW */);
92 
93         assertThat(mController.isEnabled()).isFalse();
94     }
95 
96     @Test
isWifiOnly_notEnable()97     public void isWifiOnly_notEnable() {
98         connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, false);
99 
100         mController = new AutoTimeZonePreferenceController(
101                 mContext, null /* callback */, false /* fromSUW */);
102 
103         assertThat(mController.isEnabled()).isFalse();
104     }
105 
106     @Test
testIsEnabled_shouldReadFromSettingsProvider()107     public void testIsEnabled_shouldReadFromSettingsProvider() {
108         mController = new AutoTimeZonePreferenceController(
109                 mContext, null /* callback */, false /* fromSUW */);
110 
111         // Disabled
112         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME_ZONE, 0);
113         assertThat(mController.isEnabled()).isFalse();
114 
115         // Enabled
116         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME_ZONE, 1);
117         assertThat(mController.isEnabled()).isTrue();
118     }
119 
120     @Test
updatePreferenceChange_prefIsChecked_shouldUpdatePreferenceAndNotifyCallback()121     public void updatePreferenceChange_prefIsChecked_shouldUpdatePreferenceAndNotifyCallback() {
122         mController =
123             new AutoTimeZonePreferenceController(mContext, mCallback, false /* fromSUW */);
124 
125         mController.onPreferenceChange(mPreference, true);
126 
127         assertThat(mController.isEnabled()).isTrue();
128         verify(mCallback).updateTimeAndDateDisplay(mContext);
129     }
130 
131     @Test
updatePreferenceChange_prefIsUnchecked_shouldUpdatePreferenceAndNotifyCallback()132     public void updatePreferenceChange_prefIsUnchecked_shouldUpdatePreferenceAndNotifyCallback() {
133         mController =
134             new AutoTimeZonePreferenceController(mContext, mCallback, false /* fromSUW */);
135 
136         mController.onPreferenceChange(mPreference, false);
137 
138         assertThat(mController.isEnabled()).isFalse();
139         verify(mCallback).updateTimeAndDateDisplay(mContext);
140     }
141 }
142