Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / testing / android / junit / javatests / src / org / chromium / testing / local / GtestLoggerTest.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.testing.local;
6
7 import org.junit.Assert;
8 import org.junit.Test;
9 import org.junit.runner.Description;
10 import org.junit.runner.RunWith;
11 import org.junit.runners.BlockJUnit4ClassRunner;
12
13 import java.io.ByteArrayOutputStream;
14 import java.io.PrintStream;
15 import java.io.Serializable;
16 import java.util.Comparator;
17 import java.util.HashSet;
18 import java.util.Set;
19 import java.util.TreeSet;
20
21 /**
22  *  Unit tests for GtestLogger.
23  */
24 @RunWith(BlockJUnit4ClassRunner.class)
25 public class GtestLoggerTest {
26
27     @Test
28     public void testTestStarted() {
29         ByteArrayOutputStream actual = new ByteArrayOutputStream();
30         GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
31         loggerUnderTest.testStarted(
32                 Description.createTestDescription(GtestLoggerTest.class, "testTestStarted"));
33         Assert.assertEquals(
34                 "[ RUN      ] org.chromium.testing.local.GtestLoggerTest.testTestStarted\n",
35                 actual.toString());
36     }
37
38     @Test
39     public void testTestFinishedPassed() {
40         ByteArrayOutputStream actual = new ByteArrayOutputStream();
41         GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
42         loggerUnderTest.testFinished(
43                 Description.createTestDescription(GtestLoggerTest.class, "testTestFinishedPassed"),
44                 true, 123);
45         Assert.assertEquals(
46                 "[       OK ] org.chromium.testing.local.GtestLoggerTest.testTestFinishedPassed" +
47                         " (123 ms)\n",
48                 actual.toString());
49     }
50
51     @Test
52     public void testTestFinishedFailed() {
53         ByteArrayOutputStream actual = new ByteArrayOutputStream();
54         GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
55         loggerUnderTest.testFinished(
56                 Description.createTestDescription(GtestLoggerTest.class, "testTestFinishedPassed"),
57                 false, 123);
58         Assert.assertEquals(
59                 "[   FAILED ] org.chromium.testing.local.GtestLoggerTest.testTestFinishedPassed" +
60                         " (123 ms)\n",
61                 actual.toString());
62     }
63
64     @Test
65     public void testTestCaseStarted() {
66         ByteArrayOutputStream actual = new ByteArrayOutputStream();
67         GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
68         loggerUnderTest.testCaseStarted(
69                 Description.createSuiteDescription(GtestLoggerTest.class), 456);
70         Assert.assertEquals(
71                 "[----------] Run 456 test cases from org.chromium.testing.local.GtestLoggerTest\n",
72                 actual.toString());
73     }
74
75     @Test
76     public void testTestCaseFinished() {
77         ByteArrayOutputStream actual = new ByteArrayOutputStream();
78         GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
79         loggerUnderTest.testCaseFinished(
80                 Description.createSuiteDescription(GtestLoggerTest.class), 456, 123);
81         Assert.assertEquals(
82                 "[----------] Run 456 test cases from org.chromium.testing.local.GtestLoggerTest" +
83                         " (123 ms)\n\n",
84                 actual.toString());
85     }
86
87     @Test
88     public void testTestRunStarted() {
89         ByteArrayOutputStream actual = new ByteArrayOutputStream();
90         GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
91         loggerUnderTest.testRunStarted(1234);
92         Assert.assertEquals(
93                 "[==========] Running 1234 tests.\n" +
94                 "[----------] Global test environment set-up.\n\n",
95                 actual.toString());
96     }
97
98     @Test
99     public void testTestRunFinishedNoFailures() {
100         ByteArrayOutputStream actual = new ByteArrayOutputStream();
101         GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
102         loggerUnderTest.testRunFinished(1234, new HashSet<Description>(), 4321);
103         Assert.assertEquals(
104                 "[----------] Global test environment tear-down.\n" +
105                 "[==========] 1234 tests ran. (4321 ms total)\n" +
106                 "[  PASSED  ] 1234 tests.\n",
107                 actual.toString());
108     }
109
110     @Test
111     public void testTestRunFinishedWithFailures() {
112         ByteArrayOutputStream actual = new ByteArrayOutputStream();
113         GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual));
114
115         Set<Description> failures = new TreeSet<Description>(new DescriptionComparator());
116         failures.add(Description.createTestDescription(
117                 "GtestLoggerTest", "testTestRunFinishedNoFailures"));
118         failures.add(Description.createTestDescription(
119                 "GtestLoggerTest", "testTestRunFinishedWithFailures"));
120
121         loggerUnderTest.testRunFinished(1232, failures, 4312);
122         Assert.assertEquals(
123                 "[----------] Global test environment tear-down.\n" +
124                 "[==========] 1234 tests ran. (4312 ms total)\n" +
125                 "[  PASSED  ] 1232 tests.\n" +
126                 "[  FAILED  ] 2 tests.\n" +
127                 "[  FAILED  ] GtestLoggerTest.testTestRunFinishedNoFailures\n" +
128                 "[  FAILED  ] GtestLoggerTest.testTestRunFinishedWithFailures\n" +
129                 "\n",
130                 actual.toString());
131     }
132
133     private static class DescriptionComparator implements Comparator<Description>, Serializable {
134         @Override
135         public int compare(Description o1, Description o2) {
136             return toGtestStyleString(o1).compareTo(toGtestStyleString(o2));
137         }
138
139         private static String toGtestStyleString(Description d) {
140             return d.getClassName() + "." + d.getMethodName();
141         }
142     }
143 }
144