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 "cc/layers/ui_resource_layer.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "cc/resources/resource_provider.h"
9 #include "cc/resources/scoped_ui_resource.h"
10 #include "cc/test/fake_layer_tree_host.h"
11 #include "cc/test/fake_layer_tree_host_client.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/fake_output_surface_client.h"
14 #include "cc/test/geometry_test_utils.h"
15 #include "cc/test/test_task_graph_runner.h"
16 #include "cc/trees/layer_tree_host.h"
17 #include "cc/trees/single_thread_proxy.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
22 using ::testing::Mock
;
24 using ::testing::AtLeast
;
25 using ::testing::AnyNumber
;
30 class TestUIResourceLayer
: public UIResourceLayer
{
32 static scoped_refptr
<TestUIResourceLayer
> Create(
33 const LayerSettings
& settings
) {
34 return make_scoped_refptr(new TestUIResourceLayer(settings
));
37 UIResourceId
GetUIResourceId() {
38 if (ui_resource_holder_
)
39 return ui_resource_holder_
->id();
44 explicit TestUIResourceLayer(const LayerSettings
& settings
)
45 : UIResourceLayer(settings
) {
48 ~TestUIResourceLayer() override
{}
51 class UIResourceLayerTest
: public testing::Test
{
53 UIResourceLayerTest() : fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
56 void SetUp() override
{
58 FakeLayerTreeHost::Create(&fake_client_
, &task_graph_runner_
);
59 layer_tree_host_
->InitializeSingleThreaded(
60 &fake_client_
, base::ThreadTaskRunnerHandle::Get(), nullptr);
63 void TearDown() override
{
64 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
67 FakeLayerTreeHostClient fake_client_
;
68 TestTaskGraphRunner task_graph_runner_
;
69 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
70 LayerSettings layer_settings_
;
73 TEST_F(UIResourceLayerTest
, SetBitmap
) {
74 scoped_refptr
<UIResourceLayer
> test_layer
=
75 TestUIResourceLayer::Create(layer_settings_
);
76 ASSERT_TRUE(test_layer
.get());
77 test_layer
->SetBounds(gfx::Size(100, 100));
79 layer_tree_host_
->SetRootLayer(test_layer
);
80 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
81 EXPECT_EQ(test_layer
->layer_tree_host(), layer_tree_host_
.get());
83 test_layer
->SavePaintProperties();
86 EXPECT_FALSE(test_layer
->DrawsContent());
89 bitmap
.allocN32Pixels(10, 10);
90 bitmap
.setImmutable();
92 test_layer
->SetBitmap(bitmap
);
95 EXPECT_TRUE(test_layer
->DrawsContent());
98 TEST_F(UIResourceLayerTest
, SetUIResourceId
) {
99 scoped_refptr
<TestUIResourceLayer
> test_layer
=
100 TestUIResourceLayer::Create(layer_settings_
);
101 ASSERT_TRUE(test_layer
.get());
102 test_layer
->SetBounds(gfx::Size(100, 100));
104 layer_tree_host_
->SetRootLayer(test_layer
);
105 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
106 EXPECT_EQ(test_layer
->layer_tree_host(), layer_tree_host_
.get());
108 test_layer
->SavePaintProperties();
109 test_layer
->Update();
111 EXPECT_FALSE(test_layer
->DrawsContent());
113 bool is_opaque
= false;
114 scoped_ptr
<ScopedUIResource
> resource
= ScopedUIResource::Create(
115 layer_tree_host_
.get(), UIResourceBitmap(gfx::Size(10, 10), is_opaque
));
116 test_layer
->SetUIResourceId(resource
->id());
117 test_layer
->Update();
119 EXPECT_TRUE(test_layer
->DrawsContent());
121 // ID is preserved even when you set ID first and attach it to the tree.
122 layer_tree_host_
->SetRootLayer(nullptr);
123 scoped_ptr
<ScopedUIResource
> shared_resource
= ScopedUIResource::Create(
124 layer_tree_host_
.get(), UIResourceBitmap(gfx::Size(5, 5), is_opaque
));
125 test_layer
->SetUIResourceId(shared_resource
->id());
126 layer_tree_host_
->SetRootLayer(test_layer
);
127 EXPECT_EQ(shared_resource
->id(), test_layer
->GetUIResourceId());
128 EXPECT_TRUE(test_layer
->DrawsContent());
131 TEST_F(UIResourceLayerTest
, BitmapClearedOnSetUIResourceId
) {
132 scoped_refptr
<UIResourceLayer
> test_layer
=
133 TestUIResourceLayer::Create(layer_settings_
);
134 ASSERT_TRUE(test_layer
.get());
135 test_layer
->SetBounds(gfx::Size(100, 100));
138 bitmap
.allocN32Pixels(10, 10);
139 bitmap
.setImmutable();
140 ASSERT_FALSE(bitmap
.isNull());
141 ASSERT_TRUE(bitmap
.pixelRef()->unique());
143 test_layer
->SetBitmap(bitmap
);
144 ASSERT_FALSE(bitmap
.pixelRef()->unique());
146 test_layer
->SetUIResourceId(0);
147 EXPECT_TRUE(bitmap
.pixelRef()->unique());