content_layer_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#include "cc/layers/content_layer.h"
6
7#include "cc/layers/content_layer_client.h"
8#include "cc/resources/bitmap_content_layer_updater.h"
9#include "cc/test/geometry_test_utils.h"
10#include "skia/ext/platform_canvas.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "ui/gfx/rect_conversions.h"
13
14using namespace WebKit;
15
16namespace cc {
17namespace {
18
19class MockContentLayerClient : public ContentLayerClient {
20 public:
21  explicit MockContentLayerClient(gfx::Rect opaque_layer_rect)
22      : opaque_layer_rect_(opaque_layer_rect) {}
23
24  virtual void PaintContents(SkCanvas* canvas,
25                             gfx::Rect clip,
26                             gfx::RectF* opaque) OVERRIDE {
27    *opaque = gfx::RectF(opaque_layer_rect_);
28  }
29  virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
30
31 private:
32  gfx::Rect opaque_layer_rect_;
33};
34
35TEST(ContentLayerTest, ContentLayerPainterWithDeviceScale) {
36  float contents_scale = 2.f;
37  gfx::Rect content_rect(10, 10, 100, 100);
38  gfx::Rect opaque_rect_in_layer_space(5, 5, 20, 20);
39  gfx::RectF opaque_rect_in_content_space = gfx::ScaleRect(
40      opaque_rect_in_layer_space, contents_scale, contents_scale);
41  MockContentLayerClient client(opaque_rect_in_layer_space);
42  scoped_refptr<BitmapContentLayerUpdater> updater =
43      BitmapContentLayerUpdater::Create(ContentLayerPainter::Create(&client).
44                                            PassAs<LayerPainter>());
45
46  gfx::Rect resulting_opaque_rect;
47  updater->PrepareToUpdate(content_rect,
48                           gfx::Size(256, 256),
49                           contents_scale,
50                           contents_scale,
51                           &resulting_opaque_rect,
52                           NULL);
53
54  EXPECT_RECT_EQ(gfx::ToEnclosingRect(opaque_rect_in_content_space),
55                 resulting_opaque_rect);
56}
57
58}  // namespace
59}  // namespace cc
60