blob: 64906bb27ff07318f1f200236fb697ad3077a69a [file] [log] [blame]
Kang Li7c379642017-02-24 17:41:07 -08001/*
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
17package com.android.internal.app;
18
Zhen Zhangfc7b8aa2019-10-22 11:05:38 -070019import static junit.framework.Assert.assertEquals;
20
Kang Li7c379642017-02-24 17:41:07 -080021import static org.hamcrest.CoreMatchers.is;
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.mockito.Matchers.any;
24import static org.mockito.Matchers.anyInt;
25import static org.mockito.Matchers.anyLong;
26import static org.mockito.Matchers.anyString;
27import static org.mockito.Mockito.doAnswer;
28import static org.mockito.Mockito.when;
29
Kang Li7c379642017-02-24 17:41:07 -080030import android.app.usage.IUsageStatsManager;
31import android.app.usage.UsageStats;
32import android.app.usage.UsageStatsManager;
33import android.content.Context;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090034import android.content.Intent;
Kang Li7c379642017-02-24 17:41:07 -080035import android.content.pm.PackageManager;
36import android.content.pm.ParceledListSlice;
37import android.content.pm.ResolveInfo;
38import android.content.res.Configuration;
39import android.content.res.Resources;
Kang Li7c379642017-02-24 17:41:07 -080040import android.os.RemoteException;
41import android.os.UserHandle;
Kang Li7c379642017-02-24 17:41:07 -080042import android.util.ArrayMap;
43
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090044import androidx.test.runner.AndroidJUnit4;
45
Zhen Zhangaa458b12019-10-16 12:46:44 -070046import com.android.internal.app.ResolverActivity.ResolvedComponentInfo;
47
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090048import org.junit.Before;
49import org.junit.Test;
50import org.junit.runner.RunWith;
51import org.mockito.Mock;
52import org.mockito.MockitoAnnotations;
53import org.mockito.invocation.InvocationOnMock;
54import org.mockito.stubbing.Answer;
55
Kang Li7c379642017-02-24 17:41:07 -080056import java.util.ArrayList;
Kang Li7c379642017-02-24 17:41:07 -080057import java.util.List;
58import java.util.Locale;
59import java.util.Map;
60
61/**
62 * ResolverListController Test.
63 */
64@RunWith(AndroidJUnit4.class)
65public class ResolverListControllerTest {
66
67 @Mock private Context mMockContext;
68 @Mock private PackageManager mMockPackageManager;
69 @Mock private Resources mMockResources;
70 @Mock private IUsageStatsManager mMockService;
71
72 private ResolverListController mController;
73 private UsageStatsManager mUsm;
74
75 @Before
76 public void setUp() throws Exception {
77 MockitoAnnotations.initMocks(this);
78 Configuration config = new Configuration();
79 config.locale = Locale.getDefault();
80 List<ResolveInfo> services = new ArrayList<>();
81 when(mMockPackageManager.queryIntentServices(any(), anyInt())).thenReturn(services);
82 when(mMockResources.getConfiguration()).thenReturn(config);
83 when(mMockContext.getResources()).thenReturn(mMockResources);
84 when(mMockContext.getPackageName()).thenReturn("android");
85 when(mMockContext.getUserId()).thenReturn(54321);
86 when(mMockContext.getSharedPreferences(anyString(), anyInt())).thenReturn(null);
87 when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager);
88 }
89
90 @Test
91 public void reportChooserSelection() throws InterruptedException, RemoteException {
92 String annotation = "test_annotation";
93 Intent sendIntent = createSendImageIntent(annotation);
94 String refererPackage = "test_referer_package";
95 String packageName = "test_package";
96 String action = "test_action";
97 final int initialCount = 1234;
98 UsageStats packageStats = initStats(packageName, action, annotation, initialCount);
99 UsageStats oneClickStats = initStats(packageName, action, annotation, 1);
100 final List<UsageStats> slices = new ArrayList<>();
101 slices.add(packageStats);
102 ParceledListSlice<UsageStats> stats = new ParceledListSlice<>(slices);
103 when(mMockService.queryUsageStats(anyInt(), anyLong(), anyLong(), anyString()))
104 .thenReturn(stats);
105 Answer<Void> answer = new Answer<Void>() {
106 @Override
107 public Void answer(InvocationOnMock invocation) throws Throwable {
108 slices.add(oneClickStats);
109 return null;
110 }
111 };
112 doAnswer(answer).when(mMockService).reportChooserSelection(
113 anyString(), anyInt(), anyString(), any(), anyString());
114 when(mMockContext.getOpPackageName()).thenReturn(refererPackage);
115 mUsm = new UsageStatsManager(mMockContext, mMockService);
116 when(mMockContext.getSystemService(Context.USAGE_STATS_SERVICE)).thenReturn(mUsm);
117 mController = new ResolverListController(mMockContext, mMockPackageManager, sendIntent,
arangelov2c1c37a2019-12-06 14:43:34 +0000118 refererPackage, UserHandle.USER_CURRENT, /* userHandle */ UserHandle.SYSTEM);
Zhen Zhangaa458b12019-10-16 12:46:44 -0700119 mController.sort(new ArrayList<ResolvedComponentInfo>());
Kang Li7c379642017-02-24 17:41:07 -0800120 long beforeReport = getCount(mUsm, packageName, action, annotation);
121 mController.updateChooserCounts(packageName, UserHandle.USER_CURRENT, action);
122 long afterReport = getCount(mUsm, packageName, action, annotation);
123 assertThat(afterReport, is(beforeReport + 1l));
124 }
125
Zhen Zhangaa458b12019-10-16 12:46:44 -0700126 @Test
127 public void topKEqualsToSort() {
128 String annotation = "test_annotation";
129 Intent sendIntent = createSendImageIntent(annotation);
130 String refererPackage = "test_referer_package";
Zhen Zhangfc7b8aa2019-10-22 11:05:38 -0700131 List<ResolvedComponentInfo> resolvedComponents = createResolvedComponentsForTest(10);
Zhen Zhangaa458b12019-10-16 12:46:44 -0700132 mUsm = new UsageStatsManager(mMockContext, mMockService);
133 when(mMockContext.getSystemService(Context.USAGE_STATS_SERVICE)).thenReturn(mUsm);
134 mController = new ResolverListController(mMockContext, mMockPackageManager, sendIntent,
arangelov2c1c37a2019-12-06 14:43:34 +0000135 refererPackage, UserHandle.USER_CURRENT, /* userHandle */ UserHandle.SYSTEM);
Zhen Zhangaa458b12019-10-16 12:46:44 -0700136 List<ResolvedComponentInfo> topKList = new ArrayList<>(resolvedComponents);
137 mController.topK(topKList, 5);
138 List<ResolvedComponentInfo> sortList = new ArrayList<>(topKList);
139 mController.sort(sortList);
Zhen Zhangfc7b8aa2019-10-22 11:05:38 -0700140 assertEquals("Top k elements should be sorted when input size greater than k.",
141 sortList.subList(0, 5), topKList.subList(0, 5));
142 mController.topK(topKList, 10);
143 sortList = new ArrayList<>(topKList);
144 mController.sort(sortList);
145 assertEquals("All elements should be sorted when input size equals k.",
146 sortList, topKList);
147 mController.topK(topKList, 15);
148 sortList = new ArrayList<>(topKList);
149 mController.sort(sortList);
150 assertEquals("All elements should be sorted when input size less than k.",
151 sortList, topKList);
Zhen Zhangaa458b12019-10-16 12:46:44 -0700152 }
153
Kang Li7c379642017-02-24 17:41:07 -0800154 private UsageStats initStats(String packageName, String action,
155 String annotation, int count) {
156 ArrayMap<String, ArrayMap<String, Integer>> chooserCounts = new ArrayMap<>();
157 ArrayMap<String, Integer> counts = new ArrayMap<>();
158 counts.put(annotation, count);
159 chooserCounts.put(action, counts);
160 UsageStats packageStats = new UsageStats();
161 packageStats.mPackageName = packageName;
162 packageStats.mChooserCounts = chooserCounts;
163 return packageStats;
164 }
165
166 private Intent createSendImageIntent(String annotation) {
167 Intent sendIntent = new Intent();
168 sendIntent.setAction(Intent.ACTION_SEND);
169 sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending");
170 sendIntent.setType("image/jpeg");
171 ArrayList<String> annotations = new ArrayList<>();
172 annotations.add(annotation);
173 sendIntent.putStringArrayListExtra(Intent.EXTRA_CONTENT_ANNOTATIONS, annotations);
174 return sendIntent;
175 }
176
177 private Integer getCount(
178 UsageStatsManager usm, String packageName, String action, String annotation) {
179 if (usm == null) {
180 return 0;
181 }
182 Map<String, UsageStats> stats =
183 usm.queryAndAggregateUsageStats(Long.MIN_VALUE, Long.MAX_VALUE);
184 UsageStats packageStats = stats.get(packageName);
185 if (packageStats == null || packageStats.mChooserCounts == null
186 || packageStats.mChooserCounts.get(action) == null) {
187 return 0;
188 }
189 return packageStats.mChooserCounts.get(action).getOrDefault(annotation, 0);
190 }
Zhen Zhangaa458b12019-10-16 12:46:44 -0700191
192 private List<ResolvedComponentInfo> createResolvedComponentsForTest(int numberOfResults) {
193 List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults);
194 for (int i = 0; i < numberOfResults; i++) {
195 infoList.add(ResolverDataProvider.createResolvedComponentInfo(i));
196 }
197 return infoList;
198 }
199}