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/resources/resource_update_queue.h"
11 #include "cc/test/fake_layer_tree_host.h"
12 #include "cc/test/fake_layer_tree_host_impl.h"
13 #include "cc/test/fake_proxy.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 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
27 layer_tree_host_impl_
= make_scoped_ptr(new FakeLayerTreeHostImpl(
28 impl_proxy_
.get(), shared_bitmap_manager_
.get(), nullptr));
30 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 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
43 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
44 scoped_ptr
<FakeLayerTreeHostImpl
> layer_tree_host_impl_
;
45 scoped_ptr
<FakeImplProxy
> impl_proxy_
;
48 void Noop(scoped_ptr
<base::Value
> value
) {
51 void IncrementCallCount(int* count
, scoped_ptr
<base::Value
> value
) {
55 TEST_F(MicroBenchmarkControllerTest
, ScheduleFail
) {
56 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
57 "non_existant_benchmark", nullptr, base::Bind(&Noop
));
61 TEST_F(MicroBenchmarkControllerTest
, CommitScheduled
) {
62 EXPECT_FALSE(layer_tree_host_
->needs_commit());
63 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
64 "unittest_only_benchmark", nullptr, base::Bind(&Noop
));
66 EXPECT_TRUE(layer_tree_host_
->needs_commit());
69 TEST_F(MicroBenchmarkControllerTest
, BenchmarkRan
) {
71 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
72 "unittest_only_benchmark",
74 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
77 scoped_ptr
<ResourceUpdateQueue
> queue(new ResourceUpdateQueue
);
78 layer_tree_host_
->SetOutputSurfaceLostForTesting(false);
79 layer_tree_host_
->UpdateLayers(queue
.get());
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 scoped_ptr
<ResourceUpdateQueue
> queue(new ResourceUpdateQueue
);
98 layer_tree_host_
->SetOutputSurfaceLostForTesting(false);
99 layer_tree_host_
->UpdateLayers(queue
.get());
101 EXPECT_EQ(2, run_count
);
103 id
= layer_tree_host_
->ScheduleMicroBenchmark(
104 "unittest_only_benchmark",
106 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
108 id
= layer_tree_host_
->ScheduleMicroBenchmark(
109 "unittest_only_benchmark",
111 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
114 layer_tree_host_
->UpdateLayers(queue
.get());
115 EXPECT_EQ(4, run_count
);
117 layer_tree_host_
->UpdateLayers(queue
.get());
118 EXPECT_EQ(4, run_count
);
121 TEST_F(MicroBenchmarkControllerTest
, BenchmarkImplRan
) {
123 scoped_ptr
<base::DictionaryValue
> settings(new base::DictionaryValue
);
124 settings
->SetBoolean("run_benchmark_impl", true);
126 // Schedule a main thread benchmark.
127 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
128 "unittest_only_benchmark",
130 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
133 // Schedule impl benchmarks. In production code, this is run in commit.
134 layer_tree_host_
->GetMicroBenchmarkController()->ScheduleImplBenchmarks(
135 layer_tree_host_impl_
.get());
137 // Now complete the commit (as if on the impl thread).
138 layer_tree_host_impl_
->CommitComplete();
140 // Make sure all posted messages run.
141 base::MessageLoop::current()->RunUntilIdle();
143 EXPECT_EQ(1, run_count
);
146 TEST_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);
150 bool message_handled
=
151 layer_tree_host_
->SendMessageToMicroBenchmark(0, message
.Pass());
152 EXPECT_FALSE(message_handled
);
154 // Schedule a benchmark
156 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
157 "unittest_only_benchmark",
159 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
162 // Send valid message to valid benchmark
163 message
= make_scoped_ptr(new base::DictionaryValue
);
164 message
->SetBoolean("can_handle", true);
166 layer_tree_host_
->SendMessageToMicroBenchmark(id
, message
.Pass());
167 EXPECT_TRUE(message_handled
);
169 // Send invalid message to valid benchmark
170 message
= make_scoped_ptr(new base::DictionaryValue
);
171 message
->SetBoolean("can_handle", false);
173 layer_tree_host_
->SendMessageToMicroBenchmark(id
, message
.Pass());
174 EXPECT_FALSE(message_handled
);