1 /* 2 * Copyright (C) 2017 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.tradefed.targetprep; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assert.fail; 22 23 import com.android.tradefed.build.IBuildInfo; 24 import com.android.tradefed.build.IDeviceBuildInfo; 25 import com.android.tradefed.command.remote.DeviceDescriptor; 26 import com.android.tradefed.config.OptionSetter; 27 import com.android.tradefed.device.ITestDevice; 28 import com.android.tradefed.util.FileUtil; 29 30 import org.easymock.EasyMock; 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.junit.runners.JUnit4; 36 37 import java.io.File; 38 39 /** Unit tests for {@link TestAppInstallSetup} */ 40 @RunWith(JUnit4.class) 41 public class TestAppInstallSetupTest { 42 43 private static final String SERIAL = "SERIAL"; 44 private static final String PACKAGE_NAME = "PACKAGE_NAME"; 45 private static final String APK_NAME = "fakeApk.apk"; 46 private File fakeApk; 47 private File mFakeBuildApk; 48 private TestAppInstallSetup mPrep; 49 private IDeviceBuildInfo mMockBuildInfo; 50 private ITestDevice mMockTestDevice; 51 private File mTestDir; 52 private File mBuildTestDir; 53 private OptionSetter mSetter; 54 55 @Before 56 public void setUp() throws Exception { 57 mTestDir = FileUtil.createTempDir("TestAppSetupTest"); 58 mBuildTestDir = FileUtil.createTempDir("TestAppBuildTestDir"); 59 // fake hierarchy of directory and files 60 fakeApk = FileUtil.createTempFile("fakeApk", ".apk", mTestDir); 61 FileUtil.copyFile(fakeApk, new File(mTestDir, APK_NAME)); 62 fakeApk = new File(mTestDir, APK_NAME); 63 64 mFakeBuildApk = FileUtil.createTempFile("fakeApk", ".apk", mBuildTestDir); 65 new File(mBuildTestDir, "DATA/app").mkdirs(); 66 FileUtil.copyFile(mFakeBuildApk, new File(mBuildTestDir, "DATA/app/" + APK_NAME)); 67 mFakeBuildApk = new File(mBuildTestDir, "/DATA/app/" + APK_NAME); 68 69 mPrep = 70 new TestAppInstallSetup() { 71 @Override 72 protected String parsePackageName( 73 File testAppFile, DeviceDescriptor deviceDescriptor) { 74 return PACKAGE_NAME; 75 } 76 77 @Override 78 protected File getLocalPathForFilename( 79 IBuildInfo buildInfo, String apkFileName, ITestDevice device) 80 throws TargetSetupError { 81 return fakeApk; 82 } 83 }; 84 mPrep.addTestFileName(APK_NAME); 85 86 mSetter = new OptionSetter(mPrep); 87 mSetter.setOptionValue("cleanup-apks", "true"); 88 mMockBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class); 89 mMockTestDevice = EasyMock.createMock(ITestDevice.class); 90 EasyMock.expect(mMockTestDevice.getSerialNumber()).andStubReturn(SERIAL); 91 EasyMock.expect(mMockTestDevice.getDeviceDescriptor()).andStubReturn(null); 92 } 93 94 @After 95 public void tearDown() throws Exception { 96 FileUtil.recursiveDelete(mTestDir); 97 FileUtil.recursiveDelete(mBuildTestDir); 98 } 99 100 @Test 101 public void testSetupAndTeardown() throws Exception { 102 EasyMock.expect( 103 mMockTestDevice.installPackage( 104 (File) EasyMock.anyObject(), EasyMock.eq(true))) 105 .andReturn(null); 106 EasyMock.replay(mMockBuildInfo, mMockTestDevice); 107 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 108 EasyMock.verify(mMockBuildInfo, mMockTestDevice); 109 } 110 111 @Test 112 public void testInstallFailure() throws Exception { 113 final String failure = "INSTALL_PARSE_FAILED_MANIFEST_MALFORMED"; 114 EasyMock.expect( 115 mMockTestDevice.installPackage( 116 (File) EasyMock.anyObject(), EasyMock.eq(true))) 117 .andReturn(failure); 118 EasyMock.replay(mMockBuildInfo, mMockTestDevice); 119 try { 120 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 121 fail("Expected TargetSetupError"); 122 } catch (TargetSetupError e) { 123 String expected = 124 String.format( 125 "Failed to install %s on %s. Reason: '%s' " + "null", 126 "fakeApk.apk", SERIAL, failure); 127 assertEquals(expected, e.getMessage()); 128 } 129 EasyMock.verify(mMockBuildInfo, mMockTestDevice); 130 } 131 132 @Test 133 public void testInstallFailedUpdateIncompatible() throws Exception { 134 final String failure = "INSTALL_FAILED_UPDATE_INCOMPATIBLE"; 135 EasyMock.expect( 136 mMockTestDevice.installPackage( 137 (File) EasyMock.anyObject(), EasyMock.eq(true))) 138 .andReturn(failure); 139 EasyMock.expect(mMockTestDevice.uninstallPackage(PACKAGE_NAME)).andReturn(null); 140 EasyMock.expect( 141 mMockTestDevice.installPackage( 142 (File) EasyMock.anyObject(), EasyMock.eq(true))) 143 .andReturn(null); 144 EasyMock.replay(mMockBuildInfo, mMockTestDevice); 145 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 146 EasyMock.verify(mMockBuildInfo, mMockTestDevice); 147 } 148 149 /** 150 * Test {@link TestAppInstallSetup#setUp(ITestDevice, IBuildInfo)} with a missing apk. 151 * TargetSetupError expected. 152 */ 153 @Test 154 public void testMissingApk() throws Exception { 155 fakeApk = null; // Apk doesn't exist 156 157 try { 158 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 159 fail("TestAppInstallSetup#setUp() did not raise TargetSetupError with missing apk."); 160 } catch (TargetSetupError e) { 161 assertTrue(e.getMessage().contains("not found")); 162 } 163 } 164 165 /** 166 * Test {@link TestAppInstallSetup#setUp(ITestDevice, IBuildInfo)} with an unreadable apk. 167 * TargetSetupError expected. 168 */ 169 @Test 170 public void testUnreadableApk() throws Exception { 171 fakeApk = new File("/not/a/real/path"); // Apk cannot be read 172 173 try { 174 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 175 fail("TestAppInstallSetup#setUp() did not raise TargetSetupError with unreadable apk."); 176 } catch (TargetSetupError e) { 177 assertTrue(e.getMessage().contains("not read")); 178 } 179 } 180 181 /** 182 * Test {@link TestAppInstallSetup#setUp(ITestDevice, IBuildInfo)} with a missing apk and 183 * ThrowIfNoFile=False. Silent skip expected. 184 */ 185 @Test 186 public void testMissingApk_silent() throws Exception { 187 fakeApk = null; // Apk doesn't exist 188 mSetter.setOptionValue("throw-if-not-found", "false"); 189 190 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 191 } 192 193 /** 194 * Test {@link TestAppInstallSetup#setUp(ITestDevice, IBuildInfo)} with an unreadable apk and 195 * ThrowIfNoFile=False. Silent skip expected. 196 */ 197 @Test 198 public void testUnreadableApk_silent() throws Exception { 199 fakeApk = new File("/not/a/real/path"); // Apk cannot be read 200 mSetter.setOptionValue("throw-if-not-found", "false"); 201 202 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 203 } 204 205 /** 206 * Tests that when in OVERRIDE mode we install first from alt-dirs, then from BuildInfo if not 207 * found. 208 */ 209 @Test 210 public void testFindApk_override() throws Exception { 211 mPrep = 212 new TestAppInstallSetup() { 213 @Override 214 protected String parsePackageName( 215 File testAppFile, DeviceDescriptor deviceDescriptor) { 216 return PACKAGE_NAME; 217 } 218 }; 219 mPrep.addTestFileName("fakeApk.apk"); 220 OptionSetter setter = new OptionSetter(mPrep); 221 setter.setOptionValue("alt-dir-behavior", "OVERRIDE"); 222 setter.setOptionValue("alt-dir", mTestDir.getAbsolutePath()); 223 setter.setOptionValue("install-arg", "-d"); 224 225 EasyMock.expect(mMockTestDevice.getDeviceDescriptor()).andStubReturn(null); 226 EasyMock.expect(mMockBuildInfo.getTestsDir()).andStubReturn(mBuildTestDir); 227 228 EasyMock.expect( 229 mMockTestDevice.installPackage( 230 EasyMock.eq(fakeApk), EasyMock.anyBoolean(), EasyMock.eq("-d"))) 231 .andReturn(null); 232 233 EasyMock.replay(mMockTestDevice, mMockBuildInfo); 234 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 235 EasyMock.verify(mMockTestDevice, mMockBuildInfo); 236 } 237 238 /** 239 * Test when OVERRIDE is set but there is not alt-dir, in this case we still use the BuildInfo. 240 */ 241 @Test 242 public void testFindApk_override_onlyInBuild() throws Exception { 243 mPrep = 244 new TestAppInstallSetup() { 245 @Override 246 protected String parsePackageName( 247 File testAppFile, DeviceDescriptor deviceDescriptor) { 248 return PACKAGE_NAME; 249 } 250 }; 251 mPrep.addTestFileName("fakeApk.apk"); 252 OptionSetter setter = new OptionSetter(mPrep); 253 setter.setOptionValue("alt-dir-behavior", "OVERRIDE"); 254 setter.setOptionValue("install-arg", "-d"); 255 256 EasyMock.expect(mMockTestDevice.getDeviceDescriptor()).andStubReturn(null); 257 EasyMock.expect(mMockBuildInfo.getTestsDir()).andStubReturn(mBuildTestDir); 258 259 EasyMock.expect( 260 mMockTestDevice.installPackage( 261 EasyMock.eq(mFakeBuildApk), 262 EasyMock.anyBoolean(), 263 EasyMock.eq("-d"))) 264 .andReturn(null); 265 266 EasyMock.replay(mMockTestDevice, mMockBuildInfo); 267 mPrep.setUp(mMockTestDevice, mMockBuildInfo); 268 EasyMock.verify(mMockTestDevice, mMockBuildInfo); 269 } 270 } 271