OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/test/pixel_test_utils.h" | 5 #include "cc/test/pixel_test_utils.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "ui/gfx/codec/png_codec.h" | 14 #include "ui/gfx/codec/png_codec.h" |
14 | 15 |
15 namespace cc { | 16 namespace cc { |
16 | 17 |
17 bool WritePNGFile(const SkBitmap& bitmap, const base::FilePath& file_path, | 18 bool WritePNGFile(const SkBitmap& bitmap, const base::FilePath& file_path, |
18 bool discard_transparency) { | 19 bool discard_transparency) { |
19 std::vector<unsigned char> png_data; | 20 std::vector<unsigned char> png_data; |
20 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, | 21 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, |
21 discard_transparency, | 22 discard_transparency, |
22 &png_data) && | 23 &png_data) && |
23 base::CreateDirectory(file_path.DirName())) { | 24 base::CreateDirectory(file_path.DirName())) { |
24 char* data = reinterpret_cast<char*>(&png_data[0]); | 25 char* data = reinterpret_cast<char*>(&png_data[0]); |
25 int size = static_cast<int>(png_data.size()); | 26 int size = static_cast<int>(png_data.size()); |
26 return base::WriteFile(file_path, data, size) == size; | 27 return base::WriteFile(file_path, data, size) == size; |
27 } | 28 } |
28 return false; | 29 return false; |
29 } | 30 } |
30 | 31 |
32 std::string GetPNGDataUrl(const SkBitmap& bitmap) { | |
33 std::vector<unsigned char> png_data; | |
34 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data); | |
35 std::string data_url; | |
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,"); | |
39 | |
40 return data_url; | |
41 } | |
42 | |
31 bool ReadPNGFile(const base::FilePath& file_path, SkBitmap* bitmap) { | 43 bool ReadPNGFile(const base::FilePath& file_path, SkBitmap* bitmap) { |
32 DCHECK(bitmap); | 44 DCHECK(bitmap); |
33 std::string png_data; | 45 std::string png_data; |
34 return base::ReadFileToString(file_path, &png_data) && | 46 return base::ReadFileToString(file_path, &png_data) && |
35 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), | 47 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), |
36 png_data.length(), | 48 png_data.length(), |
37 bitmap); | 49 bitmap); |
38 } | 50 } |
39 | 51 |
40 bool MatchesPNGFile(const SkBitmap& gen_bmp, base::FilePath ref_img_path, | 52 bool MatchesPNGFile(const SkBitmap& gen_bmp, base::FilePath ref_img_path, |
(...skipping 12 matching lines...) Expand all Loading... | |
53 << "Actual: " << gen_bmp.width() << "x" << gen_bmp.height() | 65 << "Actual: " << gen_bmp.width() << "x" << gen_bmp.height() |
54 << "; " | 66 << "; " |
55 << "Expected: " << ref_bmp.width() << "x" << ref_bmp.height(); | 67 << "Expected: " << ref_bmp.width() << "x" << ref_bmp.height(); |
56 return false; | 68 return false; |
57 } | 69 } |
58 | 70 |
59 // Shortcut for empty images. They are always equal. | 71 // Shortcut for empty images. They are always equal. |
60 if (gen_bmp.width() == 0 || gen_bmp.height() == 0) | 72 if (gen_bmp.width() == 0 || gen_bmp.height() == 0) |
61 return true; | 73 return true; |
62 | 74 |
75 std::string gen_bmp_data_url = GetPNGDataUrl(gen_bmp); | |
76 std::string ref_bmp_data_url = GetPNGDataUrl(ref_bmp); | |
77 LOG(ERROR) << "Pixels do not match!"; | |
vmpstr
2014/07/11 02:57:04
Is this meant to be printed only if comparator.Com
sohanjg
2014/07/11 05:15:21
Yeah, right! its better to log it based on Compare
| |
78 LOG(ERROR) << "Actual: " << gen_bmp_data_url; | |
79 LOG(ERROR) << "Expected: " << ref_bmp_data_url; | |
80 | |
63 return comparator.Compare(gen_bmp, ref_bmp); | 81 return comparator.Compare(gen_bmp, ref_bmp); |
64 } | 82 } |
65 | 83 |
66 } // namespace cc | 84 } // namespace cc |
OLD | NEW |