1 /*
2  * Copyright (C) 2021 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.applications;
18 
19 import static com.android.settings.applications.AppsPreferenceController.KEY_ALL_APP_INFO;
20 import static com.android.settings.applications.AppsPreferenceController.KEY_GENERAL_CATEGORY;
21 import static com.android.settings.applications.AppsPreferenceController.KEY_RECENT_APPS_CATEGORY;
22 import static com.android.settings.applications.AppsPreferenceController.KEY_SEE_ALL;
23 import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 
34 import android.app.usage.UsageStats;
35 import android.content.Context;
36 import android.content.pm.ApplicationInfo;
37 import android.os.UserHandle;
38 
39 import androidx.fragment.app.Fragment;
40 import androidx.preference.Preference;
41 import androidx.preference.PreferenceCategory;
42 import androidx.preference.PreferenceScreen;
43 
44 import com.android.settingslib.applications.ApplicationsState;
45 
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.robolectric.RobolectricTestRunner;
52 import org.robolectric.RuntimeEnvironment;
53 import org.robolectric.util.ReflectionHelpers;
54 
55 import java.util.ArrayList;
56 import java.util.List;
57 
58 @RunWith(RobolectricTestRunner.class)
59 public class AppsPreferenceControllerTest {
60 
61     @Mock
62     private ApplicationsState mAppState;
63     @Mock
64     private ApplicationsState.AppEntry mAppEntry;
65     @Mock
66     private ApplicationInfo mApplicationInfo;
67     @Mock
68     private Fragment mFragment;
69     @Mock
70     private PreferenceScreen mScreen;
71 
72     private AppsPreferenceController mController;
73     private List<RecentAppStatsMixin.UsageStatsWrapper> mUsageStats;
74     private PreferenceCategory mRecentAppsCategory;
75     private PreferenceCategory mGeneralCategory;
76     private Preference mSeeAllPref;
77     private Preference mAllAppsInfoPref;
78 
79     @Before
setUp()80     public void setUp() {
81         MockitoAnnotations.initMocks(this);
82         final Context context = RuntimeEnvironment.application;
83         ReflectionHelpers.setStaticField(ApplicationsState.class, "sInstance", mAppState);
84 
85         mRecentAppsCategory = spy(new PreferenceCategory(context));
86         mGeneralCategory = new PreferenceCategory(context);
87         mSeeAllPref = new Preference(context);
88         mAllAppsInfoPref = new Preference(context);
89         when(mScreen.findPreference(KEY_RECENT_APPS_CATEGORY)).thenReturn(mRecentAppsCategory);
90         when(mScreen.findPreference(KEY_GENERAL_CATEGORY)).thenReturn(mGeneralCategory);
91         when(mScreen.findPreference(KEY_SEE_ALL)).thenReturn(mSeeAllPref);
92         when(mScreen.findPreference(KEY_ALL_APP_INFO)).thenReturn(mAllAppsInfoPref);
93 
94         mController = spy(new AppsPreferenceController(context));
95         mController.setFragment(mFragment);
96         mController.mRecentAppsCategory = mRecentAppsCategory;
97         mController.mGeneralCategory = mGeneralCategory;
98         mController.mSeeAllPref = mSeeAllPref;
99         mController.mAllAppsInfoPref = mAllAppsInfoPref;
100     }
101 
102     @Test
getAvailabilityStatus_shouldReturnAVAILABLE_UNSEARCHABLE()103     public void getAvailabilityStatus_shouldReturnAVAILABLE_UNSEARCHABLE() {
104         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
105     }
106 
107     @Test
displayPreference_noRecentApps_showAllAppsInfo()108     public void displayPreference_noRecentApps_showAllAppsInfo() {
109         doNothing().when(mController).loadAllAppsCount();
110 
111         mController.displayPreference(mScreen);
112 
113         assertThat(mAllAppsInfoPref.isVisible()).isTrue();
114         assertThat(mRecentAppsCategory.isVisible()).isFalse();
115         assertThat(mGeneralCategory.isVisible()).isFalse();
116         assertThat(mSeeAllPref.isVisible()).isFalse();
117     }
118 
119     @Test
displayPreference_hasRecentApps_showRecentApps()120     public void displayPreference_hasRecentApps_showRecentApps() {
121         doNothing().when(mController).loadAllAppsCount();
122         doReturn(true).when(mRecentAppsCategory).addPreference(any());
123         initRecentApps();
124         doReturn(mUsageStats).when(mController).loadRecentApps();
125 
126         mController.displayPreference(mScreen);
127 
128         assertThat(mAllAppsInfoPref.isVisible()).isFalse();
129         assertThat(mRecentAppsCategory.isVisible()).isTrue();
130         assertThat(mGeneralCategory.isVisible()).isTrue();
131         assertThat(mSeeAllPref.isVisible()).isTrue();
132     }
133 
134     @Test
updateState_shouldRefreshUi()135     public void updateState_shouldRefreshUi() {
136         doNothing().when(mController).loadAllAppsCount();
137 
138         mController.updateState(mRecentAppsCategory);
139 
140         verify(mController).refreshUi();
141     }
142 
initRecentApps()143     private void initRecentApps() {
144         mUsageStats = new ArrayList<>();
145         final UsageStats stat1 = new UsageStats();
146         final UsageStats stat2 = new UsageStats();
147         final UsageStats stat3 = new UsageStats();
148         stat1.mLastTimeUsed = System.currentTimeMillis();
149         stat1.mPackageName = "pkg.class";
150         mUsageStats.add(statsWrapperOf(stat1));
151 
152         stat2.mLastTimeUsed = System.currentTimeMillis();
153         stat2.mPackageName = "pkg.class2";
154         mUsageStats.add(statsWrapperOf(stat2));
155 
156         stat3.mLastTimeUsed = System.currentTimeMillis();
157         stat3.mPackageName = "pkg.class3";
158         mUsageStats.add(statsWrapperOf(stat3));
159         when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
160                 .thenReturn(mAppEntry);
161         when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
162                 .thenReturn(mAppEntry);
163         when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
164                 .thenReturn(mAppEntry);
165         mAppEntry.info = mApplicationInfo;
166     }
167 
statsWrapperOf( UsageStats stats)168     private static RecentAppStatsMixin.UsageStatsWrapper statsWrapperOf(
169             UsageStats stats) {
170         return new RecentAppStatsMixin.UsageStatsWrapper(stats, /* userId= */ 0);
171     }
172 }
173