Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / renderer / skia_benchmarking_extension_unittest.cc
blob3a6e02ce4e3e9efe580f954264338e016c2a34f4
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"
13 namespace {
15 testing::AssertionResult HasInfoField(SkDebugCanvas& canvas, int index,
16 const char* field) {
18 SkTDArray<SkString*>* info = canvas.getCommandInfo(index);
19 if (info == NULL)
20 return testing::AssertionFailure() << " command info not found for index "
21 << index;
23 for (int i = 0; i < info->count(); ++i) {
24 const SkString* info_str = (*info)[i];
25 if (info_str == NULL)
26 return testing::AssertionFailure() << " NULL info string for index "
27 << 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";
39 namespace content {
41 TEST(SkiaBenchmarkingExtensionTest, SkDebugCanvas) {
42 SkGraphics::Init();
44 // Prepare canvas and resources.
45 SkDebugCanvas canvas(100, 100);
46 SkPaint red_paint;
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();
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);
58 canvas.restore();
60 // Verify the recorded commands.
61 DrawType cmd;
62 int idx = 0;
63 ASSERT_EQ(canvas.getSize(), 6);
65 ASSERT_TRUE(canvas.getDrawCommandAt(idx) != NULL);
66 cmd = canvas.getDrawCommandAt(idx)->getType();
67 EXPECT_EQ(cmd, SAVE);
68 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "Save");
70 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL);
71 cmd = canvas.getDrawCommandAt(idx)->getType();
72 EXPECT_EQ(cmd, CLIP_RECT);
73 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "Clip Rect");
74 EXPECT_TRUE(HasInfoField(canvas, idx, "SkRect"));
75 EXPECT_TRUE(HasInfoField(canvas, idx, "Op"));
76 EXPECT_TRUE(HasInfoField(canvas, idx, "doAA"));
78 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL);
79 cmd = canvas.getDrawCommandAt(idx)->getType();
80 EXPECT_EQ(cmd, TRANSLATE);
81 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "Translate");
82 EXPECT_TRUE(HasInfoField(canvas, idx, "dx"));
83 EXPECT_TRUE(HasInfoField(canvas, idx, "dy"));
85 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL);
86 cmd = canvas.getDrawCommandAt(idx)->getType();
87 EXPECT_EQ(cmd, SCALE);
88 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "Scale");
89 EXPECT_TRUE(HasInfoField(canvas, idx, "sx"));
90 EXPECT_TRUE(HasInfoField(canvas, idx, "sy"));
92 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL);
93 cmd = canvas.getDrawCommandAt(idx)->getType();
94 EXPECT_EQ(cmd, DRAW_RECT);
95 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "Draw Rect");
96 EXPECT_TRUE(HasInfoField(canvas, idx, "SkRect"));
98 ASSERT_TRUE(canvas.getDrawCommandAt(++idx) != NULL);
99 cmd = canvas.getDrawCommandAt(idx)->getType();
100 EXPECT_EQ(cmd, RESTORE);
101 EXPECT_STREQ(SkDrawCommand::GetCommandString(cmd), "Restore");
104 } // namespace content