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