1 /* 2 * Copyright (C) 2019 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.location; 18 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.times; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.graphics.drawable.Drawable; 28 import android.os.UserHandle; 29 import android.provider.DeviceConfig; 30 import android.provider.Settings; 31 32 import androidx.preference.PreferenceCategory; 33 import androidx.preference.PreferenceScreen; 34 35 import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; 36 import com.android.settings.dashboard.profileselector.ProfileSelectFragment; 37 import com.android.settings.testutils.shadow.ShadowDeviceConfig; 38 import com.android.settings.testutils.shadow.ShadowUserManager; 39 import com.android.settingslib.location.RecentLocationApps; 40 41 import org.junit.After; 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.RuntimeEnvironment; 49 import org.robolectric.annotation.Config; 50 51 import java.util.ArrayList; 52 import java.util.HashSet; 53 import java.util.List; 54 import java.util.Set; 55 56 @RunWith(RobolectricTestRunner.class) 57 @Config(shadows = {ShadowDeviceConfig.class, ShadowUserManager.class}) 58 public class RecentLocationRequestPreferenceControllerTest { 59 @Mock 60 private PreferenceScreen mScreen; 61 @Mock 62 private PreferenceCategory mCategory; 63 private Context mContext; 64 private RecentLocationRequestPreferenceController mController; 65 private ShadowUserManager mUserManager; 66 67 @Before setUp()68 public void setUp() { 69 MockitoAnnotations.initMocks(this); 70 mContext = RuntimeEnvironment.application; 71 mController = spy( 72 new RecentLocationRequestPreferenceController(mContext, "key")); 73 when(mCategory.getContext()).thenReturn(mContext); 74 when(mScreen.findPreference("key")).thenReturn(mCategory); 75 mUserManager = ShadowUserManager.getShadow(); 76 mController.mRecentLocationApps = spy(new RecentLocationApps(mContext)); 77 } 78 79 @After tearDown()80 public void tearDown() { 81 ShadowDeviceConfig.reset(); 82 } 83 84 @Test updateState_whenAppListMoreThanThree_shouldDisplayTopThreeApps()85 public void updateState_whenAppListMoreThanThree_shouldDisplayTopThreeApps() { 86 final List<RecentLocationApps.Request> requests = createMockRequest(6); 87 when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests); 88 89 mController.displayPreference(mScreen); 90 91 verify(mCategory, times(3)).addPreference(any()); 92 } 93 94 @Test updateState_whenAppListMoreThanThree_showSystem()95 public void updateState_whenAppListMoreThanThree_showSystem() { 96 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_PRIVACY, 97 SystemUiDeviceConfigFlags.PROPERTY_LOCATION_INDICATORS_SMALL_ENABLED, 98 Boolean.toString(true), 99 true); 100 when(mController.mRecentLocationApps.getAppListSorted(false)) 101 .thenReturn(createMockRequest(2)); 102 when(mController.mRecentLocationApps.getAppListSorted(true)) 103 .thenReturn(createMockRequest(3)); 104 105 mController.displayPreference(mScreen); 106 verify(mCategory, times(2)).addPreference(any()); 107 108 Settings.Secure.putInt( 109 mContext.getContentResolver(), 110 Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 111 1); 112 113 mController.displayPreference(mScreen); 114 verify(mCategory, times(5)).addPreference(any()); 115 } 116 117 @Test updateState_workProfile_shouldShowOnlyWorkProfileApps()118 public void updateState_workProfile_shouldShowOnlyWorkProfileApps() { 119 final List<RecentLocationApps.Request> requests = createMockRequest(6); 120 when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests); 121 mController.setProfileType(ProfileSelectFragment.ProfileType.WORK); 122 final Set<Integer> profileIds = new HashSet<>(); 123 profileIds.add(4); 124 profileIds.add(5); 125 mUserManager.setManagedProfiles(profileIds); 126 127 mController.displayPreference(mScreen); 128 129 // contains userId 4 and userId 5 130 verify(mCategory, times(2)).addPreference(any()); 131 } 132 133 @Test updateState_Personal_shouldShowOnlyPersonalApps()134 public void updateState_Personal_shouldShowOnlyPersonalApps() { 135 final List<RecentLocationApps.Request> requests = createMockRequest(6); 136 when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests); 137 mController.setProfileType(ProfileSelectFragment.ProfileType.PERSONAL); 138 final Set<Integer> profileIds = new HashSet<>(); 139 for (int i = 0; i < 4; i++) { 140 profileIds.add(i); 141 } 142 mUserManager.setManagedProfiles(profileIds); 143 144 mController.displayPreference(mScreen); 145 146 // contains userId 4 and userId 5 147 verify(mCategory, times(2)).addPreference(any()); 148 } 149 createMockRequest(int count)150 private List<RecentLocationApps.Request> createMockRequest(int count) { 151 final List<RecentLocationApps.Request> requests = new ArrayList<>(); 152 for (int i = 0; i < count; i++) { 153 final Drawable icon = mock(Drawable.class); 154 // Add mock accesses 155 final RecentLocationApps.Request request = new RecentLocationApps.Request( 156 "packageName", UserHandle.of(i), icon, 157 "appTitle" + i, false, "appSummary" + i, 1000 - i); 158 requests.add(request); 159 } 160 return requests; 161 } 162 } 163