Summarizer.java revision 8a6def02473ee4fbffcd1b34173daf751d316202
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 android.content.res.AssetManager;
20import android.content.res.Configuration;
21import android.content.res.Resources;
22import android.os.Build;
23import android.util.DisplayMetrics;
24import android.util.Log;
25
26import com.android.dumprendertree2.forwarder.ForwarderManager;
27
28import java.io.File;
29import java.net.MalformedURLException;
30import java.net.URI;
31import java.net.URL;
32import java.text.SimpleDateFormat;
33import java.util.ArrayList;
34import java.util.Collections;
35import java.util.Date;
36import java.util.List;
37import java.util.regex.Matcher;
38import java.util.regex.Pattern;
39
40/**
41 * A class that collects information about tests that ran and can create HTML
42 * files with summaries and easy navigation.
43 */
44public class Summarizer {
45
46    private static final String LOG_TAG = "Summarizer";
47
48    private static final String CSS =
49            "<style type=\"text/css\">" +
50            "* {" +
51            "       font-family: Verdana;" +
52            "       border: 0;" +
53            "       margin: 0;" +
54            "       padding: 0;}" +
55            "body {" +
56            "       margin: 10px;}" +
57            "h1 {" +
58            "       font-size: 24px;" +
59            "       margin: 4px 0 4px 0;}" +
60            "h2 {" +
61            "       font-size:18px;" +
62            "       text-transform: uppercase;" +
63            "       margin: 20px 0 3px 0;}" +
64            "h3, h3 a {" +
65            "       font-size: 14px;" +
66            "       color: black;" +
67            "       text-decoration: none;" +
68            "       margin-top: 4px;" +
69            "       margin-bottom: 2px;}" +
70            "h3 a span.path {" +
71            "       text-decoration: underline;}" +
72            "h3 span.tri {" +
73            "       text-decoration: none;" +
74            "       float: left;" +
75            "       width: 20px;}" +
76            "h3 span.sqr {" +
77            "       text-decoration: none;" +
78            "       float: left;" +
79            "       width: 20px;}" +
80            "h3 span.sqr_pass {" +
81            "       color: #8ee100;}" +
82            "h3 span.sqr_fail {" +
83            "       color: #c30000;}" +
84            "span.source {" +
85            "       display: block;" +
86            "       font-size: 10px;" +
87            "       color: #888;" +
88            "       margin-left: 20px;" +
89            "       margin-bottom: 1px;}" +
90            "span.source a {" +
91            "       font-size: 10px;" +
92            "       color: #888;}" +
93            "h3 img {" +
94            "       width: 8px;" +
95            "       margin-right: 4px;}" +
96            "div.diff {" +
97            "       margin-bottom: 25px;}" +
98            "div.diff a {" +
99            "       font-size: 12px;" +
100            "       color: #888;}" +
101            "table.visual_diff {" +
102            "       border-bottom: 0px solid;" +
103            "       border-collapse: collapse;" +
104            "       width: 100%;" +
105            "       margin-bottom: 2px;}" +
106            "table.visual_diff tr.headers td {" +
107            "       border-bottom: 1px solid;" +
108            "       border-top: 0;" +
109            "       padding-bottom: 3px;}" +
110            "table.visual_diff tr.results td {" +
111            "       border-top: 1px dashed;" +
112            "       border-right: 1px solid;" +
113            "       font-size: 15px;" +
114            "       vertical-align: top;}" +
115            "table.visual_diff tr.results td.line_count {" +
116            "       background-color:#aaa;" +
117            "       min-width:20px;" +
118            "       text-align: right;" +
119            "       border-right: 1px solid;" +
120            "       border-left: 1px solid;" +
121            "       padding: 2px 1px 2px 0px;}" +
122            "table.visual_diff tr.results td.line {" +
123            "       padding: 2px 0px 2px 4px;" +
124            "       border-right: 1px solid;" +
125            "       width: 49.8%;}" +
126            "table.visual_diff tr.footers td {" +
127            "       border-top: 1px solid;" +
128            "       border-bottom: 0;}" +
129            "table.visual_diff tr td.space {" +
130            "       border: 0;" +
131            "       width: 0.4%}" +
132            "div.space {" +
133            "       margin-top:4px;}" +
134            "span.eql {" +
135            "       background-color: #f3f3f3;}" +
136            "span.del {" +
137            "       background-color: #ff8888; }" +
138            "span.ins {" +
139            "       background-color: #88ff88; }" +
140            "table.summary {" +
141            "       border: 1px solid black;" +
142            "       margin-top: 20px;}" +
143            "table.summary td {" +
144            "       padding: 3px;}" +
145            "span.listItem {" +
146            "       font-size: 11px;" +
147            "       font-weight: normal;" +
148            "       text-transform: uppercase;" +
149            "       padding: 3px;" +
150            "       -webkit-border-radius: 4px;}" +
151            "span." + AbstractResult.ResultCode.RESULTS_DIFFER.name() + "{" +
152            "       background-color: #ccc;" +
153            "       color: black;}" +
154            "span." + AbstractResult.ResultCode.NO_EXPECTED_RESULT.name() + "{" +
155            "       background-color: #a700e4;" +
156            "       color: #fff;}" +
157            "span.timed_out {" +
158            "       background-color: #f3cb00;" +
159            "       color: black;}" +
160            "span.crashed {" +
161            "       background-color: #c30000;" +
162            "       color: #fff;}" +
163            "span.noLtc {" +
164            "       background-color: #944000;" +
165            "       color: #fff;}" +
166            "span.noEventSender {" +
167            "       background-color: #815600;" +
168            "       color: #fff;}" +
169            "</style>";
170
171    private static final String SCRIPT =
172            "<script type=\"text/javascript\">" +
173            "    function toggleDisplay(id) {" +
174            "        element = document.getElementById(id);" +
175            "        triangle = document.getElementById('tri.' + id);" +
176            "        if (element.style.display == 'none') {" +
177            "            element.style.display = 'inline';" +
178            "            triangle.innerHTML = '&#x25bc; ';" +
179            "        } else {" +
180            "            element.style.display = 'none';" +
181            "            triangle.innerHTML = '&#x25b6; ';" +
182            "        }" +
183            "    }" +
184            "</script>";
185
186    /** TODO: Make it a setting */
187    private static final String HTML_DETAILS_RELATIVE_PATH = "details.html";
188    private static final String TXT_SUMMARY_RELATIVE_PATH = "summary.txt";
189
190    private int mCrashedTestsCount = 0;
191    private List<AbstractResult> mUnexpectedFailures = new ArrayList<AbstractResult>();
192    private List<AbstractResult> mExpectedFailures = new ArrayList<AbstractResult>();
193    private List<AbstractResult> mExpectedPasses = new ArrayList<AbstractResult>();
194    private List<AbstractResult> mUnexpectedPasses = new ArrayList<AbstractResult>();
195
196    private FileFilter mFileFilter;
197    private String mResultsRootDirPath;
198    private String mTestsRelativePath;
199    private Date mDate;
200
201    public Summarizer(FileFilter fileFilter, String resultsRootDirPath) {
202        mFileFilter = fileFilter;
203        mResultsRootDirPath = resultsRootDirPath;
204    }
205
206    public static URI getDetailsUri() {
207        return new File(ManagerService.RESULTS_ROOT_DIR_PATH + File.separator +
208                HTML_DETAILS_RELATIVE_PATH).toURI();
209    }
210
211    public void appendTest(AbstractResult result) {
212        String relativePath = result.getRelativePath();
213
214        if (result.didCrash()) {
215            mCrashedTestsCount++;
216        }
217
218        if (result.didPass()) {
219            if (mFileFilter.isFail(relativePath)) {
220                mUnexpectedPasses.add(result);
221            } else {
222                mExpectedPasses.add(result);
223            }
224        } else {
225            if (mFileFilter.isFail(relativePath)) {
226                mExpectedFailures.add(result);
227            } else {
228                mUnexpectedFailures.add(result);
229            }
230        }
231    }
232
233    public void setTestsRelativePath(String testsRelativePath) {
234        mTestsRelativePath = testsRelativePath;
235    }
236
237    public void summarize() {
238        String webKitRevision = getWebKitRevision();
239        createHtmlDetails(webKitRevision);
240        createTxtSummary(webKitRevision);
241    }
242
243    public void reset() {
244        mCrashedTestsCount = 0;
245        mUnexpectedFailures.clear();
246        mExpectedFailures.clear();
247        mExpectedPasses.clear();
248        mDate = new Date();
249    }
250
251    private void createTxtSummary(String webKitRevision) {
252        StringBuilder txt = new StringBuilder();
253
254        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
255        txt.append("Path: " + mTestsRelativePath + "\n");
256        txt.append("Date: " + dateFormat.format(mDate) + "\n");
257        txt.append("Build fingerprint: " + Build.FINGERPRINT + "\n");
258        txt.append("WebKit version: " + getWebKitVersionFromUserAgentString() + "\n");
259        txt.append("WebKit revision: " + webKitRevision + "\n");
260
261        txt.append("TOTAL:                     " + getTotalTestCount() + "\n");
262        txt.append("CRASHED (among all tests): " + mCrashedTestsCount + "\n");
263        txt.append("UNEXPECTED FAILURES:       " + mUnexpectedFailures.size() + "\n");
264        txt.append("UNEXPECTED PASSES:         " + mUnexpectedPasses.size() + "\n");
265        txt.append("EXPECTED FAILURES:         " + mExpectedFailures.size() + "\n");
266        txt.append("EXPECTED PASSES:           " + mExpectedPasses.size() + "\n");
267
268        FsUtils.writeDataToStorage(new File(mResultsRootDirPath, TXT_SUMMARY_RELATIVE_PATH),
269                txt.toString().getBytes(), false);
270    }
271
272    private void createHtmlDetails(String webKitRevision) {
273        StringBuilder html = new StringBuilder();
274
275        html.append("<html><head>");
276        html.append(CSS);
277        html.append(SCRIPT);
278        html.append("</head><body>");
279
280        createTopSummaryTable(webKitRevision, html);
281
282        createResultsList(html, "Unexpected failures", mUnexpectedFailures);
283        createResultsList(html, "Unexpected passes", mUnexpectedPasses);
284        createResultsList(html, "Expected failures", mExpectedFailures);
285        createResultsList(html, "Expected passes", mExpectedPasses);
286
287        html.append("</body></html>");
288
289        FsUtils.writeDataToStorage(new File(mResultsRootDirPath, HTML_DETAILS_RELATIVE_PATH),
290                html.toString().getBytes(), false);
291    }
292
293    private int getTotalTestCount() {
294        return mUnexpectedFailures.size() +
295                mUnexpectedPasses.size() +
296                mExpectedPasses.size() +
297                mExpectedFailures.size();
298    }
299
300    private String getWebKitVersionFromUserAgentString() {
301        Resources resources = new Resources(new AssetManager(), new DisplayMetrics(),
302                new Configuration());
303        String userAgent =
304                resources.getString(com.android.internal.R.string.web_user_agent);
305
306        Matcher matcher = Pattern.compile("AppleWebKit/([0-9]+?\\.[0-9])").matcher(userAgent);
307        if (matcher.find()) {
308            return matcher.group(1);
309        }
310        return "unknown";
311    }
312
313    private String getWebKitRevision() {
314        URL url = null;
315        try {
316            url = new URL(ForwarderManager.getHostSchemePort(false) + "ThirdPartyProject.prop");
317        } catch (MalformedURLException e) {
318            assert false;
319        }
320
321        String thirdPartyProjectContents = new String(FsUtils.readDataFromUrl(url));
322        Matcher matcher = Pattern.compile("^version=([0-9]+)", Pattern.MULTILINE).matcher(
323                thirdPartyProjectContents);
324        if (matcher.find()) {
325            return matcher.group(1);
326        }
327        return "unknown";
328    }
329
330    private void createTopSummaryTable(String webKitRevision, StringBuilder html) {
331        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
332        html.append("<h1>" + "Layout tests' results for: " +
333                (mTestsRelativePath.equals("") ? "all tests" : mTestsRelativePath) + "</h1>");
334        html.append("<h3>" + "Date: " + dateFormat.format(new Date()) + "</h3>");
335        html.append("<h3>" + "Build fingerprint: " + Build.FINGERPRINT + "</h3>");
336        html.append("<h3>" + "WebKit version: " + getWebKitVersionFromUserAgentString() + "</h3>");
337
338        html.append("<h3>" + "WebKit revision: ");
339        html.append("<a href=\"http://trac.webkit.org/browser/trunk?rev=" + webKitRevision +
340                "\" target=\"_blank\"><span class=\"path\">" + webKitRevision + "</span></a>");
341        html.append("</h3>");
342
343        html.append("<table class=\"summary\">");
344        createSummaryTableRow(html, "TOTAL", getTotalTestCount());
345        createSummaryTableRow(html, "CRASHED (among all tests)", mCrashedTestsCount);
346        createSummaryTableRow(html, "UNEXPECTED FAILURES", mUnexpectedFailures.size());
347        createSummaryTableRow(html, "UNEXPECTED PASSES", mUnexpectedPasses.size());
348        createSummaryTableRow(html, "EXPECTED FAILURES", mExpectedFailures.size());
349        createSummaryTableRow(html, "EXPECTED PASSES", mExpectedPasses.size());
350        html.append("</table>");
351    }
352
353    private void createSummaryTableRow(StringBuilder html, String caption, int size) {
354        html.append("<tr>");
355        html.append("    <td>" + caption + "</td>");
356        html.append("    <td>" + size + "</td>");
357        html.append("</tr>");
358    }
359
360    private void createResultsList(
361            StringBuilder html, String title, List<AbstractResult> resultsList) {
362        String relativePath;
363        String id = "";
364        AbstractResult.ResultCode resultCode;
365
366        Collections.sort(resultsList);
367        html.append("<h2>" + title + " [" + resultsList.size() + "]</h2>");
368        for (AbstractResult result : resultsList) {
369            relativePath = result.getRelativePath();
370            resultCode = result.getResultCode();
371
372            html.append("<h3>");
373
374            /**
375             * Technically, two different paths could end up being the same, because
376             * ':' is a valid  character in a path. However, it is probably not going
377             * to cause any problems in this case
378             */
379            id = relativePath.replace(File.separator, ":");
380
381            /** Write the test name */
382            if (resultCode == AbstractResult.ResultCode.RESULTS_DIFFER) {
383                html.append("<a href=\"#\" onClick=\"toggleDisplay('" + id + "');");
384                html.append("return false;\">");
385                html.append("<span class=\"tri\" id=\"tri." + id + "\">&#x25b6; </span>");
386                html.append("<span class=\"path\">" + relativePath + "</span>");
387                html.append("</a>");
388            } else {
389                html.append("<a href=\"" + getViewSourceUrl(result.getRelativePath()).toString() + "\"");
390                html.append(" target=\"_blank\">");
391                html.append("<span class=\"sqr sqr_" + (result.didPass() ? "pass" : "fail"));
392                html.append("\">&#x25a0; </span>");
393                html.append("<span class=\"path\">" + result.getRelativePath() + "</span>");
394                html.append("</a>");
395            }
396
397            if (!result.didPass()) {
398                appendTags(html, result);
399            }
400
401            html.append("</h3>");
402            appendExpectedResultsSources(result, html);
403
404            if (resultCode == AbstractResult.ResultCode.RESULTS_DIFFER) {
405                html.append("<div class=\"diff\" style=\"display: none;\" id=\"" + id + "\">");
406                html.append(result.getDiffAsHtml());
407                html.append("<a href=\"#\" onClick=\"toggleDisplay('" + id + "');");
408                html.append("return false;\">Hide</a>");
409                html.append(" | ");
410                html.append("<a href=\"" + getViewSourceUrl(relativePath).toString() + "\"");
411                html.append(" target=\"_blank\">Show source</a>");
412                html.append("</div>");
413            }
414
415            html.append("<div class=\"space\"></div>");
416        }
417    }
418
419    private void appendTags(StringBuilder html, AbstractResult result) {
420        /** Tag tests which crash, time out or where results don't match */
421        if (result.didCrash()) {
422            html.append(" <span class=\"listItem crashed\">Crashed</span>");
423        } else {
424            if (result.didTimeOut()) {
425                html.append(" <span class=\"listItem timed_out\">Timed out</span>");
426            }
427            AbstractResult.ResultCode resultCode = result.getResultCode();
428            if (resultCode != AbstractResult.ResultCode.RESULTS_MATCH) {
429                html.append(" <span class=\"listItem " + resultCode.name() + "\">");
430                html.append(resultCode.toString());
431                html.append("</span>");
432            }
433        }
434
435        /** Detect missing LTC function */
436        String additionalTextOutputString = result.getAdditionalTextOutputString();
437        if (additionalTextOutputString != null &&
438                additionalTextOutputString.contains("com.android.dumprendertree") &&
439                additionalTextOutputString.contains("has no method")) {
440            if (additionalTextOutputString.contains("LayoutTestController")) {
441                html.append(" <span class=\"listItem noLtc\">LTC function missing</span>");
442            }
443            if (additionalTextOutputString.contains("EventSender")) {
444                html.append(" <span class=\"listItem noEventSender\">");
445                html.append("ES function missing</span>");
446            }
447        }
448    }
449
450    private static final void appendExpectedResultsSources(AbstractResult result,
451            StringBuilder html) {
452        String textSource = result.getExpectedTextResultPath();
453        String imageSource = result.getExpectedImageResultPath();
454
455        if (textSource != null) {
456            html.append("<span class=\"source\">Expected textual result from: ");
457            html.append("<a href=\"" + ForwarderManager.getHostSchemePort(false) + "LayoutTests/" +
458                    textSource + "\"");
459            html.append(" target=\"_blank\">");
460            html.append(textSource + "</a></span>");
461        }
462        if (imageSource != null) {
463            html.append("<span class=\"source\">Expected image result from: ");
464            html.append("<a href=\"" + ForwarderManager.getHostSchemePort(false) + "LayoutTests/" +
465                    imageSource + "\"");
466            html.append(" target=\"_blank\">");
467            html.append(imageSource + "</a></span>");
468        }
469    }
470
471    private static final URL getViewSourceUrl(String relativePath) {
472        URL url = null;
473        try {
474            url = new URL("http", "localhost", ForwarderManager.HTTP_PORT,
475                    "/WebKitTools/DumpRenderTree/android/view_source.php?src=" +
476                    relativePath);
477        } catch (MalformedURLException e) {
478            assert false : "relativePath=" + relativePath;
479        }
480        return url;
481    }
482}
483