Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 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.internal.app; |
| 18 | |
Zhen Zhang | fc7b8aa | 2019-10-22 11:05:38 -0700 | [diff] [blame] | 19 | import static junit.framework.Assert.assertEquals; |
| 20 | |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 21 | import static org.hamcrest.CoreMatchers.is; |
| 22 | import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | import static org.mockito.Matchers.any; |
| 24 | import static org.mockito.Matchers.anyInt; |
| 25 | import static org.mockito.Matchers.anyLong; |
| 26 | import static org.mockito.Matchers.anyString; |
| 27 | import static org.mockito.Mockito.doAnswer; |
| 28 | import static org.mockito.Mockito.when; |
| 29 | |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 30 | import android.app.usage.IUsageStatsManager; |
| 31 | import android.app.usage.UsageStats; |
| 32 | import android.app.usage.UsageStatsManager; |
| 33 | import android.content.Context; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 34 | import android.content.Intent; |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
| 36 | import android.content.pm.ParceledListSlice; |
| 37 | import android.content.pm.ResolveInfo; |
| 38 | import android.content.res.Configuration; |
| 39 | import android.content.res.Resources; |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 40 | import android.os.RemoteException; |
| 41 | import android.os.UserHandle; |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 42 | import android.util.ArrayMap; |
| 43 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 44 | import androidx.test.runner.AndroidJUnit4; |
| 45 | |
Zhen Zhang | aa458b1 | 2019-10-16 12:46:44 -0700 | [diff] [blame] | 46 | import com.android.internal.app.ResolverActivity.ResolvedComponentInfo; |
| 47 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
| 53 | import org.mockito.invocation.InvocationOnMock; |
| 54 | import org.mockito.stubbing.Answer; |
| 55 | |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 56 | import java.util.ArrayList; |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Locale; |
| 59 | import java.util.Map; |
| 60 | |
| 61 | /** |
| 62 | * ResolverListController Test. |
| 63 | */ |
| 64 | @RunWith(AndroidJUnit4.class) |
| 65 | public 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, |
| 118 | refererPackage, UserHandle.USER_CURRENT); |
Zhen Zhang | aa458b1 | 2019-10-16 12:46:44 -0700 | [diff] [blame] | 119 | mController.sort(new ArrayList<ResolvedComponentInfo>()); |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 120 | 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 Zhang | aa458b1 | 2019-10-16 12:46:44 -0700 | [diff] [blame] | 126 | @Test |
| 127 | public void topKEqualsToSort() { |
| 128 | String annotation = "test_annotation"; |
| 129 | Intent sendIntent = createSendImageIntent(annotation); |
| 130 | String refererPackage = "test_referer_package"; |
Zhen Zhang | fc7b8aa | 2019-10-22 11:05:38 -0700 | [diff] [blame] | 131 | List<ResolvedComponentInfo> resolvedComponents = createResolvedComponentsForTest(10); |
Zhen Zhang | aa458b1 | 2019-10-16 12:46:44 -0700 | [diff] [blame] | 132 | mUsm = new UsageStatsManager(mMockContext, mMockService); |
| 133 | when(mMockContext.getSystemService(Context.USAGE_STATS_SERVICE)).thenReturn(mUsm); |
| 134 | mController = new ResolverListController(mMockContext, mMockPackageManager, sendIntent, |
| 135 | refererPackage, UserHandle.USER_CURRENT); |
| 136 | List<ResolvedComponentInfo> topKList = new ArrayList<>(resolvedComponents); |
| 137 | mController.topK(topKList, 5); |
| 138 | List<ResolvedComponentInfo> sortList = new ArrayList<>(topKList); |
| 139 | mController.sort(sortList); |
Zhen Zhang | fc7b8aa | 2019-10-22 11:05:38 -0700 | [diff] [blame] | 140 | 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 Zhang | aa458b1 | 2019-10-16 12:46:44 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Kang Li | 7c37964 | 2017-02-24 17:41:07 -0800 | [diff] [blame] | 154 | 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 Zhang | aa458b1 | 2019-10-16 12:46:44 -0700 | [diff] [blame] | 191 | |
| 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 | } |