Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/CompositorMutatorClientTest.cpp

Issue 2765053002: Avoid exposing cc::Layer tree to CompositorProxy (Closed)
Patch Set: Rebase onto blink reformat Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "platform/graphics/CompositorMutatorClient.h" 5 #include "platform/graphics/CompositorMutatorClient.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/time/time.h"
11 #include "cc/test/fake_compositor_frame_sink.h"
12 #include "cc/test/fake_impl_task_runner_provider.h"
13 #include "cc/test/fake_layer_tree_host_impl.h"
14 #include "cc/test/test_shared_bitmap_manager.h"
15 #include "cc/test/test_task_graph_runner.h"
16 #include "cc/trees/layer_tree_host_impl.h"
17 #include "cc/trees/layer_tree_impl.h"
18 #include "platform/graphics/CompositorElementId.h"
19 #include "platform/graphics/CompositorMutableState.h"
20 #include "platform/graphics/CompositorMutableStateProvider.h"
9 #include "platform/graphics/CompositorMutation.h" 21 #include "platform/graphics/CompositorMutation.h"
10 #include "platform/graphics/CompositorMutationsTarget.h" 22 #include "platform/graphics/CompositorMutationsTarget.h"
11 #include "platform/graphics/CompositorMutator.h" 23 #include "platform/graphics/CompositorMutator.h"
12 #include "platform/wtf/PtrUtil.h" 24 #include "platform/wtf/PtrUtil.h"
13 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
15 27
16 using ::testing::_; 28 using ::testing::_;
17 29
18 namespace blink { 30 namespace blink {
19 namespace { 31 namespace {
20 32
21 class StubCompositorMutator : public CompositorMutator { 33 class StubCompositorMutator : public CompositorMutator {
22 public: 34 public:
23 StubCompositorMutator() {} 35 StubCompositorMutator() {}
24 36
25 bool Mutate(double monotonic_time_now, 37 bool Mutate(double monotonic_time_now,
26 CompositorMutableStateProvider* state_provider) override { 38 CompositorMutableStateProvider* state_provider) override {
27 return false; 39 return false;
28 } 40 }
29 }; 41 };
30 42
31 class MockCompositoMutationsTarget : public CompositorMutationsTarget { 43 class MockCompositorMutator : public CompositorMutator {
44 public:
45 MOCK_METHOD2(Mutate, bool(double, CompositorMutableStateProvider*));
46 };
47
48 class MockCompositorMutationsTarget : public CompositorMutationsTarget {
32 public: 49 public:
33 MOCK_METHOD1(ApplyMutations, void(CompositorMutations*)); 50 MOCK_METHOD1(ApplyMutations, void(CompositorMutations*));
34 }; 51 };
35 52
36 TEST(CompositorMutatorClient, CallbackForNonNullMutationsShouldApply) { 53 class CompositorMutatorClientTest : public testing::Test {
37 MockCompositoMutationsTarget target; 54 public:
55 CompositorMutatorClientTest()
56 : compositor_frame_sink_(cc::FakeCompositorFrameSink::Create3d()) {
57 cc::LayerTreeSettings settings;
58 settings.layer_transforms_should_scale_layer_contents = true;
59 host_impl_.reset(new cc::FakeLayerTreeHostImpl(
60 settings, &task_runner_provider_, &task_graph_runner_));
61 host_impl_->SetVisible(true);
62 EXPECT_TRUE(host_impl_->InitializeRenderer(compositor_frame_sink_.get()));
63 }
64
65 cc::FakeLayerTreeHostImpl& hostImpl() { return *host_impl_; }
66
67 private:
68 // The cc testing machinery has fairly deep dependency on having a main
69 // message loop (one example is the task runner provider). We construct one
70 // here so that it's installed in TLA and can be found by other cc classes.
71 base::MessageLoop message_loop_;
72 cc::TestTaskGraphRunner task_graph_runner_;
73 cc::FakeImplTaskRunnerProvider task_runner_provider_;
74 std::unique_ptr<cc::FakeCompositorFrameSink> compositor_frame_sink_;
75 std::unique_ptr<cc::FakeLayerTreeHostImpl> host_impl_;
76 };
77
78 TEST_F(CompositorMutatorClientTest, CallbackForNonNullMutationsShouldApply) {
79 MockCompositorMutationsTarget target;
38 80
39 CompositorMutatorClient client(new StubCompositorMutator, &target); 81 CompositorMutatorClient client(new StubCompositorMutator, &target);
40 std::unique_ptr<CompositorMutations> mutations = 82 std::unique_ptr<CompositorMutations> mutations =
41 WTF::MakeUnique<CompositorMutations>(); 83 WTF::MakeUnique<CompositorMutations>();
42 client.SetMutationsForTesting(std::move(mutations)); 84 client.SetMutationsForTesting(std::move(mutations));
43 85
44 EXPECT_CALL(target, ApplyMutations(_)); 86 EXPECT_CALL(target, ApplyMutations(_));
45 client.TakeMutations().Run(); 87 client.TakeMutations().Run();
46 } 88 }
47 89
48 TEST(CompositorMutatorClient, CallbackForNullMutationsShouldBeNoop) { 90 TEST_F(CompositorMutatorClientTest, CallbackForNullMutationsShouldBeNoop) {
49 MockCompositoMutationsTarget target; 91 MockCompositorMutationsTarget target;
50 CompositorMutatorClient client(new StubCompositorMutator, &target); 92 CompositorMutatorClient client(new StubCompositorMutator, &target);
51 93
52 EXPECT_CALL(target, ApplyMutations(_)).Times(0); 94 EXPECT_CALL(target, ApplyMutations(_)).Times(0);
53 EXPECT_TRUE(client.TakeMutations().is_null()); 95 EXPECT_TRUE(client.TakeMutations().is_null());
54 } 96 }
55 97
98 TEST_F(CompositorMutatorClientTest, MutateOneProxy) {
99 MockCompositorMutationsTarget target;
100 MockCompositorMutator mutator;
101 CompositorMutatorClient client(&mutator, &target);
102
103 uint64_t proxyId = 12;
104 uint64_t elementId = 15;
105 uint32_t mutableProperties = CompositorMutableProperty::kOpacity |
106 CompositorMutableProperty::kTransform |
107 CompositorMutableProperty::kScrollLeft |
108 CompositorMutableProperty::kScrollTop;
109 client.RegisterCompositorProxy(proxyId, elementId, mutableProperties);
110
111 std::unique_ptr<cc::LayerImpl> root =
112 cc::LayerImpl::Create(hostImpl().active_tree(), 42);
113 std::unique_ptr<cc::LayerImpl> scroll =
114 cc::LayerImpl::Create(hostImpl().active_tree(), 11);
115 cc::LayerImpl* scrollLayer = scroll.get();
116 cc::LayerImpl* rootLayer = root.get();
117
118 root->test_properties()->AddChild(std::move(scroll));
119 root->SetElementId(
120 CreateCompositorElementId(elementId, CompositorSubElementId::kPrimary));
121 root->SetMutableProperties(CompositorMutableProperty::kOpacity |
122 CompositorMutableProperty::kTransform);
123
124 scrollLayer->SetScrollClipLayer(root->id());
125 scrollLayer->test_properties()->transform = gfx::Transform();
126 scrollLayer->SetPosition(gfx::PointF());
127 scrollLayer->SetBounds(gfx::Size(100, 100));
128 scrollLayer->SetDrawsContent(true);
129 scrollLayer->SetElementId(
130 CreateCompositorElementId(elementId, CompositorSubElementId::kScroll));
131 scrollLayer->SetMutableProperties(CompositorMutableProperty::kScrollLeft |
132 CompositorMutableProperty::kScrollTop);
133
134 hostImpl().SetViewportSize(scrollLayer->bounds());
135 hostImpl().active_tree()->SetRootLayerForTesting(std::move(root));
136 hostImpl().UpdateNumChildrenAndDrawPropertiesForActiveTree();
137
138 gfx::Transform zero(0, 0, 0, 0, 0, 0);
139 EXPECT_CALL(mutator, Mutate(_, _))
140 .WillOnce(testing::Invoke(
141 [zero, proxyId](double now,
142 CompositorMutableStateProvider* stateProvider) {
143 std::unique_ptr<CompositorMutableState> mutableState =
144 stateProvider->GetMutableStateFor(proxyId);
145 EXPECT_TRUE(mutableState);
146 EXPECT_EQ(gfx::Transform().ToString(),
147 gfx::Transform(mutableState->Transform()).ToString());
148 EXPECT_EQ(0, mutableState->ScrollLeft());
149 EXPECT_EQ(0, mutableState->ScrollTop());
150
151 mutableState->SetOpacity(0.5);
152 mutableState->SetTransform(zero.matrix());
153 mutableState->SetScrollLeft(10);
154 mutableState->SetScrollTop(20);
155 return false;
156 }));
157 client.Mutate(base::TimeTicks(), hostImpl().active_tree());
158
159 EXPECT_EQ(0.5, rootLayer->Opacity());
160 EXPECT_EQ(zero.ToString(), rootLayer->Transform().ToString());
161 EXPECT_EQ(10, scrollLayer->CurrentScrollOffset().x());
162 EXPECT_EQ(20, scrollLayer->CurrentScrollOffset().y());
163
164 EXPECT_CALL(target, ApplyMutations(_))
165 .WillOnce(
166 testing::Invoke([elementId, zero](CompositorMutations* mutations) {
167 EXPECT_EQ(1ul, mutations->map.size());
168 const CompositorMutation& mutation =
169 *mutations->map.Find(elementId)->value;
170 EXPECT_TRUE(mutation.IsOpacityMutated());
171 EXPECT_TRUE(mutation.IsTransformMutated());
172 EXPECT_TRUE(mutation.IsScrollLeftMutated());
173 EXPECT_TRUE(mutation.IsScrollTopMutated());
174
175 EXPECT_EQ(0.5, mutation.Opacity());
176 EXPECT_EQ(zero.ToString(),
177 gfx::Transform(mutation.Transform()).ToString());
178 EXPECT_EQ(10, mutation.ScrollLeft());
179 EXPECT_EQ(20, mutation.ScrollTop());
180 }));
181 client.TakeMutations().Run();
182 }
183
56 } // namespace 184 } // namespace
57 } // namespace blink 185 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698