Chromium Code Reviews| Index: Source/platform/graphics/GraphicsContextTest.cpp |
| diff --git a/Source/platform/graphics/GraphicsContextTest.cpp b/Source/platform/graphics/GraphicsContextTest.cpp |
| index fe8b95e939a8b6b9ff9302c82b5914cec334658a..41a055128115685eba08407c6542f80bc7192ad3 100644 |
| --- a/Source/platform/graphics/GraphicsContextTest.cpp |
| +++ b/Source/platform/graphics/GraphicsContextTest.cpp |
| @@ -66,6 +66,17 @@ namespace { |
| } \ |
| } |
| +#define EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, color) \ |
| +{ \ |
| + SkAutoLockPixels locker(bitmap); \ |
| + for (int y = rect.y(); y < rect.maxY(); ++y) { \ |
| + for (int x = rect.x(); x < rect.maxX(); ++x) { \ |
| + RGBA32 pixel = *bitmap.getAddr32(x, y); \ |
| + EXPECT_EQ(color, pixel); \ |
| + } \ |
| + } \ |
| +} |
| + |
| TEST(GraphicsContextTest, trackOpaqueTest) |
| { |
| SkBitmap bitmap; |
| @@ -1214,4 +1225,77 @@ TEST(GraphicsContextTest, RecordingCanvas) |
| context.endRecording(); |
| } |
| +TEST(GraphicsContextTest, BeginLayerPreserveCanvasState) |
| +{ |
| + SkBitmap bitmap; |
| + bitmap.allocN32Pixels(1, 1); |
| + bitmap.eraseColor(0); |
| + SkCanvas canvas(bitmap); |
| + GraphicsContext context(&canvas); |
| + |
| + IntRect rect(0, 0, 1, 1); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::transparent); |
| + context.fillRect(rect, Color::gray); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::gray); |
| + |
| + // Translate out of bound not to draw. |
| + context.translate(1, 0); |
| + context.fillRect(rect, Color::darkGray); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::gray); |
| + EXPECT_EQ(SK_Scalar1, context.getTotalMatrix().getTranslateX()); |
| + |
| + context.beginLayer(1, CompositeSourceOver); |
| + context.translate(-1, 0); |
| + EXPECT_EQ(0, context.getTotalMatrix().getTranslateX()); |
| + context.fillRect(rect, Color::darkGray); |
| + context.endLayer(); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::darkGray); |
| + |
| + // GraphicsContext::beginLayer()/endLayer() behaves the same as save()/restore() |
| + // so current transform is restored. |
| + EXPECT_EQ(SK_Scalar1, context.getTotalMatrix().getTranslateX()); |
| + |
| + context.fillRect(rect, Color::lightGray); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::darkGray); |
| +} |
| + |
| +// Although this test looks like duplicated to BeginLayerPreserveCanvasState, |
| +// this is needed because GraphicsContext keeps a composite operator in SkPaint |
| +// while setting a transform to SkCanvas directly. |
| +TEST(GraphicsContextTest, BeginLayerPreservePaintState) |
| +{ |
| + SkBitmap bitmap; |
| + bitmap.allocN32Pixels(1, 1); |
| + bitmap.eraseColor(0); |
| + SkCanvas canvas(bitmap); |
| + GraphicsContext context(&canvas); |
| + |
| + IntRect rect(0, 0, 1, 1); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::transparent); |
| + EXPECT_EQ(CompositeSourceOver, context.compositeOperation()); |
| + context.fillRect(rect, Color::gray); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::gray); |
| + |
| + // Set CompositeDestinationIn not to draw. |
| + context.setCompositeOperation(CompositeDestinationIn); |
| + context.fillRect(rect, Color::darkGray); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::gray); |
| + EXPECT_EQ(CompositeDestinationIn, context.compositeOperation()); |
| + |
| + context.beginLayer(1, CompositeSourceIn); |
| + EXPECT_EQ(CompositeDestinationIn, context.compositeOperation()); |
| + context.setCompositeOperation(CompositeSourceOver); |
| + EXPECT_EQ(CompositeSourceOver, context.compositeOperation()); |
| + context.fillRect(rect, Color::darkGray); |
| + context.endLayer(); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::darkGray); |
| + |
| + // GraphicsContext::beginLayer()/endLayer() behaves the same as save()/restore() |
| + // so current composite operator is restored. |
| + EXPECT_EQ(CompositeDestinationIn, context.compositeOperation()); |
|
dshwang
2014/10/14 17:25:07
Without this CL, this line and #1298 fail, while B
|
| + |
| + context.fillRect(rect, Color::lightGray); |
| + EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::darkGray); |
| +} |
| + |
| } // namespace |