1 /*
2  * Copyright (C) 2023 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.util;
17 
18 import com.android.tradefed.config.ConfigurationDescriptor;
19 import com.android.tradefed.config.IConfiguration;
20 
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.ArrayList;
24 
25 
26 /** Contains common utility methods for checking module. */
27 public class ModuleTestTypeUtil {
28 
29     public static final String TEST_TYPE_KEY = "test-type";
30     public static final String TEST_TYPE_VALUE_PERFORMANCE = "performance";
31 
32     /**
33      * Check whether the configuration is a performance module.
34      *
35      * @param config The config to check.
36      * @return true if the config is a performance module.
37      */
isPerformanceModule(IConfiguration config)38     public static boolean isPerformanceModule(IConfiguration config) {
39         List<String> matched =
40                 getMatchedConfigTestTypes(config, Arrays.asList(TEST_TYPE_VALUE_PERFORMANCE));
41         return !matched.isEmpty();
42     }
43 
44     /**
45      * Get the declared test types of the configuration with a match in the allowed list.
46      *
47      * @param config The config to check.
48      * @param testTypesToMatch The test types to match.
49      * @return matched test types of the config or an empty list if none matches.
50      */
getMatchedConfigTestTypes( IConfiguration config, List<String> testTypesToMatch)51     public static List<String> getMatchedConfigTestTypes(
52             IConfiguration config, List<String> testTypesToMatch) {
53         List<String> matchedTypes = new ArrayList<>();
54         ConfigurationDescriptor cd = config.getConfigurationDescription();
55         if (cd == null) {
56             throw new RuntimeException(config + ": configuration descriptor is null");
57         }
58         List<String> testTypes = cd.getMetaData(TEST_TYPE_KEY);
59         if (testTypes != null) {
60             for (String testType : testTypes) {
61                 if (testTypesToMatch.contains(testType)) {
62                     matchedTypes.add(testType);
63                 }
64             }
65         }
66         return matchedTypes;
67     }
68 }
69