cd28f905b568366d50e043db272b287e70abbed5
[platform/framework/web/crosswalk.git] / src / cc / layers / picture_image_layer_impl_unittest.cc
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 "cc/layers/picture_image_layer_impl.h"
6
7 #include "cc/layers/append_quads_data.h"
8 #include "cc/resources/tile_priority.h"
9 #include "cc/test/fake_impl_proxy.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/fake_picture_layer_tiling_client.h"
13 #include "cc/test/impl_side_painting_settings.h"
14 #include "cc/test/mock_quad_culler.h"
15 #include "cc/test/test_shared_bitmap_manager.h"
16 #include "cc/trees/layer_tree_impl.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace cc {
20 namespace {
21
22 class TestablePictureImageLayerImpl : public PictureImageLayerImpl {
23  public:
24   TestablePictureImageLayerImpl(LayerTreeImpl* tree_impl, int id)
25       : PictureImageLayerImpl(tree_impl, id) {
26   }
27
28   PictureLayerTilingSet* tilings() { return tilings_.get(); }
29
30   friend class PictureImageLayerImplTest;
31 };
32
33 class PictureImageLayerImplTest : public testing::Test {
34  public:
35   PictureImageLayerImplTest()
36       : proxy_(base::MessageLoopProxy::current()),
37         host_impl_(ImplSidePaintingSettings(),
38                    &proxy_,
39                    &shared_bitmap_manager_) {
40     tiling_client_.SetTileSize(ImplSidePaintingSettings().default_tile_size);
41     host_impl_.CreatePendingTree();
42     host_impl_.InitializeRenderer(
43         FakeOutputSurface::Create3d().PassAs<OutputSurface>());
44   }
45
46   scoped_ptr<TestablePictureImageLayerImpl> CreateLayer(int id,
47                                                         WhichTree which_tree) {
48     LayerTreeImpl* tree = NULL;
49     switch (which_tree) {
50       case ACTIVE_TREE:
51         tree = host_impl_.active_tree();
52         break;
53       case PENDING_TREE:
54         tree = host_impl_.pending_tree();
55         break;
56       case NUM_TREES:
57         NOTREACHED();
58         break;
59     }
60     TestablePictureImageLayerImpl* layer =
61         new TestablePictureImageLayerImpl(tree, id);
62     layer->SetBounds(gfx::Size(100, 200));
63     layer->tilings_.reset(new PictureLayerTilingSet(&tiling_client_,
64                                                     layer->bounds()));
65     layer->pile_ = tiling_client_.pile();
66     return make_scoped_ptr(layer);
67   }
68
69   void UpdateDrawProperties() {
70     host_impl_.pending_tree()->UpdateDrawProperties();
71   }
72
73  protected:
74   FakeImplProxy proxy_;
75   FakeLayerTreeHostImpl host_impl_;
76   TestSharedBitmapManager shared_bitmap_manager_;
77   FakePictureLayerTilingClient tiling_client_;
78 };
79
80 TEST_F(PictureImageLayerImplTest, CalculateContentsScale) {
81   scoped_ptr<TestablePictureImageLayerImpl> layer(CreateLayer(1, PENDING_TREE));
82   layer->SetDrawsContent(true);
83
84   float contents_scale_x;
85   float contents_scale_y;
86   gfx::Size content_bounds;
87   layer->CalculateContentsScale(2.f, 3.f, 4.f, false,
88                                 &contents_scale_x, &contents_scale_y,
89                                 &content_bounds);
90   EXPECT_FLOAT_EQ(1.f, contents_scale_x);
91   EXPECT_FLOAT_EQ(1.f, contents_scale_y);
92   EXPECT_EQ(layer->bounds(), content_bounds);
93 }
94
95 TEST_F(PictureImageLayerImplTest, AreVisibleResourcesReady) {
96   scoped_ptr<TestablePictureImageLayerImpl> layer(CreateLayer(1, PENDING_TREE));
97   layer->SetBounds(gfx::Size(100, 200));
98   layer->SetDrawsContent(true);
99
100   UpdateDrawProperties();
101
102   float contents_scale_x;
103   float contents_scale_y;
104   gfx::Size content_bounds;
105   layer->CalculateContentsScale(2.f, 3.f, 4.f, false,
106                                 &contents_scale_x, &contents_scale_y,
107                                 &content_bounds);
108   layer->UpdateTilePriorities();
109
110   EXPECT_TRUE(layer->AreVisibleResourcesReady());
111 }
112
113 TEST_F(PictureImageLayerImplTest, IgnoreIdealContentScale) {
114   scoped_ptr<TestablePictureImageLayerImpl> pending_layer(
115       CreateLayer(1, PENDING_TREE));
116   pending_layer->SetDrawsContent(true);
117
118   // Set PictureLayerImpl::ideal_contents_scale_ to 2.f which is not equal
119   // to the content scale used by PictureImageLayerImpl.
120   const float suggested_ideal_contents_scale = 2.f;
121   const float device_scale_factor = 3.f;
122   const float page_scale_factor = 4.f;
123   const bool animating_transform_to_screen = false;
124   float contents_scale_x;
125   float contents_scale_y;
126   gfx::Size content_bounds;
127   pending_layer->CalculateContentsScale(suggested_ideal_contents_scale,
128                                         device_scale_factor,
129                                         page_scale_factor,
130                                         animating_transform_to_screen,
131                                         &contents_scale_x,
132                                         &contents_scale_y,
133                                         &content_bounds);
134
135   // Push to active layer.
136   host_impl_.ActivatePendingTree();
137   scoped_ptr<TestablePictureImageLayerImpl> active_layer(
138       CreateLayer(1, ACTIVE_TREE));
139   pending_layer->PushPropertiesTo(active_layer.get());
140   active_layer->CalculateContentsScale(suggested_ideal_contents_scale,
141                                        device_scale_factor,
142                                        page_scale_factor,
143                                        animating_transform_to_screen,
144                                        &contents_scale_x,
145                                        &contents_scale_y,
146                                        &content_bounds);
147
148   // Create tile and resource.
149   active_layer->tilings()->tiling_at(0)->CreateAllTilesForTesting();
150   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
151       active_layer->tilings()->tiling_at(0)->AllTilesForTesting(),
152       host_impl_.resource_provider());
153
154   // Draw.
155   active_layer->draw_properties().visible_content_rect =
156       gfx::Rect(active_layer->bounds());
157   MockQuadCuller quad_culler;
158   AppendQuadsData data;
159   active_layer->WillDraw(DRAW_MODE_SOFTWARE, NULL);
160   active_layer->AppendQuads(&quad_culler, &data);
161   active_layer->DidDraw(NULL);
162
163   EXPECT_EQ(DrawQuad::TILED_CONTENT, quad_culler.quad_list()[0]->material);
164
165   // Tiles are ready at correct scale, so should not set had_incomplete_tile.
166   EXPECT_FALSE(data.had_incomplete_tile);
167 }
168
169 }  // namespace
170 }  // namespace cc