Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 1 | package com.android.settingslib.location; |
| 2 | |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 3 | import static com.google.common.truth.Truth.assertThat; |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 4 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 5 | import static org.mockito.ArgumentMatchers.isA; |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 6 | import static org.mockito.Mockito.when; |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 7 | |
| 8 | import android.app.AppOpsManager; |
| 9 | import android.app.AppOpsManager.OpEntry; |
| 10 | import android.app.AppOpsManager.PackageOps; |
| 11 | import android.content.Context; |
| 12 | import android.content.pm.ApplicationInfo; |
| 13 | import android.content.pm.PackageManager; |
| 14 | import android.content.pm.PackageManager.NameNotFoundException; |
| 15 | import android.content.res.Resources; |
| 16 | import android.os.Process; |
| 17 | import android.os.UserHandle; |
| 18 | import android.os.UserManager; |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 19 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 20 | import android.util.LongSparseLongArray; |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.junit.runner.RunWith; |
| 24 | import org.mockito.Mock; |
| 25 | import org.mockito.MockitoAnnotations; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 26 | import org.robolectric.RobolectricTestRunner; |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Collections; |
| 30 | import java.util.List; |
| 31 | import java.util.concurrent.TimeUnit; |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 32 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 33 | @RunWith(RobolectricTestRunner.class) |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 34 | public class RecentLocationAppsTest { |
| 35 | |
| 36 | private static final int TEST_UID = 1234; |
| 37 | private static final long NOW = System.currentTimeMillis(); |
| 38 | // App running duration in milliseconds |
| 39 | private static final int DURATION = 10; |
| 40 | private static final long ONE_MIN_AGO = NOW - TimeUnit.MINUTES.toMillis(1); |
Wei Wang | ec281b1 | 2018-05-01 11:21:31 -0700 | [diff] [blame] | 41 | private static final long TWENTY_THREE_HOURS_AGO = NOW - TimeUnit.HOURS.toMillis(23); |
| 42 | private static final long TWO_DAYS_AGO = NOW - TimeUnit.DAYS.toMillis(2); |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 43 | private static final String[] TEST_PACKAGE_NAMES = |
| 44 | {"package_1MinAgo", "package_14MinAgo", "package_20MinAgo"}; |
| 45 | |
| 46 | @Mock |
| 47 | private Context mContext; |
| 48 | @Mock |
| 49 | private PackageManager mPackageManager; |
| 50 | @Mock |
| 51 | private AppOpsManager mAppOpsManager; |
| 52 | @Mock |
| 53 | private Resources mResources; |
| 54 | @Mock |
| 55 | private UserManager mUserManager; |
| 56 | private int mTestUserId; |
| 57 | private RecentLocationApps mRecentLocationApps; |
| 58 | |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 59 | @Before |
| 60 | public void setUp() throws NameNotFoundException { |
| 61 | MockitoAnnotations.initMocks(this); |
| 62 | |
| 63 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
| 64 | when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager); |
| 65 | when(mContext.getResources()).thenReturn(mResources); |
| 66 | when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); |
| 67 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
| 68 | when(mPackageManager.getApplicationLabel(isA(ApplicationInfo.class))) |
| 69 | .thenReturn("testApplicationLabel"); |
| 70 | when(mPackageManager.getUserBadgedLabel(isA(CharSequence.class), isA(UserHandle.class))) |
| 71 | .thenReturn("testUserBadgedLabel"); |
| 72 | mTestUserId = UserHandle.getUserId(TEST_UID); |
| 73 | when(mUserManager.getUserProfiles()) |
| 74 | .thenReturn(Collections.singletonList(new UserHandle(mTestUserId))); |
| 75 | |
Wei Wang | ec281b1 | 2018-05-01 11:21:31 -0700 | [diff] [blame] | 76 | long[] testRequestTime = {ONE_MIN_AGO, TWENTY_THREE_HOURS_AGO, TWO_DAYS_AGO}; |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 77 | List<PackageOps> appOps = createTestPackageOpsList(TEST_PACKAGE_NAMES, testRequestTime); |
| 78 | when(mAppOpsManager.getPackagesForOps(RecentLocationApps.LOCATION_OPS)).thenReturn(appOps); |
| 79 | mockTestApplicationInfos(mTestUserId, TEST_PACKAGE_NAMES); |
| 80 | |
| 81 | mRecentLocationApps = new RecentLocationApps(mContext); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testGetAppList_shouldFilterRecentApps() { |
| 86 | List<RecentLocationApps.Request> requests = mRecentLocationApps.getAppList(); |
| 87 | // Only two of the apps have requested location within 15 min. |
| 88 | assertThat(requests).hasSize(2); |
| 89 | // Make sure apps are ordered by recency |
| 90 | assertThat(requests.get(0).packageName).isEqualTo(TEST_PACKAGE_NAMES[0]); |
| 91 | assertThat(requests.get(0).requestFinishTime).isEqualTo(ONE_MIN_AGO + DURATION); |
| 92 | assertThat(requests.get(1).packageName).isEqualTo(TEST_PACKAGE_NAMES[1]); |
Wei Wang | ec281b1 | 2018-05-01 11:21:31 -0700 | [diff] [blame] | 93 | assertThat(requests.get(1).requestFinishTime).isEqualTo(TWENTY_THREE_HOURS_AGO + DURATION); |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testGetAppList_shouldNotShowAndroidOS() throws NameNotFoundException { |
| 98 | // Add android OS to the list of apps. |
| 99 | PackageOps androidSystemPackageOps = |
| 100 | createPackageOps( |
| 101 | RecentLocationApps.ANDROID_SYSTEM_PACKAGE_NAME, |
| 102 | Process.SYSTEM_UID, |
| 103 | AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION, |
| 104 | ONE_MIN_AGO, |
| 105 | DURATION); |
| 106 | long[] testRequestTime = |
Wei Wang | ec281b1 | 2018-05-01 11:21:31 -0700 | [diff] [blame] | 107 | {ONE_MIN_AGO, TWENTY_THREE_HOURS_AGO, TWO_DAYS_AGO, ONE_MIN_AGO}; |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 108 | List<PackageOps> appOps = createTestPackageOpsList(TEST_PACKAGE_NAMES, testRequestTime); |
| 109 | appOps.add(androidSystemPackageOps); |
| 110 | when(mAppOpsManager.getPackagesForOps(RecentLocationApps.LOCATION_OPS)).thenReturn(appOps); |
| 111 | mockTestApplicationInfos( |
| 112 | Process.SYSTEM_UID, RecentLocationApps.ANDROID_SYSTEM_PACKAGE_NAME); |
| 113 | |
| 114 | List<RecentLocationApps.Request> requests = mRecentLocationApps.getAppList(); |
| 115 | // Android OS shouldn't show up in the list of apps. |
| 116 | assertThat(requests).hasSize(2); |
| 117 | // Make sure apps are ordered by recency |
| 118 | assertThat(requests.get(0).packageName).isEqualTo(TEST_PACKAGE_NAMES[0]); |
| 119 | assertThat(requests.get(0).requestFinishTime).isEqualTo(ONE_MIN_AGO + DURATION); |
| 120 | assertThat(requests.get(1).packageName).isEqualTo(TEST_PACKAGE_NAMES[1]); |
Wei Wang | ec281b1 | 2018-05-01 11:21:31 -0700 | [diff] [blame] | 121 | assertThat(requests.get(1).requestFinishTime).isEqualTo(TWENTY_THREE_HOURS_AGO + DURATION); |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private void mockTestApplicationInfos(int userId, String... packageNameList) |
| 125 | throws NameNotFoundException { |
| 126 | for (String packageName : packageNameList) { |
| 127 | ApplicationInfo appInfo = new ApplicationInfo(); |
| 128 | appInfo.packageName = packageName; |
| 129 | when(mPackageManager.getApplicationInfoAsUser( |
| 130 | packageName, PackageManager.GET_META_DATA, userId)).thenReturn(appInfo); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private List<PackageOps> createTestPackageOpsList(String[] packageNameList, long[] time) { |
| 135 | List<PackageOps> packageOpsList = new ArrayList<>(); |
| 136 | for (int i = 0; i < packageNameList.length ; i++) { |
| 137 | PackageOps packageOps = createPackageOps( |
| 138 | packageNameList[i], |
| 139 | TEST_UID, |
| 140 | AppOpsManager.OP_MONITOR_LOCATION, |
| 141 | time[i], |
| 142 | DURATION); |
| 143 | packageOpsList.add(packageOps); |
| 144 | } |
| 145 | return packageOpsList; |
| 146 | } |
| 147 | |
| 148 | private PackageOps createPackageOps( |
| 149 | String packageName, int uid, int op, long time, int duration) { |
| 150 | return new PackageOps( |
| 151 | packageName, |
| 152 | uid, |
| 153 | Collections.singletonList(createOpEntryWithTime(op, time, duration))); |
| 154 | } |
| 155 | |
| 156 | private OpEntry createOpEntryWithTime(int op, long time, int duration) { |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 157 | final LongSparseLongArray accessTimes = new LongSparseLongArray(); |
| 158 | accessTimes.put(AppOpsManager.makeKey(AppOpsManager.UID_STATE_TOP, |
| 159 | AppOpsManager.OP_FLAG_SELF), time); |
| 160 | final LongSparseLongArray durations = new LongSparseLongArray(); |
| 161 | durations.put(AppOpsManager.makeKey(AppOpsManager.UID_STATE_TOP, |
| 162 | AppOpsManager.OP_FLAG_SELF), duration); |
| 163 | return new OpEntry(op, false, AppOpsManager.MODE_ALLOWED, accessTimes, |
| 164 | null /*rejectTimes*/, durations, null /* proxyUids */, null /* proxyPackages */); |
Maggie | 704c423 | 2017-12-11 18:02:36 -0800 | [diff] [blame] | 165 | } |
| 166 | } |