1 /*
2  * Copyright (C) 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.continuous;
18 
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.config.Option.Importance;
21 import com.android.tradefed.config.OptionClass;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.result.BugreportCollector;
24 import com.android.tradefed.result.DeviceFileReporter;
25 import com.android.tradefed.result.ITestInvocationListener;
26 import com.android.tradefed.result.LogDataType;
27 import com.android.tradefed.result.NameMangleListener;
28 import com.android.tradefed.result.TestDescription;
29 import com.android.tradefed.testtype.InstrumentationTest;
30 
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33 
34 /**
35  * A test that runs the smoke tests
36  *
37  * <p>Simply {@link InstrumentationTest} with extra reporting. Should be re-integrated with {@link
38  * InstrumentationTest} after it's clear that it works as expected.
39  */
40 @OptionClass(alias = "smoke")
41 public class SmokeTest extends InstrumentationTest {
42     @Option(
43             name = "capture-file-pattern",
44             description =
45                     "File glob of on-device files to log "
46                             + "if found.  Takes two arguments: the glob, and the file type "
47                             + "(text/xml/zip/gzip/png/unknown).  May be repeated.",
48             importance = Importance.IF_UNSET)
49     private Map<String, LogDataType> mUploadFilePatterns = new LinkedHashMap<String, LogDataType>();
50 
51     @Option(
52             name = "bugreport-device-wait-time",
53             description =
54                     "How many seconds to wait for the "
55                             + "device to become available so we can capture a bugreport.  Useful in case the smoke "
56                             + "tests fail due to a device reboot.")
57     private int mDeviceWaitTime = 60;
58 
59     /** Simple constructor that disables an incompatible runmode from the superclass */
SmokeTest()60     public SmokeTest() {
61         super();
62         // Re-run mode doesn't work properly with Smoke
63         setRerunMode(false);
64     }
65 
66     /** {@inheritDoc} */
67     @Override
run(final ITestInvocationListener listener)68     public void run(final ITestInvocationListener listener) throws DeviceNotAvailableException {
69         // trimListener should be the first thing to receive any results.  It will pass the results
70         // through to the bugListener, which will forward them (after collecting any necessary
71         // bugreports) to the real Listener(s).
72         final BugreportCollector bugListener = new BugreportCollector(listener, getDevice());
73         bugListener.setDeviceWaitTime(mDeviceWaitTime);
74         bugListener.addPredicate(BugreportCollector.AFTER_FAILED_TESTCASES);
75         final ITestInvocationListener trimListener = new TrimListener(bugListener);
76 
77         super.run(trimListener);
78 
79         final DeviceFileReporter dfr = new DeviceFileReporter(getDevice(), trimListener);
80         dfr.addPatterns(mUploadFilePatterns);
81         dfr.run();
82     }
83 
84     /**
85      * A class to adjust the test identifiers from something like this:
86      * com.android.smoketest.SmokeTestRunner$3#com.android.voicedialer.VoiceDialerActivity To this:
87      * SmokeFAST#com.android.voicedialer.VoiceDialerActivity
88      */
89     static class TrimListener extends NameMangleListener {
TrimListener(ITestInvocationListener listener)90         public TrimListener(ITestInvocationListener listener) {
91             super(listener);
92         }
93 
94         /** {@inheritDoc} */
95         @Override
mangleTestId(TestDescription test)96         protected TestDescription mangleTestId(TestDescription test) {
97             final String method = test.getTestName();
98             final String klass =
99                     test.getClassName()
100                             .replaceFirst(
101                                     "com.android.smoketest.SmokeTestRunner(?:\\$\\d+)?",
102                                     "SmokeFAST");
103             return new TestDescription(klass, method);
104         }
105     }
106 }
107