1 /*
2  * Copyright (C) 2015 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 package com.android.compatibility.common.tradefed.testtype;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
21 import com.android.tradefed.result.ITestInvocationListener;
22 import com.android.tradefed.result.TestDescription;
23 import com.android.tradefed.testtype.IAbi;
24 import com.android.tradefed.testtype.IAbiReceiver;
25 import com.android.tradefed.testtype.IRemoteTest;
26 import com.android.tradefed.testtype.IRuntimeHintProvider;
27 import com.android.tradefed.testtype.ITestCollector;
28 import com.android.tradefed.testtype.ITestFilterReceiver;
29 
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Set;
33 
34 /**
35  * A test Stub that can be used to fake some runs.
36  */
37 public class TestStub implements IRemoteTest, IAbiReceiver, IRuntimeHintProvider, ITestCollector,
38         ITestFilterReceiver {
39 
40     @Option(name = "module")
41     private String mModule;
42     @Option(name = "foo")
43     protected String mFoo;
44     @Option(name = "blah")
45     protected String mBlah;
46     @Option(name = "report-test")
47     protected boolean mReportTest = false;
48     @Option(name = "run-complete")
49     protected boolean mIsComplete = true;
50     @Option(name = "test-fail")
51     protected boolean mDoesOneTestFail = true;
52     @Option(name = "internal-retry")
53     protected boolean mRetry = false;
54 
55     protected List<TestDescription> mShardedTestToRun;
56     protected Integer mShardIndex = null;
57 
58     /**
59      * Tests attempt.
60      */
testAttempt(ITestInvocationListener listener)61     private void testAttempt(ITestInvocationListener listener) {
62      // We report 3 tests: 2 pass, 1 failed
63         listener.testRunStarted("module-run", 3);
64         TestDescription tid = new TestDescription("TestStub", "test1");
65         listener.testStarted(tid);
66         listener.testEnded(tid, new HashMap<String, Metric>());
67 
68         if (mIsComplete) {
69             // possibly skip this one to create some not_executed case.
70             TestDescription tid2 = new TestDescription("TestStub", "test2");
71             listener.testStarted(tid2);
72             listener.testEnded(tid2, new HashMap<String, Metric>());
73         }
74 
75         TestDescription tid3 = new TestDescription("TestStub", "test3");
76         listener.testStarted(tid3);
77         if (mDoesOneTestFail) {
78             listener.testFailed(tid3, "ouch this is bad.");
79         }
80         listener.testEnded(tid3, new HashMap<String, Metric>());
81 
82         listener.testRunEnded(0, new HashMap<String, Metric>());
83     }
84 
85     /**
86      * {@inheritDoc}
87      */
88     @Override
run(ITestInvocationListener listener)89     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
90         if (mReportTest) {
91             if (mShardedTestToRun == null) {
92                 if (!mRetry) {
93                     testAttempt(listener);
94                 } else {
95                     // We fake an internal retry by calling testRunStart/Ended again.
96                     listener.testRunStarted("module-run", 3);
97                     listener.testRunEnded(0, new HashMap<String, Metric>());
98                     testAttempt(listener);
99                 }
100             } else {
101                 // Run the shard
102                 if (mDoesOneTestFail) {
103                     listener.testRunStarted("module-run", mShardedTestToRun.size() + 1);
104                 } else {
105                     listener.testRunStarted("module-run", mShardedTestToRun.size());
106                 }
107 
108                 if (mIsComplete) {
109                     for (TestDescription tid : mShardedTestToRun) {
110                         listener.testStarted(tid);
111                         listener.testEnded(tid, new HashMap<String, Metric>());
112                     }
113                 } else {
114                     TestDescription tid = mShardedTestToRun.get(0);
115                     listener.testStarted(tid);
116                     listener.testEnded(tid, new HashMap<String, Metric>());
117                 }
118 
119                 if (mDoesOneTestFail) {
120                     TestDescription tid = new TestDescription("TestStub", "failed" + mShardIndex);
121                     listener.testStarted(tid);
122                     listener.testFailed(tid, "shard failed this one.");
123                     listener.testEnded(tid, new HashMap<String, Metric>());
124                 }
125                 listener.testRunEnded(0, new HashMap<String, Metric>());
126             }
127         }
128     }
129 
130     @Override
setAbi(IAbi abi)131     public void setAbi(IAbi abi) {
132         // Do nothing
133     }
134 
135     @Override
getAbi()136     public IAbi getAbi() {
137         return null;
138     }
139 
140     @Override
getRuntimeHint()141     public long getRuntimeHint() {
142         return 1L;
143     }
144 
145     @Override
setCollectTestsOnly(boolean shouldCollectTest)146     public void setCollectTestsOnly(boolean shouldCollectTest) {
147         // Do nothing
148     }
149 
150     @Override
addIncludeFilter(String filter)151     public void addIncludeFilter(String filter) {
152 
153     }
154 
155     @Override
addAllIncludeFilters(Set<String> filters)156     public void addAllIncludeFilters(Set<String> filters) {
157 
158     }
159 
160     @Override
addExcludeFilter(String filter)161     public void addExcludeFilter(String filter) {
162 
163     }
164 
165     @Override
addAllExcludeFilters(Set<String> filters)166     public void addAllExcludeFilters(Set<String> filters) {
167 
168     }
169 
170     @Override
getIncludeFilters()171     public Set<String> getIncludeFilters() {
172         return null;
173     }
174 
175     @Override
getExcludeFilters()176     public Set<String> getExcludeFilters() {
177         return null;
178     }
179 
180     @Override
clearIncludeFilters()181     public void clearIncludeFilters() {}
182 
183     @Override
clearExcludeFilters()184     public void clearExcludeFilters() {}
185 }
186