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 16namespace cc { 17namespace { 18 19class MicroBenchmarkControllerTest : public testing::Test { 20 public: 21 MicroBenchmarkControllerTest() 22 : layer_tree_host_client_(FakeLayerTreeHostClient::DIRECT_3D) {} 23 24 virtual 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())); 29 30 layer_tree_host_ = FakeLayerTreeHost::Create(&layer_tree_host_client_); 31 layer_tree_host_->SetRootLayer(Layer::Create()); 32 layer_tree_host_->InitializeForTesting(scoped_ptr<Proxy>(new FakeProxy)); 33 } 34 35 virtual void TearDown() OVERRIDE { 36 layer_tree_host_impl_.reset(); 37 layer_tree_host_.reset(); 38 impl_proxy_.reset(); 39 } 40 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_; 46}; 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) { 56 int id = layer_tree_host_->ScheduleMicroBenchmark( 57 "non_existant_benchmark", scoped_ptr<base::Value>(), base::Bind(&Noop)); 58 EXPECT_EQ(id, 0); 59} 60 61TEST_F(MicroBenchmarkControllerTest, CommitScheduled) { 62 EXPECT_FALSE(layer_tree_host_->needs_commit()); 63 int id = layer_tree_host_->ScheduleMicroBenchmark( 64 "unittest_only_benchmark", scoped_ptr<base::Value>(), base::Bind(&Noop)); 65 EXPECT_GT(id, 0); 66 EXPECT_TRUE(layer_tree_host_->needs_commit()); 67} 68 69TEST_F(MicroBenchmarkControllerTest, BenchmarkRan) { 70 int run_count = 0; 71 int id = layer_tree_host_->ScheduleMicroBenchmark( 72 "unittest_only_benchmark", 73 scoped_ptr<base::Value>(), 74 base::Bind(&IncrementCallCount, base::Unretained(&run_count))); 75 EXPECT_GT(id, 0); 76 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; 86 int id = layer_tree_host_->ScheduleMicroBenchmark( 87 "unittest_only_benchmark", 88 scoped_ptr<base::Value>(), 89 base::Bind(&IncrementCallCount, base::Unretained(&run_count))); 90 EXPECT_GT(id, 0); 91 id = layer_tree_host_->ScheduleMicroBenchmark( 92 "unittest_only_benchmark", 93 scoped_ptr<base::Value>(), 94 base::Bind(&IncrementCallCount, base::Unretained(&run_count))); 95 EXPECT_GT(id, 0); 96 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 103 id = layer_tree_host_->ScheduleMicroBenchmark( 104 "unittest_only_benchmark", 105 scoped_ptr<base::Value>(), 106 base::Bind(&IncrementCallCount, base::Unretained(&run_count))); 107 EXPECT_GT(id, 0); 108 id = layer_tree_host_->ScheduleMicroBenchmark( 109 "unittest_only_benchmark", 110 scoped_ptr<base::Value>(), 111 base::Bind(&IncrementCallCount, base::Unretained(&run_count))); 112 EXPECT_GT(id, 0); 113 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 121TEST_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. 127 int id = layer_tree_host_->ScheduleMicroBenchmark( 128 "unittest_only_benchmark", 129 settings.PassAs<base::Value>(), 130 base::Bind(&IncrementCallCount, base::Unretained(&run_count))); 131 EXPECT_GT(id, 0); 132 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 146TEST_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 = layer_tree_host_->SendMessageToMicroBenchmark( 151 0, message.PassAs<base::Value>()); 152 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 163 message = scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); 164 message->SetBoolean("can_handle", true); 165 message_handled = layer_tree_host_->SendMessageToMicroBenchmark( 166 id, message.PassAs<base::Value>()); 167 EXPECT_TRUE(message_handled); 168 169 // Send invalid message to valid benchmark 170 message = scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); 171 message->SetBoolean("can_handle", false); 172 message_handled = layer_tree_host_->SendMessageToMicroBenchmark( 173 id, message.PassAs<base::Value>()); 174 EXPECT_FALSE(message_handled); 175} 176 177} // namespace 178} // namespace cc 179