WebKit Roll 139512:139548
[chromium-blink-merge.git] / cc / content_layer_unittest.cc
blob56b8d198e959a3e593a2b00ffc98518095d9504c
1 // Copyright 2012 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/content_layer.h"
7 #include "cc/bitmap_content_layer_updater.h"
8 #include "cc/content_layer_client.h"
9 #include "cc/rendering_stats.h"
10 #include "cc/resource_update_queue.h"
11 #include "cc/test/geometry_test_utils.h"
12 #include "skia/ext/platform_canvas.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/rect_conversions.h"
16 using namespace WebKit;
18 namespace cc {
19 namespace {
21 class FakeContentLayer : public ContentLayer {
22 public:
23 explicit FakeContentLayer(ContentLayerClient* client) : ContentLayer(client) { }
25 virtual void setNeedsDisplayRect(const gfx::RectF& dirtyRect) OVERRIDE
27 m_lastDirtyRect = dirtyRect;
28 ContentLayer::setNeedsDisplayRect(dirtyRect);
30 gfx::RectF lastDirtyRect() const { return m_lastDirtyRect; }
32 protected:
33 virtual ~FakeContentLayer() { }
34 virtual void createUpdaterIfNeeded() OVERRIDE { }
36 private:
37 gfx::RectF m_lastDirtyRect;
40 class MockContentLayerClient : public ContentLayerClient {
41 public:
42 explicit MockContentLayerClient(gfx::Rect opaqueLayerRect)
43 : m_opaqueLayerRect(opaqueLayerRect)
47 virtual void paintContents(SkCanvas*, const gfx::Rect&, gfx::RectF& opaque) OVERRIDE
49 opaque = gfx::RectF(m_opaqueLayerRect);
52 private:
53 gfx::Rect m_opaqueLayerRect;
56 TEST(ContentLayerTest, ContentLayerPainterWithDeviceScale)
58 float contentsScale = 2;
59 gfx::Rect contentRect(10, 10, 100, 100);
60 gfx::Rect opaqueRectInLayerSpace(5, 5, 20, 20);
61 gfx::RectF opaqueRectInContentSpace = gfx::ScaleRect(opaqueRectInLayerSpace, contentsScale, contentsScale);
62 MockContentLayerClient client(opaqueRectInLayerSpace);
63 scoped_refptr<BitmapContentLayerUpdater> updater = BitmapContentLayerUpdater::create(ContentLayerPainter::create(&client).PassAs<LayerPainter>());
65 gfx::Rect resultingOpaqueRect;
66 RenderingStats stats;
67 updater->prepareToUpdate(contentRect, gfx::Size(256, 256), contentsScale, contentsScale, resultingOpaqueRect, stats);
69 EXPECT_RECT_EQ(gfx::ToEnclosingRect(opaqueRectInContentSpace), resultingOpaqueRect);
72 TEST(ContentLayerTest, UseLCDTextEnableCount)
74 scoped_refptr<FakeContentLayer> layer = new FakeContentLayer(NULL);
75 layer->setBounds(gfx::Size(100, 100));
76 ResourceUpdateQueue queue;
77 RenderingStats stats;
79 // By default LCD text is disabled.
80 EXPECT_FALSE(layer->useLCDText());
82 // LCD text can be enabled once.
83 layer->drawProperties().can_use_lcd_text = true;
84 layer->update(queue, NULL, stats);
85 EXPECT_TRUE(layer->useLCDText());
87 // LCD text can always be disabled.
88 layer->drawProperties().can_use_lcd_text = false;
89 layer->update(queue, NULL, stats);
90 EXPECT_FALSE(layer->useLCDText());
92 // LCD text cannot be enabled anymore.
93 layer->drawProperties().can_use_lcd_text = true;
94 layer->update(queue, NULL, stats);
95 EXPECT_FALSE(layer->useLCDText());
98 TEST(ContentLayerTest, UseLCDTextChangeTriggersRepaint)
100 scoped_refptr<FakeContentLayer> layer = new FakeContentLayer(NULL);
101 layer->setBounds(gfx::Size(100, 100));
102 gfx::RectF dirtyRect(100, 100);
103 ResourceUpdateQueue queue;
104 RenderingStats stats;
106 // By default LCD text is disabled.
107 EXPECT_FALSE(layer->useLCDText());
109 // Enable LCD text and verify that it triggers invalidation.
110 layer->drawProperties().can_use_lcd_text = true;
111 layer->update(queue, NULL, stats);
112 EXPECT_TRUE(layer->useLCDText());
113 EXPECT_EQ(dirtyRect, layer->lastDirtyRect());
115 // Reset dirty rect.
116 layer->setNeedsDisplayRect(gfx::RectF());
117 EXPECT_EQ(gfx::RectF(), layer->lastDirtyRect());
119 // Disable LCD text and verify that it triggers invalidation.
120 layer->drawProperties().can_use_lcd_text = false;
121 layer->update(queue, NULL, stats);
122 EXPECT_FALSE(layer->useLCDText());
123 EXPECT_EQ(dirtyRect, layer->lastDirtyRect());
126 } // namespace
127 } // namespace cc