diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-15 21:13:03 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-15 21:13:03 +0000 |
commit | 022f42453b737577e9175eb279d507981ef40b30 (patch) | |
tree | b1daf3f481988fe6612a5883b502987f8819a85d | |
parent | 48fdb18832b43df6a3b6e2e66a44fa6ab583e98e (diff) | |
download | chromium_src-022f42453b737577e9175eb279d507981ef40b30.zip chromium_src-022f42453b737577e9175eb279d507981ef40b30.tar.gz chromium_src-022f42453b737577e9175eb279d507981ef40b30.tar.bz2 |
Add an image diff link to failed layout tests.
Review URL: http://codereview.chromium.org/18023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8119 0039d316-1c4b-4281-b951-d872f2087c98
11 files changed, 177 insertions, 17 deletions
diff --git a/chrome/tools/test/image_diff/image_diff.cc b/chrome/tools/test/image_diff/image_diff.cc index 7fa6f48..c21003d 100644 --- a/chrome/tools/test/image_diff/image_diff.cc +++ b/chrome/tools/test/image_diff/image_diff.cc @@ -17,25 +17,41 @@ #include "base/command_line.h" #include "base/file_util.h" #include "base/gfx/png_decoder.h" +#include "base/gfx/png_encoder.h" #include "base/logging.h" #include "base/process_util.h" #include "base/scoped_ptr.h" #include "base/string_util.h" +#if defined(OS_WIN) +#include "windows.h" +#endif + // Causes the app to remain open, waiting for pairs of filenames on stdin. // The caller is then responsible for terminating this app. static const wchar_t kOptionPollStdin[] = L"use-stdin"; +static const wchar_t kOptionGenerateDiff[] = L"diff"; // Return codes used by this utility. static const int kStatusSame = 0; static const int kStatusDifferent = 1; static const int kStatusError = 2; +// Color codes. +static const uint32 RGBA_RED = 0x000000ff; +static const uint32 RGBA_ALPHA = 0xff000000; + class Image { public: Image() : w_(0), h_(0) { } + Image(const Image& image) + : w_(image.w_), + h_(image.h_), + data_(image.data_) { + } + bool has_image() const { return w_ > 0 && h_ > 0; } @@ -48,6 +64,10 @@ class Image { return h_; } + const unsigned char* data() const { + return &data_.front(); + } + // Creates the image from stdin with the given data length. On success, it // will return true. On failure, no other methods should be accessed. bool CreateFromStdin(size_t byte_length) { @@ -97,10 +117,17 @@ class Image { } // Returns the RGBA value of the pixel at the given location - uint32 pixel_at(int x, int y) const { + const uint32 pixel_at(int x, int y) const { + DCHECK(x >= 0 && x < w_); + DCHECK(y >= 0 && y < h_); + return *reinterpret_cast<const uint32*>(&(data_[(y * w_ + x) * 4])); + } + + void set_pixel_at(int x, int y, uint32 color) const { DCHECK(x >= 0 && x < w_); DCHECK(y >= 0 && y < h_); - return data_[(y * w_ + x) * 4]; + void* addr = &const_cast<unsigned char*>(&data_.front())[(y * w_ + x) * 4]; + *reinterpret_cast<uint32*>(addr) = color; } private: @@ -149,7 +176,10 @@ void PrintHelp() { " Compares two files on disk, returning 0 when they are the same\n" " image_diff --use-stdin\n" " Stays open reading pairs of filenames from stdin, comparing them,\n" - " and sending 0 to stdout when they are the same\n"); + " and sending 0 to stdout when they are the same\n" + " image_diff --diff <compare file> <reference file> <output file>\n" + " Compares two files on disk, outputs an image that visualizes the" + " difference to <output file>\n"); /* For unfinished webkit-like-mode (see below) "\n" " image_diff -s\n" @@ -232,10 +262,68 @@ int CompareImages(const char* file1, const char* file2) { */ } +bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) { + int w = std::min(image1.w(), image2.w()); + int h = std::min(image1.h(), image2.h()); + *out = Image(image1); + bool same = (image1.w() == image2.w()) && (image1.h() == image2.h()); + + // TODO(estade): do something with the extra pixels if the image sizes + // are different. + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + uint32 base_pixel = image1.pixel_at(x, y); + if (base_pixel != image2.pixel_at(x, y)) { + // Set differing pixels red. + out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA); + same = false; + } else { + // Set same pixels as faded. + uint32 alpha = base_pixel & RGBA_ALPHA; + uint32 new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA); + out->set_pixel_at(x, y, new_pixel); + } + } + } + + return same; +} + +int DiffImages(const char* file1, const char* file2, const char* out_file) { + Image actual_image; + Image baseline_image; + + if (!actual_image.CreateFromFilename(file1)) { + fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1); + return kStatusError; + } + if (!baseline_image.CreateFromFilename(file2)) { + fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2); + return kStatusError; + } + + Image diff_image; + bool same = CreateImageDiff(baseline_image, actual_image, &diff_image); + if (same) + return kStatusSame; + + std::vector<unsigned char> png_encoding; + PNGEncoder::Encode(diff_image.data(), PNGEncoder::FORMAT_RGBA, + diff_image.w(), diff_image.h(), diff_image.w() * 4, + false, &png_encoding); + if (file_util::WriteFile(UTF8ToWide(out_file), + reinterpret_cast<char*>(&png_encoding.front()), png_encoding.size()) < 0) + return kStatusError; + + return kStatusDifferent; +} + int main(int argc, const char* argv[]) { base::EnableTerminationOnHeapCorruption(); + // TODO(estade): why does using the default constructor (command line + // singleton) cause an exception when run in debug mode? #if defined(OS_WIN) - CommandLine parsed_command_line; + CommandLine parsed_command_line(::GetCommandLine()); #elif defined(OS_POSIX) CommandLine parsed_command_line(argc, argv); #endif @@ -265,7 +353,15 @@ int main(int argc, const char* argv[]) { return 0; } - if (argc == 3) { + if (parsed_command_line.HasSwitch(kOptionGenerateDiff)) { + if (3 == parsed_command_line.GetLooseValueCount()) { + CommandLine::LooseValueIterator iter = + parsed_command_line.GetLooseValuesBegin(); + return DiffImages(WideToUTF8(*iter).c_str(), + WideToUTF8(*(iter + 1)).c_str(), + WideToUTF8(*(iter + 2)).c_str()); + } + } else if (2 == parsed_command_line.GetLooseValueCount()) { return CompareImages(argv[1], argv[2]); } diff --git a/webkit/tools/layout_tests/layout_package/platform_utils_linux.py b/webkit/tools/layout_tests/layout_package/platform_utils_linux.py index f8cb79d..7965fb3 100644 --- a/webkit/tools/layout_tests/layout_package/platform_utils_linux.py +++ b/webkit/tools/layout_tests/layout_package/platform_utils_linux.py @@ -164,6 +164,11 @@ class PlatformUtility(object): """ return 'wdiff' + def ImageCompareExecutablePath(self, target): + """Path to the image_diff binary.""" + # See mmoss TODO below. + return PathFromBase('chrome', 'Hammer', 'image_diff') + def TestShellBinary(self): """The name of the binary for TestShell.""" return 'test_shell' diff --git a/webkit/tools/layout_tests/layout_package/platform_utils_mac.py b/webkit/tools/layout_tests/layout_package/platform_utils_mac.py index 0a5e090..dbc6963 100644 --- a/webkit/tools/layout_tests/layout_package/platform_utils_mac.py +++ b/webkit/tools/layout_tests/layout_package/platform_utils_mac.py @@ -133,6 +133,9 @@ class PlatformUtility(object): """ return 'wdiff' + def ImageCompareExecutablePath(self, target): + return PathFromBase('xcodebuild', target, 'image_diff') + def TestShellBinary(self): """The name of the binary for TestShell.""" return 'TestShell' diff --git a/webkit/tools/layout_tests/layout_package/platform_utils_win.py b/webkit/tools/layout_tests/layout_package/platform_utils_win.py index 06bc445..c6bd525 100644 --- a/webkit/tools/layout_tests/layout_package/platform_utils_win.py +++ b/webkit/tools/layout_tests/layout_package/platform_utils_win.py @@ -185,6 +185,9 @@ class PlatformUtility(google.platform_utils_win.PlatformUtility): """Path to the WDiff executable, whose binary is checked in on Win""" return PathFromBase('third_party', 'cygwin', 'bin', 'wdiff.exe') + def ImageCompareExecutablePath(self, target): + return PathFromBase('chrome', target, 'image_diff.exe') + def TestShellBinary(self): """The name of the binary for TestShell.""" return 'test_shell.exe' diff --git a/webkit/tools/layout_tests/layout_package/test_failures.py b/webkit/tools/layout_tests/layout_package/test_failures.py index 8779a68..134f2e3 100644 --- a/webkit/tools/layout_tests/layout_package/test_failures.py +++ b/webkit/tools/layout_tests/layout_package/test_failures.py @@ -194,7 +194,7 @@ class FailureMissingImage(FailureWithType): class FailureImageHashMismatch(FailureWithType): """Image hashes didn't match.""" - OUT_FILENAMES = ["-actual-win.png", "-expected.png"] + OUT_FILENAMES = ["-actual-win.png", "-expected.png", "-diff-win.png"] @staticmethod def Message(): diff --git a/webkit/tools/layout_tests/layout_package/test_shell_thread.py b/webkit/tools/layout_tests/layout_package/test_shell_thread.py index fde21de..eb2faf7 100644 --- a/webkit/tools/layout_tests/layout_package/test_shell_thread.py +++ b/webkit/tools/layout_tests/layout_package/test_shell_thread.py @@ -27,7 +27,7 @@ import test_failures # test_shell.exe. DEFAULT_TEST_TIMEOUT_MS = 10 * 1000 -def ProcessOutput(proc, filename, test_uri, test_types, test_args): +def ProcessOutput(proc, filename, test_uri, test_types, test_args, target): """Receives the output from a test_shell process, subjects it to a number of tests, and returns a list of failure types the test produced. @@ -36,6 +36,7 @@ def ProcessOutput(proc, filename, test_uri, test_types, test_args): filename: path of the test file being run test_types: list of test types to subject the output to test_args: arguments to be passed to each test + target: Debug or Release Returns: a list of failure objects for the test being processed """ @@ -82,7 +83,8 @@ def ProcessOutput(proc, filename, test_uri, test_types, test_args): for test_type in test_types: new_failures = test_type.CompareOutput(filename, proc, ''.join(outlines), - local_test_args) + local_test_args, + target) # Don't add any more failures if we already have a crash or timeout, so # we don't double-report those tests. if not crash_or_timeout: @@ -254,7 +256,8 @@ class TestShellThread(threading.Thread): # ...and read the response return ProcessOutput(self._test_shell_proc, filename, test_uri, - self._test_types, self._test_args) + self._test_types, self._test_args, + self._options.target) def _EnsureTestShellIsRunning(self): diff --git a/webkit/tools/layout_tests/test_types/fuzzy_image_diff.py b/webkit/tools/layout_tests/test_types/fuzzy_image_diff.py index 579a30a..6a87e22 100644 --- a/webkit/tools/layout_tests/test_types/fuzzy_image_diff.py +++ b/webkit/tools/layout_tests/test_types/fuzzy_image_diff.py @@ -17,7 +17,7 @@ from layout_package import test_failures from test_types import test_type_base class FuzzyImageDiff(test_type_base.TestTypeBase): - def CompareOutput(self, filename, proc, output, test_args): + def CompareOutput(self, filename, proc, output, test_args, target): """Implementation of CompareOutput that checks the output image and checksum against the expected files from the LayoutTest directory. """ diff --git a/webkit/tools/layout_tests/test_types/image_diff.py b/webkit/tools/layout_tests/test_types/image_diff.py index 07c7561..077e0c4 100644 --- a/webkit/tools/layout_tests/test_types/image_diff.py +++ b/webkit/tools/layout_tests/test_types/image_diff.py @@ -13,11 +13,17 @@ import errno import logging import os import shutil +import subprocess from layout_package import path_utils +from layout_package import platform_utils from layout_package import test_failures from test_types import test_type_base +# Cache whether we have the image_diff executable available. +_compare_available = True +_compare_msg_printed = False + class ImageDiff(test_type_base.TestTypeBase): def _CopyOutputPNGs(self, filename, actual_png, expected_png): """Copies result files into the output directory with appropriate names. @@ -53,7 +59,48 @@ class ImageDiff(test_type_base.TestTypeBase): self._SaveBaselineData(filename, png_data, ".png") self._SaveBaselineData(filename, checksum, ".checksum") - def CompareOutput(self, filename, proc, output, test_args): + def _CreateImageDiff(self, filename, target): + """Creates the visual diff of the expected/actual PNGs. + + Args: + filename: the name of the test + target: Debug or Release + """ + diff_filename = self.OutputFilename(filename, + self.FILENAME_SUFFIX_COMPARE) + actual_filename = self.OutputFilename(filename, + self.FILENAME_SUFFIX_ACTUAL + '.png') + expected_win_filename = self.OutputFilename(filename, + self.FILENAME_SUFFIX_EXPECTED + '.png') + platform_util = platform_utils.PlatformUtility('') + + global _compare_available + cmd = '' + + try: + executable = platform_util.ImageCompareExecutablePath(target) + cmd = [executable, '--diff', actual_filename, expected_win_filename, + diff_filename] + except Exception, e: + _compare_available = False + + if _compare_available: + try: + subprocess.call(cmd) + except OSError, e: + if e.errno == errno.ENOENT or e.errno == errno.EACCES: + _compare_available = False + else: + raise e + + global _compare_msg_printed + + if not _compare_available and not _compare_msg_printed: + _compare_msg_printed = True + print('image_diff not found. Make sure you have a ' + target + + ' build of the image_diff executable.') + + def CompareOutput(self, filename, proc, output, test_args, target): """Implementation of CompareOutput that checks the output image and checksum against the expected files from the LayoutTest directory. """ @@ -102,10 +149,11 @@ class ImageDiff(test_type_base.TestTypeBase): # If anything was wrong, write the output files. if len(failures): - self.WriteOutputFiles(filename, '', '.checksum', test_args.hash, - expected_hash, diff=False) self._CopyOutputPNGs(filename, test_args.png_path, - expected_png_file) + expected_png_file) + self._CreateImageDiff(filename, target) + self.WriteOutputFiles(filename, '', '.checksum', test_args.hash, + expected_hash, diff=False, wdiff=False) return failures diff --git a/webkit/tools/layout_tests/test_types/simplified_text_diff.py b/webkit/tools/layout_tests/test_types/simplified_text_diff.py index bd1de3e..00af6c1 100644 --- a/webkit/tools/layout_tests/test_types/simplified_text_diff.py +++ b/webkit/tools/layout_tests/test_types/simplified_text_diff.py @@ -83,7 +83,7 @@ class SimplifiedTextDiff(text_diff.TestTextDiff): return text - def CompareOutput(self, filename, proc, output, test_args): + def CompareOutput(self, filename, proc, output, test_args, target): """Implementation of CompareOutput that removes most numbers before computing the diff. diff --git a/webkit/tools/layout_tests/test_types/test_type_base.py b/webkit/tools/layout_tests/test_types/test_type_base.py index fd7558f..7750b6e 100644 --- a/webkit/tools/layout_tests/test_types/test_type_base.py +++ b/webkit/tools/layout_tests/test_types/test_type_base.py @@ -46,6 +46,7 @@ class TestTypeBase(object): FILENAME_SUFFIX_EXPECTED = "-expected" FILENAME_SUFFIX_DIFF = "-diff-win" FILENAME_SUFFIX_WDIFF = "-wdiff-win.html" + FILENAME_SUFFIX_COMPARE = "-diff-win.png" def __init__(self, platform, root_output_dir): """Initialize a TestTypeBase object. @@ -119,7 +120,7 @@ class TestTypeBase(object): """ return os.path.splitext(filename)[0] + modifier - def CompareOutput(self, filename, proc, output, test_args): + def CompareOutput(self, filename, proc, output, test_args, target): """Method that compares the output from the test with the expected value. This is an abstract method to be implemented by all sub classes. @@ -129,6 +130,7 @@ class TestTypeBase(object): proc: a reference to the test_shell process output: a string containing the output of the test test_args: a TestArguments object holding optional additional arguments + target: Debug or Release Return: a list of TestFailure objects, empty if the test passes diff --git a/webkit/tools/layout_tests/test_types/text_diff.py b/webkit/tools/layout_tests/test_types/text_diff.py index 7cf6627..d23a912 100644 --- a/webkit/tools/layout_tests/test_types/text_diff.py +++ b/webkit/tools/layout_tests/test_types/text_diff.py @@ -45,7 +45,7 @@ class TestTextDiff(test_type_base.TestTypeBase): # Normalize line endings return expected.strip("\r\n").replace("\r\n", "\n") + "\n" - def CompareOutput(self, filename, proc, output, test_args): + def CompareOutput(self, filename, proc, output, test_args, target): """Implementation of CompareOutput that checks the output text against the expected text from the LayoutTest directory.""" failures = [] |