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

Side by Side Diff: base/gfx/platform_canvas_unittest_win.cc

Issue 1853: Refactor the platform canvas unit test a bit so that it can be run on... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 3 months 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 | « base/gfx/platform_canvas_mac.cc ('k') | base/gfx/platform_device.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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.
4
5 #include <windows.h>
6
7 #include "base/gfx/platform_canvas_win.h"
8 #include "base/gfx/platform_device_win.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 #include "SkColor.h"
12
13 namespace gfx {
14
15 namespace {
16
17 // Return true if the canvas is filled to canvas_color,
18 // and contains a single rectangle filled to rect_color.
19 bool VerifyRect(const PlatformCanvasWin& canvas,
20 uint32_t canvas_color, uint32_t rect_color,
21 int x, int y, int w, int h) {
22 PlatformDeviceWin& device = canvas.getTopPlatformDevice();
23 const SkBitmap& bitmap = device.accessBitmap(false);
24 SkAutoLockPixels lock(bitmap);
25
26 for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) {
27 for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) {
28 if (cur_x >= x && cur_x < x + w &&
29 cur_y >= y && cur_y < y + h) {
30 // Inside the square should be rect_color
31 if (*bitmap.getAddr32(cur_x, cur_y) != rect_color)
32 return false;
33 } else {
34 // Outside the square should be canvas_color
35 if (*bitmap.getAddr32(cur_x, cur_y) != canvas_color)
36 return false;
37 }
38 }
39 }
40 return true;
41 }
42
43 // Checks whether there is a white canvas with a black square at the given
44 // location in pixels (not in the canvas coordinate system).
45 // TODO(ericroman): rename Square to Rect
46 bool VerifyBlackSquare(const PlatformCanvasWin& canvas, int x, int y, int w, int h) {
47 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h);
48 }
49
50 // Check that every pixel in the canvas is a single color.
51 bool VerifyCanvasColor(const PlatformCanvasWin& canvas, uint32_t canvas_color) {
52 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0);
53 }
54
55 void DrawGDIRect(PlatformCanvasWin& canvas, int x, int y, int w, int h) {
56 HDC dc = canvas.beginPlatformPaint();
57
58 RECT inner_rc;
59 inner_rc.left = x;
60 inner_rc.top = y;
61 inner_rc.right = x + w;
62 inner_rc.bottom = y + h;
63 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH))) ;
64
65 canvas.endPlatformPaint();
66 }
67
68 // Clips the contents of the canvas to the given rectangle. This will be
69 // intersected with any existing clip.
70 void AddClip(PlatformCanvasWin& canvas, int x, int y, int w, int h) {
71 SkRect rect;
72 rect.set(SkIntToScalar(x), SkIntToScalar(y),
73 SkIntToScalar(x + w), SkIntToScalar(y + h));
74 canvas.clipRect(rect);
75 }
76
77 class LayerSaver {
78 public:
79 LayerSaver(PlatformCanvasWin& canvas, int x, int y, int w, int h)
80 : canvas_(canvas),
81 x_(x),
82 y_(y),
83 w_(w),
84 h_(h) {
85 SkRect bounds;
86 bounds.set(SkIntToScalar(x_), SkIntToScalar(y_),
87 SkIntToScalar(right()), SkIntToScalar(bottom()));
88 canvas_.saveLayer(&bounds, NULL);
89 }
90
91 ~LayerSaver() {
92 canvas_.getTopPlatformDevice().fixupAlphaBeforeCompositing();
93 canvas_.restore();
94 }
95
96 int x() const { return x_; }
97 int y() const { return y_; }
98 int w() const { return w_; }
99 int h() const { return h_; }
100
101 // Returns the EXCLUSIVE far bounds of the layer.
102 int right() const { return x_ + w_; }
103 int bottom() const { return y_ + h_; }
104
105 private:
106 PlatformCanvasWin& canvas_;
107 int x_, y_, w_, h_;
108 };
109
110 // Size used for making layers in many of the below tests.
111 const int kLayerX = 2;
112 const int kLayerY = 3;
113 const int kLayerW = 9;
114 const int kLayerH = 7;
115
116 // Size used by some tests to draw a rectangle inside the layer.
117 const int kInnerX = 4;
118 const int kInnerY = 5;
119 const int kInnerW = 2;
120 const int kInnerH = 3;
121
122 }
123
124 // This just checks that our checking code is working properly, it just uses
125 // regular skia primitives.
126 TEST(PlatformCanvasWin, SkLayer) {
127 // Create the canvas initialized to opaque white.
128 PlatformCanvasWin canvas(16, 16, true);
129 canvas.drawColor(SK_ColorWHITE);
130
131 // Make a layer and fill it completely to make sure that the bounds are
132 // correct.
133 {
134 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
135 canvas.drawColor(SK_ColorBLACK);
136 }
137 EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX, kLayerY, kLayerW, kLayerH));
138 }
139
140 // Test the GDI clipping.
141 TEST(PlatformCanvasWin, GDIClipRegion) {
142 // Initialize a white canvas
143 PlatformCanvasWin canvas(16, 16, true);
144 canvas.drawColor(SK_ColorWHITE);
145 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE));
146
147 // Test that initially the canvas has no clip region, by filling it
148 // with a black rectangle.
149 // Note: Don't use LayerSaver, since internally it sets a clip region.
150 DrawGDIRect(canvas, 0, 0, 16, 16);
151 canvas.getTopPlatformDevice().fixupAlphaBeforeCompositing();
152 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorBLACK));
153
154 // Test that intersecting disjoint clip rectangles sets an empty clip region
155 canvas.drawColor(SK_ColorWHITE);
156 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE));
157 {
158 LayerSaver layer(canvas, 0, 0, 16, 16);
159 AddClip(canvas, 2, 3, 4, 5);
160 AddClip(canvas, 4, 9, 10, 10);
161 DrawGDIRect(canvas, 0, 0, 16, 16);
162 }
163 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE));
164 }
165
166 // Test the layers get filled properly by GDI.
167 TEST(PlatformCanvasWin, GDILayer) {
168 // Create the canvas initialized to opaque white.
169 PlatformCanvasWin canvas(16, 16, true);
170
171 // Make a layer and fill it completely to make sure that the bounds are
172 // correct.
173 canvas.drawColor(SK_ColorWHITE);
174 {
175 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
176 DrawGDIRect(canvas, 0, 0, 100, 100);
177 }
178 EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX, kLayerY, kLayerW, kLayerH));
179
180 // Make a layer and fill it partially to make sure the translation is correct.
181 canvas.drawColor(SK_ColorWHITE);
182 {
183 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
184 DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH);
185 }
186 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH));
187
188 // Add a clip on the layer and fill to make sure clip is correct.
189 canvas.drawColor(SK_ColorWHITE);
190 {
191 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
192 canvas.save();
193 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH);
194 DrawGDIRect(canvas, 0, 0, 100, 100);
195 canvas.restore();
196 }
197 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH));
198
199 // Add a clip and then make the layer to make sure the clip is correct.
200 canvas.drawColor(SK_ColorWHITE);
201 canvas.save();
202 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH);
203 {
204 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
205 DrawGDIRect(canvas, 0, 0, 100, 100);
206 }
207 canvas.restore();
208 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH));
209 }
210
211 // Test that translation + make layer works properly.
212 TEST(PlatformCanvasWin, GDITranslateLayer) {
213 // Create the canvas initialized to opaque white.
214 PlatformCanvasWin canvas(16, 16, true);
215
216 // Make a layer and fill it completely to make sure that the bounds are
217 // correct.
218 canvas.drawColor(SK_ColorWHITE);
219 canvas.save();
220 canvas.translate(1, 1);
221 {
222 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
223 DrawGDIRect(canvas, 0, 0, 100, 100);
224 }
225 canvas.restore();
226 EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX + 1, kLayerY + 1,
227 kLayerW, kLayerH));
228
229 // Translate then make the layer.
230 canvas.drawColor(SK_ColorWHITE);
231 canvas.save();
232 canvas.translate(1, 1);
233 {
234 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
235 DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH);
236 }
237 canvas.restore();
238 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 1, kInnerY + 1,
239 kInnerW, kInnerH));
240
241 // Make the layer then translate.
242 canvas.drawColor(SK_ColorWHITE);
243 canvas.save();
244 {
245 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
246 canvas.translate(1, 1);
247 DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH);
248 }
249 canvas.restore();
250 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 1, kInnerY + 1,
251 kInnerW, kInnerH));
252
253 // Translate both before and after, and have a clip.
254 canvas.drawColor(SK_ColorWHITE);
255 canvas.save();
256 canvas.translate(1, 1);
257 {
258 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH);
259 canvas.translate(1, 1);
260 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH);
261 DrawGDIRect(canvas, 0, 0, 100, 100);
262 }
263 canvas.restore();
264 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 2, kInnerY + 2,
265 kInnerW, kInnerH));
266 }
267
268 } // namespace
269
OLDNEW
« no previous file with comments | « base/gfx/platform_canvas_mac.cc ('k') | base/gfx/platform_device.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698