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 
30 import androidx.preference.PreferenceCategory;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
34 import com.android.settings.testutils.shadow.ShadowUserManager;
35 import com.android.settingslib.location.RecentLocationApps;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.annotation.Config;
45 
46 import java.util.ArrayList;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Set;
50 
51 @RunWith(RobolectricTestRunner.class)
52 @Config(shadows = {ShadowUserManager.class})
53 public class RecentLocationRequestPreferenceControllerTest {
54     @Mock
55     private PreferenceScreen mScreen;
56     @Mock
57     private PreferenceCategory mCategory;
58     private Context mContext;
59     private RecentLocationRequestPreferenceController mController;
60     private ShadowUserManager mUserManager;
61 
62     @Before
setUp()63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65         mContext = RuntimeEnvironment.application;
66         mController = spy(
67                 new RecentLocationRequestPreferenceController(mContext, "key"));
68         when(mCategory.getContext()).thenReturn(mContext);
69         when(mScreen.findPreference("key")).thenReturn(mCategory);
70         mUserManager = ShadowUserManager.getShadow();
71         mController.mRecentLocationApps = spy(new RecentLocationApps(mContext));
72     }
73 
74     @Test
updateState_whenAppListMoreThanThree_shouldDisplayTopThreeApps()75     public void updateState_whenAppListMoreThanThree_shouldDisplayTopThreeApps() {
76         final List<RecentLocationApps.Request> requests = createMockRequest(6);
77         when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests);
78 
79         mController.displayPreference(mScreen);
80 
81         verify(mCategory, times(3)).addPreference(any());
82     }
83 
84     @Test
updateState_workProfile_shouldShowOnlyWorkProfileApps()85     public void updateState_workProfile_shouldShowOnlyWorkProfileApps() {
86         final List<RecentLocationApps.Request> requests = createMockRequest(6);
87         when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests);
88         mController.setProfileType(ProfileSelectFragment.ProfileType.WORK);
89         final Set<Integer> profileIds = new HashSet<>();
90         profileIds.add(4);
91         profileIds.add(5);
92         mUserManager.setManagedProfiles(profileIds);
93 
94         mController.displayPreference(mScreen);
95 
96         // contains userId 4 and userId 5
97         verify(mCategory, times(2)).addPreference(any());
98     }
99 
100     @Test
updateState_Personal_shouldShowOnlyPersonalApps()101     public void updateState_Personal_shouldShowOnlyPersonalApps() {
102         final List<RecentLocationApps.Request> requests = createMockRequest(6);
103         when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests);
104         mController.setProfileType(ProfileSelectFragment.ProfileType.PERSONAL);
105         final Set<Integer> profileIds = new HashSet<>();
106         for (int i = 0; i < 4; i++) {
107             profileIds.add(i);
108         }
109         mUserManager.setManagedProfiles(profileIds);
110 
111         mController.displayPreference(mScreen);
112 
113         // contains userId 4 and userId 5
114         verify(mCategory, times(2)).addPreference(any());
115     }
116 
createMockRequest(int count)117     private List<RecentLocationApps.Request> createMockRequest(int count) {
118         final List<RecentLocationApps.Request> requests = new ArrayList<>();
119         for (int i = 0; i < count; i++) {
120             final Drawable icon = mock(Drawable.class);
121             // Add mock accesses
122             final RecentLocationApps.Request request = new RecentLocationApps.Request(
123                     "packageName", UserHandle.of(i), icon,
124                     "appTitle" + i, false, "appSummary" + i, 1000 - i);
125             requests.add(request);
126         }
127         return requests;
128     }
129 }
130