summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/skia_benchmarking_extension_unittest.cc
blob: 0e97ae701dfc0cd8029290154977666aaac1bda5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2013 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 "content/renderer/skia_benchmarking_extension.h"

#include "base/values.h"
#include "skia/ext/benchmarking_canvas.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkGraphics.h"

namespace {

testing::AssertionResult HasArg(const base::ListValue* args,
                                const char name[]) {
  const base::DictionaryValue* arg;

  for (size_t i = 0; i < args->GetSize(); ++i) {
    if (!args->GetDictionary(i, &arg) || arg->size() != 1)
      return testing::AssertionFailure() << " malformed argument for index "
                                         << i;

    if (arg->HasKey(name))
      return testing::AssertionSuccess() << " argument '" << name
                                         << "' found at index " << i;
  }

  return testing::AssertionFailure() << "argument not found: '" << name << "'";
}

}

namespace content {

TEST(SkiaBenchmarkingExtensionTest, BenchmarkingCanvas) {
  SkGraphics::Init();

  // Prepare canvas and resources.
  SkCanvas canvas(100, 100);
  skia::BenchmarkingCanvas benchmarking_canvas(&canvas);

  SkPaint red_paint;
  red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
  SkRect fullRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
  SkRect fillRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25),
                                     SkIntToScalar(50), SkIntToScalar(50));

  SkMatrix trans;
  trans.setTranslate(SkIntToScalar(10), SkIntToScalar(10));

  // Draw a trivial scene.
  benchmarking_canvas.save();
  benchmarking_canvas.clipRect(fullRect, SkRegion::kIntersect_Op, false);
  benchmarking_canvas.setMatrix(trans);
  benchmarking_canvas.drawRect(fillRect, red_paint);
  benchmarking_canvas.restore();

  // Verify the recorded commands.
  const base::ListValue& ops = benchmarking_canvas.Commands();
  ASSERT_EQ(ops.GetSize(), static_cast<size_t>(5));

  size_t index = 0;
  const base::DictionaryValue* op;
  const base::ListValue* op_args;
  std::string op_name;

  ASSERT_TRUE(ops.GetDictionary(index++, &op));
  EXPECT_TRUE(op->GetString("cmd_string", &op_name));
  EXPECT_EQ(op_name, "Save");
  ASSERT_TRUE(op->GetList("info", &op_args));
  EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(0));

  ASSERT_TRUE(ops.GetDictionary(index++, &op));
  EXPECT_TRUE(op->GetString("cmd_string", &op_name));
  EXPECT_EQ(op_name, "ClipRect");
  ASSERT_TRUE(op->GetList("info", &op_args));
  EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(3));
  EXPECT_TRUE(HasArg(op_args, "rect"));
  EXPECT_TRUE(HasArg(op_args, "op"));
  EXPECT_TRUE(HasArg(op_args, "anti-alias"));

  ASSERT_TRUE(ops.GetDictionary(index++, &op));
  EXPECT_TRUE(op->GetString("cmd_string", &op_name));
  EXPECT_EQ(op_name, "SetMatrix");
  ASSERT_TRUE(op->GetList("info", &op_args));
  EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(1));
  EXPECT_TRUE(HasArg(op_args, "matrix"));

  ASSERT_TRUE(ops.GetDictionary(index++, &op));
  EXPECT_TRUE(op->GetString("cmd_string", &op_name));
  EXPECT_EQ(op_name, "DrawRect");
  ASSERT_TRUE(op->GetList("info", &op_args));
  EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(2));
  EXPECT_TRUE(HasArg(op_args, "rect"));
  EXPECT_TRUE(HasArg(op_args, "paint"));

  ASSERT_TRUE(ops.GetDictionary(index++, &op));
  EXPECT_TRUE(op->GetString("cmd_string", &op_name));
  EXPECT_EQ(op_name, "Restore");
  ASSERT_TRUE(op->GetList("info", &op_args));
  EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(0));
}

} // namespace content