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