1 /*
2  * Copyright (C) 2016 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.compatibility.testtype;
18 
19 import static java.util.stream.Collectors.toList;
20 
21 import com.android.tradefed.config.Option;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.invoker.TestInformation;
24 import com.android.tradefed.log.LogUtil.CLog;
25 import com.android.tradefed.result.ITestInvocationListener;
26 import com.android.tradefed.testtype.AndroidJUnitTest;
27 import com.android.tradefed.util.ArrayUtil;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Objects;
32 import java.util.stream.Stream;
33 
34 /**
35  * A specialized test type for Libcore tests that provides the ability to specify a set of
36  * expectation files. Expectation files are used to indicate tests that are expected to fail,
37  * often because they come from upstream projects, and should not be run under CTS.
38  */
39 public class LibcoreTest extends AndroidJUnitTest {
40 
41     private static final String INSTRUMENTATION_ARG_NAME = "core-expectations";
42 
43     @Option(name = "core-expectation", description = "Provides failure expectations for libcore "
44             + "tests via the specified file; the path must be absolute and will be resolved to "
45             + "matching bundled resource files; this parameter should be repeated for each "
46             + "expectation file")
47     private List<String> mCoreExpectations = new ArrayList<>();
48 
49     @Option(name = "virtual-device-core-expectation", description = "Provides failure expectations "
50             + "on virtual devices only for libcore tests via the specified file; the path must be "
51             + "absolute and will be resolved to matching bundled resource files; this parameter "
52             + "should be repeated for each expectation file")
53     private List<String> mVirtualDeviceCoreExpectations = new ArrayList<>();
54 
55     /**
56      * {@inheritDoc}
57      */
58     @Override
run(TestInformation testInfo, ITestInvocationListener listener)59     public void run(TestInformation testInfo, ITestInvocationListener listener) throws DeviceNotAvailableException {
60         List<String> coreExpectations = getCoreExpectations();
61         if (!coreExpectations.isEmpty()) {
62             addInstrumentationArg(INSTRUMENTATION_ARG_NAME, ArrayUtil.join(",", coreExpectations));
63         }
64 
65         if (getTestPackageName() != null && getClassName() != null) {
66             // If the test-package is set, we should ignore --class, --method it might cause issue
67             // in more complex libcore suites.
68             setClassName(null);
69             setMethodName(null);
70             CLog.d(
71                     "Setting --class and --method to null to avoid conflict with --test-package "
72                             + "option.");
73         }
74         super.run(testInfo, listener);
75     }
76 
getCoreExpectations()77     private List<String> getCoreExpectations() throws DeviceNotAvailableException {
78         if (isVirtualDevice()) {
79                 return Stream.concat(
80                         mCoreExpectations.stream(), mVirtualDeviceCoreExpectations.stream())
81                     .collect(toList());
82         } else {
83             return mCoreExpectations;
84         }
85     }
86 
isVirtualDevice()87     private boolean isVirtualDevice() throws DeviceNotAvailableException {
88         return Objects.equals(getDevice().getProperty("ro.hardware.virtual_device"), "1");
89     }
90 }
91