/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.dumprendertree2; import android.content.Context; import android.content.res.AssetManager; import android.content.res.Configuration; import android.content.res.Resources; import android.database.Cursor; import android.os.Build; import android.os.Message; import android.util.DisplayMetrics; import android.util.Log; import com.android.dumprendertree2.forwarder.ForwarderManager; import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * A class that collects information about tests that ran and can create HTML * files with summaries and easy navigation. */ public class Summarizer { private static final String LOG_TAG = "Summarizer"; private static final String CSS = ""; private static final String SCRIPT = ""; /** TODO: Make it a setting */ private static final String HTML_DETAILS_RELATIVE_PATH = "details.html"; private static final String TXT_SUMMARY_RELATIVE_PATH = "summary.txt"; private static final int RESULTS_PER_DUMP = 500; private static final int RESULTS_PER_DB_ACCESS = 50; private int mCrashedTestsCount = 0; private List mUnexpectedFailures = new ArrayList(); private List mExpectedFailures = new ArrayList(); private List mExpectedPasses = new ArrayList(); private List mUnexpectedPasses = new ArrayList(); private Cursor mUnexpectedFailuresCursor; private Cursor mExpectedFailuresCursor; private Cursor mUnexpectedPassesCursor; private Cursor mExpectedPassesCursor; private FileFilter mFileFilter; private String mResultsRootDirPath; private String mTestsRelativePath; private Date mDate; private int mResultsSinceLastHtmlDump = 0; private int mResultsSinceLastDbAccess = 0; private SummarizerDBHelper mDbHelper; public Summarizer(String resultsRootDirPath, Context context) { mFileFilter = new FileFilter(); mResultsRootDirPath = resultsRootDirPath; /** * We don't run the database I/O in a separate thread to avoid consumer/producer problem * and to simplify code. */ mDbHelper = new SummarizerDBHelper(context); mDbHelper.open(); } public static URI getDetailsUri() { return new File(ManagerService.RESULTS_ROOT_DIR_PATH + File.separator + HTML_DETAILS_RELATIVE_PATH).toURI(); } public void appendTest(AbstractResult result) { String relativePath = result.getRelativePath(); if (result.didCrash()) { mCrashedTestsCount++; } if (result.didPass()) { result.clearResults(); if (mFileFilter.isFail(relativePath)) { mUnexpectedPasses.add(result); } else { mExpectedPasses.add(result); } } else { if (mFileFilter.isFail(relativePath)) { mExpectedFailures.add(result); } else { mUnexpectedFailures.add(result); } } if (++mResultsSinceLastDbAccess == RESULTS_PER_DB_ACCESS) { persistLists(); clearLists(); } } private void clearLists() { mUnexpectedFailures.clear(); mExpectedFailures.clear(); mUnexpectedPasses.clear(); mExpectedPasses.clear(); } private void persistLists() { persistListToTable(mUnexpectedFailures, SummarizerDBHelper.UNEXPECTED_FAILURES_TABLE); persistListToTable(mExpectedFailures, SummarizerDBHelper.EXPECTED_FAILURES_TABLE); persistListToTable(mUnexpectedPasses, SummarizerDBHelper.UNEXPECTED_PASSES_TABLE); persistListToTable(mExpectedPasses, SummarizerDBHelper.EXPECTED_PASSES_TABLE); mResultsSinceLastDbAccess = 0; } private void persistListToTable(List results, String table) { for (AbstractResult abstractResult : results) { mDbHelper.insertAbstractResult(abstractResult, table); } } public void setTestsRelativePath(String testsRelativePath) { mTestsRelativePath = testsRelativePath; } public void summarize(Message onFinishMessage) { persistLists(); clearLists(); mUnexpectedFailuresCursor = mDbHelper.getAbstractResults(SummarizerDBHelper.UNEXPECTED_FAILURES_TABLE); mUnexpectedPassesCursor = mDbHelper.getAbstractResults(SummarizerDBHelper.UNEXPECTED_PASSES_TABLE); mExpectedFailuresCursor = mDbHelper.getAbstractResults(SummarizerDBHelper.EXPECTED_FAILURES_TABLE); mExpectedPassesCursor = mDbHelper.getAbstractResults(SummarizerDBHelper.EXPECTED_PASSES_TABLE); String webKitRevision = getWebKitRevision(); createHtmlDetails(webKitRevision); createTxtSummary(webKitRevision); clearLists(); mUnexpectedFailuresCursor.close(); mUnexpectedPassesCursor.close(); mExpectedFailuresCursor.close(); mExpectedPassesCursor.close(); onFinishMessage.sendToTarget(); } public void reset() { mCrashedTestsCount = 0; clearLists(); mDbHelper.reset(); mDate = new Date(); } private void dumpHtmlToFile(StringBuilder html, boolean append) { FsUtils.writeDataToStorage(new File(mResultsRootDirPath, HTML_DETAILS_RELATIVE_PATH), html.toString().getBytes(), append); html.setLength(0); mResultsSinceLastHtmlDump = 0; } private void createTxtSummary(String webKitRevision) { StringBuilder txt = new StringBuilder(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); txt.append("Path: " + mTestsRelativePath + "\n"); txt.append("Date: " + dateFormat.format(mDate) + "\n"); txt.append("Build fingerprint: " + Build.FINGERPRINT + "\n"); txt.append("WebKit version: " + getWebKitVersionFromUserAgentString() + "\n"); txt.append("WebKit revision: " + webKitRevision + "\n"); txt.append("TOTAL: " + getTotalTestCount() + "\n"); txt.append("CRASHED (among all tests): " + mCrashedTestsCount + "\n"); txt.append("UNEXPECTED FAILURES: " + mUnexpectedFailuresCursor.getCount() + "\n"); txt.append("UNEXPECTED PASSES: " + mUnexpectedPassesCursor.getCount() + "\n"); txt.append("EXPECTED FAILURES: " + mExpectedFailuresCursor.getCount() + "\n"); txt.append("EXPECTED PASSES: " + mExpectedPassesCursor.getCount() + "\n"); FsUtils.writeDataToStorage(new File(mResultsRootDirPath, TXT_SUMMARY_RELATIVE_PATH), txt.toString().getBytes(), false); } private void createHtmlDetails(String webKitRevision) { StringBuilder html = new StringBuilder(); html.append(""); html.append(CSS); html.append(SCRIPT); html.append(""); createTopSummaryTable(webKitRevision, html); dumpHtmlToFile(html, false); createResultsList(html, "Unexpected failures", mUnexpectedFailuresCursor); createResultsList(html, "Unexpected passes", mUnexpectedPassesCursor); createResultsList(html, "Expected failures", mExpectedFailuresCursor); createResultsList(html, "Expected passes", mExpectedPassesCursor); html.append(""); dumpHtmlToFile(html, true); } private int getTotalTestCount() { return mUnexpectedFailuresCursor.getCount() + mUnexpectedPassesCursor.getCount() + mExpectedPassesCursor.getCount() + mExpectedFailuresCursor.getCount(); } private String getWebKitVersionFromUserAgentString() { Resources resources = new Resources(new AssetManager(), new DisplayMetrics(), new Configuration()); String userAgent = resources.getString(com.android.internal.R.string.web_user_agent); Matcher matcher = Pattern.compile("AppleWebKit/([0-9]+?\\.[0-9])").matcher(userAgent); if (matcher.find()) { return matcher.group(1); } return "unknown"; } private String getWebKitRevision() { URL url = null; try { url = new URL(ForwarderManager.getHostSchemePort(false) + "ThirdPartyProject.prop"); } catch (MalformedURLException e) { assert false; } String thirdPartyProjectContents = new String(FsUtils.readDataFromUrl(url)); Matcher matcher = Pattern.compile("^version=([0-9]+)", Pattern.MULTILINE).matcher( thirdPartyProjectContents); if (matcher.find()) { return matcher.group(1); } return "unknown"; } private void createTopSummaryTable(String webKitRevision, StringBuilder html) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); html.append("" + "Layout tests' results for: " + (mTestsRelativePath.equals("") ? "all tests" : mTestsRelativePath) + ""); html.append("" + "Date: " + dateFormat.format(new Date()) + ""); html.append("" + "Build fingerprint: " + Build.FINGERPRINT + ""); html.append("" + "WebKit version: " + getWebKitVersionFromUserAgentString() + ""); html.append("" + "WebKit revision: "); html.append("" + webKitRevision + ""); html.append(""); html.append(""); createSummaryTableRow(html, "TOTAL", getTotalTestCount()); createSummaryTableRow(html, "CRASHED (among all tests)", mCrashedTestsCount); createSummaryTableRow(html, "UNEXPECTED FAILURES", mUnexpectedFailuresCursor.getCount()); createSummaryTableRow(html, "UNEXPECTED PASSES", mUnexpectedPassesCursor.getCount()); createSummaryTableRow(html, "EXPECTED FAILURES", mExpectedFailuresCursor.getCount()); createSummaryTableRow(html, "EXPECTED PASSES", mExpectedPassesCursor.getCount()); html.append(""); } private void createSummaryTableRow(StringBuilder html, String caption, int size) { html.append(""); html.append(" " + caption + ""); html.append(" " + size + ""); html.append(""); } private void createResultsList( StringBuilder html, String title, Cursor cursor) { String relativePath; String id = ""; AbstractResult.ResultCode resultCode; html.append("" + title + " [" + cursor.getCount() + "]"); if (!cursor.moveToFirst()) { return; } AbstractResult result; do { result = SummarizerDBHelper.getAbstractResult(cursor); relativePath = result.getRelativePath(); resultCode = result.getResultCode(); html.append(""); /** * Technically, two different paths could end up being the same, because * ':' is a valid character in a path. However, it is probably not going * to cause any problems in this case */ id = relativePath.replace(File.separator, ":"); /** Write the test name */ if (resultCode == AbstractResult.ResultCode.RESULTS_DIFFER) { html.append(""); html.append("▶ "); html.append("" + relativePath + ""); html.append(""); } else { html.append(""); html.append("■ "); html.append("" + result.getRelativePath() + ""); html.append(""); } if (!result.didPass()) { appendTags(html, result); } html.append(""); appendExpectedResultsSources(result, html); if (resultCode == AbstractResult.ResultCode.RESULTS_DIFFER) { html.append(""); html.append(result.getDiffAsHtml()); html.append("Hide"); html.append(" | "); html.append("Show source"); html.append(""); } html.append(""); if (++mResultsSinceLastHtmlDump == RESULTS_PER_DUMP) { dumpHtmlToFile(html, true); } cursor.moveToNext(); } while (!cursor.isAfterLast()); } private void appendTags(StringBuilder html, AbstractResult result) { /** Tag tests which crash, time out or where results don't match */ if (result.didCrash()) { html.append(" Crashed"); } else { if (result.didTimeOut()) { html.append(" Timed out"); } AbstractResult.ResultCode resultCode = result.getResultCode(); if (resultCode != AbstractResult.ResultCode.RESULTS_MATCH) { html.append(" "); html.append(resultCode.toString()); html.append(""); } } /** Detect missing LTC function */ String additionalTextOutputString = result.getAdditionalTextOutputString(); if (additionalTextOutputString != null && additionalTextOutputString.contains("com.android.dumprendertree") && additionalTextOutputString.contains("has no method")) { if (additionalTextOutputString.contains("LayoutTestController")) { html.append(" LTC function missing"); } if (additionalTextOutputString.contains("EventSender")) { html.append(" "); html.append("ES function missing"); } } } private static final void appendExpectedResultsSources(AbstractResult result, StringBuilder html) { String textSource = result.getExpectedTextResultPath(); String imageSource = result.getExpectedImageResultPath(); if (result.didCrash()) { html.append("Did not look for expected results"); return; } if (textSource == null) { // Show if a text result is missing. We may want to revisit this decision when we add // support for image results. html.append("Expected textual result missing"); } else { html.append("Expected textual result from: "); html.append(""); html.append(textSource + ""); } if (imageSource != null) { html.append("Expected image result from: "); html.append(""); html.append(imageSource + ""); } } private static final URL getViewSourceUrl(String relativePath) { URL url = null; try { url = new URL("http", "localhost", ForwarderManager.HTTP_PORT, "/Tools/DumpRenderTree/android/view_source.php?src=" + relativePath); } catch (MalformedURLException e) { assert false : "relativePath=" + relativePath; } return url; } }