summaryrefslogtreecommitdiffstats
path: root/cc/content_layer_unittest.cc
blob: 56b8d198e959a3e593a2b00ffc98518095d9504c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/content_layer.h"

#include "cc/bitmap_content_layer_updater.h"
#include "cc/content_layer_client.h"
#include "cc/rendering_stats.h"
#include "cc/resource_update_queue.h"
#include "cc/test/geometry_test_utils.h"
#include "skia/ext/platform_canvas.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/rect_conversions.h"

using namespace WebKit;

namespace cc {
namespace {

class FakeContentLayer : public ContentLayer {
public:
    explicit FakeContentLayer(ContentLayerClient* client) : ContentLayer(client) { }

    virtual void setNeedsDisplayRect(const gfx::RectF& dirtyRect) OVERRIDE
    {
        m_lastDirtyRect = dirtyRect;
        ContentLayer::setNeedsDisplayRect(dirtyRect);
    }
    gfx::RectF lastDirtyRect() const { return m_lastDirtyRect; }

protected:
    virtual ~FakeContentLayer() { }
    virtual void createUpdaterIfNeeded() OVERRIDE { }

private:
    gfx::RectF m_lastDirtyRect;
};

class MockContentLayerClient : public ContentLayerClient {
public:
    explicit MockContentLayerClient(gfx::Rect opaqueLayerRect)
        : m_opaqueLayerRect(opaqueLayerRect)
    {
    }

    virtual void paintContents(SkCanvas*, const gfx::Rect&, gfx::RectF& opaque) OVERRIDE
    {
        opaque = gfx::RectF(m_opaqueLayerRect);
    }

private:
    gfx::Rect m_opaqueLayerRect;
};

TEST(ContentLayerTest, ContentLayerPainterWithDeviceScale)
{
    float contentsScale = 2;
    gfx::Rect contentRect(10, 10, 100, 100);
    gfx::Rect opaqueRectInLayerSpace(5, 5, 20, 20);
    gfx::RectF opaqueRectInContentSpace = gfx::ScaleRect(opaqueRectInLayerSpace, contentsScale, contentsScale);
    MockContentLayerClient client(opaqueRectInLayerSpace);
    scoped_refptr<BitmapContentLayerUpdater> updater = BitmapContentLayerUpdater::create(ContentLayerPainter::create(&client).PassAs<LayerPainter>());

    gfx::Rect resultingOpaqueRect;
    RenderingStats stats;
    updater->prepareToUpdate(contentRect, gfx::Size(256, 256), contentsScale, contentsScale, resultingOpaqueRect, stats);

    EXPECT_RECT_EQ(gfx::ToEnclosingRect(opaqueRectInContentSpace), resultingOpaqueRect);
}

TEST(ContentLayerTest, UseLCDTextEnableCount)
{
    scoped_refptr<FakeContentLayer> layer = new FakeContentLayer(NULL);
    layer->setBounds(gfx::Size(100, 100));
    ResourceUpdateQueue queue;
    RenderingStats stats;

    // By default LCD text is disabled.
    EXPECT_FALSE(layer->useLCDText());

    // LCD text can be enabled once.
    layer->drawProperties().can_use_lcd_text = true;
    layer->update(queue, NULL, stats);
    EXPECT_TRUE(layer->useLCDText());

    // LCD text can always be disabled.
    layer->drawProperties().can_use_lcd_text = false;
    layer->update(queue, NULL, stats);
    EXPECT_FALSE(layer->useLCDText());

    // LCD text cannot be enabled anymore.
    layer->drawProperties().can_use_lcd_text = true;
    layer->update(queue, NULL, stats);
    EXPECT_FALSE(layer->useLCDText());
}

TEST(ContentLayerTest, UseLCDTextChangeTriggersRepaint)
{
    scoped_refptr<FakeContentLayer> layer = new FakeContentLayer(NULL);
    layer->setBounds(gfx::Size(100, 100));
    gfx::RectF dirtyRect(100, 100);
    ResourceUpdateQueue queue;
    RenderingStats stats;

    // By default LCD text is disabled.
    EXPECT_FALSE(layer->useLCDText());

    // Enable LCD text and verify that it triggers invalidation.
    layer->drawProperties().can_use_lcd_text = true;
    layer->update(queue, NULL, stats);
    EXPECT_TRUE(layer->useLCDText());
    EXPECT_EQ(dirtyRect, layer->lastDirtyRect());

    // Reset dirty rect.
    layer->setNeedsDisplayRect(gfx::RectF());
    EXPECT_EQ(gfx::RectF(), layer->lastDirtyRect());

    // Disable LCD text and verify that it triggers invalidation.
    layer->drawProperties().can_use_lcd_text = false;
    layer->update(queue, NULL, stats);
    EXPECT_FALSE(layer->useLCDText());
    EXPECT_EQ(dirtyRect, layer->lastDirtyRect());
}

}  // namespace
}  // namespace cc