Summarizer.java revision 7e2a39b5985a1449588a397195c178bfef9421db
1/*
2 * Copyright (C) 2010 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
17package com.android.dumprendertree2;
18
19import java.io.File;
20import java.text.SimpleDateFormat;
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.Date;
24import java.util.List;
25
26/**
27 * A class that collects information about tests that ran and can create HTML
28 * files with summaries and easy navigation.
29 */
30public class Summarizer {
31
32    private static final String LOG_TAG = "Summarizer";
33
34    private static final String CSS =
35            "<style type=\"text/css\">" +
36            "* {" +
37            "       font-family: Verdana;" +
38            "       border: 0;" +
39            "       margin: 0;" +
40            "       padding: 0;}" +
41            "body {" +
42            "       margin: 10px;}" +
43            "h1 {" +
44            "       font-size: 24px;" +
45            "       margin: 4px 0 4px 0;}" +
46            "h2 {" +
47            "       font-size:18px;" +
48            "       text-transform: uppercase;" +
49            "       margin: 20px 0 3px 0;}" +
50            "h3, h3 a {" +
51            "       font-size: 14px;" +
52            "       color: black;" +
53            "       text-decoration: none;" +
54            "       margin-bottom: 4px;}" +
55            "h3 a span.path {" +
56            "       text-decoration: underline;}" +
57            "h3 span.tri {" +
58            "       text-decoration: none;" +
59            "       float: left;" +
60            "       width: 20px;}" +
61            "h3 span.sqr {" +
62            "       text-decoration: none;" +
63            "       color: #8ee100;" +
64            "       float: left;" +
65            "       width: 20px;}" +
66            "h3 img {" +
67            "       width: 8px;" +
68            "       margin-right: 4px;}" +
69            "div.diff {" +
70            "       margin-bottom: 25px;}" +
71            "div.diff a {" +
72            "       font-size: 12px;" +
73            "       color: #888;}" +
74            "table.visual_diff {" +
75            "       border-bottom: 0px solid;" +
76            "       border-collapse: collapse;" +
77            "       width: 100%;" +
78            "       margin-bottom: 2px;}" +
79            "table.visual_diff tr.headers td {" +
80            "       border-bottom: 1px solid;" +
81            "       border-top: 0;" +
82            "       padding-bottom: 3px;}" +
83            "table.visual_diff tr.results td {" +
84            "       border-top: 1px dashed;" +
85            "       border-right: 1px solid;" +
86            "       font-size: 15px;" +
87            "       vertical-align: top;}" +
88            "table.visual_diff tr.results td.line_count {" +
89            "       background-color:#aaa;" +
90            "       min-width:20px;" +
91            "       text-align: right;" +
92            "       border-right: 1px solid;" +
93            "       border-left: 1px solid;" +
94            "       padding: 2px 1px 2px 0px;}" +
95            "table.visual_diff tr.results td.line {" +
96            "       padding: 2px 0px 2px 4px;" +
97            "       border-right: 1px solid;" +
98            "       width: 49.8%;}" +
99            "table.visual_diff tr.footers td {" +
100            "       border-top: 1px solid;" +
101            "       border-bottom: 0;}" +
102            "table.visual_diff tr td.space {" +
103            "       border: 0;" +
104            "       width: 0.4%}" +
105            "div.space {" +
106            "       margin-top:4px;}" +
107            "span.eql {" +
108            "       background-color: #f3f3f3;}" +
109            "span.del {" +
110            "       background-color: #ff8888; }" +
111            "span.ins {" +
112            "       background-color: #88ff88; }" +
113            "span.fail {" +
114            "       color: red;}" +
115            "span.pass {" +
116            "       color: green;}" +
117            "span.time_out {" +
118            "       color: orange;}" +
119            "table.summary {" +
120            "       border: 1px solid black;" +
121            "       margin-top: 20px;}" +
122            "table.summary td {" +
123            "       padding: 3px;}" +
124            "span.listItem {" +
125            "       font-size: 11px;" +
126            "       font-weight: normal;" +
127            "       text-transform: uppercase;" +
128            "       padding: 3px;" +
129            "       -webkit-border-radius: 4px;}" +
130            "span." + AbstractResult.ResultCode.PASS.name() + "{" +
131            "       background-color: #8ee100;" +
132            "       color: black;}" +
133            "span." + AbstractResult.ResultCode.FAIL_RESULT_DIFFERS.name() + "{" +
134            "       background-color: #ccc;" +
135            "       color: black;}" +
136            "span." + AbstractResult.ResultCode.FAIL_NO_EXPECTED_RESULT.name() + "{" +
137            "       background-color: #a700e4;" +
138            "       color: #fff;}" +
139            "span." + AbstractResult.ResultCode.FAIL_TIMED_OUT.name() + "{" +
140            "       background-color: #f3cb00;" +
141            "       color: black;}" +
142            "span." + AbstractResult.ResultCode.FAIL_CRASHED.name() + "{" +
143            "       background-color: #c30000;" +
144            "       color: #fff;}" +
145            "span.noLtc {" +
146            "       background-color: #944000;" +
147            "       color: #fff;}" +
148            "span.noEventSender {" +
149            "       background-color: #815600;" +
150            "       color: #fff;}" +
151            "</style>";
152
153    private static final String SCRIPT =
154            "<script type=\"text/javascript\">" +
155            "    function toggleDisplay(id) {" +
156            "        element = document.getElementById(id);" +
157            "        triangle = document.getElementById('tri.' + id);" +
158            "        if (element.style.display == 'none') {" +
159            "            element.style.display = 'inline';" +
160            "            triangle.innerHTML = '&#x25bc; ';" +
161            "        } else {" +
162            "            element.style.display = 'none';" +
163            "            triangle.innerHTML = '&#x25b6; ';" +
164            "        }" +
165            "    }" +
166            "</script>";
167
168    /** TODO: Make it a setting */
169    private static final String HTML_SUMMARY_RELATIVE_PATH = "summary.html";
170
171    private int mCrashedTestsCount = 0;
172    private List<AbstractResult> mFailedNotIgnoredTests = new ArrayList<AbstractResult>();
173    private List<AbstractResult> mIgnoredTests = new ArrayList<AbstractResult>();
174    private List<String> mPassedNotIgnoredTests = new ArrayList<String>();
175
176    private FileFilter mFileFilter;
177    private String mResultsRootDirPath;
178
179    public Summarizer(FileFilter fileFilter, String resultsRootDirPath) {
180        mFileFilter = fileFilter;
181        mResultsRootDirPath = resultsRootDirPath;
182    }
183
184    public void appendTest(AbstractResult result) {
185        String relativePath = result.getRelativePath();
186
187        if (result.getResultCode() == AbstractResult.ResultCode.FAIL_CRASHED) {
188            mCrashedTestsCount++;
189        }
190
191        if (mFileFilter.isIgnoreRes(relativePath)) {
192            mIgnoredTests.add(result);
193        } else if (result.getResultCode() == AbstractResult.ResultCode.PASS) {
194            mPassedNotIgnoredTests.add(relativePath);
195        } else {
196            mFailedNotIgnoredTests.add(result);
197        }
198    }
199
200    public void summarize() {
201        StringBuilder html = new StringBuilder();
202
203        html.append("<html><head>");
204        html.append(CSS);
205        html.append(SCRIPT);
206        html.append("</head><body>");
207
208        createTopSummaryTable(html);
209
210        createResultsListWithDiff(html, "Failed", mFailedNotIgnoredTests);
211
212        createResultsListWithDiff(html, "Ignored", mIgnoredTests);
213
214        createResultsListNoDiff(html, "Passed", mPassedNotIgnoredTests);
215
216        html.append("</body></html>");
217
218        FsUtils.writeDataToStorage(new File(mResultsRootDirPath, HTML_SUMMARY_RELATIVE_PATH),
219                html.toString().getBytes(), false);
220    }
221
222    private void createTopSummaryTable(StringBuilder html) {
223        int total = mFailedNotIgnoredTests.size() +
224                mPassedNotIgnoredTests.size() +
225                mIgnoredTests.size();
226        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
227        html.append("<h1> - total of " + total + " tests - ");
228        html.append(dateFormat.format(new Date()) + "</h1>");
229
230        html.append("<table class=\"summary\">");
231        createSummaryTableRow(html, "CRASHED", mCrashedTestsCount);
232        createSummaryTableRow(html, "FAILED", mFailedNotIgnoredTests.size());
233        createSummaryTableRow(html, "IGNORED", mIgnoredTests.size());
234        createSummaryTableRow(html, "PASSED", mPassedNotIgnoredTests.size());
235        html.append("</table>");
236    }
237
238    private void createSummaryTableRow(StringBuilder html, String caption, int size) {
239        html.append("<tr>");
240        html.append("    <td>" + caption + "</td>");
241        html.append("    <td>" + size + "</td>");
242        html.append("</tr>");
243    }
244
245    private void createResultsListWithDiff(StringBuilder html, String title,
246            List<AbstractResult> resultsList) {
247        String relativePath;
248        String id = "";
249        AbstractResult.ResultCode resultCode;
250
251        Collections.sort(resultsList);
252        html.append("<h2>" + title + " [" + resultsList.size() + "]</h2>");
253        for (AbstractResult result : resultsList) {
254            relativePath = result.getRelativePath();
255            resultCode = result.getResultCode();
256
257            html.append("<h3>");
258
259            if (resultCode == AbstractResult.ResultCode.PASS) {
260                html.append("<span class=\"sqr\">&#x25a0; </span>");
261                html.append("<span class=\"path\">" + relativePath + "</span>");
262            } else {
263                /**
264                 * Technically, two different paths could end up being the same, because
265                 * ':' is a valid  character in a path. However, it is probably not going
266                 * to cause any problems in this case
267                 */
268                id = relativePath.replace(File.separator, ":");
269                html.append("<a href=\"#\" onClick=\"toggleDisplay('" + id + "');");
270                html.append("return false;\">");
271                html.append("<span class=\"tri\" id=\"tri." + id + "\">&#x25b6; </span>");
272                html.append("<span class=\"path\">" + relativePath + "</span>");
273                html.append("</a>");
274            }
275
276            html.append(" <span class=\"listItem " + resultCode.name() + "\">");
277            html.append(resultCode.toString());
278            html.append("</span>");
279
280            /** Detect missing LTC function */
281            String additionalTextOutputString = result.getAdditionalTextOutputString();
282            if (additionalTextOutputString != null &&
283                    additionalTextOutputString.contains("com.android.dumprendertree") &&
284                    additionalTextOutputString.contains("has no method")) {
285                if (additionalTextOutputString.contains("LayoutTestController")) {
286                    html.append(" <span class=\"listItem noLtc\">LTC function missing</span>");
287                }
288                if (additionalTextOutputString.contains("EventSender")) {
289                    html.append(" <span class=\"listItem noEventSender\">");
290                    html.append("ES function missing</span>");
291                }
292            }
293
294            html.append("</h3>");
295
296            if (resultCode != AbstractResult.ResultCode.PASS) {
297                html.append("<div class=\"diff\" style=\"display: none;\" id=\"" + id + "\">");
298                html.append(result.getDiffAsHtml());
299                html.append("<a href=\"#\" onClick=\"toggleDisplay('" + id + "');");
300                html.append("return false;\">Hide</a>");
301                html.append("</div>");
302            }
303
304            html.append("<div class=\"space\"></div>");
305        }
306    }
307
308    private void createResultsListNoDiff(StringBuilder html, String title,
309            List<String> resultsList) {
310        Collections.sort(resultsList);
311        html.append("<h2>Passed [" + resultsList.size() + "]</h2>");
312        for (String result : resultsList) {
313            html.append("<h3>");
314            html.append("<span class=\"sqr\">&#x25a0; </span>");
315            html.append("<span class=\"path\">" + result + "</span>");
316            html.append("</h3>");
317            html.append("<div class=\"space\"></div>");
318        }
319    }
320}