use of android.content.pm.PackageStats in project android_frameworks_base by ResurrectionRemix.
the class AppCollectorTest method testOneValidApp.
@Test
public void testOneValidApp() throws Exception {
addApplication("com.test.app", "testuuid");
VolumeInfo volume = new VolumeInfo("testuuid", 0, null, null);
volume.fsUuid = "testuuid";
AppCollector collector = new AppCollector(mContext, volume);
PackageStats stats = new PackageStats("com.test.app");
// Set up this to handle the asynchronous call to the PackageManager. This returns the
// package info for the specified package.
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) {
try {
((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(stats, true);
} catch (Exception e) {
// We fail instead of just letting the exception fly because throwing
// out of the callback like this on the background thread causes the test
// runner to crash, rather than reporting the failure.
fail();
}
return null;
}
}).when(mPm).getPackageSizeInfoAsUser(eq("com.test.app"), eq(0), any());
// Because getPackageStats is a blocking call, we block execution of the test until the
// call finishes. In order to finish the call, we need the above answer to execute.
List<PackageStats> myStats = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(1);
new Thread(new Runnable() {
@Override
public void run() {
myStats.addAll(collector.getPackageStats(TIMEOUT));
latch.countDown();
}
}).start();
latch.await();
assertThat(myStats).containsExactly(stats);
}
use of android.content.pm.PackageStats in project android_frameworks_base by ResurrectionRemix.
the class DiskStatsFileLoggerTest method testEmulatedExternalStorageCounted.
@Test
public void testEmulatedExternalStorageCounted() throws Exception {
PackageStats app = new PackageStats("com.test.app");
app.dataSize = 1000;
app.externalDataSize = 1000;
app.cacheSize = 20;
mPackages.add(app);
DiskStatsFileLogger logger = new DiskStatsFileLogger(mMainResult, mDownloadsResult, mPackages, 0L);
logger.dumpToFile(mOutputFile);
JSONObject output = getOutputFileAsJson();
JSONArray appSizes = output.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
assertThat(appSizes.length()).isEqualTo(1);
assertThat(appSizes.getLong(0)).isEqualTo(2000);
}
use of android.content.pm.PackageStats in project android_frameworks_base by ResurrectionRemix.
the class DiskStatsFileLoggerTest method testDuplicatePackageNameIsMergedAcrossMultipleUsers.
@Test
public void testDuplicatePackageNameIsMergedAcrossMultipleUsers() throws Exception {
PackageStats app = new PackageStats("com.test.app");
app.dataSize = 1000;
app.externalDataSize = 1000;
app.cacheSize = 20;
app.userHandle = 0;
mPackages.add(app);
PackageStats secondApp = new PackageStats("com.test.app");
secondApp.dataSize = 100;
secondApp.externalDataSize = 100;
secondApp.cacheSize = 2;
secondApp.userHandle = 1;
mPackages.add(secondApp);
DiskStatsFileLogger logger = new DiskStatsFileLogger(mMainResult, mDownloadsResult, mPackages, 0L);
logger.dumpToFile(mOutputFile);
JSONObject output = getOutputFileAsJson();
assertThat(output.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY)).isEqualTo(2200);
assertThat(output.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY)).isEqualTo(22);
JSONArray packageNames = output.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY);
assertThat(packageNames.length()).isEqualTo(1);
assertThat(packageNames.getString(0)).isEqualTo("com.test.app");
JSONArray appSizes = output.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
assertThat(appSizes.length()).isEqualTo(1);
assertThat(appSizes.getLong(0)).isEqualTo(2200);
JSONArray cacheSizes = output.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY);
assertThat(cacheSizes.length()).isEqualTo(1);
assertThat(cacheSizes.getLong(0)).isEqualTo(22);
}
use of android.content.pm.PackageStats in project android_frameworks_base by crdroidandroid.
the class DiskStatsFileLogger method addAppsToJson.
private void addAppsToJson(JSONObject json) throws JSONException {
JSONArray names = new JSONArray();
JSONArray appSizeList = new JSONArray();
JSONArray cacheSizeList = new JSONArray();
long appSizeSum = 0L;
long cacheSizeSum = 0L;
boolean isExternal = Environment.isExternalStorageEmulated();
for (Map.Entry<String, PackageStats> entry : mergePackagesAcrossUsers().entrySet()) {
PackageStats stat = entry.getValue();
long appSize = stat.codeSize + stat.dataSize;
long cacheSize = stat.cacheSize;
if (isExternal) {
appSize += stat.externalCodeSize + stat.externalDataSize;
cacheSize += stat.externalCacheSize;
}
appSizeSum += appSize;
cacheSizeSum += cacheSize;
names.put(stat.packageName);
appSizeList.put(appSize);
cacheSizeList.put(cacheSize);
}
json.put(PACKAGE_NAMES_KEY, names);
json.put(APP_SIZES_KEY, appSizeList);
json.put(APP_CACHES_KEY, cacheSizeList);
json.put(APP_SIZE_AGG_KEY, appSizeSum);
json.put(APP_CACHE_AGG_KEY, cacheSizeSum);
}
use of android.content.pm.PackageStats in project android_frameworks_base by crdroidandroid.
the class DiskStatsFileLoggerTest method testAppsReported.
@Test
public void testAppsReported() throws Exception {
PackageStats firstPackage = new PackageStats("com.test.app");
firstPackage.codeSize = 100;
firstPackage.dataSize = 1000;
firstPackage.cacheSize = 20;
mPackages.add(firstPackage);
PackageStats secondPackage = new PackageStats("com.test.app2");
secondPackage.codeSize = 10;
secondPackage.dataSize = 1;
secondPackage.cacheSize = 2;
mPackages.add(secondPackage);
DiskStatsFileLogger logger = new DiskStatsFileLogger(mMainResult, mDownloadsResult, mPackages, 0L);
logger.dumpToFile(mOutputFile);
JSONObject output = getOutputFileAsJson();
assertThat(output.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY)).isEqualTo(1111);
assertThat(output.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY)).isEqualTo(22);
JSONArray packageNames = output.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY);
assertThat(packageNames.length()).isEqualTo(2);
JSONArray appSizes = output.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
assertThat(appSizes.length()).isEqualTo(2);
JSONArray cacheSizes = output.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY);
assertThat(cacheSizes.length()).isEqualTo(2);
// We need to do this crazy Set over this because the DiskStatsFileLogger provides no
// guarantee of the ordering of the apps in its output. By using a set, we avoid any order
// problems.
ArraySet<AppSizeGrouping> apps = new ArraySet<>();
for (int i = 0; i < packageNames.length(); i++) {
AppSizeGrouping app = new AppSizeGrouping(packageNames.getString(i), appSizes.getLong(i), cacheSizes.getLong(i));
apps.add(app);
}
assertThat(apps).containsAllOf(new AppSizeGrouping("com.test.app", 1100, 20), new AppSizeGrouping("com.test.app2", 11, 2));
}
Aggregations