1 // 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.
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/test/fake_layer_tree_host.h"
11 #include "cc/test/fake_layer_tree_host_impl.h"
12 #include "cc/test/fake_proxy.h"
13 #include "cc/test/test_task_graph_runner.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 class MicroBenchmarkControllerTest
: public testing::Test
{
21 MicroBenchmarkControllerTest()
22 : layer_tree_host_client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
24 void SetUp() override
{
25 impl_proxy_
= make_scoped_ptr(new FakeImplProxy
);
26 layer_tree_host_impl_
= make_scoped_ptr(new FakeLayerTreeHostImpl(
27 impl_proxy_
.get(), &shared_bitmap_manager_
, &task_graph_runner_
));
29 layer_tree_host_
= FakeLayerTreeHost::Create(&layer_tree_host_client_
,
31 layer_tree_host_
->SetRootLayer(Layer::Create(LayerSettings()));
32 layer_tree_host_
->InitializeForTesting(scoped_ptr
<Proxy
>(new FakeProxy
));
35 void TearDown() override
{
36 layer_tree_host_impl_
= nullptr;
37 layer_tree_host_
= nullptr;
38 impl_proxy_
= nullptr;
41 FakeLayerTreeHostClient layer_tree_host_client_
;
42 TestTaskGraphRunner task_graph_runner_
;
43 TestSharedBitmapManager shared_bitmap_manager_
;
44 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
45 scoped_ptr
<FakeLayerTreeHostImpl
> layer_tree_host_impl_
;
46 scoped_ptr
<FakeImplProxy
> impl_proxy_
;
49 void Noop(scoped_ptr
<base::Value
> value
) {
52 void IncrementCallCount(int* count
, scoped_ptr
<base::Value
> value
) {
56 TEST_F(MicroBenchmarkControllerTest
, ScheduleFail
) {
57 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
58 "non_existant_benchmark", nullptr, base::Bind(&Noop
));
62 TEST_F(MicroBenchmarkControllerTest
, CommitScheduled
) {
63 EXPECT_FALSE(layer_tree_host_
->needs_commit());
64 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
65 "unittest_only_benchmark", nullptr, base::Bind(&Noop
));
67 EXPECT_TRUE(layer_tree_host_
->needs_commit());
70 TEST_F(MicroBenchmarkControllerTest
, BenchmarkRan
) {
72 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
73 "unittest_only_benchmark",
75 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
78 layer_tree_host_
->SetOutputSurfaceLostForTesting(false);
79 layer_tree_host_
->UpdateLayers();
81 EXPECT_EQ(1, run_count
);
84 TEST_F(MicroBenchmarkControllerTest
, MultipleBenchmarkRan
) {
86 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
87 "unittest_only_benchmark",
89 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
91 id
= layer_tree_host_
->ScheduleMicroBenchmark(
92 "unittest_only_benchmark",
94 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
97 layer_tree_host_
->SetOutputSurfaceLostForTesting(false);
98 layer_tree_host_
->UpdateLayers();
100 EXPECT_EQ(2, run_count
);
102 id
= layer_tree_host_
->ScheduleMicroBenchmark(
103 "unittest_only_benchmark",
105 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
107 id
= layer_tree_host_
->ScheduleMicroBenchmark(
108 "unittest_only_benchmark",
110 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
113 layer_tree_host_
->UpdateLayers();
114 EXPECT_EQ(4, run_count
);
116 layer_tree_host_
->UpdateLayers();
117 EXPECT_EQ(4, run_count
);
120 TEST_F(MicroBenchmarkControllerTest
, BenchmarkImplRan
) {
122 scoped_ptr
<base::DictionaryValue
> settings(new base::DictionaryValue
);
123 settings
->SetBoolean("run_benchmark_impl", true);
125 // Schedule a main thread benchmark.
126 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
127 "unittest_only_benchmark",
129 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
132 // Schedule impl benchmarks. In production code, this is run in commit.
133 layer_tree_host_
->GetMicroBenchmarkController()->ScheduleImplBenchmarks(
134 layer_tree_host_impl_
.get());
136 // Now complete the commit (as if on the impl thread).
137 layer_tree_host_impl_
->CommitComplete();
139 // Make sure all posted messages run.
140 base::MessageLoop::current()->RunUntilIdle();
142 EXPECT_EQ(1, run_count
);
145 TEST_F(MicroBenchmarkControllerTest
, SendMessage
) {
146 // Send valid message to invalid benchmark (id = 0)
147 scoped_ptr
<base::DictionaryValue
> message(new base::DictionaryValue
);
148 message
->SetBoolean("can_handle", true);
149 bool message_handled
=
150 layer_tree_host_
->SendMessageToMicroBenchmark(0, message
.Pass());
151 EXPECT_FALSE(message_handled
);
153 // Schedule a benchmark
155 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
156 "unittest_only_benchmark",
158 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
161 // Send valid message to valid benchmark
162 message
= make_scoped_ptr(new base::DictionaryValue
);
163 message
->SetBoolean("can_handle", true);
165 layer_tree_host_
->SendMessageToMicroBenchmark(id
, message
.Pass());
166 EXPECT_TRUE(message_handled
);
168 // Send invalid message to valid benchmark
169 message
= make_scoped_ptr(new base::DictionaryValue
);
170 message
->SetBoolean("can_handle", false);
172 layer_tree_host_
->SendMessageToMicroBenchmark(id
, message
.Pass());
173 EXPECT_FALSE(message_handled
);