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 package com.android.compatibility.targetprep; 17 18 import com.android.tradefed.build.BuildInfo; 19 import com.android.tradefed.build.IBuildInfo; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.invoker.TestInformation; 23 import com.android.tradefed.targetprep.TargetSetupError; 24 import com.android.tradefed.targetprep.TestAppInstallSetup; 25 26 import static org.mockito.ArgumentMatchers.any; 27 import static org.mockito.Mockito.mock; 28 import static org.mockito.Mockito.times; 29 import static org.mockito.Mockito.verify; 30 import static org.testng.Assert.assertThrows; 31 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.rules.TemporaryFolder; 35 import org.junit.runner.RunWith; 36 import org.junit.runners.JUnit4; 37 38 import java.io.File; 39 import java.io.IOException; 40 import java.nio.file.Files; 41 import java.nio.file.Paths; 42 43 @RunWith(JUnit4.class) 44 public class AppSetupPreparerTest { 45 46 private static final String OPTION_GCS_APK_DIR = "gcs-apk-dir"; 47 public static final ITestDevice NULL_DEVICE = null; 48 49 @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); 50 51 private final IBuildInfo mBuildInfo = new BuildInfo(); 52 private final TestAppInstallSetup mMockAppInstallSetup = mock(TestAppInstallSetup.class); 53 private final AppSetupPreparer mPreparer = 54 new AppSetupPreparer("package_name", mMockAppInstallSetup); 55 56 @Test setUp_gcsApkDirIsNull_throwsException()57 public void setUp_gcsApkDirIsNull_throwsException() 58 throws DeviceNotAvailableException, TargetSetupError { 59 mBuildInfo.addBuildAttribute(OPTION_GCS_APK_DIR, null); 60 61 assertThrows(NullPointerException.class, () -> mPreparer.setUp(NULL_DEVICE, mBuildInfo)); 62 } 63 64 @Test setUp_gcsApkDirIsNotDir_throwsException()65 public void setUp_gcsApkDirIsNotDir_throwsException() 66 throws IOException, DeviceNotAvailableException, TargetSetupError { 67 File tempFile = tempFolder.newFile("temp_file_name"); 68 mBuildInfo.addBuildAttribute(OPTION_GCS_APK_DIR, tempFile.getPath()); 69 70 assertThrows( 71 IllegalArgumentException.class, () -> mPreparer.setUp(NULL_DEVICE, mBuildInfo)); 72 } 73 74 @Test setUp_packageDirDoesNotExist_throwsError()75 public void setUp_packageDirDoesNotExist_throwsError() 76 throws IOException, DeviceNotAvailableException, TargetSetupError { 77 File gcsApkDir = tempFolder.newFolder("gcs_apk_dir"); 78 mBuildInfo.addBuildAttribute(OPTION_GCS_APK_DIR, gcsApkDir.getPath()); 79 80 assertThrows( 81 IllegalArgumentException.class, () -> mPreparer.setUp(NULL_DEVICE, mBuildInfo)); 82 } 83 84 @Test setUp_apkDoesNotExist()85 public void setUp_apkDoesNotExist() throws Exception { 86 File gcsApkDir = tempFolder.newFolder("gcs_apk_dir"); 87 createPackageFile(gcsApkDir, "package_name", "non_apk_file"); 88 mBuildInfo.addBuildAttribute(OPTION_GCS_APK_DIR, gcsApkDir.getPath()); 89 90 assertThrows(TargetSetupError.class, () -> mPreparer.setUp(NULL_DEVICE, mBuildInfo)); 91 } 92 93 @Test setUp_installSplitApk()94 public void setUp_installSplitApk() throws Exception { 95 File gcsApkDir = tempFolder.newFolder("gcs_apk_dir"); 96 File packageDir = new File(gcsApkDir.getPath(), "package_name"); 97 createPackageFile(gcsApkDir, "package_name", "apk_name_1.apk"); 98 createPackageFile(gcsApkDir, "package_name", "apk_name_2.apk"); 99 mBuildInfo.addBuildAttribute(OPTION_GCS_APK_DIR, gcsApkDir.getPath()); 100 101 mPreparer.setUp(NULL_DEVICE, mBuildInfo); 102 103 verify(mMockAppInstallSetup).setAltDir(packageDir); 104 verify(mMockAppInstallSetup).addSplitApkFileNames("apk_name_2.apk,apk_name_1.apk"); 105 verify(mMockAppInstallSetup).setUp(any(), any()); 106 } 107 108 @Test setUp_installNonSplitApk()109 public void setUp_installNonSplitApk() throws Exception { 110 File gcsApkDir = tempFolder.newFolder("gcs_apk_dir"); 111 File packageDir = new File(gcsApkDir.getPath(), "package_name"); 112 createPackageFile(gcsApkDir, "package_name", "apk_name_1.apk"); 113 mBuildInfo.addBuildAttribute(OPTION_GCS_APK_DIR, gcsApkDir.getPath()); 114 115 mPreparer.setUp(NULL_DEVICE, mBuildInfo); 116 117 verify(mMockAppInstallSetup).setAltDir(packageDir); 118 verify(mMockAppInstallSetup).addTestFileName("apk_name_1.apk"); 119 verify(mMockAppInstallSetup).setUp(any(), any()); 120 } 121 122 @Test tearDown()123 public void tearDown() throws Exception { 124 TestInformation testInfo = TestInformation.newBuilder().build(); 125 126 mPreparer.tearDown(testInfo, null); 127 128 verify(mMockAppInstallSetup, times(1)).tearDown(testInfo, null); 129 } 130 createPackageFile(File parentDir, String packageName, String apkName)131 private File createPackageFile(File parentDir, String packageName, String apkName) 132 throws IOException { 133 File packageDir = 134 Files.createDirectories(Paths.get(parentDir.getAbsolutePath(), packageName)) 135 .toFile(); 136 137 return Files.createFile(Paths.get(packageDir.getAbsolutePath(), apkName)).toFile(); 138 } 139 } 140