1 /*
2  * Copyright 2012 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.tradefed.testtype;
17 
18 import com.android.ddmlib.testrunner.ITestRunListener;
19 import com.android.tradefed.log.LogUtil.CLog;
20 
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.ArrayList;
24 
25 public class WrappedGTestResultParser extends GeeTestResultParser {
26 
27     private boolean mInstrumentationError;
28 
29     /**
30      * Creates the WrappedGTestResultParser.
31      *
32      * @param testRunId the test run id to provide to
33      *            {@link ITestRunListener#testRunStarted(String, int)}
34      * @param listeners informed of test results as the tests are executing
35      */
WrappedGTestResultParser(String testRunId, Collection<ITestRunListener> listeners)36     public WrappedGTestResultParser(String testRunId, Collection<ITestRunListener> listeners) {
37         super(testRunId, listeners);
38     }
39 
40     /**
41      * Creates the WrappedGTestResultParser for a single listener.
42      *
43      * @param testRunId the test run id to provide to
44      *            {@link ITestRunListener#testRunStarted(String, int)}
45      * @param listener informed of test results as the tests are executing
46      */
WrappedGTestResultParser(String testRunId, ITestRunListener listener)47     public WrappedGTestResultParser(String testRunId, ITestRunListener listener) {
48         super(testRunId, listener);
49     }
50 
51     /**
52      * Strips the instrumentation information and then forwards
53      * the raw gtest output to the {@link GeeTestResultParser}.
54      */
55     @Override
processNewLines(String[] lines)56     public void processNewLines(String[] lines) {
57         if (mInstrumentationError) {
58             return;
59         }
60 
61         String[] gtestOutput = parseInstrumentation(lines);
62         super.processNewLines(gtestOutput);
63     }
64 
65     /**
66      * Parses raw instrumentation output and returns the
67      * contained gtest output
68      *
69      * @param lines the raw instrumentation output
70      * @return the gtest output
71      */
parseInstrumentation(String[] lines)72     public String[] parseInstrumentation(String[] lines) {
73         List<String> output = new ArrayList<String>();
74         boolean readMultiLine = false;
75         for (String line : lines) {
76 
77             if (line.startsWith("INSTRUMENTATION_RESULT: ")) {
78                 CLog.e("Instrumentation Error:");
79                 mInstrumentationError = true;
80             }
81 
82             if (mInstrumentationError) {
83                 CLog.e(line);
84                 continue;
85             }
86 
87             if (line.startsWith("INSTRUMENTATION_STATUS: gtest=")) {
88                 output.add(line.replace("INSTRUMENTATION_STATUS: gtest=", ""));
89                 readMultiLine = true;
90                 continue;
91             }
92 
93             if (line.startsWith("INSTRUMENTATION_")) {
94                 readMultiLine = false;
95                 continue;
96             }
97 
98             if (readMultiLine) {
99                 output.add(line);
100             }
101         }
102 
103         return output.toArray(new String[output.size()]);
104     }
105 }
106 
107