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 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));
52 // Draw a trivial scene.
53 canvas
.save(SkCanvas::kMatrixClip_SaveFlag
);
54 canvas
.clipRect(fullRect
, SkRegion::kIntersect_Op
, false);
55 canvas
.translate(SkIntToScalar(10), SkIntToScalar(10));
56 canvas
.scale(SkIntToScalar(2), SkIntToScalar(2));
57 canvas
.drawRect(fillRect
, red_paint
);
60 // Verify the recorded commands.
63 ASSERT_EQ(canvas
.getSize(), 6);
65 ASSERT_TRUE(canvas
.getDrawCommandAt(idx
) != NULL
);
66 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
68 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "Save");
69 EXPECT_TRUE(HasInfoField(canvas
, idx
, "SaveFlags"));
71 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
72 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
73 EXPECT_EQ(cmd
, CLIP_RECT
);
74 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "Clip Rect");
75 EXPECT_TRUE(HasInfoField(canvas
, idx
, "SkRect"));
76 EXPECT_TRUE(HasInfoField(canvas
, idx
, "Op"));
77 EXPECT_TRUE(HasInfoField(canvas
, idx
, "doAA"));
79 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
80 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
81 EXPECT_EQ(cmd
, TRANSLATE
);
82 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "Translate");
83 EXPECT_TRUE(HasInfoField(canvas
, idx
, "dx"));
84 EXPECT_TRUE(HasInfoField(canvas
, idx
, "dy"));
86 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
87 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
88 EXPECT_EQ(cmd
, SCALE
);
89 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "Scale");
90 EXPECT_TRUE(HasInfoField(canvas
, idx
, "sx"));
91 EXPECT_TRUE(HasInfoField(canvas
, idx
, "sy"));
93 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
94 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
95 EXPECT_EQ(cmd
, DRAW_RECT
);
96 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "Draw Rect");
97 EXPECT_TRUE(HasInfoField(canvas
, idx
, "SkRect"));
99 ASSERT_TRUE(canvas
.getDrawCommandAt(++idx
) != NULL
);
100 cmd
= canvas
.getDrawCommandAt(idx
)->getType();
101 EXPECT_EQ(cmd
, RESTORE
);
102 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd
), "Restore");
105 } // namespace content