Search in sources :

Example 6 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by ResurrectionRemix.

the class AppCollectorTest method testMultipleUsersOneApp.

@Test
public void testMultipleUsersOneApp() throws Exception {
    addApplication("com.test.app", "testuuid");
    ApplicationInfo otherUsersApp = new ApplicationInfo();
    otherUsersApp.packageName = "com.test.app";
    otherUsersApp.volumeUuid = "testuuid";
    otherUsersApp.uid = 1;
    mUsers.add(new UserInfo(1, "", 0));
    VolumeInfo volume = new VolumeInfo("testuuid", 0, null, null);
    volume.fsUuid = "testuuid";
    AppCollector collector = new AppCollector(mContext, volume);
    PackageStats stats = new PackageStats("com.test.app");
    PackageStats otherStats = new PackageStats("com.test.app");
    otherStats.userHandle = 1;
    // Set up this to handle the asynchronous call to the PackageManager. This returns the
    // package info for our packages.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            try {
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(stats, true);
                // Now callback for the other uid.
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(otherStats, 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).containsAllOf(stats, otherStats);
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) ArrayList(java.util.ArrayList) UserInfo(android.content.pm.UserInfo) VolumeInfo(android.os.storage.VolumeInfo) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PackageStats(android.content.pm.PackageStats) IPackageStatsObserver(android.content.pm.IPackageStatsObserver) Test(org.junit.Test)

Example 7 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by DirtyUnicorns.

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));
}
Also used : ArraySet(android.util.ArraySet) JSONObject(org.json.JSONObject) PackageStats(android.content.pm.PackageStats) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 8 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by DirtyUnicorns.

the class DiskStatsLoggingServiceTest method testPopulatedLogTask.

@Test
public void testPopulatedLogTask() throws Exception {
    // Write data to directories.
    writeDataToFile(mDownloads.newFile(), "lol");
    writeDataToFile(mRootDirectory.newFile("test.jpg"), "1234");
    writeDataToFile(mRootDirectory.newFile("test.mp4"), "12345");
    writeDataToFile(mRootDirectory.newFile("test.mp3"), "123456");
    writeDataToFile(mRootDirectory.newFile("test.whatever"), "1234567");
    // Write apps.
    ArrayList<PackageStats> apps = new ArrayList<>();
    PackageStats testApp = new PackageStats("com.test.app");
    testApp.dataSize = 5L;
    testApp.cacheSize = 55L;
    testApp.codeSize = 10L;
    apps.add(testApp);
    when(mCollector.getPackageStats(anyInt())).thenReturn(apps);
    LogRunnable task = new LogRunnable();
    task.setAppCollector(mCollector);
    task.setDownloadsDirectory(mDownloads.getRoot());
    task.setRootDirectory(mRootDirectory.getRoot());
    task.setLogOutputFile(mInputFile);
    task.setSystemSize(10L);
    task.run();
    JSONObject json = getJsonOutput();
    assertThat(json.getLong(DiskStatsFileLogger.PHOTOS_KEY)).isEqualTo(4L);
    assertThat(json.getLong(DiskStatsFileLogger.VIDEOS_KEY)).isEqualTo(5L);
    assertThat(json.getLong(DiskStatsFileLogger.AUDIO_KEY)).isEqualTo(6L);
    assertThat(json.getLong(DiskStatsFileLogger.DOWNLOADS_KEY)).isEqualTo(3L);
    assertThat(json.getLong(DiskStatsFileLogger.SYSTEM_KEY)).isEqualTo(10L);
    assertThat(json.getLong(DiskStatsFileLogger.MISC_KEY)).isEqualTo(7L);
    assertThat(json.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY)).isEqualTo(15L);
    assertThat(json.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY)).isEqualTo(55L);
    assertThat(json.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY).length()).isEqualTo(1L);
    assertThat(json.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY).length()).isEqualTo(1L);
    assertThat(json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY).length()).isEqualTo(1L);
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) PackageStats(android.content.pm.PackageStats) LogRunnable(com.android.server.storage.DiskStatsLoggingService.LogRunnable) Test(org.junit.Test)

Example 9 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by DirtyUnicorns.

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);
}
Also used : ArrayList(java.util.ArrayList) VolumeInfo(android.os.storage.VolumeInfo) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PackageStats(android.content.pm.PackageStats) IPackageStatsObserver(android.content.pm.IPackageStatsObserver) Test(org.junit.Test)

Example 10 with PackageStats

use of android.content.pm.PackageStats in project android_frameworks_base by DirtyUnicorns.

the class AppCollectorTest method testMultipleUsersOneApp.

@Test
public void testMultipleUsersOneApp() throws Exception {
    addApplication("com.test.app", "testuuid");
    ApplicationInfo otherUsersApp = new ApplicationInfo();
    otherUsersApp.packageName = "com.test.app";
    otherUsersApp.volumeUuid = "testuuid";
    otherUsersApp.uid = 1;
    mUsers.add(new UserInfo(1, "", 0));
    VolumeInfo volume = new VolumeInfo("testuuid", 0, null, null);
    volume.fsUuid = "testuuid";
    AppCollector collector = new AppCollector(mContext, volume);
    PackageStats stats = new PackageStats("com.test.app");
    PackageStats otherStats = new PackageStats("com.test.app");
    otherStats.userHandle = 1;
    // Set up this to handle the asynchronous call to the PackageManager. This returns the
    // package info for our packages.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            try {
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(stats, true);
                // Now callback for the other uid.
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(otherStats, 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).containsAllOf(stats, otherStats);
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) ArrayList(java.util.ArrayList) UserInfo(android.content.pm.UserInfo) VolumeInfo(android.os.storage.VolumeInfo) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PackageStats(android.content.pm.PackageStats) IPackageStatsObserver(android.content.pm.IPackageStatsObserver) Test(org.junit.Test)

Aggregations

PackageStats (android.content.pm.PackageStats)43 Test (org.junit.Test)20 IPackageStatsObserver (android.content.pm.IPackageStatsObserver)13 JSONArray (org.json.JSONArray)12 JSONObject (org.json.JSONObject)12 ArrayList (java.util.ArrayList)11 RemoteException (android.os.RemoteException)8 VolumeInfo (android.os.storage.VolumeInfo)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 PackageManager (android.content.pm.PackageManager)6 ArrayMap (android.util.ArrayMap)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ApplicationInfo (android.content.pm.ApplicationInfo)5 Message (android.os.Message)5 PackageInfo (android.content.pm.PackageInfo)4 UserInfo (android.content.pm.UserInfo)3 Handler (android.os.Handler)3 ArraySet (android.util.ArraySet)3 LogRunnable (com.android.server.storage.DiskStatsLoggingService.LogRunnable)3 Method (java.lang.reflect.Method)3