Add test for clicking bookmark star in presence of ctrl-D keybinding
[chromium-blink-merge.git] / cc / layers / surface_layer_unittest.cc
blob285acee2e122287d9a3c0d7b9d2ad6ca6b626587
1 // Copyright 2014 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 <set>
6 #include <vector>
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "cc/layers/solid_color_layer.h"
10 #include "cc/layers/surface_layer.h"
11 #include "cc/test/fake_impl_proxy.h"
12 #include "cc/test/fake_layer_tree_host.h"
13 #include "cc/test/fake_layer_tree_host_client.h"
14 #include "cc/test/fake_layer_tree_host_impl.h"
15 #include "cc/test/fake_output_surface.h"
16 #include "cc/test/layer_tree_test.h"
17 #include "cc/test/test_shared_bitmap_manager.h"
18 #include "cc/trees/layer_tree_host.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace cc {
23 namespace {
25 class SurfaceLayerTest : public testing::Test {
26 public:
27 SurfaceLayerTest()
28 : fake_client_(
29 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {}
31 protected:
32 void SetUp() override {
33 layer_tree_host_ = FakeLayerTreeHost::Create(&fake_client_);
34 layer_tree_host_->SetViewportSize(gfx::Size(10, 10));
37 void TearDown() override {
38 if (layer_tree_host_) {
39 layer_tree_host_->SetRootLayer(nullptr);
40 layer_tree_host_ = nullptr;
44 scoped_ptr<FakeLayerTreeHost> layer_tree_host_;
45 FakeLayerTreeHostClient fake_client_;
46 TestSharedBitmapManager shared_bitmap_manager_;
49 void SatisfyCallback(SurfaceSequence* out, SurfaceSequence in) {
50 *out = in;
53 void RequireCallback(SurfaceId* out_id,
54 std::set<SurfaceSequence>* out,
55 SurfaceId in_id,
56 SurfaceSequence in) {
57 *out_id = in_id;
58 out->insert(in);
61 // Check that one surface can be referenced by multiple LayerTreeHosts, and
62 // each will create its own SurfaceSequence that's satisfied on destruction.
63 TEST_F(SurfaceLayerTest, MultipleFramesOneSurface) {
64 SurfaceSequence blank_change; // Receives sequence if commit doesn't happen.
66 SurfaceId required_id;
67 std::set<SurfaceSequence> required_seq;
68 scoped_refptr<SurfaceLayer> layer(SurfaceLayer::Create(
69 base::Bind(&SatisfyCallback, &blank_change),
70 base::Bind(&RequireCallback, &required_id, &required_seq)));
71 layer->SetSurfaceId(SurfaceId(1), 1.f, gfx::Size(1, 1));
72 layer_tree_host_->set_surface_id_namespace(1);
73 layer_tree_host_->SetRootLayer(layer);
75 scoped_ptr<FakeLayerTreeHost> layer_tree_host2 =
76 FakeLayerTreeHost::Create(&fake_client_);
77 scoped_refptr<SurfaceLayer> layer2(SurfaceLayer::Create(
78 base::Bind(&SatisfyCallback, &blank_change),
79 base::Bind(&RequireCallback, &required_id, &required_seq)));
80 layer2->SetSurfaceId(SurfaceId(1), 1.f, gfx::Size(1, 1));
81 layer_tree_host2->set_surface_id_namespace(2);
82 layer_tree_host2->SetRootLayer(layer2);
84 // Layers haven't been removed, so no sequence should be satisfied.
85 EXPECT_TRUE(blank_change.is_null());
87 SurfaceSequence expected1(1u, 1u);
88 SurfaceSequence expected2(2u, 1u);
90 layer_tree_host2->SetRootLayer(nullptr);
91 layer_tree_host2.reset();
93 // Layer was removed so sequence from second LayerTreeHost should be
94 // satisfied.
95 EXPECT_TRUE(blank_change == expected2);
97 // Set of sequences that need to be satisfied should include sequences from
98 // both trees.
99 EXPECT_TRUE(required_id == SurfaceId(1));
100 EXPECT_EQ(2u, required_seq.size());
101 EXPECT_TRUE(required_seq.count(expected1));
102 EXPECT_TRUE(required_seq.count(expected2));
104 layer_tree_host_->SetRootLayer(nullptr);
105 layer_tree_host_.reset();
107 // Layer was removed so sequence from first LayerTreeHost should be
108 // satisfied.
109 EXPECT_TRUE(blank_change == expected1);
111 // No more SurfaceSequences should have been generated that need to have be
112 // satisfied.
113 EXPECT_EQ(2u, required_seq.size());
116 static void CalcDrawProps(FakeLayerTreeHost* host, float device_scale_factor) {
117 RenderSurfaceLayerList render_surface_layer_list;
118 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
119 host->root_layer(), gfx::Size(500, 500), &render_surface_layer_list);
120 inputs.device_scale_factor = device_scale_factor;
121 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
124 // Check that setting content scale on the surface works.
125 TEST_F(SurfaceLayerTest, ScaleSurface) {
126 SurfaceSequence blank_change;
127 SurfaceId required_id;
128 std::set<SurfaceSequence> required_seq;
129 scoped_refptr<SurfaceLayer> layer(SurfaceLayer::Create(
130 base::Bind(&SatisfyCallback, &blank_change),
131 base::Bind(&RequireCallback, &required_id, &required_seq)));
132 gfx::Size surface_size(10, 15);
133 layer->SetSurfaceId(SurfaceId(1), 2.f, surface_size);
134 layer->SetBounds(gfx::Size(25, 45));
135 layer_tree_host_->SetRootLayer(layer);
137 CalcDrawProps(layer_tree_host_.get(), 5.f);
138 EXPECT_EQ(2.f, layer->contents_scale_x());
139 EXPECT_EQ(2.f, layer->contents_scale_y());
140 EXPECT_EQ(surface_size.ToString(), layer->content_bounds().ToString());
142 layer_tree_host_->SetRootLayer(nullptr);
143 layer_tree_host_.reset();
146 // Check that SurfaceSequence is sent through swap promise.
147 class SurfaceLayerSwapPromise : public LayerTreeTest {
148 public:
149 SurfaceLayerSwapPromise()
150 : commit_count_(0), sequence_was_satisfied_(false) {}
152 void BeginTest() override {
153 layer_tree_host()->set_surface_id_namespace(1);
154 layer_ = SurfaceLayer::Create(
155 base::Bind(&SatisfyCallback, &satisfied_sequence_),
156 base::Bind(&RequireCallback, &required_id_, &required_set_));
157 layer_->SetSurfaceId(SurfaceId(1), 1.f, gfx::Size(1, 1));
159 // Layer hasn't been added to tree so no SurfaceSequence generated yet.
160 EXPECT_EQ(0u, required_set_.size());
162 layer_tree_host()->SetRootLayer(layer_);
164 // Should have SurfaceSequence from first tree.
165 SurfaceSequence expected(1u, 1u);
166 EXPECT_TRUE(required_id_ == SurfaceId(1));
167 EXPECT_EQ(1u, required_set_.size());
168 EXPECT_TRUE(required_set_.count(expected));
170 gfx::Size bounds(100, 100);
171 layer_tree_host()->SetViewportSize(bounds);
172 PostSetNeedsCommitToMainThread();
175 void DidCommitAndDrawFrame() override {
176 base::MessageLoopProxy::current()->PostTask(
177 FROM_HERE, base::Bind(&SurfaceLayerSwapPromise::ChangeTree,
178 base::Unretained(this)));
181 void ChangeTree() {
182 ++commit_count_;
183 switch (commit_count_) {
184 case 1:
185 // Remove SurfaceLayer from tree to cause SwapPromise to be created.
186 blank_layer_ = SolidColorLayer::Create();
187 blank_layer_->SetIsDrawable(true);
188 blank_layer_->SetBounds(gfx::Size(10, 10));
189 layer_tree_host()->SetRootLayer(blank_layer_);
190 break;
191 case 2:
192 break;
193 default:
194 NOTREACHED();
195 break;
199 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
200 EXPECT_TRUE(result);
201 std::vector<uint32_t>& satisfied =
202 output_surface()->last_sent_frame().metadata.satisfies_sequences;
203 EXPECT_LE(satisfied.size(), 1u);
204 if (satisfied.size() == 1) {
205 // Eventually the one SurfaceSequence should be satisfied, but only
206 // after the layer was removed from the tree, and only once.
207 EXPECT_EQ(1u, satisfied[0]);
208 EXPECT_LE(1, commit_count_);
209 EXPECT_FALSE(sequence_was_satisfied_);
210 sequence_was_satisfied_ = true;
211 EndTest();
215 void AfterTest() override {
216 EXPECT_TRUE(required_id_ == SurfaceId(1));
217 EXPECT_EQ(1u, required_set_.size());
218 // Sequence should have been satisfied through Swap, not with the
219 // callback.
220 EXPECT_TRUE(satisfied_sequence_.is_null());
223 private:
224 int commit_count_;
225 bool sequence_was_satisfied_;
226 scoped_refptr<SurfaceLayer> layer_;
227 scoped_refptr<Layer> blank_layer_;
228 SurfaceSequence satisfied_sequence_;
230 SurfaceId required_id_;
231 std::set<SurfaceSequence> required_set_;
234 // TODO(jbauman): Reenable on single thread once http://crbug.com/421923 is
235 // fixed.
236 MULTI_THREAD_TEST_F(SurfaceLayerSwapPromise);
238 } // namespace
239 } // namespace cc