1 /*
2  * Copyright (C) 2021 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 static com.google.common.truth.Truth.assertThat;
20 
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.compatibility.common.tradefed.build.CompatibilityBuildHelper;
26 import com.android.tradefed.build.IBuildInfo;
27 import com.android.tradefed.device.ITestDevice;
28 import com.android.tradefed.testtype.DeviceTestCase;
29 import com.android.tradefed.testtype.IBuildReceiver;
30 
31 import java.io.File;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34 
35 public class PackageManagerStatsTestsBase extends DeviceTestCase implements IBuildReceiver {
36 
37     protected static final String TEST_REMOTE_DIR = "/data/local/tmp/statsdatom";
38     protected IBuildInfo mCtsBuild;
39 
40     @Override
setUp()41     protected void setUp() throws Exception {
42         super.setUp();
43         assertThat(mCtsBuild).isNotNull();
44         ConfigUtils.removeConfig(getDevice());
45         ReportUtils.clearReports(getDevice());
46         DeviceUtils.installStatsdTestApp(getDevice(), mCtsBuild);
47     }
48 
49     @Override
tearDown()50     protected void tearDown() throws Exception {
51         ConfigUtils.removeConfig(getDevice());
52         ReportUtils.clearReports(getDevice());
53         DeviceUtils.uninstallStatsdTestApp(getDevice());
54         getDevice().deleteFile(TEST_REMOTE_DIR);
55         super.tearDown();
56     }
57 
58     @Override
setBuild(IBuildInfo buildInfo)59     public void setBuild(IBuildInfo buildInfo) {
60         mCtsBuild = buildInfo;
61     }
62 
installPackageUsingIncremental(String[] apkNames)63     protected void installPackageUsingIncremental(String[] apkNames)
64             throws Exception {
65         getDevice().executeShellCommand("mkdir -p " + TEST_REMOTE_DIR);
66         String[] remoteApkPaths = new String[apkNames.length];
67         for (int i = 0; i < remoteApkPaths.length; i++) {
68             remoteApkPaths[i] = Utils.pushApkToRemote(
69                     apkNames[i], TEST_REMOTE_DIR, mCtsBuild, getDevice());
70         }
71         String installResult = getDevice().executeShellCommand(
72                 "pm install-incremental -t -g " + "--user " + getDevice().getCurrentUser() + " "
73                         + String.join(" ", remoteApkPaths));
74         assertEquals("Success\n", installResult);
75     }
76 
getAppUid(String packageName)77     protected int getAppUid(String packageName) throws Exception {
78         return getAppUid(getDevice(), packageName);
79     }
80 
getAppUid(ITestDevice device, String pkgName)81     public static int getAppUid(ITestDevice device, String pkgName) throws Exception {
82         final int currentUser = device.getCurrentUser();
83         final String uidLine = device.executeShellCommand(
84                 "cmd package list packages --match-libraries -U --user " + currentUser + " "
85                         + pkgName);
86         final Pattern pattern = Pattern.compile("package:" + pkgName + " uid:(\\d+)");
87         final Matcher matcher = pattern.matcher(uidLine);
88         if (matcher.find()) {
89             return Integer.parseInt(matcher.group(1));
90         }
91         throw new IllegalStateException("Package " + pkgName + " is not installed for user "
92                 + currentUser);
93     }
94 
getTestFileSize(String fileName)95     protected long getTestFileSize(String fileName) throws Exception {
96         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
97         final File file = buildHelper.getTestFile(fileName);
98         return file.length();
99     }
100 
isLibraryInstalled(String libraryName)101     protected boolean isLibraryInstalled(String libraryName) throws Exception {
102         final String uidLine = getDevice().executeShellCommand("cmd package list libraries");
103         final Pattern pattern = Pattern.compile("library:" + libraryName);
104         final Matcher matcher = pattern.matcher(uidLine);
105         return matcher.find();
106     }
107 }
108