1 /*
2  * Copyright (C) 2018 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 com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
19 import com.android.tradefed.invoker.IInvocationContext;
20 import com.android.tradefed.log.LogUtil.CLog;
21 import com.android.tradefed.result.proto.FileProtoResultReporter;
22 import com.android.tradefed.result.proto.TestRecordProto.TestRecord;
23 
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 
27 /** Proto reporter that will drop a {@link TestRecord} protobuf in the result directory. */
28 public class CompatibilityProtoResultReporter extends FileProtoResultReporter {
29 
30     public static final String PROTO_FILE_NAME = "test-record.pb";
31     public static final String PROTO_DIR = "proto";
32 
33     private CompatibilityBuildHelper mBuildHelper;
34 
35     /** The directory containing the proto results */
36     private File mResultDir = null;
37 
38     @Override
processStartInvocation( TestRecord invocationStartRecord, IInvocationContext invocationContext)39     public void processStartInvocation(
40             TestRecord invocationStartRecord, IInvocationContext invocationContext) {
41         if (mBuildHelper == null) {
42             mBuildHelper = new CompatibilityBuildHelper(invocationContext.getBuildInfos().get(0));
43             mResultDir = getProtoResultDirectory();
44             File protoFile = new File(mResultDir, PROTO_FILE_NAME);
45             setFileOutput(protoFile);
46         }
47         super.processStartInvocation(invocationStartRecord, invocationContext);
48     }
49 
getProtoResultDirectory()50     private File getProtoResultDirectory() {
51         File protoDir = null;
52         try {
53             File resultDir = mBuildHelper.getResultDir();
54             if (resultDir != null) {
55                 resultDir.mkdirs();
56             }
57             protoDir = new File(resultDir, PROTO_DIR);
58             protoDir.mkdir();
59         } catch (FileNotFoundException e) {
60             throw new RuntimeException(e);
61         }
62         if (!protoDir.exists()) {
63             throw new RuntimeException(
64                     "Result Directory was not created: " + protoDir.getAbsolutePath());
65         }
66         CLog.d("Proto Results Directory: %s", protoDir.getAbsolutePath());
67         return protoDir;
68     }
69 }