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.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.provider.Settings; 27 import android.telephony.TelephonyManager; 28 29 import androidx.preference.Preference; 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.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 39 @RunWith(RobolectricTestRunner.class) 40 public class AutoTimeZonePreferenceControllerTest { 41 42 @Mock 43 private UpdateTimeAndDateCallback mCallback; 44 @Mock 45 private Context mContext; 46 private AutoTimeZonePreferenceController mController; 47 private Preference mPreference; 48 @Mock 49 private TelephonyManager mTelephonyManager; 50 51 @Before setUp()52 public void setUp() { 53 MockitoAnnotations.initMocks(this); 54 mContext = spy(RuntimeEnvironment.application); 55 56 mPreference = new Preference(mContext); 57 58 when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager); 59 when(mTelephonyManager.isDataCapable()).thenReturn(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 when(mTelephonyManager.isDataCapable()).thenReturn(false); 81 mController = new AutoTimeZonePreferenceController( 82 mContext, null /* callback */, false /* fromSUW */); 83 84 assertThat(mController.isAvailable()).isFalse(); 85 } 86 87 @Test isFromSUW_notEnable()88 public void isFromSUW_notEnable() { 89 mController = 90 new AutoTimeZonePreferenceController(mContext, null /* callback */, true /* fromSUW */); 91 92 assertThat(mController.isEnabled()).isFalse(); 93 } 94 95 @Test isWifiOnly_notEnable()96 public void isWifiOnly_notEnable() { 97 when(mTelephonyManager.isDataCapable()).thenReturn(false); 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