xref: /aosp_15_r20/tools/asuite/atest/bazel/runner/src/com/android/tradefed/testtype/bazel/InvocationLogCollector.java (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
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.testtype.bazel;
17 
18 import com.android.tradefed.invoker.IInvocationContext;
19 import com.android.tradefed.result.InputStreamSource;
20 import com.android.tradefed.result.LogDataType;
21 import com.android.tradefed.result.ITestInvocationListener;
22 import com.android.tradefed.result.LogDataType;
23 import com.android.tradefed.result.LogFile;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.function.Consumer;
28 
29 /** Listener which collects all invocation-level test logs. */
30 final class InvocationLogCollector extends NullTestListener {
31 
32     private final List<Consumer<ITestInvocationListener>> mLogCalls;
33     private boolean mInModule;
34 
InvocationLogCollector()35     InvocationLogCollector() {
36         mLogCalls = new ArrayList<>();
37     }
38 
getLogCalls()39     public List<Consumer<ITestInvocationListener>> getLogCalls() {
40         return mLogCalls;
41     }
42 
43     @Override
testLog(String dataName, LogDataType dataType, InputStreamSource dataStream)44     public void testLog(String dataName, LogDataType dataType, InputStreamSource dataStream) {
45         if (mInModule) {
46             return;
47         }
48         mLogCalls.add(
49                 (ITestInvocationListener l) -> {
50                     l.testLog(dataName, dataType, dataStream);
51                 });
52     }
53 
54     @Override
testModuleStarted(IInvocationContext moduleContext)55     public void testModuleStarted(IInvocationContext moduleContext) {
56         mInModule = true;
57     }
58 
59     @Override
testModuleEnded()60     public void testModuleEnded() {
61         mInModule = false;
62     }
63 
64     @Override
testLogSaved( String dataName, LogDataType dataType, InputStreamSource dataStream, LogFile logFile)65     public void testLogSaved(
66             String dataName, LogDataType dataType, InputStreamSource dataStream, LogFile logFile) {
67 
68         if (mInModule) {
69             return;
70         }
71         mLogCalls.add(
72                 (ITestInvocationListener l) -> {
73                     TestListeners.testLogSaved(l, dataName, dataType, dataStream, logFile);
74                 });
75     }
76 
77     @Override
logAssociation(String dataName, LogFile logFile)78     public void logAssociation(String dataName, LogFile logFile) {
79         if (mInModule) {
80             return;
81         }
82         mLogCalls.add(
83                 (ITestInvocationListener l) -> {
84                     TestListeners.logAssociation(l, dataName, logFile);
85                 });
86     }
87 }
88