blob: 23699e21c1c2603521e9faf065ecaeb310fe8189 [file] [log] [blame]
Jeff Sharkey82ca9012017-01-08 17:06:58 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
19import android.content.pm.ApplicationInfo;
20import android.content.pm.PackageManager;
21import android.content.pm.PackageStats;
22import android.os.UserHandle;
23import android.test.AndroidTestCase;
24import android.util.Log;
25
26import com.android.internal.util.ArrayUtils;
27
28import java.util.Arrays;
29
30public class InstallerTest extends AndroidTestCase {
31 private static final String TAG = "InstallerTest";
32
33 private Installer mInstaller;
34
35 @Override
36 public void setUp() throws Exception {
37 mInstaller = new Installer(getContext());
38 mInstaller.onStart();
39 }
40
41 @Override
42 public void tearDown() throws Exception {
43 mInstaller = null;
44 }
45
46 public void testGetAppSize() throws Exception {
Jeff Sharkeydd0edac2017-01-17 15:22:30 -070047 int[] appIds = null;
48
Jeff Sharkey82ca9012017-01-08 17:06:58 -070049 final PackageManager pm = getContext().getPackageManager();
50 for (ApplicationInfo app : pm.getInstalledApplications(0)) {
51 final int userId = UserHandle.getUserId(app.uid);
52 final int appId = UserHandle.getAppId(app.uid);
53
Jeff Sharkeydd0edac2017-01-17 15:22:30 -070054 if (ArrayUtils.contains(appIds, appId)) {
55 continue;
56 } else {
57 appIds = ArrayUtils.appendInt(appIds, appId);
58 }
59
Jeff Sharkey82ca9012017-01-08 17:06:58 -070060 final String[] packageNames = pm.getPackagesForUid(app.uid);
61 final long[] ceDataInodes = new long[packageNames.length];
62 final String[] codePaths = new String[packageNames.length];
63
64 for (int i = 0; i < packageNames.length; i++) {
65 final ApplicationInfo info = pm.getApplicationInfo(packageNames[i], 0);
66 codePaths[i] = info.getCodePath();
67 }
68
69 final PackageStats stats = new PackageStats(app.packageName);
70 final PackageStats quotaStats = new PackageStats(app.packageName);
71
72 mInstaller.getAppSize(app.volumeUuid, packageNames, userId, 0,
73 appId, ceDataInodes, codePaths, stats);
74
75 mInstaller.getAppSize(app.volumeUuid, packageNames, userId, Installer.FLAG_USE_QUOTA,
76 appId, ceDataInodes, codePaths, quotaStats);
77
78 checkEquals(Arrays.toString(packageNames) + " UID=" + app.uid, stats, quotaStats);
79 }
80 }
81
82 public void testGetUserSize() throws Exception {
83 int[] appIds = null;
84
85 final PackageManager pm = getContext().getPackageManager();
86 for (ApplicationInfo app : pm.getInstalledApplications(0)) {
87 final int appId = UserHandle.getAppId(app.uid);
88 if (!ArrayUtils.contains(appIds, appId)) {
89 appIds = ArrayUtils.appendInt(appIds, appId);
90 }
91 }
92
93 final PackageStats stats = new PackageStats("android");
94 final PackageStats quotaStats = new PackageStats("android");
95
96 mInstaller.getUserSize(null, UserHandle.USER_SYSTEM, 0,
97 appIds, stats);
98
99 mInstaller.getUserSize(null, UserHandle.USER_SYSTEM, Installer.FLAG_USE_QUOTA,
100 appIds, quotaStats);
101
102 checkEquals(Arrays.toString(appIds), stats, quotaStats);
103 }
104
105 public void testGetExternalSize() throws Exception {
106
107 final long[] stats = mInstaller.getExternalSize(null, UserHandle.USER_SYSTEM, 0);
108
109 final long[] quotaStats = mInstaller.getExternalSize(null, UserHandle.USER_SYSTEM,
110 Installer.FLAG_USE_QUOTA);
111
112 for (int i = 0; i < stats.length; i++) {
113 checkEquals("#" + i, stats[i], quotaStats[i]);
114 }
115 }
116
117 private static void checkEquals(String msg, PackageStats a, PackageStats b) {
118 checkEquals(msg + " codeSize", a.codeSize, b.codeSize);
119 checkEquals(msg + " dataSize", a.dataSize, b.dataSize);
120 checkEquals(msg + " cacheSize", a.cacheSize, b.cacheSize);
121 checkEquals(msg + " externalCodeSize", a.externalCodeSize, b.externalCodeSize);
122 checkEquals(msg + " externalDataSize", a.externalDataSize, b.externalDataSize);
123 checkEquals(msg + " externalCacheSize", a.externalCacheSize, b.externalCacheSize);
124 }
125
126 private static void checkEquals(String msg, long expected, long actual) {
127 if (expected != actual) {
128 Log.e(TAG, msg + " expected " + expected + " actual " + actual);
129 }
130 }
131}