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 
20 import junit.framework.TestCase;
21 
22 
23 
24 /**
25  * Unit tests for {@link WrappedGTestResultParser}.
26  */
27 public class WrappedGTestResultParserTest extends TestCase {
28 
29     private WrappedGTestResultParser mParser;
30     private final String[] INPUT1 = new String[] {
31         "INSTRUMENTATION_STATUS: gtest=[==========] Running 9 tests from 2 test cases.",
32         "INSTRUMENTATION_STATUS_CODE: 1",
33         "INSTRUMENTATION_STATUS: gtest=[ RUN      ] GLTest.Test1",
34         "INSTRUMENTATION_STATUS: gtest=[       OK ] GLTest.Test1 (10 ms)",
35         "INSTRUMENTATION_STATUS: gtest=/tests/SomeTestFile.cpp:1337: Failure",
36         "Value of: 1 == 0",
37         "  Actual: false",
38         "Expected: true",
39         "INSTRUMENTATION_STATUS: gtest=[  FAILED  ] GLTest.Test2 (1016 ms)",
40         "INSTRUMENTATION_STATUS: gtest=[==========] 2 tests from 1 test cases ran. (17 ms total)",
41         "INSTRUMENTATION_CODE: -1"
42     };
43 
44     private final String[] EXPECTED_OUTPUT1 = new String[] {
45         "[==========] Running 9 tests from 2 test cases.",
46         "[ RUN      ] GLTest.Test1",
47         "[       OK ] GLTest.Test1 (10 ms)",
48         "/tests/SomeTestFile.cpp:1337: Failure",
49         "Value of: 1 == 0",
50         "  Actual: false",
51         "Expected: true",
52         "[  FAILED  ] GLTest.Test2 (1016 ms)",
53         "[==========] 2 tests from 1 test cases ran. (17 ms total)",
54     };
55 
56     private final String[] INPUT2 = new String[] {
57         "INSTRUMENTATION_STATUS_CODE: 1",
58         "invalid text",
59         "INSTRUMENTATION_STATUS: gtest=[==========] Running 9 tests from 2 test cases.",
60         "INSTRUMENTATION_RESULT: some error",
61         "INSTRUMENTATION_STATUS: gtest=[ RUN      ] GLTest.ExpectTestThatShouldBeSuccessful",
62     };
63 
64     private final String[] EXPECTED_OUTPUT2 = new String[] {
65         "[==========] Running 9 tests from 2 test cases.",
66     };
67 
68     /**
69      * {@inheritDoc}
70      */
71     @Override
setUp()72     protected void setUp() throws Exception {
73         super.setUp();
74         mParser = new WrappedGTestResultParser("unused", (ITestRunListener)null);
75     }
76 
assertArrayEquals(String[] expected, String[] result)77     private void assertArrayEquals(String[] expected, String[] result) throws Exception {
78         if (expected == null) {
79             assertNull(result);
80             return;
81         }
82 
83         assertEquals(expected.length, result.length);
84 
85         for (int i = 0; i < expected.length; i++) {
86             assertEquals(expected[i], result[i]);
87         }
88     }
89 
90     /**
91      * Test normal case {@link WrappedGTestResultParser#getRawGTestOutput(java.lang.String[])}
92      * with all kinds of valid input lines.
93      */
testGetRawGTestOutput_valid()94     public void testGetRawGTestOutput_valid() throws Exception {
95         assertArrayEquals(EXPECTED_OUTPUT1, mParser.parseInstrumentation(INPUT1));
96     }
97 
98     /**
99      * Test normal case {@link WrappedGTestResultParser#getRawGTestOutput(java.lang.String[])}
100      * with a instrumentation error/invalid input.
101      */
testGetRawGTestOutput_invalid()102     public void testGetRawGTestOutput_invalid() throws Exception {
103         assertArrayEquals(EXPECTED_OUTPUT2, mParser.parseInstrumentation(INPUT2));
104     }
105 }
106