1 /* 2 * Copyright (C) 2010 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.testtype; 18 19 import com.android.ddmlib.Log.LogLevel; 20 import com.android.tradefed.config.IConfiguration; 21 import com.android.tradefed.config.IConfigurationReceiver; 22 import com.android.tradefed.config.Option; 23 import com.android.tradefed.device.DeviceNotAvailableException; 24 import com.android.tradefed.device.DeviceUnresponsiveException; 25 import com.android.tradefed.invoker.TestInformation; 26 import com.android.tradefed.log.LogUtil.CLog; 27 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 28 import com.android.tradefed.result.ITestInvocationListener; 29 import com.android.tradefed.result.TestDescription; 30 31 import java.util.ArrayList; 32 import java.util.Collection; 33 import java.util.LinkedHashMap; 34 import java.util.List; 35 36 /** No-op empty test implementation. */ 37 public class StubTest implements IShardableTest, IConfigurationReceiver, IAbiReceiver { 38 39 public static final String DNAE_MESSAGE = "StubTest DeviceNotAvailableException"; 40 41 @Option( 42 name = "num-shards", 43 description = "Shard this test into given number of separately runnable chunks") 44 private int mNumShards = 1; 45 46 @Option( 47 name = "test-throw-runtime", 48 description = 49 "test option to force the stub test to throw a runtime exception." 50 + "Used for testing." 51 ) 52 private boolean mThrowRuntime = false; 53 54 @Option( 55 name = "test-throw-not-available", 56 description = 57 "test option to force the stub test to throw a DeviceNotAvailable " 58 + "exception. Used for testing." 59 ) 60 private boolean mThrowNotAvailable = false; 61 62 @Option( 63 name = "test-throw-unresponsive", 64 description = 65 "test option to force the stub test to throw a DeviceUnresponsive " 66 + "exception. Used for testing." 67 ) 68 private boolean mThrowUnresponsive = false; 69 70 @Option( 71 name = "run-a-test", 72 description = 73 "Test option to make the stub test trigger some test callbacks on the invocation." 74 ) 75 private boolean mRunTest = false; 76 77 private IConfiguration mConfig; 78 private IAbi mAbi; 79 80 /* Get whether the stub test trigger some test callbacks on the invocation. */ getRunTest()81 public boolean getRunTest() { 82 return mRunTest; 83 } 84 85 /** {@inheritDoc} */ 86 @Override run(TestInformation testInfo, ITestInvocationListener listener)87 public void run(TestInformation testInfo, ITestInvocationListener listener) 88 throws DeviceNotAvailableException { 89 if (mThrowRuntime) { 90 throw new RuntimeException("StubTest RuntimeException"); 91 } 92 if (mThrowNotAvailable) { 93 throw new DeviceNotAvailableException(DNAE_MESSAGE, "serial"); 94 } 95 if (mThrowUnresponsive) { 96 throw new DeviceUnresponsiveException("StubTest DeviceUnresponsiveException", "serial"); 97 } 98 if (!mRunTest) { 99 CLog.i("nothing to test!"); 100 } else { 101 listener.testRunStarted("TestStub", 1); 102 TestDescription testId = new TestDescription("StubTest", "StubMethod"); 103 listener.testStarted(testId); 104 listener.testEnded(testId, new LinkedHashMap<String, Metric>()); 105 listener.testRunEnded(500, new LinkedHashMap<String, Metric>()); 106 } 107 } 108 109 @Override split()110 public Collection<IRemoteTest> split() { 111 if (mNumShards > 1) { 112 List<IRemoteTest> shards = new ArrayList<IRemoteTest>(mNumShards); 113 for (int i=0; i < mNumShards; i++) { 114 shards.add(new StubTest()); 115 } 116 CLog.logAndDisplay( 117 LogLevel.INFO, "splitting into %d shards", mNumShards); 118 return shards; 119 } 120 return null; 121 } 122 123 @Override setConfiguration(IConfiguration configuration)124 public void setConfiguration(IConfiguration configuration) { 125 mConfig = configuration; 126 } 127 getConfiguration()128 public IConfiguration getConfiguration() { 129 return mConfig; 130 } 131 132 @Override setAbi(IAbi abi)133 public void setAbi(IAbi abi) { 134 mAbi = abi; 135 } 136 137 @Override getAbi()138 public IAbi getAbi() { 139 return mAbi; 140 } 141 142 @Override toString()143 public String toString() { 144 return super.toString() + mAbi; 145 } 146 } 147