blob: 527e85034deb017ba8ead253813140ef17034d42 [file] [log] [blame]
[email protected]666d7cf2013-10-12 01:30:291// Copyright 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 "base/callback.h"
6#include "base/memory/scoped_ptr.h"
7#include "cc/debug/micro_benchmark.h"
8#include "cc/debug/micro_benchmark_controller.h"
9#include "cc/layers/layer.h"
10#include "cc/resources/resource_update_queue.h"
11#include "cc/test/fake_layer_tree_host.h"
[email protected]5e5648a2013-11-18 00:39:3312#include "cc/test/fake_layer_tree_host_impl.h"
[email protected]666d7cf2013-10-12 01:30:2913#include "cc/test/fake_proxy.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace cc {
17namespace {
18
19class MicroBenchmarkControllerTest : public testing::Test {
20 public:
enne2097cab2014-09-25 20:16:3121 MicroBenchmarkControllerTest()
22 : layer_tree_host_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
23
mostynbf68776d82014-10-06 18:07:3724 virtual void SetUp() override {
[email protected]5e5648a2013-11-18 00:39:3325 impl_proxy_ = make_scoped_ptr(new FakeImplProxy);
[email protected]4e2eb352014-03-20 17:25:4526 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
27 layer_tree_host_impl_ = make_scoped_ptr(new FakeLayerTreeHostImpl(
28 impl_proxy_.get(), shared_bitmap_manager_.get()));
[email protected]5e5648a2013-11-18 00:39:3329
enne2097cab2014-09-25 20:16:3130 layer_tree_host_ = FakeLayerTreeHost::Create(&layer_tree_host_client_);
[email protected]666d7cf2013-10-12 01:30:2931 layer_tree_host_->SetRootLayer(Layer::Create());
[email protected]5e5648a2013-11-18 00:39:3332 layer_tree_host_->InitializeForTesting(scoped_ptr<Proxy>(new FakeProxy));
33 }
34
mostynbf68776d82014-10-06 18:07:3735 virtual void TearDown() override {
danakjf446a072014-09-27 21:55:4836 layer_tree_host_impl_ = nullptr;
37 layer_tree_host_ = nullptr;
38 impl_proxy_ = nullptr;
[email protected]666d7cf2013-10-12 01:30:2939 }
40
enne2097cab2014-09-25 20:16:3141 FakeLayerTreeHostClient layer_tree_host_client_;
[email protected]666d7cf2013-10-12 01:30:2942 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
[email protected]4e2eb352014-03-20 17:25:4543 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
[email protected]5e5648a2013-11-18 00:39:3344 scoped_ptr<FakeLayerTreeHostImpl> layer_tree_host_impl_;
45 scoped_ptr<FakeImplProxy> impl_proxy_;
[email protected]666d7cf2013-10-12 01:30:2946};
47
48void Noop(scoped_ptr<base::Value> value) {
49}
50
51void IncrementCallCount(int* count, scoped_ptr<base::Value> value) {
52 ++(*count);
53}
54
55TEST_F(MicroBenchmarkControllerTest, ScheduleFail) {
[email protected]67e703e12014-05-30 05:31:5156 int id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]752e2cf2013-10-21 21:41:1557 "non_existant_benchmark", scoped_ptr<base::Value>(), base::Bind(&Noop));
[email protected]67e703e12014-05-30 05:31:5158 EXPECT_EQ(id, 0);
[email protected]666d7cf2013-10-12 01:30:2959}
60
61TEST_F(MicroBenchmarkControllerTest, CommitScheduled) {
62 EXPECT_FALSE(layer_tree_host_->needs_commit());
[email protected]67e703e12014-05-30 05:31:5163 int id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]752e2cf2013-10-21 21:41:1564 "unittest_only_benchmark", scoped_ptr<base::Value>(), base::Bind(&Noop));
[email protected]67e703e12014-05-30 05:31:5165 EXPECT_GT(id, 0);
[email protected]666d7cf2013-10-12 01:30:2966 EXPECT_TRUE(layer_tree_host_->needs_commit());
67}
68
69TEST_F(MicroBenchmarkControllerTest, BenchmarkRan) {
70 int run_count = 0;
[email protected]67e703e12014-05-30 05:31:5171 int id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:2972 "unittest_only_benchmark",
[email protected]752e2cf2013-10-21 21:41:1573 scoped_ptr<base::Value>(),
[email protected]666d7cf2013-10-12 01:30:2974 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:5175 EXPECT_GT(id, 0);
[email protected]666d7cf2013-10-12 01:30:2976
77 scoped_ptr<ResourceUpdateQueue> queue(new ResourceUpdateQueue);
78 layer_tree_host_->SetOutputSurfaceLostForTesting(false);
79 layer_tree_host_->UpdateLayers(queue.get());
80
81 EXPECT_EQ(1, run_count);
82}
83
84TEST_F(MicroBenchmarkControllerTest, MultipleBenchmarkRan) {
85 int run_count = 0;
[email protected]67e703e12014-05-30 05:31:5186 int id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:2987 "unittest_only_benchmark",
[email protected]752e2cf2013-10-21 21:41:1588 scoped_ptr<base::Value>(),
[email protected]666d7cf2013-10-12 01:30:2989 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:5190 EXPECT_GT(id, 0);
91 id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:2992 "unittest_only_benchmark",
[email protected]752e2cf2013-10-21 21:41:1593 scoped_ptr<base::Value>(),
[email protected]666d7cf2013-10-12 01:30:2994 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:5195 EXPECT_GT(id, 0);
[email protected]666d7cf2013-10-12 01:30:2996
97 scoped_ptr<ResourceUpdateQueue> queue(new ResourceUpdateQueue);
98 layer_tree_host_->SetOutputSurfaceLostForTesting(false);
99 layer_tree_host_->UpdateLayers(queue.get());
100
101 EXPECT_EQ(2, run_count);
102
[email protected]67e703e12014-05-30 05:31:51103 id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:29104 "unittest_only_benchmark",
[email protected]752e2cf2013-10-21 21:41:15105 scoped_ptr<base::Value>(),
[email protected]666d7cf2013-10-12 01:30:29106 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:51107 EXPECT_GT(id, 0);
108 id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:29109 "unittest_only_benchmark",
[email protected]752e2cf2013-10-21 21:41:15110 scoped_ptr<base::Value>(),
[email protected]666d7cf2013-10-12 01:30:29111 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:51112 EXPECT_GT(id, 0);
[email protected]666d7cf2013-10-12 01:30:29113
114 layer_tree_host_->UpdateLayers(queue.get());
115 EXPECT_EQ(4, run_count);
116
117 layer_tree_host_->UpdateLayers(queue.get());
118 EXPECT_EQ(4, run_count);
119}
120
[email protected]5e5648a2013-11-18 00:39:33121TEST_F(MicroBenchmarkControllerTest, BenchmarkImplRan) {
122 int run_count = 0;
123 scoped_ptr<base::DictionaryValue> settings(new base::DictionaryValue);
124 settings->SetBoolean("run_benchmark_impl", true);
125
126 // Schedule a main thread benchmark.
[email protected]67e703e12014-05-30 05:31:51127 int id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]5e5648a2013-11-18 00:39:33128 "unittest_only_benchmark",
danakjf446a072014-09-27 21:55:48129 settings.Pass(),
[email protected]5e5648a2013-11-18 00:39:33130 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:51131 EXPECT_GT(id, 0);
[email protected]5e5648a2013-11-18 00:39:33132
133 // Schedule impl benchmarks. In production code, this is run in commit.
134 layer_tree_host_->GetMicroBenchmarkController()->ScheduleImplBenchmarks(
135 layer_tree_host_impl_.get());
136
137 // Now complete the commit (as if on the impl thread).
138 layer_tree_host_impl_->CommitComplete();
139
140 // Make sure all posted messages run.
141 base::MessageLoop::current()->RunUntilIdle();
142
143 EXPECT_EQ(1, run_count);
144}
145
[email protected]67e703e12014-05-30 05:31:51146TEST_F(MicroBenchmarkControllerTest, SendMessage) {
147 // Send valid message to invalid benchmark (id = 0)
148 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue);
149 message->SetBoolean("can_handle", true);
danakjf446a072014-09-27 21:55:48150 bool message_handled =
151 layer_tree_host_->SendMessageToMicroBenchmark(0, message.Pass());
[email protected]67e703e12014-05-30 05:31:51152 EXPECT_FALSE(message_handled);
153
154 // Schedule a benchmark
155 int run_count = 0;
156 int id = layer_tree_host_->ScheduleMicroBenchmark(
157 "unittest_only_benchmark",
158 scoped_ptr<base::Value>(),
159 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
160 EXPECT_GT(id, 0);
161
162 // Send valid message to valid benchmark
danakjf446a072014-09-27 21:55:48163 message = make_scoped_ptr(new base::DictionaryValue);
[email protected]67e703e12014-05-30 05:31:51164 message->SetBoolean("can_handle", true);
danakjf446a072014-09-27 21:55:48165 message_handled =
166 layer_tree_host_->SendMessageToMicroBenchmark(id, message.Pass());
[email protected]67e703e12014-05-30 05:31:51167 EXPECT_TRUE(message_handled);
168
169 // Send invalid message to valid benchmark
danakjf446a072014-09-27 21:55:48170 message = make_scoped_ptr(new base::DictionaryValue);
[email protected]67e703e12014-05-30 05:31:51171 message->SetBoolean("can_handle", false);
danakjf446a072014-09-27 21:55:48172 message_handled =
173 layer_tree_host_->SendMessageToMicroBenchmark(id, message.Pass());
[email protected]67e703e12014-05-30 05:31:51174 EXPECT_FALSE(message_handled);
175}
176
[email protected]666d7cf2013-10-12 01:30:29177} // namespace
178} // namespace cc