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 virtual void SetUp() OVERRIDE
{
22 impl_proxy_
= make_scoped_ptr(new FakeImplProxy
);
23 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
24 layer_tree_host_impl_
= make_scoped_ptr(new FakeLayerTreeHostImpl(
25 impl_proxy_
.get(), shared_bitmap_manager_
.get()));
27 layer_tree_host_
= FakeLayerTreeHost::Create();
28 layer_tree_host_
->SetRootLayer(Layer::Create());
29 layer_tree_host_
->InitializeForTesting(scoped_ptr
<Proxy
>(new FakeProxy
));
32 virtual void TearDown() OVERRIDE
{
33 layer_tree_host_impl_
.reset();
34 layer_tree_host_
.reset();
38 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
39 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
40 scoped_ptr
<FakeLayerTreeHostImpl
> layer_tree_host_impl_
;
41 scoped_ptr
<FakeImplProxy
> impl_proxy_
;
44 void Noop(scoped_ptr
<base::Value
> value
) {
47 void IncrementCallCount(int* count
, scoped_ptr
<base::Value
> value
) {
51 TEST_F(MicroBenchmarkControllerTest
, ScheduleFail
) {
52 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
53 "non_existant_benchmark", scoped_ptr
<base::Value
>(), base::Bind(&Noop
));
57 TEST_F(MicroBenchmarkControllerTest
, CommitScheduled
) {
58 EXPECT_FALSE(layer_tree_host_
->needs_commit());
59 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
60 "unittest_only_benchmark", scoped_ptr
<base::Value
>(), base::Bind(&Noop
));
62 EXPECT_TRUE(layer_tree_host_
->needs_commit());
65 TEST_F(MicroBenchmarkControllerTest
, BenchmarkRan
) {
67 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
68 "unittest_only_benchmark",
69 scoped_ptr
<base::Value
>(),
70 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
73 scoped_ptr
<ResourceUpdateQueue
> queue(new ResourceUpdateQueue
);
74 layer_tree_host_
->SetOutputSurfaceLostForTesting(false);
75 layer_tree_host_
->UpdateLayers(queue
.get());
77 EXPECT_EQ(1, run_count
);
80 TEST_F(MicroBenchmarkControllerTest
, MultipleBenchmarkRan
) {
82 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
83 "unittest_only_benchmark",
84 scoped_ptr
<base::Value
>(),
85 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
87 id
= layer_tree_host_
->ScheduleMicroBenchmark(
88 "unittest_only_benchmark",
89 scoped_ptr
<base::Value
>(),
90 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
93 scoped_ptr
<ResourceUpdateQueue
> queue(new ResourceUpdateQueue
);
94 layer_tree_host_
->SetOutputSurfaceLostForTesting(false);
95 layer_tree_host_
->UpdateLayers(queue
.get());
97 EXPECT_EQ(2, run_count
);
99 id
= layer_tree_host_
->ScheduleMicroBenchmark(
100 "unittest_only_benchmark",
101 scoped_ptr
<base::Value
>(),
102 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
104 id
= layer_tree_host_
->ScheduleMicroBenchmark(
105 "unittest_only_benchmark",
106 scoped_ptr
<base::Value
>(),
107 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
110 layer_tree_host_
->UpdateLayers(queue
.get());
111 EXPECT_EQ(4, run_count
);
113 layer_tree_host_
->UpdateLayers(queue
.get());
114 EXPECT_EQ(4, run_count
);
117 TEST_F(MicroBenchmarkControllerTest
, BenchmarkImplRan
) {
119 scoped_ptr
<base::DictionaryValue
> settings(new base::DictionaryValue
);
120 settings
->SetBoolean("run_benchmark_impl", true);
122 // Schedule a main thread benchmark.
123 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
124 "unittest_only_benchmark",
125 settings
.PassAs
<base::Value
>(),
126 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
129 // Schedule impl benchmarks. In production code, this is run in commit.
130 layer_tree_host_
->GetMicroBenchmarkController()->ScheduleImplBenchmarks(
131 layer_tree_host_impl_
.get());
133 // Now complete the commit (as if on the impl thread).
134 layer_tree_host_impl_
->CommitComplete();
136 // Make sure all posted messages run.
137 base::MessageLoop::current()->RunUntilIdle();
139 EXPECT_EQ(1, run_count
);
142 TEST_F(MicroBenchmarkControllerTest
, SendMessage
) {
143 // Send valid message to invalid benchmark (id = 0)
144 scoped_ptr
<base::DictionaryValue
> message(new base::DictionaryValue
);
145 message
->SetBoolean("can_handle", true);
146 bool message_handled
= layer_tree_host_
->SendMessageToMicroBenchmark(
147 0, message
.PassAs
<base::Value
>());
148 EXPECT_FALSE(message_handled
);
150 // Schedule a benchmark
152 int id
= layer_tree_host_
->ScheduleMicroBenchmark(
153 "unittest_only_benchmark",
154 scoped_ptr
<base::Value
>(),
155 base::Bind(&IncrementCallCount
, base::Unretained(&run_count
)));
158 // Send valid message to valid benchmark
159 message
= scoped_ptr
<base::DictionaryValue
>(new base::DictionaryValue
);
160 message
->SetBoolean("can_handle", true);
161 message_handled
= layer_tree_host_
->SendMessageToMicroBenchmark(
162 id
, message
.PassAs
<base::Value
>());
163 EXPECT_TRUE(message_handled
);
165 // Send invalid message to valid benchmark
166 message
= scoped_ptr
<base::DictionaryValue
>(new base::DictionaryValue
);
167 message
->SetBoolean("can_handle", false);
168 message_handled
= layer_tree_host_
->SendMessageToMicroBenchmark(
169 id
, message
.PassAs
<base::Value
>());
170 EXPECT_FALSE(message_handled
);