fix: 首次提交
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.location;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class InjectedSettingTest {
|
||||
|
||||
private static final String TEST_STRING = "test";
|
||||
|
||||
@Test
|
||||
public void buildWithoutPackageName_ShouldReturnNull() {
|
||||
assertThat(((new InjectedSetting.Builder())
|
||||
.setClassName(TEST_STRING)
|
||||
.setTitle(TEST_STRING)
|
||||
.setSettingsActivity(TEST_STRING).build())).isNull();
|
||||
}
|
||||
|
||||
private InjectedSetting getTestSetting() {
|
||||
return new InjectedSetting.Builder()
|
||||
.setPackageName(TEST_STRING)
|
||||
.setClassName(TEST_STRING)
|
||||
.setTitle(TEST_STRING)
|
||||
.setSettingsActivity(TEST_STRING).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
InjectedSetting setting1 = getTestSetting();
|
||||
InjectedSetting setting2 = getTestSetting();
|
||||
assertThat(setting1).isEqualTo(setting2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
InjectedSetting setting = getTestSetting();
|
||||
assertThat(setting.hashCode()).isEqualTo(1225314048);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package com.android.settingslib.location;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.app.AppOpsManager.OpEntry;
|
||||
import android.app.AppOpsManager.AttributedOpEntry;
|
||||
import android.app.AppOpsManager.PackageOps;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Process;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.util.LongSparseArray;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class RecentLocationAppsTest {
|
||||
|
||||
private static final int TEST_UID = 1234;
|
||||
private static final long NOW = System.currentTimeMillis();
|
||||
// App running duration in milliseconds
|
||||
private static final int DURATION = 10;
|
||||
private static final long ONE_MIN_AGO = NOW - TimeUnit.MINUTES.toMillis(1);
|
||||
private static final long TWENTY_THREE_HOURS_AGO = NOW - TimeUnit.HOURS.toMillis(23);
|
||||
private static final long TWO_DAYS_AGO = NOW - TimeUnit.DAYS.toMillis(2);
|
||||
private static final String[] TEST_PACKAGE_NAMES =
|
||||
{"package_1MinAgo", "package_14MinAgo", "package_20MinAgo"};
|
||||
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
private AppOpsManager mAppOpsManager;
|
||||
@Mock
|
||||
private Resources mResources;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
private int mTestUserId;
|
||||
private RecentLocationApps mRecentLocationApps;
|
||||
|
||||
@Before
|
||||
public void setUp() throws NameNotFoundException {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||
when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager);
|
||||
when(mContext.getResources()).thenReturn(mResources);
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
when(mPackageManager.getApplicationLabel(isA(ApplicationInfo.class)))
|
||||
.thenReturn("testApplicationLabel");
|
||||
when(mPackageManager.getUserBadgedLabel(isA(CharSequence.class), isA(UserHandle.class)))
|
||||
.thenReturn("testUserBadgedLabel");
|
||||
mTestUserId = UserHandle.getUserId(TEST_UID);
|
||||
when(mUserManager.getUserProfiles())
|
||||
.thenReturn(Collections.singletonList(new UserHandle(mTestUserId)));
|
||||
|
||||
long[] testRequestTime = {ONE_MIN_AGO, TWENTY_THREE_HOURS_AGO, TWO_DAYS_AGO};
|
||||
List<PackageOps> appOps = createTestPackageOpsList(TEST_PACKAGE_NAMES, testRequestTime);
|
||||
when(mAppOpsManager.getPackagesForOps(RecentLocationApps.LOCATION_REQUEST_OPS)).thenReturn(
|
||||
appOps);
|
||||
mockTestApplicationInfos(mTestUserId, TEST_PACKAGE_NAMES);
|
||||
|
||||
mRecentLocationApps = new RecentLocationApps(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAppList_shouldFilterRecentApps() {
|
||||
List<RecentLocationApps.Request> requests = mRecentLocationApps.getAppList(true);
|
||||
// Only two of the apps have requested location within 15 min.
|
||||
assertThat(requests).hasSize(2);
|
||||
// Make sure apps are ordered by recency
|
||||
assertThat(requests.get(0).packageName).isEqualTo(TEST_PACKAGE_NAMES[0]);
|
||||
assertThat(requests.get(0).requestFinishTime).isEqualTo(ONE_MIN_AGO + DURATION);
|
||||
assertThat(requests.get(1).packageName).isEqualTo(TEST_PACKAGE_NAMES[1]);
|
||||
assertThat(requests.get(1).requestFinishTime).isEqualTo(TWENTY_THREE_HOURS_AGO + DURATION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAppList_shouldNotShowAndroidOS() throws NameNotFoundException {
|
||||
// Add android OS to the list of apps.
|
||||
PackageOps androidSystemPackageOps =
|
||||
createPackageOps(
|
||||
RecentLocationApps.ANDROID_SYSTEM_PACKAGE_NAME,
|
||||
Process.SYSTEM_UID,
|
||||
AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION,
|
||||
ONE_MIN_AGO,
|
||||
DURATION);
|
||||
long[] testRequestTime =
|
||||
{ONE_MIN_AGO, TWENTY_THREE_HOURS_AGO, TWO_DAYS_AGO, ONE_MIN_AGO};
|
||||
List<PackageOps> appOps = createTestPackageOpsList(TEST_PACKAGE_NAMES, testRequestTime);
|
||||
appOps.add(androidSystemPackageOps);
|
||||
when(mAppOpsManager.getPackagesForOps(RecentLocationApps.LOCATION_REQUEST_OPS)).thenReturn(
|
||||
appOps);
|
||||
mockTestApplicationInfos(
|
||||
Process.SYSTEM_UID, RecentLocationApps.ANDROID_SYSTEM_PACKAGE_NAME);
|
||||
|
||||
List<RecentLocationApps.Request> requests = mRecentLocationApps.getAppList(true);
|
||||
// Android OS shouldn't show up in the list of apps.
|
||||
assertThat(requests).hasSize(2);
|
||||
// Make sure apps are ordered by recency
|
||||
assertThat(requests.get(0).packageName).isEqualTo(TEST_PACKAGE_NAMES[0]);
|
||||
assertThat(requests.get(0).requestFinishTime).isEqualTo(ONE_MIN_AGO + DURATION);
|
||||
assertThat(requests.get(1).packageName).isEqualTo(TEST_PACKAGE_NAMES[1]);
|
||||
assertThat(requests.get(1).requestFinishTime).isEqualTo(TWENTY_THREE_HOURS_AGO + DURATION);
|
||||
}
|
||||
|
||||
private void mockTestApplicationInfos(int userId, String... packageNameList)
|
||||
throws NameNotFoundException {
|
||||
for (String packageName : packageNameList) {
|
||||
ApplicationInfo appInfo = new ApplicationInfo();
|
||||
appInfo.packageName = packageName;
|
||||
when(mPackageManager.getApplicationInfoAsUser(
|
||||
packageName, PackageManager.GET_META_DATA, userId)).thenReturn(appInfo);
|
||||
}
|
||||
}
|
||||
|
||||
private List<PackageOps> createTestPackageOpsList(String[] packageNameList, long[] time) {
|
||||
List<PackageOps> packageOpsList = new ArrayList<>();
|
||||
for (int i = 0; i < packageNameList.length; i++) {
|
||||
PackageOps packageOps = createPackageOps(
|
||||
packageNameList[i],
|
||||
TEST_UID,
|
||||
AppOpsManager.OP_MONITOR_LOCATION,
|
||||
time[i],
|
||||
DURATION);
|
||||
packageOpsList.add(packageOps);
|
||||
}
|
||||
return packageOpsList;
|
||||
}
|
||||
|
||||
private PackageOps createPackageOps(
|
||||
String packageName, int uid, int op, long time, int duration) {
|
||||
return new PackageOps(
|
||||
packageName,
|
||||
uid,
|
||||
Collections.singletonList(createOpEntryWithTime(op, time, duration)));
|
||||
}
|
||||
|
||||
private OpEntry createOpEntryWithTime(int op, long time, int duration) {
|
||||
final LongSparseArray<AppOpsManager.NoteOpEvent> accessEvents = new LongSparseArray<>();
|
||||
accessEvents.put(AppOpsManager.makeKey(AppOpsManager.UID_STATE_TOP,
|
||||
AppOpsManager.OP_FLAG_SELF), new AppOpsManager.NoteOpEvent(time, duration, null));
|
||||
|
||||
return new OpEntry(op, AppOpsManager.MODE_ALLOWED, Collections.singletonMap(null,
|
||||
new AttributedOpEntry(op, false, accessEvents, null)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user