1 /*
2  * Copyright (C) 2017 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.tradefed.testtype.suite.module;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.invoker.IInvocationContext;
21 import com.android.tradefed.testtype.Abi;
22 import com.android.tradefed.testtype.IAbi;
23 import com.android.tradefed.testtype.suite.ModuleDefinition;
24 import com.android.tradefed.util.AbiUtils;
25 
26 /**
27  * Basic implementation of {@link IModuleController} that should be implemented for checking if a
28  * module should run or not.
29  */
30 public abstract class BaseModuleController implements IModuleController {
31 
32     @Option(
33         name = "bugreportz-on-failure",
34         description = "Module option to capture a bugreportz on its test failure."
35     )
36     private boolean mBugReportOnFailure = true;
37 
38     @Option(
39         name = "logcat-on-failure",
40         description = "Module option to capture a logcat on its test failure."
41     )
42     private boolean mLogcatOnFailure = true;
43 
44     @Option(
45         name = "screenshot-on-failure",
46         description = "Module option to capture a screenshot on its test failure."
47     )
48     private boolean mScreenshotOnFailure = true;
49 
50     private IInvocationContext mContext;
51 
52     @Override
shouldRunModule(IInvocationContext context)53     public final RunStrategy shouldRunModule(IInvocationContext context)
54             throws DeviceNotAvailableException {
55         mContext = context;
56         return shouldRun(context);
57     }
58 
59     /**
60      * Method to decide if the module should run or not.
61      *
62      * @param context the {@link IInvocationContext} of the module
63      * @return True if the module should run, false otherwise.
64      */
shouldRun(IInvocationContext context)65     public abstract RunStrategy shouldRun(IInvocationContext context)
66             throws DeviceNotAvailableException;
67 
68     /** Helper method to get the module abi. */
getModuleAbi()69     public final IAbi getModuleAbi() {
70         String abi = mContext.getAttributes().get(ModuleDefinition.MODULE_ABI).get(0);
71         return new Abi(abi, AbiUtils.getBitness(abi));
72     }
73 
74     /** Helper method to get the module name. */
getModuleName()75     public final String getModuleName() {
76         return mContext.getAttributes().get(ModuleDefinition.MODULE_NAME).get(0);
77     }
78 
79     /** Returns whether of not the module wants to capture the logcat on test failure. */
shouldCaptureLogcat()80     public final boolean shouldCaptureLogcat() {
81         return mLogcatOnFailure;
82     }
83 
84     /** Returns whether of not the module wants to capture the screenshot on test failure. */
shouldCaptureScreenshot()85     public final boolean shouldCaptureScreenshot() {
86         return mScreenshotOnFailure;
87     }
88 
89     /** Returns whether of not the module wants to capture the bugreport on test failure. */
shouldCaptureBugreport()90     public final boolean shouldCaptureBugreport() {
91         return mBugReportOnFailure;
92     }
93 }
94