1 /* 2 * Copyright (C) 2014 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.sdk; 18 19 import com.android.sdk.tests.EmulatorGpsPreparer; 20 import com.android.sdk.tests.EmulatorSmsPreparer; 21 import com.android.tradefed.build.IBuildInfo; 22 import com.android.tradefed.config.IConfiguration; 23 import com.android.tradefed.config.IConfigurationReceiver; 24 import com.android.tradefed.device.DeviceNotAvailableException; 25 import com.android.tradefed.device.DeviceUnresponsiveException; 26 import com.android.tradefed.device.ITestDevice; 27 import com.android.tradefed.log.LogUtil.CLog; 28 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 29 import com.android.tradefed.result.ITestInvocationListener; 30 import com.android.tradefed.result.TestDescription; 31 import com.android.tradefed.targetprep.BuildError; 32 import com.android.tradefed.targetprep.SdkAvdPreparer; 33 import com.android.tradefed.targetprep.TargetSetupError; 34 import com.android.tradefed.testtype.IBuildReceiver; 35 import com.android.tradefed.testtype.IDeviceTest; 36 import com.android.tradefed.testtype.IRemoteTest; 37 import com.android.tradefed.util.StreamUtil; 38 39 import java.util.HashMap; 40 41 /** 42 * A class for posting emulator boot result as a test 43 */ 44 public class EmulatorBootTest implements IDeviceTest, IRemoteTest, IBuildReceiver, 45 IConfigurationReceiver { 46 private IConfiguration mConfiguration; 47 private String mTestLabel = "emulator_boot_test"; 48 private SdkAvdPreparer mAvdPreparer; 49 private EmulatorSmsPreparer mSmsPreparer; 50 private EmulatorGpsPreparer mGpsPreparer; 51 private ITestDevice mDevice; 52 53 void createPreparers() { 54 55 mAvdPreparer = (SdkAvdPreparer) mConfiguration.getConfigurationObject("sdk-avd-preparer"); 56 mSmsPreparer = (EmulatorSmsPreparer) mConfiguration.getConfigurationObject("sms-preparer"); 57 mGpsPreparer = (EmulatorGpsPreparer) mConfiguration.getConfigurationObject("gps-preparer"); 58 } 59 60 IBuildInfo mBuildInfo; 61 62 /** 63 * {@inheritDoc} 64 */ 65 @Override 66 public void setConfiguration(IConfiguration configuration) { 67 mConfiguration = configuration; 68 } 69 70 /** 71 * {@inheritDoc} 72 */ 73 @Override 74 public void setBuild(IBuildInfo buildInfo) { 75 mBuildInfo = buildInfo; 76 } 77 78 /** 79 * {@inheritDoc} 80 */ 81 @Override 82 public ITestDevice getDevice() { 83 return mDevice; 84 } 85 86 /** 87 * {@inheritDoc} 88 */ 89 @Override 90 public void setDevice(ITestDevice device) { 91 mDevice = device; 92 } 93 94 /** 95 * {@inheritDoc} 96 */ 97 @Override 98 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 99 TestDescription bootTest = 100 new TestDescription(EmulatorBootTest.class.getSimpleName(), mTestLabel); 101 listener.testRunStarted(EmulatorBootTest.class.getSimpleName(), 1); 102 listener.testStarted(bootTest); 103 try { 104 createPreparers(); 105 mAvdPreparer.setUp(mDevice, mBuildInfo); 106 mSmsPreparer.setUp(mDevice, mBuildInfo); 107 mGpsPreparer.setUp(mDevice, mBuildInfo); 108 checkLauncherRunningOnEmulator(mDevice); 109 } 110 catch(BuildError b) { 111 listener.testFailed(bootTest, StreamUtil.getStackTrace(b)); 112 // throw exception to prevent other tests from executing needlessly 113 throw new DeviceUnresponsiveException("The emulator failed to boot", b, 114 mDevice.getSerialNumber()); 115 } 116 catch(RuntimeException e) { 117 listener.testFailed(bootTest, StreamUtil.getStackTrace(e)); 118 throw e; 119 } catch (TargetSetupError e) { 120 listener.testFailed(bootTest, StreamUtil.getStackTrace(e)); 121 throw new RuntimeException(e); 122 } 123 finally { 124 listener.testEnded(bootTest, new HashMap<String, Metric>()); 125 listener.testRunEnded(0, new HashMap<String, Metric>()); 126 } 127 } 128 129 private void checkLauncherRunningOnEmulator(ITestDevice device) throws BuildError, 130 DeviceNotAvailableException { 131 Integer apiLevel = device.getApiLevel(); 132 String cmd = "ps"; 133 if (apiLevel >= 21) { 134 cmd = "am stack list"; 135 } else if (apiLevel == 19) { 136 cmd = "am stack boxes"; 137 } 138 String cmdResult = device.executeShellCommand(cmd); 139 CLog.i("%s on device %s is %s", cmd, mDevice.getSerialNumber(), cmdResult); 140 141 String[] cmdResultLines = cmdResult.split("\n"); 142 int i; 143 for(i = 0; i < cmdResultLines.length; ++i) { 144 if (cmdResultLines[i].contains("com.android.launcher")) { 145 break; 146 } 147 } 148 if(i == cmdResultLines.length) { 149 throw new BuildError("The emulator do not have launcher run", 150 device.getDeviceDescriptor()); 151 } 152 } 153 } 154