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 
17 package com.android.settings.applications;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.mock;
22 
23 import android.content.pm.ApplicationInfo;
24 
25 import com.android.settingslib.applications.AppUtils;
26 import com.android.settingslib.applications.ApplicationsState;
27 import com.android.settingslib.applications.ApplicationsState.AppFilter;
28 
29 import com.android.settingslib.applications.ApplicationsState.CompoundFilter;
30 import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
31 import java.lang.reflect.Field;
32 import org.junit.Test;
33 
34 public class ManageApplicationsTest {
35     @Test
getCompositeFilter_filtersVolumeForAudio()36     public void getCompositeFilter_filtersVolumeForAudio() {
37         AppFilter filter =
38                 ManageApplications.getCompositeFilter(
39                         ManageApplications.LIST_TYPE_STORAGE,
40                         ManageApplications.STORAGE_TYPE_MUSIC,
41                         "uuid");
42         final ApplicationInfo info = new ApplicationInfo();
43         info.volumeUuid = "uuid";
44         info.category = ApplicationInfo.CATEGORY_AUDIO;
45         final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
46         appEntry.info = info;
47 
48         assertThat(filter.filterApp(appEntry)).isTrue();
49     }
50 
51     @Test
getCompositeFilter_filtersVolumeForVideo()52     public void getCompositeFilter_filtersVolumeForVideo() {
53         AppFilter filter =
54                 ManageApplications.getCompositeFilter(
55                         ManageApplications.LIST_TYPE_MOVIES,
56                         ManageApplications.STORAGE_TYPE_DEFAULT,
57                         "uuid");
58         final ApplicationInfo info = new ApplicationInfo();
59         info.volumeUuid = "uuid";
60         info.category = ApplicationInfo.CATEGORY_VIDEO;
61         final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
62         appEntry.info = info;
63 
64         assertThat(filter.filterApp(appEntry)).isTrue();
65     }
66 
67     @Test
getCompositeFilter_filtersVolumeForGames()68     public void getCompositeFilter_filtersVolumeForGames() {
69         ApplicationsState.AppFilter filter =
70                 ManageApplications.getCompositeFilter(
71                         ManageApplications.LIST_TYPE_GAMES,
72                         ManageApplications.STORAGE_TYPE_DEFAULT,
73                         "uuid");
74         final ApplicationInfo info = new ApplicationInfo();
75         info.volumeUuid = "uuid";
76         info.category = ApplicationInfo.CATEGORY_GAME;
77         final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
78         appEntry.info = info;
79 
80         assertThat(filter.filterApp(appEntry)).isTrue();
81     }
82 
83     @Test
getCompositeFilter_isEmptyNormally()84     public void getCompositeFilter_isEmptyNormally() {
85         ApplicationsState.AppFilter filter =
86                 ManageApplications.getCompositeFilter(
87                         ManageApplications.LIST_TYPE_MAIN,
88                         ManageApplications.STORAGE_TYPE_DEFAULT,
89                         "uuid");
90         assertThat(filter).isNull();
91     }
92 
93     @Test
getCompositeFilter_worksWithInstantApps()94     public void getCompositeFilter_worksWithInstantApps() throws Exception {
95         Field field = AppUtils.class.getDeclaredField("sInstantAppDataProvider");
96         field.setAccessible(true);
97         field.set(AppUtils.class, (InstantAppDataProvider) (i -> true));
98 
99         AppFilter filter =
100             ManageApplications.getCompositeFilter(
101                 ManageApplications.LIST_TYPE_STORAGE,
102                 ManageApplications.STORAGE_TYPE_MUSIC,
103                 "uuid");
104         AppFilter composedFilter = new CompoundFilter(ApplicationsState.FILTER_INSTANT, filter);
105 
106         final ApplicationInfo info = new ApplicationInfo();
107         info.volumeUuid = "uuid";
108         info.category = ApplicationInfo.CATEGORY_AUDIO;
109         info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT;
110         final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
111         appEntry.info = info;
112 
113         assertThat(composedFilter.filterApp(appEntry)).isTrue();
114     }
115 }
116