view src/org/gfxtest/ImageDiffer/ResultWriters/HtmlWriter.java @ 785:e9bbfa68cc70 draft

Updated comments in the class HtmlWriter.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Wed, 06 Jan 2016 12:12:52 +0100
parents 70dddc9b0917
children
line wrap: on
line source

/*
  Java gfx-test framework

   Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016  Red Hat

This file is part of IcedTea.

IcedTea is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

IcedTea is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

package org.gfxtest.ImageDiffer.ResultWriters;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import org.gfxtest.ImageDiffer.ComparisonResult;
import org.gfxtest.ImageDiffer.Configuration;



/**
 * Class that can generates HTML page containing test results.
 */
public class HtmlWriter extends ResultWriter
{
    /**
     * Header of HTML page + first part of its body.
     */
    private static final String HTML_HEADER =
        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n " +
        "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n " +
        "<html>\n" + 
        "    <head>\n" +
        "        <title>Visual tester results</title>\n" +
        "        <meta name='Generator' content='ImageDiffer' />\n" +
        "    </head>\n" +
        "    <body style='font-family: sans-serif, arial, helvetica, sans-serif; color:#000000; background-color:#ffffff; margin-left: 20px; margin-top: 20px; border:0px; border-color:#808080;border-style:solid'>\n" +
        "        <h1>Visual tester results</h1>\n" +
        "        <table border='1' frame='border' cellspacing='3' cellpadding='1' style='background-color: #f0f0dd; vertical-align: top; border-collapse: collapse; border-color:#808080' summary=''>\n" +
        "            <tr style='background-color:#a0c0c0;'><th>Test</th><th>Comparison result</th><th>Images</th><th colspan='2'>Perception</th><td>Diff image thumbnail</td></tr>\n";

    /**
     * Footer of HTML page.
     */
    private static final String HTML_FOOTER =
        "        </table>\n" +
        "    </body>\n" +
        "</html>\n";

    /**
     * Name of output file.
     */
    private static final String HTML_FILE_NAME = "results.html";

    /**
     * Constructor that accepts directory, where the resulting HTML page should
     * be stored.
     */
    public HtmlWriter(File outputDirectory) throws IOException
    {
        super(outputDirectory, HTML_FILE_NAME);
    }

    /**
     * Print HTML header.
     */
    @Override
    public void printHeader() throws IOException
    {
        this.writer.write(HTML_HEADER);
    }

    /**
     * Print HTML footer.
     */
    @Override
    public void printFooter() throws IOException
    {
        this.writer.write(HTML_FOOTER);
    }

    /**
     * Print result for one result from image comparison into HTML page.
     */
    @Override
    public void printResultForOneImage(Configuration configuration, String imageFileName, BufferedImage[] sourceImages,
            ComparisonResult comparisonResult) throws IOException
    {
        String dirName = imageFileName.substring(0, imageFileName.lastIndexOf('.'));
        this.println("            <tr valign='top'>");
        printTestName(configuration, dirName);
        printComparisonResult(comparisonResult);
        printImageHrefs(configuration, dirName);
        printPerceptionDatas(comparisonResult);
        printImageThumbnail(imageFileName);

        this.println("            </tr>");
    }

    private void printImageThumbnail(String imageFileName) throws IOException
    {
        this.println("<td><a href='%s'><img src='%s' width='320px' /></a></td>", imageFileName, imageFileName);
    }

    private void printTestName(Configuration configuration, String dirName) throws IOException
    {
        if (configuration.isHtmlFilesCreationEnabled())
        {
            this.println("                <td><a href='%s/result.html'>%s</a></td>", dirName, dirName);
        }
        else
        {
            this.println("                <td>" + dirName + "</td>");
        }
    }

    private void printComparisonResult(ComparisonResult comparisonResult) throws IOException
    {
        boolean equalImages = comparisonResult.isEqualsImages();
        String result = equalImages ? "<div style='color:#008000'>same</div>" : "<div style='color:#c00000'>different</div>";
        this.println("                <td>" + result + "</td>");
    }

    private void printImageHrefs(Configuration configuration, String dirName) throws IOException
    {
        this.println("                <td>");
        if (configuration.isDiffImagesCreationEnabled())
        {
            this.println("<a href='%s'>[diff image]</a><br />", dirName + ".png");
        }
        if (configuration.isStructDiffImagesCreationEnabled())
        {
            this.println("<a href='%s'>[source1 image]</a><br />", dirName + "/source1.png");
            this.println("<a href='%s'>[source2 image]</a><br />", dirName + "/source2.png");
        }
        this.println("                </td>");
    }

    @SuppressWarnings("boxing")
    private void printPerceptionDatas(ComparisonResult comparisonResult) throws IOException
    {
        this.println("                <td><table border='0' style='font-size:66%%'>");
        this.println("<tr><td>Area:           </td><td>%d&times;%d px</td></tr>", comparisonResult.getAreaWidth(), comparisonResult.getAreaHeight(), comparisonResult.getTotalPixels());
        this.println("<tr><td>Rectangle:      </td><td>%d&times;%d - %d&times;%d</td></tr>", comparisonResult.getRectangleMin().x, comparisonResult.getRectangleMin().y, comparisonResult.getRectangleMax().x, comparisonResult.getRectangleMax().y);
        this.println("                </table></td>");
        this.println("                <td><table border='0' style='font-size:66%%'>");

        this.println("<tr><td>Total pixels:    </td><td align='right'>%d</td></tr>", comparisonResult.getTotalPixels());
        this.println("<tr><td>DifferentPixels:</td><td align='right'>%d</td></tr>", comparisonResult.getDifferentPixels());
        this.println("<tr><td>UnperceptibleDifferentPixels:</td><td align='right'>%d</td></tr>", comparisonResult.getSmallDifferences());
        this.println("<tr><td>SamePixels:</td><td align='right'>%d</td></tr>", comparisonResult.getEqualPixels());
        this.println("                </table></td>");
    }

}