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