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 package com.android.tradefed.device;
17 
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertTrue;
21 
22 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
23 import com.android.tradefed.testtype.IDeviceTest;
24 import com.android.tradefed.util.FileUtil;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.Ignore;
29 import org.junit.runner.RunWith;
30 
31 import java.io.File;
32 
33 /** Functional tests the {@link ITestDevice} package management APIs */
34 @RunWith(DeviceJUnit4ClassRunner.class)
35 public class TestDevicePackageFuncTest implements IDeviceTest {
36     private TestDevice mTestDevice;
37 
38     @Override
setDevice(ITestDevice device)39     public void setDevice(ITestDevice device) {
40         mTestDevice = (TestDevice) device;
41     }
42 
43     @Override
getDevice()44     public ITestDevice getDevice() {
45         return mTestDevice;
46     }
47 
48     @Before
setUp()49     public void setUp() throws Exception {
50         // Ensure at set-up that the device is available.
51         mTestDevice.waitForDeviceAvailable();
52 
53         boolean wasInstalled =
54                 mTestDevice.getInstalledPackageNames().contains(WifiHelper.INSTRUMENTATION_PKG);
55         if (wasInstalled) {
56             // Try to make sure the WiFi helper isn't already installed
57             mTestDevice.uninstallPackage(WifiHelper.INSTRUMENTATION_PKG);
58         }
59     }
60 
61     /**
62      * Performs a basic install/uninstall flow
63      *
64      * <p>This is very similar to the old test except this tries to uninstall the package after
65      * installing it, and this one includes more documentation.
66      */
67     @Test
testInstallListUninstall_basic()68     public void testInstallListUninstall_basic() throws Exception {
69         File testApkFile = WifiHelper.extractWifiUtilApk();
70         try {
71             // Install the WiFi helper
72             assertNull(mTestDevice.installPackage(testApkFile, false));
73             assertTrue(
74                     mTestDevice
75                             .getInstalledPackageNames()
76                             .contains(WifiHelper.INSTRUMENTATION_PKG));
77 
78             // Ensure the APK is cleaned up
79             assertFalse(
80                     "apk file was not cleaned up after install",
81                     mTestDevice.doesFileExist(
82                             String.format("/data/local/tmp/%s", testApkFile.getName())));
83 
84             // Try uninstalling the WiFi helper
85             mTestDevice.uninstallPackage(WifiHelper.INSTRUMENTATION_PKG);
86             assertFalse(
87                     mTestDevice
88                             .getInstalledPackageNames()
89                             .contains(WifiHelper.INSTRUMENTATION_PKG));
90         } finally {
91             FileUtil.deleteFile(testApkFile);
92         }
93     }
94 
95     /**
96      * Tests the {@link ITestDevice#isPackageInstalled(String)} method
97      *
98      * <p>This test ensures the method is consistent with the package listing method.
99      */
100     @Test
testIsPackageInstalled_basic()101     public void testIsPackageInstalled_basic() throws Exception {
102         File testApkFile = WifiHelper.extractWifiUtilApk();
103         try {
104             // Install the WiFi helper
105             assertNull(mTestDevice.installPackage(testApkFile, false));
106             assertTrue(
107                     mTestDevice
108                             .getInstalledPackageNames()
109                             .contains(WifiHelper.INSTRUMENTATION_PKG));
110 
111             // Only try to uninstall the package if the install appears to have succeeded
112             try {
113                 assertTrue(mTestDevice.isPackageInstalled(WifiHelper.INSTRUMENTATION_PKG));
114             } finally {
115                 // Try cleaning up the WiFi helper
116                 mTestDevice.uninstallPackage(WifiHelper.INSTRUMENTATION_PKG);
117             }
118         } finally {
119             FileUtil.deleteFile(testApkFile);
120         }
121     }
122 
123     /**
124      * Performs a install/uninstall flow with a created user
125      *
126      * <p>This tests the user-specific flow of installing packages
127      */
128     @Ignore
129     @Test
testInstallListUninstall_forUser()130     public void testInstallListUninstall_forUser() throws Exception {
131         File testApkFile = WifiHelper.extractWifiUtilApk();
132         final String username = "Google";
133         int userId = -1;
134         try {
135             userId = mTestDevice.createUser(username, false, false);
136 
137             // Install the WiFi helper
138             assertNull(mTestDevice.installPackageForUser(testApkFile, false, userId));
139             assertTrue(
140                     mTestDevice.isPackageInstalled(
141                             WifiHelper.INSTRUMENTATION_PKG, Integer.toString(userId)));
142 
143             // Ensure the APK is cleaned up
144             assertFalse(
145                     "apk file was not cleaned up after install",
146                     mTestDevice.doesFileExist(
147                             String.format("/data/local/tmp/%s", testApkFile.getName())));
148 
149             // Try uninstalling the WiFi helper
150             mTestDevice.uninstallPackage(WifiHelper.INSTRUMENTATION_PKG);
151             assertFalse(
152                     mTestDevice
153                             .getInstalledPackageNames()
154                             .contains(WifiHelper.INSTRUMENTATION_PKG));
155         } finally {
156             if (userId != -1) {
157                 mTestDevice.removeUser(userId);
158             }
159             FileUtil.deleteFile(testApkFile);
160         }
161     }
162 }
163