• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.cts.net.hostside;
17 
18 import static com.android.cts.net.hostside.AbstractRestrictBackgroundNetworkTestCase.TAG;
19 
20 import android.text.TextUtils;
21 import android.util.ArraySet;
22 import android.util.Log;
23 
24 import com.android.compatibility.common.util.BeforeAfterRule;
25 
26 import org.junit.Assume;
27 import org.junit.runner.Description;
28 import org.junit.runners.model.Statement;
29 
30 import java.util.ArrayList;
31 import java.util.Collections;
32 
33 public class RequiredPropertiesRule extends BeforeAfterRule {
34 
35     private static ArraySet<Property> mRequiredProperties;
36 
37     @Override
onBefore(Statement base, Description description)38     public void onBefore(Statement base, Description description) {
39         mRequiredProperties = getAllRequiredProperties(description);
40 
41         final String testName = description.getClassName() + "#" + description.getMethodName();
42         assertTestIsValid(testName, mRequiredProperties);
43         Log.i(TAG, "Running test " + testName + " with required properties: "
44                 + propertiesToString(mRequiredProperties));
45     }
46 
getAllRequiredProperties(Description description)47     private ArraySet<Property> getAllRequiredProperties(Description description) {
48         final ArraySet<Property> allRequiredProperties = new ArraySet<>();
49         RequiredProperties requiredProperties = description.getAnnotation(RequiredProperties.class);
50         if (requiredProperties != null) {
51             Collections.addAll(allRequiredProperties, requiredProperties.value());
52         }
53 
54         for (Class<?> clazz = description.getTestClass();
55                 clazz != null; clazz = clazz.getSuperclass()) {
56             requiredProperties = clazz.getDeclaredAnnotation(RequiredProperties.class);
57             if (requiredProperties == null) {
58                 continue;
59             }
60             for (Property requiredProperty : requiredProperties.value()) {
61                 for (Property p : Property.values()) {
62                     if (p.getValue() == ~requiredProperty.getValue()
63                             && allRequiredProperties.contains(p)) {
64                         continue;
65                     }
66                 }
67                 allRequiredProperties.add(requiredProperty);
68             }
69         }
70         return allRequiredProperties;
71     }
72 
assertTestIsValid(String testName, ArraySet<Property> requiredProperies)73     private void assertTestIsValid(String testName, ArraySet<Property> requiredProperies) {
74         if (requiredProperies == null) {
75             return;
76         }
77         final ArrayList<Property> unsupportedProperties = new ArrayList<>();
78         for (Property property : requiredProperies) {
79             if (!property.isSupported()) {
80                 unsupportedProperties.add(property);
81             }
82         }
83         Assume.assumeTrue("Unsupported properties: "
84                 + propertiesToString(unsupportedProperties), unsupportedProperties.isEmpty());
85     }
86 
getRequiredProperties()87     public static ArraySet<Property> getRequiredProperties() {
88         return mRequiredProperties;
89     }
90 
propertiesToString(Iterable<Property> properties)91     private static String propertiesToString(Iterable<Property> properties) {
92         return "[" + TextUtils.join(",", properties) + "]";
93     }
94 }
95