| Index: cc/paint/paint_op_buffer_unittest.cc | 
| diff --git a/cc/paint/paint_op_buffer_unittest.cc b/cc/paint/paint_op_buffer_unittest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..e141459c9f53d5aa6b46147c0f627766e6a3bfcc | 
| --- /dev/null | 
| +++ b/cc/paint/paint_op_buffer_unittest.cc | 
| @@ -0,0 +1,134 @@ | 
| +// Copyright 2017 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "cc/paint/paint_op_buffer.h" | 
| +#include "testing/gtest/include/gtest/gtest.h" | 
| + | 
| +namespace cc { | 
| + | 
| +TEST(PaintOpBufferTest, Empty) { | 
| +  PaintOpBuffer buffer; | 
| +  EXPECT_EQ(buffer.approximateOpCount(), 0); | 
| +  EXPECT_EQ(buffer.approximateBytesUsed(), 0u); | 
| +  EXPECT_EQ(PaintOpBuffer::Iterator(&buffer), false); | 
| + | 
| +  buffer.Reset(); | 
| +  EXPECT_EQ(buffer.approximateOpCount(), 0); | 
| +  EXPECT_EQ(buffer.approximateBytesUsed(), 0u); | 
| +  EXPECT_EQ(PaintOpBuffer::Iterator(&buffer), false); | 
| +} | 
| + | 
| +TEST(PaintOpBufferTest, SimpleAppend) { | 
| +  SkRect rect = SkRect::MakeXYWH(2, 3, 4, 5); | 
| +  PaintFlags flags; | 
| +  flags.setColor(SK_ColorMAGENTA); | 
| +  flags.setAlpha(100); | 
| +  SkColor draw_color = SK_ColorRED; | 
| +  SkBlendMode blend = SkBlendMode::kSrc; | 
| + | 
| +  PaintOpBuffer buffer; | 
| +  buffer.push<SaveLayerOp>(&rect, &flags); | 
| +  buffer.push<SaveOp>(); | 
| +  buffer.push<DrawColorOp>(draw_color, blend); | 
| +  buffer.push<RestoreOp>(); | 
| + | 
| +  EXPECT_EQ(buffer.approximateOpCount(), 4); | 
| + | 
| +  PaintOpBuffer::Iterator iter(&buffer); | 
| +  ASSERT_EQ(iter->GetType(), PaintOpType::SaveLayer); | 
| +  SaveLayerOp* save_op = static_cast<SaveLayerOp*>(*iter); | 
| +  EXPECT_EQ(save_op->bounds, rect); | 
| +  EXPECT_TRUE(save_op->flags == flags); | 
| +  ++iter; | 
| + | 
| +  ASSERT_EQ(iter->GetType(), PaintOpType::Save); | 
| +  ++iter; | 
| + | 
| +  ASSERT_EQ(iter->GetType(), PaintOpType::DrawColor); | 
| +  DrawColorOp* op = static_cast<DrawColorOp*>(*iter); | 
| +  EXPECT_EQ(op->color, draw_color); | 
| +  EXPECT_EQ(op->mode, blend); | 
| +  ++iter; | 
| + | 
| +  ASSERT_EQ(iter->GetType(), PaintOpType::Restore); | 
| +  ++iter; | 
| + | 
| +  EXPECT_FALSE(iter); | 
| +} | 
| + | 
| +// PaintOpBuffer has a special case for first ops stored locally, so | 
| +// make sure that appending different kind of ops as a first op works | 
| +// properly, as well as resetting and reusing the first local op. | 
| +TEST(PaintOpBufferTest, FirstOpWithAndWithoutData) { | 
| +  PaintOpBuffer buffer; | 
| +  char text[] = "asdf"; | 
| + | 
| +  // Use a color filter and its ref count to verify that the destructor | 
| +  // is called on ops after reset. | 
| +  PaintFlags flags; | 
| +  sk_sp<SkColorFilter> filter = | 
| +      SkColorFilter::MakeModeFilter(SK_ColorMAGENTA, SkBlendMode::kSrcOver); | 
| +  flags.setColorFilter(filter); | 
| +  EXPECT_EQ(filter->getRefCnt(), 2); | 
| + | 
| +  buffer.push_with_data<DrawTextOp>(text, arraysize(text), 0.f, 0.f, flags); | 
| +  EXPECT_EQ(filter->getRefCnt(), 3); | 
| + | 
| +  // Verify that when the first op has data, which may not fit in the | 
| +  // PaintRecord internal buffer, that it adds a noop as the first op | 
| +  // and then appends the "op with data" into the heap buffer. | 
| +  ASSERT_EQ(buffer.approximateOpCount(), 2); | 
| +  EXPECT_EQ(buffer.GetFirstOp()->GetType(), PaintOpType::Noop); | 
| + | 
| +  // Verify iteration behavior and brief smoke test of op state. | 
| +  { | 
| +    PaintOpBuffer::Iterator iter(&buffer); | 
| +    PaintOp* noop = *iter; | 
| +    EXPECT_EQ(buffer.GetFirstOp(), noop); | 
| +    ++iter; | 
| + | 
| +    PaintOp* op = *iter; | 
| +    ASSERT_EQ(op->GetType(), PaintOpType::DrawText); | 
| +    DrawTextOp* draw_text_op = static_cast<DrawTextOp*>(op); | 
| +    EXPECT_EQ(draw_text_op->bytes, arraysize(text)); | 
| + | 
| +    void* data = paint_op_data(draw_text_op); | 
| +    EXPECT_EQ(memcmp(data, text, arraysize(text)), 0); | 
| + | 
| +    ++iter; | 
| +    EXPECT_FALSE(iter); | 
| +  } | 
| + | 
| +  // Reset, verify state, and append an op that will fit in the first slot. | 
| +  buffer.Reset(); | 
| +  EXPECT_EQ(filter->getRefCnt(), 2); | 
| + | 
| +  ASSERT_EQ(buffer.approximateOpCount(), 0); | 
| +  EXPECT_EQ(PaintOpBuffer::Iterator(&buffer), false); | 
| + | 
| +  SkRect rect = SkRect::MakeXYWH(1, 2, 3, 4); | 
| +  buffer.push<DrawRectOp>(rect, flags); | 
| +  EXPECT_EQ(filter->getRefCnt(), 3); | 
| + | 
| +  ASSERT_EQ(buffer.approximateOpCount(), 1); | 
| +  EXPECT_EQ(buffer.GetFirstOp()->GetType(), PaintOpType::DrawRect); | 
| + | 
| +  PaintOpBuffer::Iterator iter(&buffer); | 
| +  ASSERT_EQ(iter->GetType(), PaintOpType::DrawRect); | 
| +  DrawRectOp* draw_rect_op = static_cast<DrawRectOp*>(*iter); | 
| +  EXPECT_EQ(draw_rect_op->rect, rect); | 
| + | 
| +  ++iter; | 
| +  EXPECT_FALSE(iter); | 
| + | 
| +  buffer.Reset(); | 
| +  ASSERT_EQ(buffer.approximateOpCount(), 0); | 
| +  EXPECT_EQ(filter->getRefCnt(), 2); | 
| +} | 
| + | 
| +// TODO(enne): test Save/restore optimization | 
| +// TODO(enne): Blink webkit_unit_tests cover slow path counting, but should be | 
| +// tested separately here. | 
| + | 
| +}  // namespace cc | 
|  |