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.
4
5#include "content/renderer/skia_benchmarking_extension.h"
6
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"
12
13namespace {
14
15testing::AssertionResult HasInfoField(SkDebugCanvas& canvas, int index,
16                                      const char* field) {
17
18  SkTDArray<SkString*>* info = canvas.getCommandInfo(index);
19  if (info == NULL)
20    return testing::AssertionFailure() << " command info not found for index "
21                                       << index;
22
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;
28
29    // FIXME: loose info paramter test.
30    if (strstr(info_str->c_str(), field) != NULL)
31      return testing::AssertionSuccess() << field << " found";
32  }
33
34  return testing::AssertionFailure() << field << " not found";
35}
36
37}
38
39namespace content {
40
41TEST(SkiaBenchmarkingExtensionTest, SkDebugCanvas) {
42  SkGraphics::Init();
43
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));
51
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);
58  canvas.restore();
59
60  // Verify the recorded commands.
61  DrawType cmd;
62  int idx = 0;
63  ASSERT_EQ(canvas.getSize(), 6);
64
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");
69  EXPECT_TRUE(HasInfoField(canvas, idx, "SaveFlags"));
70
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"));
78
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"));
85
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"));
92
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"));
98
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");
103}
104
105} // namespace content
106