1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/test/pixel_test_utils.h"
10 #include "base/base64.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/gfx/codec/png_codec.h"
18 bool WritePNGFile(const SkBitmap& bitmap, const base::FilePath& file_path,
19 bool discard_transparency) {
20 std::vector<unsigned char> png_data;
21 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap,
24 base::CreateDirectory(file_path.DirName())) {
25 char* data = reinterpret_cast<char*>(&png_data[0]);
26 int size = static_cast<int>(png_data.size());
27 return base::WriteFile(file_path, data, size) == size;
32 std::string GetPNGDataUrl(const SkBitmap& bitmap) {
33 std::vector<unsigned char> png_data;
34 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data);
36 data_url.insert(data_url.end(), png_data.begin(), png_data.end());
37 base::Base64Encode(data_url, &data_url);
38 data_url.insert(0, "data:image/png;base64,");
43 bool ReadPNGFile(const base::FilePath& file_path, SkBitmap* bitmap) {
46 return base::ReadFileToString(file_path, &png_data) &&
47 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]),
52 bool MatchesPNGFile(const SkBitmap& gen_bmp, base::FilePath ref_img_path,
53 const PixelComparator& comparator) {
55 if (!ReadPNGFile(ref_img_path, &ref_bmp)) {
56 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value();
60 // Check if images size matches
61 if (gen_bmp.width() != ref_bmp.width() ||
62 gen_bmp.height() != ref_bmp.height()) {
64 << "Dimensions do not match! "
65 << "Actual: " << gen_bmp.width() << "x" << gen_bmp.height()
67 << "Expected: " << ref_bmp.width() << "x" << ref_bmp.height();
71 // Shortcut for empty images. They are always equal.
72 if (gen_bmp.width() == 0 || gen_bmp.height() == 0)
75 bool compare = comparator.Compare(gen_bmp, ref_bmp);
77 std::string gen_bmp_data_url = GetPNGDataUrl(gen_bmp);
78 std::string ref_bmp_data_url = GetPNGDataUrl(ref_bmp);
79 LOG(ERROR) << "Pixels do not match!";
80 LOG(ERROR) << "Actual: " << gen_bmp_data_url;
81 LOG(ERROR) << "Expected: " << ref_bmp_data_url;