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