1 /* 2 * Copyright (C) 2017 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 package com.android.settings.location; 17 18 import static org.mockito.ArgumentMatchers.anyInt; 19 import static org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.mock; 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.os.UserManager; 27 import android.provider.Settings; 28 import android.widget.Switch; 29 30 import androidx.lifecycle.LifecycleOwner; 31 32 import com.android.settings.widget.SettingsMainSwitchBar; 33 import com.android.settingslib.RestrictedLockUtils; 34 import com.android.settingslib.core.lifecycle.Lifecycle; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.RuntimeEnvironment; 43 import org.robolectric.util.ReflectionHelpers; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class LocationSwitchBarControllerTest { 47 48 @Mock 49 private SettingsMainSwitchBar mSwitchBar; 50 @Mock 51 private Switch mSwitch; 52 @Mock 53 private LocationEnabler mEnabler; 54 55 private Context mContext; 56 private LocationSwitchBarController mController; 57 private LifecycleOwner mLifecycleOwner; 58 private Lifecycle mLifecycle; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 mContext = RuntimeEnvironment.application; 64 ReflectionHelpers.setField(mSwitchBar, "mSwitch", mSwitch); 65 mLifecycleOwner = () -> mLifecycle; 66 mLifecycle = new Lifecycle(mLifecycleOwner); 67 mController = spy(new LocationSwitchBarController(mContext, mSwitchBar, mLifecycle)); 68 ReflectionHelpers.setField(mController, "mLocationEnabler", mEnabler); 69 } 70 71 @Test onStart_shouldAddOnSwitchChangeListener()72 public void onStart_shouldAddOnSwitchChangeListener() { 73 mController.onStart(); 74 75 verify(mSwitchBar).addOnSwitchChangeListener(mController); 76 } 77 78 @Test onStop_shouldRemoveOnSwitchChangeListener()79 public void onStop_shouldRemoveOnSwitchChangeListener() { 80 mController.onStart(); 81 mController.onStop(); 82 83 verify(mSwitchBar).removeOnSwitchChangeListener(mController); 84 } 85 86 @Test onSwitchChanged_switchChecked_shouldSetLocationEnabled()87 public void onSwitchChanged_switchChecked_shouldSetLocationEnabled() { 88 mController.onCheckedChanged(mSwitch, true); 89 90 verify(mEnabler).setLocationEnabled(true); 91 } 92 93 @Test onSwitchChanged_switchUnchecked_shouldSetLocationDisabled()94 public void onSwitchChanged_switchUnchecked_shouldSetLocationDisabled() { 95 mController.onCheckedChanged(mSwitch, false); 96 97 verify(mEnabler).setLocationEnabled(false); 98 } 99 100 @Test onLocationModeChanged_hasEnforcedAdmin_shouldDisableSwitchByAdmin()101 public void onLocationModeChanged_hasEnforcedAdmin_shouldDisableSwitchByAdmin() { 102 final RestrictedLockUtils.EnforcedAdmin admin = 103 mock(RestrictedLockUtils.EnforcedAdmin.class); 104 doReturn(admin).when(mEnabler).getShareLocationEnforcedAdmin(anyInt()); 105 doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt()); 106 107 mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false); 108 109 verify(mSwitchBar).setDisabledByAdmin(admin); 110 } 111 112 @Test onLocationModeChanged_Restricted_shouldDisableSwitchByAdmin()113 public void onLocationModeChanged_Restricted_shouldDisableSwitchByAdmin() { 114 final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtils.EnforcedAdmin 115 .createDefaultEnforcedAdminWithRestriction(UserManager.DISALLOW_SHARE_LOCATION); 116 doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt()); 117 doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt()); 118 119 mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, true); 120 121 verify(mSwitchBar).setDisabledByAdmin(admin); 122 } 123 124 @Test onLocationModeChanged_Restricted_shouldDisableSwitch()125 public void onLocationModeChanged_Restricted_shouldDisableSwitch() { 126 doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt()); 127 doReturn(true).when(mEnabler).hasShareLocationRestriction(anyInt()); 128 129 mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false); 130 131 verify(mSwitchBar).setEnabled(true); 132 } 133 134 @Test onLocationModeChanged_notRestricted_shouldEnableSwitch()135 public void onLocationModeChanged_notRestricted_shouldEnableSwitch() { 136 doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt()); 137 doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt()); 138 139 mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false); 140 141 verify(mSwitchBar).setEnabled(true); 142 } 143 144 @Test onLocationModeChanged_locationOn_shouldCheckSwitch()145 public void onLocationModeChanged_locationOn_shouldCheckSwitch() { 146 doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt()); 147 doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt()); 148 when(mSwitchBar.isChecked()).thenReturn(false); 149 doReturn(true).when(mEnabler).isEnabled(anyInt()); 150 151 mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false); 152 153 verify(mSwitchBar).setChecked(true); 154 } 155 156 @Test onLocationModeChanged_locationOff_shouldUncheckSwitch()157 public void onLocationModeChanged_locationOff_shouldUncheckSwitch() { 158 doReturn(null).when(mEnabler).getShareLocationEnforcedAdmin(anyInt()); 159 doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt()); 160 when(mSwitchBar.isChecked()).thenReturn(true); 161 162 mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_OFF, false); 163 164 verify(mSwitchBar).setChecked(false); 165 } 166 } 167