1 // Copyright (c) 2013 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.
5 #include "content/renderer/skia_benchmarking_extension.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "third_party/skia/include/core/SkGraphics.h"
10 #include "third_party/skia/src/utils/debugger/SkDebugCanvas.h"
11 #include "third_party/skia/src/utils/debugger/SkDrawCommand.h"
15 testing::AssertionResult
HasInfoField(SkDebugCanvas
& canvas
, int index
,
18 const SkTDArray
<SkString
*>* info
= canvas
.getCommandInfo(index
);
20 return testing::AssertionFailure() << " command info not found for index "
23 for (int i
= 0; i
< info
->count(); ++i
) {
24 const SkString
* info_str
= (*info
)[i
];
26 return testing::AssertionFailure() << " NULL info string for index "
29 // FIXME: loose info paramter test.
30 if (strstr(info_str
->c_str(), field
) != NULL
)
31 return testing::AssertionSuccess() << field
<< " found";
34 return testing::AssertionFailure() << field
<< " not found";
41 TEST(SkiaBenchmarkingExtensionTest
, SkDebugCanvas
) {
44 // Prepare canvas and resources.
45 SkDebugCanvas
canvas(100, 100);
47 red_paint
.setColor(SkColorSetARGB(255, 255, 0, 0));
48 SkRect fullRect
= SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
49 SkRect fillRect
= SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25),
50 SkIntToScalar(50), SkIntToScalar(50));
53 trans
.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
55 // Draw a trivial scene.
57 canvas
.clipRect(fullRect
, SkRegion::kIntersect_Op
, false);
58 canvas
.setMatrix(trans
);
59 canvas
.drawRect(fillRect
, red_paint
);
62 // Verify the recorded commands.
63 SkDrawCommand::OpType cmd
;
65 ASSERT_EQ(canvas
.getSize(), 5);
67 ASSERT_TRUE(canvas
.getDrawCommandAt(idx
) != NULL
);
68 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
69 EXPECT_EQ(cmd
, SkDrawCommand::kSave_OpType
);
70 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "Save");
72 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
73 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
74 EXPECT_EQ(cmd
, SkDrawCommand::kClipRect_OpType
);
75 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
),
76 SkDrawCommand::kClipRectString
);
77 EXPECT_TRUE(HasInfoField(canvas
, idx
, "SkRect"));
78 EXPECT_TRUE(HasInfoField(canvas
, idx
, "Op"));
79 EXPECT_TRUE(HasInfoField(canvas
, idx
, "doAA"));
81 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
82 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
83 EXPECT_EQ(cmd
, SkDrawCommand::kSetMatrix_OpType
);
84 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "SetMatrix");
86 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
87 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
88 EXPECT_EQ(cmd
, SkDrawCommand::kDrawRect_OpType
);
89 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
),
90 SkDrawCommand::kDrawRectString
);
91 EXPECT_TRUE(HasInfoField(canvas
, idx
, "SkRect"));
93 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
94 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
95 EXPECT_EQ(cmd
, SkDrawCommand::kRestore_OpType
);
96 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "Restore");
99 } // namespace content