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 com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.ArgumentMatchers.anyBoolean; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.ArgumentMatchers.eq; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.reset; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.content.Context; 32 import android.content.Intent; 33 import android.content.pm.UserInfo; 34 import android.os.UserHandle; 35 import android.os.UserManager; 36 import android.provider.Settings; 37 import android.text.TextUtils; 38 39 import androidx.lifecycle.LifecycleOwner; 40 41 import com.android.settings.testutils.shadow.ShadowSecureSettings; 42 import com.android.settingslib.core.lifecycle.Lifecycle; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.ArgumentMatcher; 48 import org.mockito.Mock; 49 import org.mockito.MockitoAnnotations; 50 import org.robolectric.RobolectricTestRunner; 51 import org.robolectric.RuntimeEnvironment; 52 import org.robolectric.annotation.Config; 53 54 import java.util.ArrayList; 55 import java.util.List; 56 57 @RunWith(RobolectricTestRunner.class) 58 @Config(shadows = ShadowSecureSettings.class) 59 public class LocationEnablerTest { 60 61 @Mock 62 private UserManager mUserManager; 63 @Mock 64 private LocationEnabler.LocationModeChangeListener mListener; 65 66 private Context mContext; 67 private LocationEnabler mEnabler; 68 private LifecycleOwner mLifecycleOwner; 69 private Lifecycle mLifecycle; 70 71 @Before setUp()72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 mContext = spy(RuntimeEnvironment.application); 75 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 76 mLifecycleOwner = () -> mLifecycle; 77 mLifecycle = new Lifecycle(mLifecycleOwner); 78 mEnabler = spy(new LocationEnabler(mContext, mListener, mLifecycle)); 79 } 80 81 @Test onStart_shouldSetActiveAndRegisterListener()82 public void onStart_shouldSetActiveAndRegisterListener() { 83 mEnabler.onStart(); 84 85 verify(mContext).registerReceiver(eq(mEnabler.mReceiver), 86 eq(LocationEnabler.INTENT_FILTER_LOCATION_MODE_CHANGED)); 87 } 88 89 @Test onStart_shouldRefreshLocationMode()90 public void onStart_shouldRefreshLocationMode() { 91 mEnabler.onStart(); 92 93 verify(mEnabler).refreshLocationMode(); 94 } 95 96 @Test onStop_shouldUnregisterListener()97 public void onStop_shouldUnregisterListener() { 98 mEnabler.onStart(); 99 mEnabler.onStop(); 100 101 verify(mContext).unregisterReceiver(mEnabler.mReceiver); 102 } 103 104 @Test onReceive_shouldRefreshLocationMode()105 public void onReceive_shouldRefreshLocationMode() { 106 mEnabler.onStart(); 107 reset(mListener); 108 mEnabler.mReceiver.onReceive(mContext, new Intent()); 109 110 verify(mListener).onLocationModeChanged(anyInt(), anyBoolean()); 111 } 112 113 @Test isEnabled_locationOff_shouldReturnFalse()114 public void isEnabled_locationOff_shouldReturnFalse() { 115 assertThat(mEnabler.isEnabled(Settings.Secure.LOCATION_MODE_OFF)).isFalse(); 116 } 117 118 @Test isEnabled_restricted_shouldReturnFalse()119 public void isEnabled_restricted_shouldReturnFalse() { 120 when(mUserManager.hasUserRestriction(anyString())).thenReturn(true); 121 122 assertThat(mEnabler.isEnabled(Settings.Secure.LOCATION_MODE_OFF)).isFalse(); 123 } 124 125 @Test isEnabled_locationNotRestricted_shouldReturnTrue()126 public void isEnabled_locationNotRestricted_shouldReturnTrue() { 127 when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); 128 129 assertThat(mEnabler.isEnabled(Settings.Secure.LOCATION_MODE_BATTERY_SAVING)).isTrue(); 130 } 131 132 @Test refreshLocationMode_shouldCallOnLocationModeChanged()133 public void refreshLocationMode_shouldCallOnLocationModeChanged() { 134 mEnabler.refreshLocationMode(); 135 136 verify(mListener).onLocationModeChanged(anyInt(), anyBoolean()); 137 } 138 139 @Test setLocationEnabled_notRestricted_shouldRefreshLocation()140 public void setLocationEnabled_notRestricted_shouldRefreshLocation() { 141 when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); 142 Settings.Secure.putInt(mContext.getContentResolver(), 143 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF); 144 mEnabler.setLocationEnabled(true); 145 146 verify(mEnabler).refreshLocationMode(); 147 } 148 149 @Test setLocationEnabled_notRestricted_shouldBroadcastUpdateAndSetChanger()150 public void setLocationEnabled_notRestricted_shouldBroadcastUpdateAndSetChanger() { 151 when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); 152 Settings.Secure.putInt(mContext.getContentResolver(), 153 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF); 154 mEnabler.setLocationEnabled(true); 155 156 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 157 Settings.Secure.LOCATION_CHANGER, Settings.Secure.LOCATION_CHANGER_UNKNOWN)) 158 .isEqualTo(Settings.Secure.LOCATION_CHANGER_SYSTEM_SETTINGS); 159 } 160 161 @Test isManagedProfileRestrictedByBase_notManagedProfile_shouldReturnFalse()162 public void isManagedProfileRestrictedByBase_notManagedProfile_shouldReturnFalse() { 163 assertThat(mEnabler.isManagedProfileRestrictedByBase()).isFalse(); 164 } 165 166 @Test isManagedProfileRestrictedByBase_notRestricted_shouldReturnFalse()167 public void isManagedProfileRestrictedByBase_notRestricted_shouldReturnFalse() { 168 mockManagedProfile(); 169 doReturn(false).when(mEnabler).hasShareLocationRestriction(anyInt()); 170 171 assertThat(mEnabler.isManagedProfileRestrictedByBase()).isFalse(); 172 } 173 174 @Test isManagedProfileRestrictedByBase_hasManagedProfile_shouldReturnFalse()175 public void isManagedProfileRestrictedByBase_hasManagedProfile_shouldReturnFalse() { 176 mockManagedProfile(); 177 doReturn(true).when(mEnabler).hasShareLocationRestriction(anyInt()); 178 179 assertThat(mEnabler.isManagedProfileRestrictedByBase()).isTrue(); 180 } 181 182 @Test setRestriction_getShareLocationEnforcedAdmin_shouldReturnEnforcedAdmin()183 public void setRestriction_getShareLocationEnforcedAdmin_shouldReturnEnforcedAdmin() { 184 int userId = UserHandle.myUserId(); 185 List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>(); 186 // Add two enforcing users so that RestrictedLockUtils.checkIfRestrictionEnforced returns 187 // non-null. 188 enforcingUsers.add(new UserManager.EnforcingUser(userId, 189 UserManager.RESTRICTION_SOURCE_DEVICE_OWNER)); 190 enforcingUsers.add(new UserManager.EnforcingUser(userId, 191 UserManager.RESTRICTION_SOURCE_PROFILE_OWNER)); 192 when(mUserManager.getUserRestrictionSources( 193 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))) 194 .thenReturn(enforcingUsers); 195 196 assertThat(mEnabler.getShareLocationEnforcedAdmin(userId) != null).isTrue(); 197 } 198 mockManagedProfile()199 private void mockManagedProfile() { 200 final List<UserHandle> userProfiles = new ArrayList<>(); 201 final UserHandle userHandle = mock(UserHandle.class); 202 when(userHandle.getIdentifier()).thenReturn(5); 203 userProfiles.add(userHandle); 204 when(mUserManager.getUserProfiles()).thenReturn(userProfiles); 205 when(mUserManager.getProcessUserId()).thenReturn(1); 206 when(mUserManager.getUserInfo(5)) 207 .thenReturn(new UserInfo(5, "user 5", UserInfo.FLAG_MANAGED_PROFILE)); 208 } 209 actionMatches(String expected)210 private static ArgumentMatcher<Intent> actionMatches(String expected) { 211 return intent -> TextUtils.equals(expected, intent.getAction()); 212 } 213 } 214