1 /* 2 * Copyright 2012 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.tradefed.testtype; 18 19 import com.android.cts.tradefed.build.CtsBuildHelper; 20 import com.android.cts.util.AbiUtils; 21 import com.android.ddmlib.testrunner.ITestRunListener; 22 import com.android.tradefed.build.IBuildInfo; 23 import com.android.tradefed.device.DeviceNotAvailableException; 24 import com.android.tradefed.device.ITestDevice; 25 import com.android.tradefed.log.LogUtil.CLog; 26 import com.android.tradefed.result.ITestInvocationListener; 27 import com.android.tradefed.testtype.IAbi; 28 import com.android.tradefed.testtype.IBuildReceiver; 29 import com.android.tradefed.testtype.IDeviceTest; 30 import com.android.tradefed.testtype.IRemoteTest; 31 32 import java.io.File; 33 import java.io.FileNotFoundException; 34 35 /** 36 * Test runner for wrapped (native) GTests 37 */ 38 public class WrappedGTest implements IBuildReceiver, IDeviceTest, IRemoteTest { 39 40 private static final String LOG_TAG = WrappedGTest.class.getSimpleName(); 41 42 private int mMaxTestTimeMs = 1 * 60 * 1000; 43 44 private CtsBuildHelper mCtsBuild; 45 private ITestDevice mDevice; 46 private IAbi mAbi; 47 48 private final String mAppNameSpace; 49 private final String mPackageName; 50 private final String mName; 51 private final String mRunner; 52 53 public WrappedGTest(String appNameSpace, String packageName, String name, String runner) { 54 mAppNameSpace = appNameSpace; 55 mPackageName = packageName; 56 mName = name; 57 mRunner = runner; 58 } 59 60 /** 61 * @param abi The ABI to run the test on 62 */ 63 public void setAbi(IAbi abi) { 64 mAbi = abi; 65 } 66 67 @Override 68 public void setBuild(IBuildInfo buildInfo) { 69 mCtsBuild = CtsBuildHelper.createBuildHelper(buildInfo); 70 } 71 72 @Override 73 public void setDevice(ITestDevice device) { 74 mDevice = device; 75 } 76 77 @Override 78 public ITestDevice getDevice() { 79 return mDevice; 80 } 81 82 @Override 83 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 84 if (installTest()) { 85 runTest(listener); 86 uninstallTest(); 87 } else { 88 CLog.e("Failed to install test"); 89 } 90 } 91 92 private boolean installTest() throws DeviceNotAvailableException { 93 try { 94 File testApp = mCtsBuild.getTestApp(String.format("%s.apk", mName)); 95 String[] options = {AbiUtils.createAbiFlag(mAbi.getName())}; 96 String installCode = mDevice.installPackage(testApp, true, options); 97 98 if (installCode != null) { 99 CLog.e("Failed to install %s.apk on %s. Reason: %s", mName, 100 mDevice.getSerialNumber(), installCode); 101 return false; 102 } 103 } 104 catch (FileNotFoundException e) { 105 CLog.e("Package %s.apk not found", mName); 106 return false; 107 } 108 return true; 109 } 110 111 private void runTest(ITestRunListener listener) throws DeviceNotAvailableException { 112 String id = AbiUtils.createId(mAbi.getName(), mPackageName); 113 WrappedGTestResultParser resultParser = new WrappedGTestResultParser(id, listener); 114 resultParser.setFakePackagePrefix(mPackageName + "."); 115 try { 116 String options = mAbi == null ? "" : String.format("--abi %s ", mAbi.getName()); 117 String command = String.format("am instrument -w %s%s/.%s", options, mAppNameSpace, mRunner); 118 mDevice.executeShellCommand(command, resultParser, mMaxTestTimeMs, 0); 119 } catch (DeviceNotAvailableException e) { 120 resultParser.flush(); 121 throw e; 122 } catch (RuntimeException e) { 123 resultParser.flush(); 124 throw e; 125 } 126 } 127 128 private void uninstallTest() throws DeviceNotAvailableException { 129 mDevice.uninstallPackage(mAppNameSpace); 130 } 131 } 132