1 /*
2  * Copyright (C) 2020 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.compatibility.common.tradefed.result.suite;
17 
18 import static org.junit.Assert.assertTrue;
19 
20 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
21 import com.android.tradefed.build.BuildInfo;
22 import com.android.tradefed.build.IBuildInfo;
23 import com.android.tradefed.config.Configuration;
24 import com.android.tradefed.config.ConfigurationDef;
25 import com.android.tradefed.config.IConfiguration;
26 import com.android.tradefed.config.OptionSetter;
27 import com.android.tradefed.invoker.IInvocationContext;
28 import com.android.tradefed.invoker.InvocationContext;
29 import com.android.tradefed.util.FileUtil;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 import java.io.File;
38 
39 /** Unit tests for {@link CertificationSuiteResultReporter}. */
40 @RunWith(JUnit4.class)
41 public class CertificationSuiteResultReporterTest {
42     private CertificationSuiteResultReporter mReporter;
43     private IConfiguration mConfiguration;
44     private IInvocationContext mContext;
45     private CompatibilityBuildHelper mBuildHelper;
46     private File mResultDir;
47     private File mFakeDir;
48 
49     @Before
setUp()50     public void setUp() throws Exception {
51         mFakeDir = FileUtil.createTempDir("result-dir");
52         mResultDir = new File(mFakeDir, "android-cts/results");
53         mResultDir.mkdirs();
54 
55         IBuildInfo info = new BuildInfo();
56         info.addBuildAttribute(CompatibilityBuildHelper.ROOT_DIR, mFakeDir.getAbsolutePath());
57         info.addBuildAttribute(
58                 CompatibilityBuildHelper.START_TIME_MS, Long.toString(System.currentTimeMillis()));
59         mContext = new InvocationContext();
60         mContext.addDeviceBuildInfo(ConfigurationDef.DEFAULT_DEVICE_NAME, info);
61 
62         mBuildHelper =
63                 new CompatibilityBuildHelper(info) {
64                     @Override
65                     public String getSuiteName() {
66                         return "CTS";
67                     }
68 
69                     @Override
70                     public String getSuiteVersion() {
71                         return "version";
72                     }
73 
74                     @Override
75                     public String getSuitePlan() {
76                         return "cts";
77                     }
78 
79                     @Override
80                     public String getSuiteBuild() {
81                         return "R1";
82                     }
83                 };
84     }
85 
86     @After
tearDown()87     public void tearDown() throws Exception {
88         FileUtil.recursiveDelete(mFakeDir);
89     }
90 
91     /** Check that the basic overall structure is good an contains all the information. */
92     @Test
testSuiteVariant()93     public void testSuiteVariant() throws Exception {
94         mConfiguration = new Configuration("test", "test");
95         mConfiguration.setCommandLine(new String[] {"cts", "-m", "CtsGestureTestCases"});
96 
97         mReporter =
98                 new CertificationSuiteResultReporter() {
99                     @Override
100                     CompatibilityBuildHelper createBuildHelper() {
101                         return mBuildHelper;
102                     }
103                 };
104         mReporter.setConfiguration(mConfiguration);
105 
106         mReporter.invocationStarted(mContext);
107         mReporter.invocationEnded(500L);
108 
109         File reportFile = new File(mBuildHelper.getResultDir(), "test_result.xml");
110         assertTrue(reportFile.exists());
111         String content = FileUtil.readStringFromFile(reportFile);
112         assertTrue(content.contains("suite_name=\"CTS\""));
113         assertTrue(content.contains("suite_variant=\"CTS\""));
114         assertTrue(content.contains("suite_version=\"version\""));
115     }
116 
117     /** Check that when running cts-on-gsi the variant is changed. */
118     @Test
testSuiteVariantGSI()119     public void testSuiteVariantGSI() throws Exception {
120         mConfiguration = new Configuration("test", "test");
121         mConfiguration.setCommandLine(new String[] {"cts-on-gsi"});
122 
123         mReporter =
124                 new CertificationSuiteResultReporter() {
125                     @Override
126                     CompatibilityBuildHelper createBuildHelper() {
127                         return mBuildHelper;
128                     }
129                 };
130         mReporter.setConfiguration(mConfiguration);
131 
132         mReporter.invocationStarted(mContext);
133         mReporter.invocationEnded(500L);
134 
135         File reportFile = new File(mBuildHelper.getResultDir(), "test_result.xml");
136         assertTrue(reportFile.exists());
137         String content = FileUtil.readStringFromFile(reportFile);
138         assertTrue(content.contains("suite_name=\"CTS\""));
139         assertTrue(content.contains("suite_variant=\"CTS_ON_GSI\""));
140         assertTrue(content.contains("suite_version=\"version\""));
141     }
142 
143     /**
144      * For the R release, ensure that CTS-on-GSI still report as VTS for APFE to ingest it properly
145      */
146     @Test
testSuiteVariantGSI_R_Compatibility()147     public void testSuiteVariantGSI_R_Compatibility() throws Exception {
148         mConfiguration = new Configuration("test", "test");
149         mConfiguration.setCommandLine(new String[] {"cts-on-gsi"});
150 
151         mReporter =
152                 new CertificationSuiteResultReporter() {
153                     @Override
154                     CompatibilityBuildHelper createBuildHelper() {
155                         return mBuildHelper;
156                     }
157                 };
158         OptionSetter setter = new OptionSetter(mReporter);
159         setter.setOptionValue("cts-on-gsi-variant", "true");
160         mReporter.setConfiguration(mConfiguration);
161 
162         mReporter.invocationStarted(mContext);
163         mReporter.invocationEnded(500L);
164 
165         File reportFile = new File(mBuildHelper.getResultDir(), "test_result.xml");
166         assertTrue(reportFile.exists());
167         String content = FileUtil.readStringFromFile(reportFile);
168         // Suite name is overridden to VTS for the R release
169         assertTrue(content.contains("suite_name=\"VTS\""));
170         assertTrue(content.contains("suite_variant=\"CTS_ON_GSI\""));
171         assertTrue(content.contains("suite_version=\"version\""));
172     }
173 
174     /**
175      * For the R release, ensure that CTS-on-GSI still report as VTS for APFE to ingest it properly
176      */
177     @Test
testSuiteVariantGSI_R_Compatibility_ATS()178     public void testSuiteVariantGSI_R_Compatibility_ATS() throws Exception {
179         mConfiguration = new Configuration("test", "test");
180         // ATS renames the config so we need to handle it.
181         mConfiguration.setCommandLine(new String[] {"_cts-on-gsi.xml"});
182 
183         mReporter =
184                 new CertificationSuiteResultReporter() {
185                     @Override
186                     CompatibilityBuildHelper createBuildHelper() {
187                         return mBuildHelper;
188                     }
189                 };
190         OptionSetter setter = new OptionSetter(mReporter);
191         setter.setOptionValue("cts-on-gsi-variant", "true");
192         mReporter.setConfiguration(mConfiguration);
193 
194         mReporter.invocationStarted(mContext);
195         mReporter.invocationEnded(500L);
196 
197         File reportFile = new File(mBuildHelper.getResultDir(), "test_result.xml");
198         assertTrue(reportFile.exists());
199         String content = FileUtil.readStringFromFile(reportFile);
200         // Suite name is overridden to VTS for the R release
201         assertTrue(content.contains("suite_name=\"VTS\""));
202         assertTrue(content.contains("suite_version=\"version\""));
203     }
204 }
205