InstallerTest.java revision 8dedad31f7fa604028ee6d28963bcc564e76c467
1/*
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.SystemClock;
23import android.os.UserHandle;
24import android.test.AndroidTestCase;
25import android.util.Log;
26
27import com.android.internal.util.ArrayUtils;
28
29import java.util.Arrays;
30
31public class InstallerTest extends AndroidTestCase {
32    private static final String TAG = "InstallerTest";
33
34    private Installer mInstaller;
35
36    private final Timer mManual = new Timer("Manual");
37    private final Timer mQuota = new Timer("Quota");
38
39    private static class Timer {
40        private final String mTitle;
41        private long mStart;
42        private long mTotal;
43
44        public Timer(String title) {
45            mTitle = title;
46        }
47
48        public void start() {
49            mStart = SystemClock.currentTimeMicro();
50        }
51
52        public void stop() {
53            mTotal += SystemClock.currentTimeMicro() - mStart;
54        }
55
56        public void reset() {
57            mStart = 0;
58            mTotal = 0;
59        }
60
61        @Override
62        public String toString() {
63            return mTitle + ": " + (mTotal / 1000) + "ms";
64        }
65    }
66
67    @Override
68    public void setUp() throws Exception {
69        mInstaller = new Installer(getContext());
70        mInstaller.onStart();
71        mManual.reset();
72        mQuota.reset();
73    }
74
75    @Override
76    public void tearDown() throws Exception {
77        Log.i(TAG, mManual.toString());
78        Log.i(TAG, mQuota.toString());
79        mInstaller = null;
80    }
81
82    public void testGetAppSize() throws Exception {
83        final PackageManager pm = getContext().getPackageManager();
84        for (ApplicationInfo app : pm.getInstalledApplications(0)) {
85            final int userId = UserHandle.getUserId(app.uid);
86            final int appId = UserHandle.getAppId(app.uid);
87
88            final String[] packageNames = pm.getPackagesForUid(app.uid);
89            final long[] ceDataInodes = new long[packageNames.length];
90            final String[] codePaths = new String[packageNames.length];
91
92            for (int i = 0; i < packageNames.length; i++) {
93                final ApplicationInfo info = pm.getApplicationInfo(packageNames[i], 0);
94                codePaths[i] = info.getCodePath();
95            }
96
97            final PackageStats stats = new PackageStats(app.packageName);
98            final PackageStats quotaStats = new PackageStats(app.packageName);
99
100            mManual.start();
101            mInstaller.getAppSize(app.volumeUuid, packageNames, userId, 0,
102                    appId, ceDataInodes, codePaths, stats);
103            mManual.stop();
104
105            mQuota.start();
106            mInstaller.getAppSize(app.volumeUuid, packageNames, userId, Installer.FLAG_USE_QUOTA,
107                    appId, ceDataInodes, codePaths, quotaStats);
108            mQuota.stop();
109
110            checkEquals(Arrays.toString(packageNames) + " UID=" + app.uid, stats, quotaStats);
111        }
112    }
113
114    public void testGetUserSize() throws Exception {
115        int[] appIds = null;
116
117        final PackageManager pm = getContext().getPackageManager();
118        for (ApplicationInfo app : pm.getInstalledApplications(0)) {
119            final int appId = UserHandle.getAppId(app.uid);
120            if (!ArrayUtils.contains(appIds, appId)) {
121                appIds = ArrayUtils.appendInt(appIds, appId);
122            }
123        }
124
125        final PackageStats stats = new PackageStats("android");
126        final PackageStats quotaStats = new PackageStats("android");
127
128        mManual.start();
129        mInstaller.getUserSize(null, UserHandle.USER_SYSTEM, 0,
130                appIds, stats);
131        mManual.stop();
132
133        mQuota.start();
134        mInstaller.getUserSize(null, UserHandle.USER_SYSTEM, Installer.FLAG_USE_QUOTA,
135                appIds, quotaStats);
136        mQuota.stop();
137
138        checkEquals(Arrays.toString(appIds), stats, quotaStats);
139    }
140
141    public void testGetExternalSize() throws Exception {
142        mManual.start();
143        final long[] stats = mInstaller.getExternalSize(null, UserHandle.USER_SYSTEM, 0);
144        mManual.stop();
145
146        mQuota.start();
147        final long[] quotaStats = mInstaller.getExternalSize(null, UserHandle.USER_SYSTEM,
148                Installer.FLAG_USE_QUOTA);
149        mQuota.stop();
150
151        for (int i = 0; i < stats.length; i++) {
152            checkEquals("#" + i, stats[i], quotaStats[i]);
153        }
154    }
155
156    private static void checkEquals(String msg, PackageStats a, PackageStats b) {
157        checkEquals(msg + " codeSize", a.codeSize, b.codeSize);
158        checkEquals(msg + " dataSize", a.dataSize, b.dataSize);
159        checkEquals(msg + " cacheSize", a.cacheSize, b.cacheSize);
160        checkEquals(msg + " externalCodeSize", a.externalCodeSize, b.externalCodeSize);
161        checkEquals(msg + " externalDataSize", a.externalDataSize, b.externalDataSize);
162        checkEquals(msg + " externalCacheSize", a.externalCacheSize, b.externalCacheSize);
163    }
164
165    private static void checkEquals(String msg, long expected, long actual) {
166        if (expected != actual) {
167            Log.e(TAG, msg + " expected " + expected + " actual " + actual);
168        }
169    }
170}
171