1 /* 2 * Copyright (C) 2020 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 17 package com.android.cts.packagemanager.stats.host; 18 19 import com.android.tradefed.util.RunUtil; 20 import android.cts.statsdatom.lib.AtomTestUtils; 21 import android.cts.statsdatom.lib.ConfigUtils; 22 import android.cts.statsdatom.lib.DeviceUtils; 23 import android.cts.statsdatom.lib.ReportUtils; 24 25 import com.android.os.AtomsProto; 26 import com.android.os.StatsLog; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 public class PackageInstallerV2StatsTests extends PackageManagerStatsTestsBase { 32 private static final String TEST_INSTALL_APK = "CtsStatsdAtomEmptyApp.apk"; 33 private static final String TEST_INSTALL_APK_BASE = "CtsStatsdAtomEmptySplitApp.apk"; 34 private static final String TEST_INSTALL_APK_SPLIT = "CtsStatsdAtomEmptySplitApp_pl.apk"; 35 private static final String TEST_INSTALL_PACKAGE = 36 "com.android.cts.packagemanager.stats.emptyapp"; 37 38 @Override setUp()39 protected void setUp() throws Exception { 40 super.setUp(); 41 RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_LONG); 42 } 43 44 @Override tearDown()45 protected void tearDown() throws Exception { 46 getDevice().uninstallPackage(TEST_INSTALL_PACKAGE); 47 super.tearDown(); 48 } 49 testPackageInstallerV2MetricsReported()50 public void testPackageInstallerV2MetricsReported() throws Throwable { 51 if (!Utils.hasIncrementalFeature(getDevice())) { 52 return; 53 } 54 final AtomsProto.PackageInstallerV2Reported report = installPackageUsingV2AndGetReport( 55 new String[]{TEST_INSTALL_APK}); 56 assertTrue(report.getIsIncremental()); 57 // tests are ran using SHELL_UID and installation will be treated as adb install 58 assertEquals("", report.getPackageName()); 59 assertEquals(1, report.getReturnCode()); 60 assertTrue(report.getDurationMillis() > 0); 61 assertEquals(getTestFileSize(TEST_INSTALL_APK), report.getApksSizeBytes()); 62 assertTrue(report.getUid() != 0); 63 assertEquals(getAppUid(TEST_INSTALL_PACKAGE), report.getUid()); 64 } 65 testPackageInstallerV2MetricsReportedForSplits()66 public void testPackageInstallerV2MetricsReportedForSplits() throws Throwable { 67 if (!Utils.hasIncrementalFeature(getDevice())) { 68 return; 69 } 70 final AtomsProto.PackageInstallerV2Reported report = installPackageUsingV2AndGetReport( 71 new String[]{TEST_INSTALL_APK_BASE, TEST_INSTALL_APK_SPLIT}); 72 assertTrue(report.getIsIncremental()); 73 // tests are ran using SHELL_UID and installation will be treated as adb install 74 assertEquals("", report.getPackageName()); 75 assertEquals(1, report.getReturnCode()); 76 assertTrue(report.getDurationMillis() > 0); 77 assertEquals( 78 getTestFileSize(TEST_INSTALL_APK_BASE) + getTestFileSize(TEST_INSTALL_APK_SPLIT), 79 report.getApksSizeBytes()); 80 assertTrue(report.getUid() != 0); 81 assertEquals(getAppUid(TEST_INSTALL_PACKAGE), report.getUid()); 82 } 83 installPackageUsingV2AndGetReport( String[] apkNames)84 private AtomsProto.PackageInstallerV2Reported installPackageUsingV2AndGetReport( 85 String[] apkNames) throws Exception { 86 ConfigUtils.uploadConfigForPushedAtom(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG, 87 AtomsProto.Atom.PACKAGE_INSTALLER_V2_REPORTED_FIELD_NUMBER); 88 RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT); 89 installPackageUsingIncremental(apkNames); 90 assertTrue(getDevice().isPackageInstalled(TEST_INSTALL_PACKAGE, 91 String.valueOf(getDevice().getCurrentUser()))); 92 RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT); 93 94 List<AtomsProto.PackageInstallerV2Reported> reports = new ArrayList<>(); 95 for (StatsLog.EventMetricData data : ReportUtils.getEventMetricDataList(getDevice())) { 96 if (data.getAtom().hasPackageInstallerV2Reported()) { 97 reports.add(data.getAtom().getPackageInstallerV2Reported()); 98 } 99 } 100 assertEquals(1, reports.size()); 101 return reports.get(0); 102 } 103 } 104