blob: 87ea6a0d2979e73d5b3453b5ba56f9ac7c7f6273 [file] [log] [blame]
[email protected]cb9118b32013-06-26 14:15:071// 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
avi1023d012015-12-25 02:39:147#include <stddef.h>
8
fmalitaf5190e02015-02-26 15:48:429#include "base/values.h"
10#include "skia/ext/benchmarking_canvas.h"
[email protected]cb9118b32013-06-26 14:15:0711#include "testing/gtest/include/gtest/gtest.h"
12#include "third_party/skia/include/core/SkCanvas.h"
13#include "third_party/skia/include/core/SkGraphics.h"
[email protected]cb9118b32013-06-26 14:15:0714
15namespace {
16
fmalitaf5190e02015-02-26 15:48:4217testing::AssertionResult HasArg(const base::ListValue* args,
18 const char name[]) {
19 const base::DictionaryValue* arg;
[email protected]cb9118b32013-06-26 14:15:0720
fmalitaf5190e02015-02-26 15:48:4221 for (size_t i = 0; i < args->GetSize(); ++i) {
22 if (!args->GetDictionary(i, &arg) || arg->size() != 1)
23 return testing::AssertionFailure() << " malformed argument for index "
24 << i;
[email protected]cb9118b32013-06-26 14:15:0725
fmalitaf5190e02015-02-26 15:48:4226 if (arg->HasKey(name))
27 return testing::AssertionSuccess() << " argument '" << name
28 << "' found at index " << i;
[email protected]cb9118b32013-06-26 14:15:0729 }
30
fmalitaf5190e02015-02-26 15:48:4231 return testing::AssertionFailure() << "argument not found: '" << name << "'";
[email protected]cb9118b32013-06-26 14:15:0732}
33
34}
35
36namespace content {
37
fmalitaf5190e02015-02-26 15:48:4238TEST(SkiaBenchmarkingExtensionTest, BenchmarkingCanvas) {
[email protected]cb9118b32013-06-26 14:15:0739 SkGraphics::Init();
40
41 // Prepare canvas and resources.
fmalitaf5190e02015-02-26 15:48:4242 SkCanvas canvas(100, 100);
43 skia::BenchmarkingCanvas benchmarking_canvas(&canvas);
44
[email protected]cb9118b32013-06-26 14:15:0745 SkPaint red_paint;
46 red_paint.setColor(SkColorSetARGB(255, 255, 0, 0));
47 SkRect fullRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
48 SkRect fillRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25),
49 SkIntToScalar(50), SkIntToScalar(50));
50
robertphillipse83e75c2015-02-12 19:40:5151 SkMatrix trans;
52 trans.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
53
[email protected]cb9118b32013-06-26 14:15:0754 // Draw a trivial scene.
fmalitaf5190e02015-02-26 15:48:4255 benchmarking_canvas.save();
56 benchmarking_canvas.clipRect(fullRect, SkRegion::kIntersect_Op, false);
57 benchmarking_canvas.setMatrix(trans);
58 benchmarking_canvas.drawRect(fillRect, red_paint);
59 benchmarking_canvas.restore();
[email protected]cb9118b32013-06-26 14:15:0760
61 // Verify the recorded commands.
fmalitaf5190e02015-02-26 15:48:4262 const base::ListValue& ops = benchmarking_canvas.Commands();
63 ASSERT_EQ(ops.GetSize(), static_cast<size_t>(5));
[email protected]cb9118b32013-06-26 14:15:0764
fmalitaf5190e02015-02-26 15:48:4265 size_t index = 0;
66 const base::DictionaryValue* op;
67 const base::ListValue* op_args;
68 std::string op_name;
[email protected]cb9118b32013-06-26 14:15:0769
fmalitaf5190e02015-02-26 15:48:4270 ASSERT_TRUE(ops.GetDictionary(index++, &op));
71 EXPECT_TRUE(op->GetString("cmd_string", &op_name));
72 EXPECT_EQ(op_name, "Save");
73 ASSERT_TRUE(op->GetList("info", &op_args));
74 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(0));
[email protected]cb9118b32013-06-26 14:15:0775
fmalitaf5190e02015-02-26 15:48:4276 ASSERT_TRUE(ops.GetDictionary(index++, &op));
77 EXPECT_TRUE(op->GetString("cmd_string", &op_name));
78 EXPECT_EQ(op_name, "ClipRect");
79 ASSERT_TRUE(op->GetList("info", &op_args));
80 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(3));
81 EXPECT_TRUE(HasArg(op_args, "rect"));
82 EXPECT_TRUE(HasArg(op_args, "op"));
83 EXPECT_TRUE(HasArg(op_args, "anti-alias"));
[email protected]cb9118b32013-06-26 14:15:0784
fmalitaf5190e02015-02-26 15:48:4285 ASSERT_TRUE(ops.GetDictionary(index++, &op));
86 EXPECT_TRUE(op->GetString("cmd_string", &op_name));
87 EXPECT_EQ(op_name, "SetMatrix");
88 ASSERT_TRUE(op->GetList("info", &op_args));
89 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(1));
90 EXPECT_TRUE(HasArg(op_args, "matrix"));
[email protected]cb9118b32013-06-26 14:15:0791
fmalitaf5190e02015-02-26 15:48:4292 ASSERT_TRUE(ops.GetDictionary(index++, &op));
93 EXPECT_TRUE(op->GetString("cmd_string", &op_name));
94 EXPECT_EQ(op_name, "DrawRect");
95 ASSERT_TRUE(op->GetList("info", &op_args));
96 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(2));
97 EXPECT_TRUE(HasArg(op_args, "rect"));
98 EXPECT_TRUE(HasArg(op_args, "paint"));
99
100 ASSERT_TRUE(ops.GetDictionary(index++, &op));
101 EXPECT_TRUE(op->GetString("cmd_string", &op_name));
102 EXPECT_EQ(op_name, "Restore");
103 ASSERT_TRUE(op->GetList("info", &op_args));
104 EXPECT_EQ(op_args->GetSize(), static_cast<size_t>(0));
[email protected]cb9118b32013-06-26 14:15:07105}
106
107} // namespace content