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