/*
* 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 java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* 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_SUMMARY_RELATIVE_PATH = "summary.html";
private static final String TXT_SUMMARY_RELATIVE_PATH = "summary.txt";
private int mCrashedTestsCount = 0;
private List mFailedNotIgnoredTests = new ArrayList();
private List mIgnoredTests = new ArrayList();
private List mPassedNotIgnoredTests = new ArrayList();
private FileFilter mFileFilter;
private String mResultsRootDirPath;
private String mTitleString;
public Summarizer(FileFilter fileFilter, String resultsRootDirPath) {
mFileFilter = fileFilter;
mResultsRootDirPath = resultsRootDirPath;
}
public void appendTest(AbstractResult result) {
String relativePath = result.getRelativePath();
if (result.getResultCode() == AbstractResult.ResultCode.FAIL_CRASHED) {
mCrashedTestsCount++;
}
if (mFileFilter.isIgnoreRes(relativePath)) {
mIgnoredTests.add(result);
} else if (result.getResultCode() == AbstractResult.ResultCode.PASS) {
mPassedNotIgnoredTests.add(relativePath);
} else {
mFailedNotIgnoredTests.add(result);
}
}
public void summarize() {
createHtmlSummary();
createTxtSummary();
}
private void createTxtSummary() {
StringBuilder txt = new StringBuilder();
txt.append(getTitleString() + "\n");
if (mCrashedTestsCount > 0) {
txt.append("CRASHED (total among all tests): " + mCrashedTestsCount + "\n");
txt.append("-------------");
}
txt.append("FAILED: " + mFailedNotIgnoredTests.size() + "\n");
txt.append("IGNORED: " + mIgnoredTests.size() + "\n");
txt.append("PASSED: " + mPassedNotIgnoredTests.size() + "\n");
FsUtils.writeDataToStorage(new File(mResultsRootDirPath, TXT_SUMMARY_RELATIVE_PATH),
txt.toString().getBytes(), false);
}
private void createHtmlSummary() {
StringBuilder html = new StringBuilder();
html.append("");
html.append(CSS);
html.append(SCRIPT);
html.append("");
createTopSummaryTable(html);
createResultsListWithDiff(html, "Failed", mFailedNotIgnoredTests);
createResultsListWithDiff(html, "Ignored", mIgnoredTests);
createResultsListNoDiff(html, "Passed", mPassedNotIgnoredTests);
html.append("");
FsUtils.writeDataToStorage(new File(mResultsRootDirPath, HTML_SUMMARY_RELATIVE_PATH),
html.toString().getBytes(), false);
}
private String getTitleString() {
if (mTitleString == null) {
int total = mFailedNotIgnoredTests.size() +
mPassedNotIgnoredTests.size() +
mIgnoredTests.size();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
mTitleString = " - total of " + total + " tests - " + dateFormat.format(new Date());
}
return mTitleString;
}
private void createTopSummaryTable(StringBuilder html) {
html.append("
");
for (AbstractResult result : resultsList) {
relativePath = result.getRelativePath();
resultCode = result.getResultCode();
html.append("
");
if (resultCode == AbstractResult.ResultCode.PASS) {
html.append("■ ");
html.append("" + relativePath + "");
} else {
/**
* 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, ":");
html.append("");
html.append("▶ ");
html.append("" + relativePath + "");
html.append("");
}
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");
}
}
html.append("
");
if (resultCode != AbstractResult.ResultCode.PASS) {
html.append("