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