Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: ui/gfx/compositor/layer_unittest.cc

Issue 8533019: Add two pixel-based compositor unittests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removing images from this CL Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/compositor/layer_animator_unittest.cc ('k') | ui/gfx/compositor/run_all_unittests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/canvas_skia.h" 13 #include "ui/gfx/canvas_skia.h"
14 #include "ui/gfx/codec/png_codec.h" 14 #include "ui/gfx/codec/png_codec.h"
15 #include "ui/gfx/compositor/compositor_observer.h" 15 #include "ui/gfx/compositor/compositor_observer.h"
16 #include "ui/gfx/compositor/layer.h" 16 #include "ui/gfx/compositor/layer.h"
17 #include "ui/gfx/compositor/layer_animation_sequence.h" 17 #include "ui/gfx/compositor/layer_animation_sequence.h"
18 #include "ui/gfx/compositor/test_compositor.h" 18 #include "ui/gfx/compositor/test/test_compositor.h"
19 #include "ui/gfx/compositor/test_compositor_host.h" 19 #include "ui/gfx/compositor/test/test_compositor_host.h"
20 #include "ui/gfx/gfx_paths.h"
20 21
21 namespace ui { 22 namespace ui {
22 23
23 namespace { 24 namespace {
24 25
26 // Encodes a bitmap into a PNG and write to disk. Returns true on success. The
27 // parent directory does not have to exist.
28 bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) {
29 std::vector<unsigned char> png_data;
30 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data) &&
31 file_util::CreateDirectory(file_path.DirName())) {
32 char* data = reinterpret_cast<char*>(&png_data[0]);
33 int size = static_cast<int>(png_data.size());
34 return file_util::WriteFile(file_path, data, size) == size;
35 }
36 return false;
37 }
38
39 // Reads and decodes a PNG image to a bitmap. Returns true on success. The PNG
40 // should have been encoded using |gfx::PNGCodec::Encode|.
41 bool ReadPNGFile(const FilePath& file_path, SkBitmap* bitmap) {
42 DCHECK(bitmap);
43 std::string png_data;
44 return file_util::ReadFileToString(file_path, &png_data) &&
45 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]),
46 png_data.length(),
47 bitmap);
48 }
49
50 // Compares with a PNG file on disk, and returns true if it is the same as
51 // the given image. |ref_img_path| is absolute.
52 bool IsSameAsPNGFile(const SkBitmap& gen_bmp, FilePath ref_img_path) {
53 SkBitmap ref_bmp;
54 if (!ReadPNGFile(ref_img_path, &ref_bmp)) {
55 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value();
56 return false;
57 }
58
59 if (ref_bmp.width() != gen_bmp.width() ||
60 ref_bmp.height() != gen_bmp.height()) {
61 LOG(ERROR)
62 << "Dimensions do not match (Expected) vs (Actual):"
63 << "(" << ref_bmp.width() << "x" << ref_bmp.height()
64 << ") vs. "
65 << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")";
66 return false;
67 }
68
69 // Compare pixels and create a simple diff image.
70 int diff_pixels_count = 0;
71 SkAutoLockPixels lock_bmp(gen_bmp);
72 SkAutoLockPixels lock_ref_bmp(ref_bmp);
73 // The reference images were saved with no alpha channel. Use the mask to
74 // set alpha to 0.
75 uint32_t kAlphaMask = 0x00FFFFFF;
76 for (int x = 0; x < gen_bmp.width(); ++x) {
77 for (int y = 0; y < gen_bmp.height(); ++y) {
78 if ((*gen_bmp.getAddr32(x, y) & kAlphaMask) !=
79 (*ref_bmp.getAddr32(x, y) & kAlphaMask)) {
80 ++diff_pixels_count;
81 }
82 }
83 }
84
85 if (diff_pixels_count != 0) {
86 LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count;
87 return false;
88 }
89
90 return true;
91 }
92
93
25 // There are three test classes in here that configure the Compositor and 94 // There are three test classes in here that configure the Compositor and
26 // Layer's slightly differently: 95 // Layer's slightly differently:
27 // - LayerWithNullDelegateTest uses TestCompositor and NullLayerDelegate as the 96 // - LayerWithNullDelegateTest uses TestCompositor and NullLayerDelegate as the
28 // LayerDelegate. This is typically the base class you want to use. 97 // LayerDelegate. This is typically the base class you want to use.
29 // - LayerWithDelegateTest uses TestCompositor and does not set a LayerDelegate 98 // - LayerWithDelegateTest uses TestCompositor and does not set a LayerDelegate
30 // on the delegates. 99 // on the delegates.
31 // - LayerWithRealCompositorTest when a real compositor is required for testing. 100 // - LayerWithRealCompositorTest when a real compositor is required for testing.
32 // - Slow because they bring up a window and run the real compositor. This 101 // - Slow because they bring up a window and run the real compositor. This
33 // is typically not what you want. 102 // is typically not what you want.
34 103
(...skipping 11 matching lines...) Expand all
46 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { 115 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
47 canvas->GetSkCanvas()->drawColor(color_); 116 canvas->GetSkCanvas()->drawColor(color_);
48 } 117 }
49 118
50 private: 119 private:
51 SkColor color_; 120 SkColor color_;
52 }; 121 };
53 122
54 class LayerWithRealCompositorTest : public testing::Test { 123 class LayerWithRealCompositorTest : public testing::Test {
55 public: 124 public:
56 LayerWithRealCompositorTest() {} 125 LayerWithRealCompositorTest() {
126 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) {
127 test_data_directory_ = test_data_directory_.AppendASCII("compositor");
128 } else {
129 LOG(ERROR) << "Could not open test data directory.";
130 }
131 }
57 virtual ~LayerWithRealCompositorTest() {} 132 virtual ~LayerWithRealCompositorTest() {}
58 133
59 // Overridden from testing::Test: 134 // Overridden from testing::Test:
60 virtual void SetUp() OVERRIDE { 135 virtual void SetUp() OVERRIDE {
61 const gfx::Rect host_bounds(10, 10, 500, 500); 136 const gfx::Rect host_bounds(10, 10, 500, 500);
62 window_.reset(TestCompositorHost::Create(host_bounds)); 137 window_.reset(TestCompositorHost::Create(host_bounds));
63 window_->Show(); 138 window_->Show();
64 } 139 }
65 140
66 virtual void TearDown() OVERRIDE { 141 virtual void TearDown() OVERRIDE {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 void RunPendingMessages() { 173 void RunPendingMessages() {
99 MessageLoopForUI::current()->RunAllPending(); 174 MessageLoopForUI::current()->RunAllPending();
100 } 175 }
101 176
102 // Invalidates the entire contents of the layer. 177 // Invalidates the entire contents of the layer.
103 void SchedulePaintForLayer(Layer* layer) { 178 void SchedulePaintForLayer(Layer* layer) {
104 layer->SchedulePaint( 179 layer->SchedulePaint(
105 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); 180 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
106 } 181 }
107 182
183 const FilePath& test_data_directory() const { return test_data_directory_; }
184
108 private: 185 private:
109 scoped_ptr<TestCompositorHost> window_; 186 scoped_ptr<TestCompositorHost> window_;
110 187
188 // The root directory for test files.
189 FilePath test_data_directory_;
190
111 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest); 191 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest);
112 }; 192 };
113 193
114 // LayerDelegate that paints colors to the layer. 194 // LayerDelegate that paints colors to the layer.
115 class TestLayerDelegate : public LayerDelegate { 195 class TestLayerDelegate : public LayerDelegate {
116 public: 196 public:
117 explicit TestLayerDelegate() : color_index_(0) {} 197 explicit TestLayerDelegate() : color_index_(0) {}
118 virtual ~TestLayerDelegate() {} 198 virtual ~TestLayerDelegate() {}
119 199
120 void AddColor(SkColor color) { 200 void AddColor(SkColor color) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 virtual ~NullLayerDelegate() {} 251 virtual ~NullLayerDelegate() {}
172 252
173 private: 253 private:
174 // Overridden from LayerDelegate: 254 // Overridden from LayerDelegate:
175 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { 255 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
176 } 256 }
177 257
178 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate); 258 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate);
179 }; 259 };
180 260
181 // Encodes a bitmap into a PNG and write to disk. Returns true on success. The
182 // parent directory does not have to exist.
183 bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) {
184 std::vector<unsigned char> png_data;
185 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data) &&
186 file_util::CreateDirectory(file_path.DirName())) {
187 int bytes_written = file_util::WriteFile(
188 file_path, reinterpret_cast<char*>(&png_data[0]), png_data.size());
189 if (bytes_written == static_cast<int>(png_data.size()))
190 return true;
191 }
192 return false;
193 }
194
195 // Remembers if it has been notified. 261 // Remembers if it has been notified.
196 class TestCompositorObserver : public CompositorObserver { 262 class TestCompositorObserver : public CompositorObserver {
197 public: 263 public:
198 TestCompositorObserver() : notified_(false) {} 264 TestCompositorObserver() : notified_(false) {}
199 265
200 bool notified() const { return notified_; } 266 bool notified() const { return notified_; }
201 267
202 void Reset() { notified_ = false; } 268 void Reset() { notified_ = false; }
203 269
204 private: 270 private:
(...skipping 12 matching lines...) Expand all
217 // These are disabled on windows as they don't run correctly on the buildbot. 283 // These are disabled on windows as they don't run correctly on the buildbot.
218 // Reenable once we move to the real compositor. 284 // Reenable once we move to the real compositor.
219 #define MAYBE_Delegate DISABLED_Delegate 285 #define MAYBE_Delegate DISABLED_Delegate
220 #define MAYBE_Draw DISABLED_Draw 286 #define MAYBE_Draw DISABLED_Draw
221 #define MAYBE_DrawTree DISABLED_DrawTree 287 #define MAYBE_DrawTree DISABLED_DrawTree
222 #define MAYBE_Hierarchy DISABLED_Hierarchy 288 #define MAYBE_Hierarchy DISABLED_Hierarchy
223 #define MAYBE_HierarchyNoTexture DISABLED_HierarchyNoTexture 289 #define MAYBE_HierarchyNoTexture DISABLED_HierarchyNoTexture
224 #define MAYBE_DrawPixels DISABLED_DrawPixels 290 #define MAYBE_DrawPixels DISABLED_DrawPixels
225 #define MAYBE_SetRootLayer DISABLED_SetRootLayer 291 #define MAYBE_SetRootLayer DISABLED_SetRootLayer
226 #define MAYBE_CompositorObservers DISABLED_CompositorObservers 292 #define MAYBE_CompositorObservers DISABLED_CompositorObservers
293 #define MAYBE_ModifyHierarchy DISABLED_ModifyHierarchy
294 #define MAYBE_Opacity DISABLED_Opacity
227 #else 295 #else
228 #define MAYBE_Delegate Delegate 296 #define MAYBE_Delegate Delegate
229 #define MAYBE_Draw Draw 297 #define MAYBE_Draw Draw
230 #define MAYBE_DrawTree DrawTree 298 #define MAYBE_DrawTree DrawTree
231 #define MAYBE_Hierarchy Hierarchy 299 #define MAYBE_Hierarchy Hierarchy
232 #define MAYBE_HierarchyNoTexture HierarchyNoTexture 300 #define MAYBE_HierarchyNoTexture HierarchyNoTexture
233 #define MAYBE_DrawPixels DrawPixels 301 #define MAYBE_DrawPixels DrawPixels
234 #define MAYBE_SetRootLayer SetRootLayer 302 #define MAYBE_SetRootLayer SetRootLayer
235 #define MAYBE_CompositorObservers CompositorObservers 303 #define MAYBE_CompositorObservers CompositorObservers
304 #define MAYBE_ModifyHierarchy ModifyHierarchy
305 #define MAYBE_Opacity Opacity
236 #endif 306 #endif
237 307
238 TEST_F(LayerWithRealCompositorTest, MAYBE_Draw) { 308 TEST_F(LayerWithRealCompositorTest, MAYBE_Draw) {
239 scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED, 309 scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED,
240 gfx::Rect(20, 20, 50, 50))); 310 gfx::Rect(20, 20, 50, 50)));
241 DrawTree(layer.get()); 311 DrawTree(layer.get());
242 } 312 }
243 313
244 // Create this hierarchy: 314 // Create this hierarchy:
245 // L1 - red 315 // L1 - red
(...skipping 864 matching lines...) Expand 10 before | Expand all | Expand 10 after
1110 1180
1111 GetCompositor()->RemoveObserver(&observer); 1181 GetCompositor()->RemoveObserver(&observer);
1112 1182
1113 // Opacity changes should no longer alert the removed observer. 1183 // Opacity changes should no longer alert the removed observer.
1114 observer.Reset(); 1184 observer.Reset();
1115 l2->SetOpacity(0.5f); 1185 l2->SetOpacity(0.5f);
1116 RunPendingMessages(); 1186 RunPendingMessages();
1117 EXPECT_FALSE(observer.notified()); 1187 EXPECT_FALSE(observer.notified());
1118 } 1188 }
1119 1189
1190 // Checks that modifying the hierarchy correctly affects final composite.
1191 TEST_F(LayerWithRealCompositorTest, MAYBE_ModifyHierarchy) {
1192 GetCompositor()->WidgetSizeChanged(gfx::Size(50, 50));
1193
1194 // l0
1195 // +-l11
1196 // | +-l21
1197 // +-l12
1198 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED,
1199 gfx::Rect(0, 0, 50, 50)));
1200 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN,
1201 gfx::Rect(0, 0, 25, 25)));
1202 scoped_ptr<Layer> l21(CreateColorLayer(SK_ColorMAGENTA,
1203 gfx::Rect(0, 0, 15, 15)));
1204 scoped_ptr<Layer> l12(CreateColorLayer(SK_ColorBLUE,
1205 gfx::Rect(10, 10, 25, 25)));
1206
1207 FilePath ref_img1 = test_data_directory().AppendASCII("ModifyHierarchy1.png");
1208 FilePath ref_img2 = test_data_directory().AppendASCII("ModifyHierarchy2.png");
1209 SkBitmap bitmap;
1210
1211 l0->Add(l11.get());
1212 l11->Add(l21.get());
1213 l0->Add(l12.get());
1214 DrawTree(l0.get());
1215 ASSERT_TRUE(GetCompositor()->ReadPixels(&bitmap));
1216 ASSERT_FALSE(bitmap.empty());
1217 // WritePNGFile(bitmap, ref_img1);
1218 EXPECT_TRUE(IsSameAsPNGFile(bitmap, ref_img1));
1219
1220 l0->MoveToFront(l11.get());
1221 DrawTree(l0.get());
1222 ASSERT_TRUE(GetCompositor()->ReadPixels(&bitmap));
1223 ASSERT_FALSE(bitmap.empty());
1224 // WritePNGFile(bitmap, ref_img2);
1225 EXPECT_TRUE(IsSameAsPNGFile(bitmap, ref_img2));
1226
1227 // l11 is already at the front, should have no effect.
1228 l0->MoveToFront(l11.get());
1229 DrawTree(l0.get());
1230 ASSERT_TRUE(GetCompositor()->ReadPixels(&bitmap));
1231 ASSERT_FALSE(bitmap.empty());
1232 EXPECT_TRUE(IsSameAsPNGFile(bitmap, ref_img2));
1233
1234 // l11 is already at the front, should have no effect.
1235 l0->MoveAbove(l11.get(), l12.get());
1236 DrawTree(l0.get());
1237 ASSERT_TRUE(GetCompositor()->ReadPixels(&bitmap));
1238 ASSERT_FALSE(bitmap.empty());
1239 EXPECT_TRUE(IsSameAsPNGFile(bitmap, ref_img2));
1240
1241 // should restore to original configuration
1242 l0->MoveAbove(l12.get(), l11.get());
1243 DrawTree(l0.get());
1244 ASSERT_TRUE(GetCompositor()->ReadPixels(&bitmap));
1245 ASSERT_FALSE(bitmap.empty());
1246 EXPECT_TRUE(IsSameAsPNGFile(bitmap, ref_img1));
1247 }
1248
1249 // Opacity is rendered correctly.
1250 // Checks that modifying the hierarchy correctly affects final composite.
1251 TEST_F(LayerWithRealCompositorTest, MAYBE_Opacity) {
1252 GetCompositor()->WidgetSizeChanged(gfx::Size(50, 50));
1253
1254 // l0
1255 // +-l11
1256 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED,
1257 gfx::Rect(0, 0, 50, 50)));
1258 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN,
1259 gfx::Rect(0, 0, 25, 25)));
1260
1261 FilePath ref_img = test_data_directory().AppendASCII("Opacity.png");
1262
1263 l11->SetOpacity(0.75);
1264 l0->Add(l11.get());
1265 DrawTree(l0.get());
1266 SkBitmap bitmap;
1267 ASSERT_TRUE(GetCompositor()->ReadPixels(&bitmap));
1268 ASSERT_FALSE(bitmap.empty());
1269 // WritePNGFile(bitmap, ref_img);
1270 EXPECT_TRUE(IsSameAsPNGFile(bitmap, ref_img));
1271 }
1272
1120 } // namespace ui 1273 } // namespace ui
OLDNEW
« no previous file with comments | « ui/gfx/compositor/layer_animator_unittest.cc ('k') | ui/gfx/compositor/run_all_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698