1 /* 2 * Copyright (C) 2018 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.fastdeploy; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 23 import com.android.ddmlib.Log.LogLevel; 24 import com.android.tradefed.device.DeviceNotAvailableException; 25 import com.android.tradefed.log.LogUtil.CLog; 26 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 27 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.io.IOException; 34 import java.util.Arrays; 35 36 @RunWith(DeviceJUnit4ClassRunner.class) 37 public class FastDeployTest extends BaseHostJUnit4Test { 38 39 private static final String TEST_APP_PACKAGE = "com.example.helloworld"; 40 private static final String TEST_APK5_NAME = "helloworld5.apk"; 41 private static final String TEST_APK7_NAME = "helloworld7.apk"; 42 43 private String mTestApk5Path; 44 private String mTestApk7Path; 45 46 @Before setUp()47 public void setUp() throws Exception { 48 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(getBuild()); 49 getDevice().uninstallPackage(TEST_APP_PACKAGE); 50 mTestApk5Path = buildHelper.getTestFile(TEST_APK5_NAME).getAbsolutePath(); 51 mTestApk7Path = buildHelper.getTestFile(TEST_APK7_NAME).getAbsolutePath(); 52 } 53 54 @Test testAppInstalls()55 public void testAppInstalls() throws Exception { 56 fastInstallPackage(mTestApk5Path); 57 assertTrue(isAppInstalled(TEST_APP_PACKAGE)); 58 getDevice().uninstallPackage(TEST_APP_PACKAGE); 59 assertFalse(isAppInstalled(TEST_APP_PACKAGE)); 60 } 61 62 @Test testAppPatch()63 public void testAppPatch() throws Exception { 64 fastInstallPackage(mTestApk5Path); 65 assertTrue(isAppInstalled(TEST_APP_PACKAGE)); 66 fastInstallPackage(mTestApk7Path); 67 assertTrue(isAppInstalled(TEST_APP_PACKAGE)); 68 getDevice().uninstallPackage(TEST_APP_PACKAGE); 69 assertFalse(isAppInstalled(TEST_APP_PACKAGE)); 70 } 71 isAppInstalled(String packageName)72 private boolean isAppInstalled(String packageName) throws DeviceNotAvailableException { 73 final String result = getDevice().executeShellCommand("pm list packages"); 74 CLog.logAndDisplay(LogLevel.INFO, result); 75 final int prefixLength = "package:".length(); 76 return Arrays.stream(result.split("\\r?\\n")) 77 .anyMatch(line -> line.substring(prefixLength).equals(packageName)); 78 } 79 80 // Mostly copied from PkgInstallSignatureVerificationTest.java. fastInstallPackage(String apkPath)81 private void fastInstallPackage(String apkPath) 82 throws IOException, DeviceNotAvailableException { 83 String result = getDevice().executeAdbCommand("install", "-t", "--fastdeploy", "--force-agent", 84 apkPath); 85 CLog.logAndDisplay(LogLevel.INFO, result); 86 } 87 } 88 89 90