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 "cc/resources/prioritized_resource_manager.h"
8 #include "cc/resources/resource_provider.h"
9 #include "cc/resources/resource_update_queue.h"
10 #include "cc/resources/scoped_ui_resource.h"
11 #include "cc/test/fake_layer_tree_host.h"
12 #include "cc/test/fake_layer_tree_host_client.h"
13 #include "cc/test/fake_output_surface.h"
14 #include "cc/test/fake_output_surface_client.h"
15 #include "cc/test/geometry_test_utils.h"
16 #include "cc/trees/layer_tree_host.h"
17 #include "cc/trees/occlusion_tracker.h"
18 #include "cc/trees/single_thread_proxy.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
23 using ::testing::Mock
;
25 using ::testing::AtLeast
;
26 using ::testing::AnyNumber
;
31 class TestUIResourceLayer
: public UIResourceLayer
{
33 static scoped_refptr
<TestUIResourceLayer
> Create() {
34 return make_scoped_refptr(new TestUIResourceLayer());
37 UIResourceId
GetUIResourceId() {
38 if (ui_resource_holder_
)
39 return ui_resource_holder_
->id();
44 TestUIResourceLayer() : UIResourceLayer() { SetIsDrawable(true); }
45 ~TestUIResourceLayer() override
{}
48 class UIResourceLayerTest
: public testing::Test
{
50 UIResourceLayerTest() : fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
53 void SetUp() override
{
54 layer_tree_host_
= FakeLayerTreeHost::Create(&fake_client_
);
55 layer_tree_host_
->InitializeSingleThreaded(
57 base::MessageLoopProxy::current(),
61 void TearDown() override
{
62 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
65 FakeLayerTreeHostClient fake_client_
;
66 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
69 TEST_F(UIResourceLayerTest
, SetBitmap
) {
70 scoped_refptr
<UIResourceLayer
> test_layer
= TestUIResourceLayer::Create();
71 ASSERT_TRUE(test_layer
.get());
72 test_layer
->SetBounds(gfx::Size(100, 100));
74 layer_tree_host_
->SetRootLayer(test_layer
);
75 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
76 EXPECT_EQ(test_layer
->layer_tree_host(), layer_tree_host_
.get());
78 ResourceUpdateQueue queue
;
79 gfx::Rect screen_space_clip_rect
;
80 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
81 test_layer
->SavePaintProperties();
82 test_layer
->Update(&queue
, &occlusion_tracker
);
84 EXPECT_FALSE(test_layer
->DrawsContent());
87 bitmap
.allocN32Pixels(10, 10);
88 bitmap
.setImmutable();
90 test_layer
->SetBitmap(bitmap
);
91 test_layer
->Update(&queue
, &occlusion_tracker
);
93 EXPECT_TRUE(test_layer
->DrawsContent());
96 TEST_F(UIResourceLayerTest
, SetUIResourceId
) {
97 scoped_refptr
<TestUIResourceLayer
> test_layer
= TestUIResourceLayer::Create();
98 ASSERT_TRUE(test_layer
.get());
99 test_layer
->SetBounds(gfx::Size(100, 100));
101 layer_tree_host_
->SetRootLayer(test_layer
);
102 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
103 EXPECT_EQ(test_layer
->layer_tree_host(), layer_tree_host_
.get());
105 ResourceUpdateQueue queue
;
106 gfx::Rect screen_space_clip_rect
;
107 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
108 test_layer
->SavePaintProperties();
109 test_layer
->Update(&queue
, &occlusion_tracker
);
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(&queue
, &occlusion_tracker
);
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
= TestUIResourceLayer::Create();
133 ASSERT_TRUE(test_layer
.get());
134 test_layer
->SetBounds(gfx::Size(100, 100));
137 bitmap
.allocN32Pixels(10, 10);
138 bitmap
.setImmutable();
139 ASSERT_FALSE(bitmap
.isNull());
140 ASSERT_TRUE(bitmap
.pixelRef()->unique());
142 test_layer
->SetBitmap(bitmap
);
143 ASSERT_FALSE(bitmap
.pixelRef()->unique());
145 test_layer
->SetUIResourceId(0);
146 EXPECT_TRUE(bitmap
.pixelRef()->unique());