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.
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"
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"
15
16 namespace cc {
17 namespace {
18
19 class MicroBenchmarkControllerTest : public testing::Test {
20 public:
SetUp()21 virtual void SetUp() OVERRIDE {
22 impl_proxy_ = make_scoped_ptr(new FakeImplProxy);
23 layer_tree_host_impl_ =
24 make_scoped_ptr(new FakeLayerTreeHostImpl(impl_proxy_.get()));
25
26 layer_tree_host_ = FakeLayerTreeHost::Create();
27 layer_tree_host_->SetRootLayer(Layer::Create());
28 layer_tree_host_->InitializeForTesting(scoped_ptr<Proxy>(new FakeProxy));
29 }
30
TearDown()31 virtual void TearDown() OVERRIDE {
32 layer_tree_host_impl_.reset();
33 layer_tree_host_.reset();
34 impl_proxy_.reset();
35 }
36
37 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
38 scoped_ptr<FakeLayerTreeHostImpl> layer_tree_host_impl_;
39 scoped_ptr<FakeImplProxy> impl_proxy_;
40 };
41
Noop(scoped_ptr<base::Value> value)42 void Noop(scoped_ptr<base::Value> value) {
43 }
44
IncrementCallCount(int * count,scoped_ptr<base::Value> value)45 void IncrementCallCount(int* count, scoped_ptr<base::Value> value) {
46 ++(*count);
47 }
48
TEST_F(MicroBenchmarkControllerTest,ScheduleFail)49 TEST_F(MicroBenchmarkControllerTest, ScheduleFail) {
50 bool result = layer_tree_host_->ScheduleMicroBenchmark(
51 "non_existant_benchmark", scoped_ptr<base::Value>(), base::Bind(&Noop));
52 EXPECT_FALSE(result);
53 }
54
TEST_F(MicroBenchmarkControllerTest,CommitScheduled)55 TEST_F(MicroBenchmarkControllerTest, CommitScheduled) {
56 EXPECT_FALSE(layer_tree_host_->needs_commit());
57 bool result = layer_tree_host_->ScheduleMicroBenchmark(
58 "unittest_only_benchmark", scoped_ptr<base::Value>(), base::Bind(&Noop));
59 EXPECT_TRUE(result);
60 EXPECT_TRUE(layer_tree_host_->needs_commit());
61 }
62
TEST_F(MicroBenchmarkControllerTest,BenchmarkRan)63 TEST_F(MicroBenchmarkControllerTest, BenchmarkRan) {
64 int run_count = 0;
65 bool result = layer_tree_host_->ScheduleMicroBenchmark(
66 "unittest_only_benchmark",
67 scoped_ptr<base::Value>(),
68 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
69 EXPECT_TRUE(result);
70
71 scoped_ptr<ResourceUpdateQueue> queue(new ResourceUpdateQueue);
72 layer_tree_host_->SetOutputSurfaceLostForTesting(false);
73 layer_tree_host_->UpdateLayers(queue.get());
74
75 EXPECT_EQ(1, run_count);
76 }
77
TEST_F(MicroBenchmarkControllerTest,MultipleBenchmarkRan)78 TEST_F(MicroBenchmarkControllerTest, MultipleBenchmarkRan) {
79 int run_count = 0;
80 bool result = layer_tree_host_->ScheduleMicroBenchmark(
81 "unittest_only_benchmark",
82 scoped_ptr<base::Value>(),
83 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
84 EXPECT_TRUE(result);
85 result = layer_tree_host_->ScheduleMicroBenchmark(
86 "unittest_only_benchmark",
87 scoped_ptr<base::Value>(),
88 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
89 EXPECT_TRUE(result);
90
91 scoped_ptr<ResourceUpdateQueue> queue(new ResourceUpdateQueue);
92 layer_tree_host_->SetOutputSurfaceLostForTesting(false);
93 layer_tree_host_->UpdateLayers(queue.get());
94
95 EXPECT_EQ(2, run_count);
96
97 result = layer_tree_host_->ScheduleMicroBenchmark(
98 "unittest_only_benchmark",
99 scoped_ptr<base::Value>(),
100 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
101 EXPECT_TRUE(result);
102 result = layer_tree_host_->ScheduleMicroBenchmark(
103 "unittest_only_benchmark",
104 scoped_ptr<base::Value>(),
105 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
106 EXPECT_TRUE(result);
107
108 layer_tree_host_->UpdateLayers(queue.get());
109 EXPECT_EQ(4, run_count);
110
111 layer_tree_host_->UpdateLayers(queue.get());
112 EXPECT_EQ(4, run_count);
113 }
114
TEST_F(MicroBenchmarkControllerTest,BenchmarkImplRan)115 TEST_F(MicroBenchmarkControllerTest, BenchmarkImplRan) {
116 int run_count = 0;
117 scoped_ptr<base::DictionaryValue> settings(new base::DictionaryValue);
118 settings->SetBoolean("run_benchmark_impl", true);
119
120 // Schedule a main thread benchmark.
121 bool result = layer_tree_host_->ScheduleMicroBenchmark(
122 "unittest_only_benchmark",
123 settings.PassAs<base::Value>(),
124 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
125 EXPECT_TRUE(result);
126
127 // Schedule impl benchmarks. In production code, this is run in commit.
128 layer_tree_host_->GetMicroBenchmarkController()->ScheduleImplBenchmarks(
129 layer_tree_host_impl_.get());
130
131 // Now complete the commit (as if on the impl thread).
132 layer_tree_host_impl_->CommitComplete();
133
134 // Make sure all posted messages run.
135 base::MessageLoop::current()->RunUntilIdle();
136
137 EXPECT_EQ(1, run_count);
138 }
139
140 } // namespace
141 } // namespace cc
142