• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.any;
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.admin.DevicePolicyManager;
29 import android.content.ComponentName;
30 import android.content.Context;
31 import android.content.pm.UserInfo;
32 import android.os.UserHandle;
33 import android.os.UserManager;
34 import android.provider.Settings;
35 import android.util.ArrayMap;
36 
37 import androidx.lifecycle.LifecycleOwner;
38 import androidx.preference.Preference;
39 import androidx.preference.PreferenceCategory;
40 import androidx.preference.PreferenceScreen;
41 
42 import com.android.settings.testutils.shadow.ShadowUserManager;
43 import com.android.settings.widget.RestrictedAppPreference;
44 import com.android.settingslib.core.lifecycle.Lifecycle;
45 
46 import org.junit.Before;
47 import org.junit.Ignore;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Answers;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53 import org.robolectric.RobolectricTestRunner;
54 import org.robolectric.RuntimeEnvironment;
55 import org.robolectric.annotation.Config;
56 
57 import java.util.ArrayList;
58 import java.util.List;
59 import java.util.Map;
60 
61 @RunWith(RobolectricTestRunner.class)
62 @Config(shadows = ShadowUserManager.class)
63 public class LocationInjectedServicesPreferenceControllerTest {
64 
65     private static final String KEY_LOCATION_SERVICES = "location_service";
66 
67     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
68     private LocationSettings mFragment;
69     @Mock
70     private PreferenceCategory mCategoryPrimary;
71     @Mock
72     private PreferenceScreen mScreen;
73     @Mock
74     private AppSettingsInjector mSettingsInjector;
75     @Mock
76     private DevicePolicyManager mDevicePolicyManager;
77 
78     private Context mContext;
79     private LocationInjectedServicesPreferenceController mController;
80     private LifecycleOwner mLifecycleOwner;
81     private Lifecycle mLifecycle;
82 
83     @Before
setUp()84     public void setUp() {
85         MockitoAnnotations.initMocks(this);
86         mContext = spy(RuntimeEnvironment.application);
87         mLifecycleOwner = () -> mLifecycle;
88         mLifecycle = new Lifecycle(mLifecycleOwner);
89         mController = spy(
90                 new LocationInjectedServicesPreferenceController(mContext, KEY_LOCATION_SERVICES));
91         when(mFragment.getSettingsLifecycle()).thenReturn(mLifecycle);
92         mController.init(mFragment);
93         mController.mInjector = mSettingsInjector;
94         final String key = mController.getPreferenceKey();
95         when(mScreen.findPreference(key)).thenReturn(mCategoryPrimary);
96         when(mCategoryPrimary.getKey()).thenReturn(key);
97         when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
98                 .thenReturn(mDevicePolicyManager);
99     }
100 
101     @Test
onResume_shouldRegisterListener()102     public void onResume_shouldRegisterListener() {
103         mController.onResume();
104 
105         verify(mContext).registerReceiver(eq(mController.mInjectedSettingsReceiver),
106                 eq(mController.INTENT_FILTER_INJECTED_SETTING_CHANGED),
107                 anyInt());
108     }
109 
110     @Test
onPause_shouldUnregisterListener()111     public void onPause_shouldUnregisterListener() {
112         mController.onResume();
113         mController.onPause();
114 
115         verify(mContext).unregisterReceiver(mController.mInjectedSettingsReceiver);
116     }
117 
118     @Ignore
119     @Test
workProfileDisallowShareLocationOn_getParentUserLocationServicesOnly()120     public void workProfileDisallowShareLocationOn_getParentUserLocationServicesOnly() {
121         final int fakeWorkProfileId = 123;
122         ShadowUserManager.getShadow().setProfileIdsWithDisabled(
123                 new int[]{UserHandle.myUserId(), fakeWorkProfileId});
124         ShadowUserManager.getShadow().addProfile(new UserInfo(UserHandle.myUserId(), "", 0));
125         ShadowUserManager.getShadow().addProfile(new UserInfo(fakeWorkProfileId, "",
126                 UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_PROFILE));
127 
128         // Mock RestrictedLockUtils.checkIfRestrictionEnforced and let it return non-null.
129         final List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
130         enforcingUsers.add(new UserManager.EnforcingUser(fakeWorkProfileId,
131                 UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));
132         final ComponentName componentName = new ComponentName("test", "test");
133         // Ensure that RestrictedLockUtils.checkIfRestrictionEnforced doesn't return null.
134         ShadowUserManager.getShadow().setUserRestrictionSources(
135                 UserManager.DISALLOW_SHARE_LOCATION,
136                 UserHandle.of(fakeWorkProfileId),
137                 enforcingUsers);
138         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
139 
140         mController.displayPreference(mScreen);
141         verify(mSettingsInjector).getInjectedSettings(
142                 any(Context.class), eq(UserHandle.myUserId()));
143     }
144 
145     @Test
workProfileDisallowShareLocationOff_getAllUserLocationServices()146     public void workProfileDisallowShareLocationOff_getAllUserLocationServices() {
147         final int fakeWorkProfileId = 123;
148         ShadowUserManager.getShadow().setProfileIdsWithDisabled(
149                 new int[]{UserHandle.myUserId(), fakeWorkProfileId});
150 
151         // Mock RestrictedLockUtils.checkIfRestrictionEnforced and let it return null.
152         // Empty enforcing users.
153         final List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
154         ShadowUserManager.getShadow().setUserRestrictionSources(
155                 UserManager.DISALLOW_SHARE_LOCATION,
156                 UserHandle.of(fakeWorkProfileId),
157                 enforcingUsers);
158 
159         mController.displayPreference(mScreen);
160         verify(mSettingsInjector).getInjectedSettings(
161                 any(Context.class), eq(UserHandle.USER_CURRENT));
162     }
163 
164     @Test
onLocationModeChanged_shouldRequestReloadInjectedSettigns()165     public void onLocationModeChanged_shouldRequestReloadInjectedSettigns() {
166         mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
167 
168         verify(mSettingsInjector).reloadStatusMessages();
169     }
170 
171     @Ignore
172     @Test
withUserRestriction_shouldDisableLocationAccuracy()173     public void withUserRestriction_shouldDisableLocationAccuracy() {
174         final List<Preference> preferences = new ArrayList<>();
175         final RestrictedAppPreference pref = new RestrictedAppPreference(mContext,
176                 UserManager.DISALLOW_CONFIG_LOCATION);
177         pref.setTitle("Location Accuracy");
178         preferences.add(pref);
179         final Map<Integer, List<Preference>> map = new ArrayMap<>();
180         map.put(UserHandle.myUserId(), preferences);
181         doReturn(map).when(mSettingsInjector)
182                 .getInjectedSettings(any(Context.class), anyInt());
183         ShadowUserManager.getShadow().setProfileIdsWithDisabled(new int[]{UserHandle.myUserId()});
184 
185         final int userId = UserHandle.myUserId();
186         List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
187         enforcingUsers.add(new UserManager.EnforcingUser(userId,
188                 UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));
189         ComponentName componentName = new ComponentName("test", "test");
190         // Ensure that RestrictedLockUtils.checkIfRestrictionEnforced doesn't return null.
191         ShadowUserManager.getShadow().setUserRestrictionSources(
192                 UserManager.DISALLOW_CONFIG_LOCATION,
193                 UserHandle.of(userId),
194                 enforcingUsers);
195         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
196 
197         mController.displayPreference(mScreen);
198 
199         assertThat(pref.isEnabled()).isFalse();
200         assertThat(pref.isDisabledByAdmin()).isTrue();
201     }
202 }
203