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.Option;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.DeviceUnresponsiveException;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
25 import com.android.tradefed.result.ITestInvocationListener;
26 import com.android.tradefed.result.TestDescription;
27 
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 
33 /**
34  * No-op empty test implementation.
35  */
36 public class StubTest implements IShardableTest {
37 
38     @Option(
39             name = "num-shards",
40             description = "Shard this test into given number of separately runnable chunks")
41     private int mNumShards = 1;
42 
43     @Option(
44         name = "test-throw-runtime",
45         description =
46                 "test option to force the stub test to throw a runtime exception."
47                         + "Used for testing."
48     )
49     private boolean mThrowRuntime = false;
50 
51     @Option(
52         name = "test-throw-not-available",
53         description =
54                 "test option to force the stub test to throw a DeviceNotAvailable "
55                         + "exception. Used for testing."
56     )
57     private boolean mThrowNotAvailable = false;
58 
59     @Option(
60         name = "test-throw-unresponsive",
61         description =
62                 "test option to force the stub test to throw a DeviceUnresponsive "
63                         + "exception. Used for testing."
64     )
65     private boolean mThrowUnresponsive = false;
66 
67     @Option(
68         name = "run-a-test",
69         description =
70                 "Test option to make the stub test trigger some test callbacks on the invocation."
71     )
72     private boolean mRunTest = false;
73 
74     /**
75      * {@inheritDoc}
76      */
77     @Override
run(ITestInvocationListener listener)78     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
79         if (mThrowRuntime) {
80             throw new RuntimeException("StubTest RuntimeException");
81         }
82         if (mThrowNotAvailable) {
83             throw new DeviceNotAvailableException("StubTest DeviceNotAvailableException", "serial");
84         }
85         if (mThrowUnresponsive) {
86             throw new DeviceUnresponsiveException("StubTest DeviceUnresponsiveException", "serial");
87         }
88         if (!mRunTest) {
89             CLog.i("nothing to test!");
90         } else {
91             listener.testRunStarted("TestStub", 1);
92             TestDescription testId = new TestDescription("StubTest", "StubMethod");
93             listener.testStarted(testId);
94             listener.testEnded(testId, new LinkedHashMap<String, Metric>());
95             listener.testRunEnded(500, new LinkedHashMap<String, Metric>());
96         }
97     }
98 
99     @Override
split()100     public Collection<IRemoteTest> split() {
101         if (mNumShards > 1) {
102             List<IRemoteTest> shards = new ArrayList<IRemoteTest>(mNumShards);
103             for (int i=0; i < mNumShards; i++) {
104                 shards.add(new StubTest());
105             }
106             CLog.logAndDisplay(
107                     LogLevel.INFO, "splitting into %d shards", mNumShards);
108             return shards;
109         }
110         return null;
111     }
112 }
113