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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "cc/layer.h" | 13 #include "cc/layer.h" |
| 14 #include "cc/test/pixel_test_utils.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "ui/compositor/compositor_observer.h" | 16 #include "ui/compositor/compositor_observer.h" |
16 #include "ui/compositor/compositor_setup.h" | 17 #include "ui/compositor/compositor_setup.h" |
17 #include "ui/compositor/layer.h" | 18 #include "ui/compositor/layer.h" |
18 #include "ui/compositor/layer_animation_sequence.h" | 19 #include "ui/compositor/layer_animation_sequence.h" |
19 #include "ui/compositor/test/test_compositor_host.h" | 20 #include "ui/compositor/test/test_compositor_host.h" |
20 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
21 #include "ui/gfx/codec/png_codec.h" | 22 #include "ui/gfx/codec/png_codec.h" |
22 #include "ui/gfx/gfx_paths.h" | 23 #include "ui/gfx/gfx_paths.h" |
23 #include "ui/gfx/skia_util.h" | 24 #include "ui/gfx/skia_util.h" |
24 | 25 |
| 26 using cc::test::IsSameAsPNGFile; |
| 27 |
25 namespace ui { | 28 namespace ui { |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 // Encodes a bitmap into a PNG and write to disk. Returns true on success. The | |
30 // parent directory does not have to exist. | |
31 bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) { | |
32 std::vector<unsigned char> png_data; | |
33 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data) && | |
34 file_util::CreateDirectory(file_path.DirName())) { | |
35 char* data = reinterpret_cast<char*>(&png_data[0]); | |
36 int size = static_cast<int>(png_data.size()); | |
37 return file_util::WriteFile(file_path, data, size) == size; | |
38 } | |
39 return false; | |
40 } | |
41 | |
42 // Reads and decodes a PNG image to a bitmap. Returns true on success. The PNG | |
43 // should have been encoded using |gfx::PNGCodec::Encode|. | |
44 bool ReadPNGFile(const FilePath& file_path, SkBitmap* bitmap) { | |
45 DCHECK(bitmap); | |
46 std::string png_data; | |
47 return file_util::ReadFileToString(file_path, &png_data) && | |
48 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), | |
49 png_data.length(), | |
50 bitmap); | |
51 } | |
52 | |
53 // Compares with a PNG file on disk, and returns true if it is the same as | |
54 // the given image. |ref_img_path| is absolute. | |
55 bool IsSameAsPNGFile(const SkBitmap& gen_bmp, FilePath ref_img_path) { | |
56 SkBitmap ref_bmp; | |
57 if (!ReadPNGFile(ref_img_path, &ref_bmp)) { | |
58 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value(); | |
59 return false; | |
60 } | |
61 | |
62 if (ref_bmp.width() != gen_bmp.width() || | |
63 ref_bmp.height() != gen_bmp.height()) { | |
64 LOG(ERROR) | |
65 << "Dimensions do not match (Expected) vs (Actual):" | |
66 << "(" << ref_bmp.width() << "x" << ref_bmp.height() | |
67 << ") vs. " | |
68 << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")"; | |
69 return false; | |
70 } | |
71 | |
72 // Compare pixels and create a simple diff image. | |
73 int diff_pixels_count = 0; | |
74 SkAutoLockPixels lock_bmp(gen_bmp); | |
75 SkAutoLockPixels lock_ref_bmp(ref_bmp); | |
76 // The reference images were saved with no alpha channel. Use the mask to | |
77 // set alpha to 0. | |
78 uint32_t kAlphaMask = 0x00FFFFFF; | |
79 for (int x = 0; x < gen_bmp.width(); ++x) { | |
80 for (int y = 0; y < gen_bmp.height(); ++y) { | |
81 if ((*gen_bmp.getAddr32(x, y) & kAlphaMask) != | |
82 (*ref_bmp.getAddr32(x, y) & kAlphaMask)) { | |
83 ++diff_pixels_count; | |
84 } | |
85 } | |
86 } | |
87 | |
88 if (diff_pixels_count != 0) { | |
89 LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count; | |
90 return false; | |
91 } | |
92 | |
93 return true; | |
94 } | |
95 | |
96 // Returns a comma-separated list of the names of |layer|'s children in | 32 // Returns a comma-separated list of the names of |layer|'s children in |
97 // bottom-to-top stacking order. | 33 // bottom-to-top stacking order. |
98 std::string GetLayerChildrenNames(const Layer& layer) { | 34 std::string GetLayerChildrenNames(const Layer& layer) { |
99 std::string names; | 35 std::string names; |
100 for (std::vector<Layer*>::const_iterator it = layer.children().begin(); | 36 for (std::vector<Layer*>::const_iterator it = layer.children().begin(); |
101 it != layer.children().end(); ++it) { | 37 it != layer.children().end(); ++it) { |
102 if (!names.empty()) | 38 if (!names.empty()) |
103 names += ","; | 39 names += ","; |
104 names += (*it)->name(); | 40 names += (*it)->name(); |
105 } | 41 } |
(...skipping 1191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1297 | 1233 |
1298 // Resize layer. | 1234 // Resize layer. |
1299 child->SetBounds(gfx::Rect(200, 200, 400, 400)); | 1235 child->SetBounds(gfx::Rect(200, 200, 400, 400)); |
1300 child->SetVisible(true); | 1236 child->SetVisible(true); |
1301 EXPECT_TRUE(schedule_draw_invoked_); | 1237 EXPECT_TRUE(schedule_draw_invoked_); |
1302 DrawTree(root.get()); | 1238 DrawTree(root.get()); |
1303 EXPECT_TRUE(delegate.painted()); | 1239 EXPECT_TRUE(delegate.painted()); |
1304 } | 1240 } |
1305 | 1241 |
1306 } // namespace ui | 1242 } // namespace ui |
OLD | NEW |