summaryrefslogtreecommitdiffstats
path: root/ui/gfx
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-28 23:45:38 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-28 23:45:38 +0000
commit4916d62ca7ae3365d8234682dc28b815ad4c5031 (patch)
treecee8d9c4f6bba9d3bdcfc4bf7d2c7378a3ce58c1 /ui/gfx
parent5bce52305fac51d1bafffbd9dee9bb1e261c01ef (diff)
downloadchromium_src-4916d62ca7ae3365d8234682dc28b815ad4c5031.zip
chromium_src-4916d62ca7ae3365d8234682dc28b815ad4c5031.tar.gz
chromium_src-4916d62ca7ae3365d8234682dc28b815ad4c5031.tar.bz2
Refactors TestCompositor out of views_unittests into a place it can be
used by both the layer tests and the views test. Also fixes a bug that was causing test to fail and fixes up defines. I've disabled a portion of the compositor unit tests on linux as they don't compile. BUG=none TEST=none R=ben@chromium.org Review URL: http://codereview.chromium.org/8072010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103208 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r--ui/gfx/compositor/compositor.gyp12
-rw-r--r--ui/gfx/compositor/compositor.h2
-rw-r--r--ui/gfx/compositor/layer.cc3
-rw-r--r--ui/gfx/compositor/layer_unittest.cc111
-rw-r--r--ui/gfx/compositor/test_compositor.cc46
-rw-r--r--ui/gfx/compositor/test_compositor.h41
-rw-r--r--ui/gfx/compositor/test_texture.cc35
-rw-r--r--ui/gfx/compositor/test_texture.h50
8 files changed, 282 insertions, 18 deletions
diff --git a/ui/gfx/compositor/compositor.gyp b/ui/gfx/compositor/compositor.gyp
index 18ae769..a0317d7 100644
--- a/ui/gfx/compositor/compositor.gyp
+++ b/ui/gfx/compositor/compositor.gyp
@@ -96,14 +96,26 @@
'sources': [
'layer_unittest.cc',
'run_all_unittests.cc',
+ 'test_compositor.cc',
+ 'test_compositor.h',
'test_compositor_host.h',
'test_compositor_host_linux.cc',
'test_compositor_host_win.cc',
'test_suite.cc',
'test_suite.h',
+ 'test_texture.cc',
+ 'test_texture.h',
'<(SHARED_INTERMEDIATE_DIR)/ui/gfx/gfx_resources.rc',
'<(SHARED_INTERMEDIATE_DIR)/ui/ui_resources/ui_resources.rc',
],
+ 'conditions': [
+ ['OS=="linux"', {
+ 'sources!': [
+ 'test_compositor_host.h',
+ 'test_compositor_host_linux.cc',
+ ],
+ }],
+ ],
},
],
}
diff --git a/ui/gfx/compositor/compositor.h b/ui/gfx/compositor/compositor.h
index f1932c0..75cd2a9 100644
--- a/ui/gfx/compositor/compositor.h
+++ b/ui/gfx/compositor/compositor.h
@@ -113,7 +113,7 @@ class COMPOSITOR_EXPORT Compositor : public base::RefCounted<Compositor> {
// Sets the root of the layer tree drawn by this Compositor.
// The Compositor does not own the root layer.
- const Layer* root_layer() const { return root_layer_; }
+ Layer* root_layer() { return root_layer_; }
void SetRootLayer(Layer* root_layer);
// Draws the scene created by the layer tree and any visual effects. If
diff --git a/ui/gfx/compositor/layer.cc b/ui/gfx/compositor/layer.cc
index 79d587e..ff5a5ff 100644
--- a/ui/gfx/compositor/layer.cc
+++ b/ui/gfx/compositor/layer.cc
@@ -279,7 +279,8 @@ void Layer::UpdateLayerCanvas() {
if (!delegate_ || layer_updated_externally_)
return;
gfx::Rect local_bounds = gfx::Rect(gfx::Point(), bounds_.size());
- gfx::Rect draw_rect = invalid_rect_.Intersect(local_bounds);
+ gfx::Rect draw_rect = texture_.get() ? invalid_rect_.Intersect(local_bounds) :
+ local_bounds;
if (draw_rect.IsEmpty()) {
invalid_rect_ = gfx::Rect();
return;
diff --git a/ui/gfx/compositor/layer_unittest.cc b/ui/gfx/compositor/layer_unittest.cc
index beac36d..a0693ed 100644
--- a/ui/gfx/compositor/layer_unittest.cc
+++ b/ui/gfx/compositor/layer_unittest.cc
@@ -9,16 +9,22 @@
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/compositor/compositor_observer.h"
#include "ui/gfx/compositor/layer.h"
+#include "ui/gfx/compositor/test_compositor.h"
+
+// TestCompositorHost needs to be ported to linux for these to work.
+#if !defined(OS_LINUX)
#include "ui/gfx/compositor/test_compositor_host.h"
+#endif
namespace ui {
namespace {
-class LayerTest : public testing::Test {
+#if !defined(OS_LINUX)
+class LayerWithRealCompositorTest : public testing::Test {
public:
- LayerTest() {}
- virtual ~LayerTest() {}
+ LayerWithRealCompositorTest() {}
+ virtual ~LayerWithRealCompositorTest() {}
// Overridden from testing::Test:
virtual void SetUp() OVERRIDE {
@@ -83,8 +89,9 @@ class LayerTest : public testing::Test {
MessageLoopForUI message_loop_;
scoped_ptr<TestCompositorHost> window_;
- DISALLOW_COPY_AND_ASSIGN(LayerTest);
+ DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest);
};
+#endif
// LayerDelegate that paints colors to the layer.
class TestLayerDelegate : public LayerDelegate {
@@ -157,7 +164,9 @@ class NullLayerDelegate : public LayerDelegate {
}
-TEST_F(LayerTest, Draw) {
+#if !defined(OS_LINUX)
+
+TEST_F(LayerWithRealCompositorTest, Draw) {
scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED,
gfx::Rect(20, 20, 50, 50)));
DrawTree(layer.get());
@@ -169,7 +178,7 @@ TEST_F(LayerTest, Draw) {
// | +-- L3 - yellow
// +-- L4 - magenta
//
-TEST_F(LayerTest, Hierarchy) {
+TEST_F(LayerWithRealCompositorTest, Hierarchy) {
scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
gfx::Rect(20, 20, 400, 400)));
scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
@@ -186,6 +195,76 @@ TEST_F(LayerTest, Hierarchy) {
DrawTree(l1.get());
}
+#endif
+
+// LayerTest uses TestCompositor as the Compositor implementation.
+class LayerTest : public testing::Test {
+ public:
+ LayerTest() {}
+ virtual ~LayerTest() {}
+
+ // Overridden from testing::Test:
+ virtual void SetUp() OVERRIDE {
+ compositor_ = new TestCompositor();
+ }
+
+ virtual void TearDown() OVERRIDE {
+ }
+
+ Compositor* compositor() { return compositor_.get(); }
+
+ Layer* CreateLayer(Layer::LayerType type) {
+ return new Layer(compositor(), type);
+ }
+
+ Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) {
+ Layer* layer = CreateLayer(Layer::LAYER_HAS_TEXTURE);
+ layer->SetBounds(bounds);
+ PaintColorToLayer(layer, color);
+ return layer;
+ }
+
+ Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
+ Layer* layer = CreateLayer(Layer::LAYER_HAS_NO_TEXTURE);
+ layer->SetBounds(bounds);
+ return layer;
+ }
+
+ gfx::Canvas* CreateCanvasForLayer(const Layer* layer) {
+ return gfx::Canvas::CreateCanvas(layer->bounds().width(),
+ layer->bounds().height(),
+ false);
+ }
+
+ void PaintColorToLayer(Layer* layer, SkColor color) {
+ scoped_ptr<gfx::Canvas> canvas(CreateCanvasForLayer(layer));
+ canvas->FillRectInt(color, 0, 0, layer->bounds().width(),
+ layer->bounds().height());
+ layer->SetCanvas(*canvas->AsCanvasSkia(), layer->bounds().origin());
+ }
+
+ void DrawTree(Layer* root) {
+ compositor()->SetRootLayer(root);
+ compositor()->Draw(false);
+ }
+
+ // Invalidates the entire contents of the layer.
+ void SchedulePaintForLayer(Layer* layer) {
+ layer->SchedulePaint(
+ gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
+ }
+
+ // Invokes DrawTree on the compositor.
+ void Draw() {
+ compositor_->root_layer()->DrawTree();
+ }
+
+ private:
+ scoped_refptr<TestCompositor> compositor_;
+
+ DISALLOW_COPY_AND_ASSIGN(LayerTest);
+};
+
// L1
// +-- L2
TEST_F(LayerTest, ConvertPointToLayer_Simple) {
@@ -241,20 +320,20 @@ TEST_F(LayerTest, Delegate) {
delegate.AddColor(SK_ColorYELLOW);
delegate.AddColor(SK_ColorGREEN);
- GetCompositor()->SetRootLayer(l1.get());
+ compositor()->SetRootLayer(l1.get());
l1->SchedulePaint(gfx::Rect(0, 0, 400, 400));
- RunPendingMessages();
+ Draw();
EXPECT_EQ(delegate.color_index(), 1);
EXPECT_EQ(delegate.paint_size(), l1->bounds().size());
l1->SchedulePaint(gfx::Rect(10, 10, 200, 200));
- RunPendingMessages();
+ Draw();
EXPECT_EQ(delegate.color_index(), 2);
EXPECT_EQ(delegate.paint_size(), gfx::Size(200, 200));
l1->SchedulePaint(gfx::Rect(5, 5, 50, 50));
- RunPendingMessages();
+ Draw();
EXPECT_EQ(delegate.color_index(), 0);
EXPECT_EQ(delegate.paint_size(), gfx::Size(50, 50));
}
@@ -276,10 +355,10 @@ TEST_F(LayerTest, DrawTree) {
DrawTreeLayerDelegate d3;
l3->set_delegate(&d3);
- GetCompositor()->SetRootLayer(l1.get());
+ compositor()->SetRootLayer(l1.get());
l2->SchedulePaint(gfx::Rect(5, 5, 5, 5));
- RunPendingMessages();
+ Draw();
EXPECT_FALSE(d1.painted());
EXPECT_TRUE(d2.painted());
EXPECT_FALSE(d3.painted());
@@ -310,11 +389,11 @@ TEST_F(LayerTest, HierarchyNoTexture) {
DrawTreeLayerDelegate d3;
l3->set_delegate(&d3);
- GetCompositor()->SetRootLayer(l1.get());
+ compositor()->SetRootLayer(l1.get());
l2->SchedulePaint(gfx::Rect(5, 5, 5, 5));
l3->SchedulePaint(gfx::Rect(5, 5, 5, 5));
- RunPendingMessages();
+ Draw();
// |d2| should not have received a paint notification since it has no texture.
EXPECT_FALSE(d2.painted());
@@ -372,7 +451,7 @@ TEST_F(LayerTest, NoCompositor) {
EXPECT_EQ(NULL, l11->texture());
- GetCompositor()->SetRootLayer(l1.get());
+ compositor()->SetRootLayer(l1.get());
EXPECT_EQ(NULL, l1->texture());
@@ -394,7 +473,7 @@ TEST_F(LayerTest, NoCompositor) {
// By asking l121 and l122 to paint, we cause them to generate a texture.
SchedulePaintForLayer(l121.get());
SchedulePaintForLayer(l122.get());
- RunPendingMessages();
+ Draw();
EXPECT_EQ(NULL, l12->texture());
EXPECT_TRUE(NULL != l121->texture());
diff --git a/ui/gfx/compositor/test_compositor.cc b/ui/gfx/compositor/test_compositor.cc
new file mode 100644
index 0000000..2c32bef
--- /dev/null
+++ b/ui/gfx/compositor/test_compositor.cc
@@ -0,0 +1,46 @@
+// Copyright (c) 2011 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 "ui/gfx/compositor/test_compositor.h"
+
+#include "ui/gfx/compositor/test_texture.h"
+
+namespace ui {
+
+class TestCompositorDelegate : public ui::CompositorDelegate {
+ public:
+ TestCompositorDelegate() {}
+ virtual ~TestCompositorDelegate() {}
+
+ virtual void ScheduleCompositorPaint() OVERRIDE {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestCompositorDelegate);
+};
+
+TestCompositor::TestCompositor()
+ : Compositor(new TestCompositorDelegate, gfx::Size(100, 100)) {
+ owned_delegate_.reset(static_cast<TestCompositorDelegate*>(delegate()));
+}
+
+TestCompositor::~TestCompositor() {
+}
+
+ui::Texture* TestCompositor::CreateTexture() {
+ return new TestTexture();
+}
+
+void TestCompositor::OnNotifyStart(bool clear) {
+}
+
+void TestCompositor::OnNotifyEnd() {
+}
+
+void TestCompositor::Blur(const gfx::Rect& bounds) {
+}
+
+void TestCompositor::OnWidgetSizeChanged() {
+}
+
+} // namespace ui
diff --git a/ui/gfx/compositor/test_compositor.h b/ui/gfx/compositor/test_compositor.h
new file mode 100644
index 0000000..b7d2cfa
--- /dev/null
+++ b/ui/gfx/compositor/test_compositor.h
@@ -0,0 +1,41 @@
+// Copyright (c) 2011 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.
+
+#ifndef UI_GFX_COMPOSITOR_TEST_COMPOSITOR_H_
+#define UI_GFX_COMPOSITOR_TEST_COMPOSITOR_H_
+#pragma once
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/compiler_specific.h"
+#include "ui/gfx/compositor/compositor.h"
+
+namespace ui {
+
+class TestCompositorDelegate;
+
+// Trivial Compositor implementation that creates Textures of type TestTexture.
+class TestCompositor : public ui::Compositor {
+ public:
+ TestCompositor();
+ virtual ~TestCompositor();
+
+ // ui::Compositor:
+ virtual ui::Texture* CreateTexture() OVERRIDE;
+ virtual void OnNotifyStart(bool clear) OVERRIDE;
+ virtual void OnNotifyEnd() OVERRIDE;
+ virtual void Blur(const gfx::Rect& bounds) OVERRIDE;
+
+ protected:
+ virtual void OnWidgetSizeChanged() OVERRIDE;
+
+ private:
+ scoped_ptr<TestCompositorDelegate> owned_delegate_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestCompositor);
+};
+
+} // namespace ui
+
+#endif // UI_GFX_COMPOSITOR_TEST_COMPOSITOR_H_
diff --git a/ui/gfx/compositor/test_texture.cc b/ui/gfx/compositor/test_texture.cc
new file mode 100644
index 0000000..ce1b9f4
--- /dev/null
+++ b/ui/gfx/compositor/test_texture.cc
@@ -0,0 +1,35 @@
+// Copyright (c) 2011 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 "ui/gfx/compositor/test_texture.h"
+
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkDevice.h"
+
+namespace ui {
+
+// static
+int TestTexture::live_count_ = 0;
+
+TestTexture::TestTexture() {
+ live_count_++;
+}
+
+TestTexture::~TestTexture() {
+ live_count_--;
+}
+
+void TestTexture::SetCanvas(const SkCanvas& canvas,
+ const gfx::Point& origin,
+ const gfx::Size& overall_size) {
+ const SkBitmap& bitmap = canvas.getDevice()->accessBitmap(false);
+ bounds_of_last_paint_.SetRect(
+ origin.x(), origin.y(), bitmap.width(), bitmap.height());
+}
+
+void TestTexture::Draw(const ui::TextureDrawParams& params,
+ const gfx::Rect& clip_bounds) {
+}
+
+} // namespace ui
diff --git a/ui/gfx/compositor/test_texture.h b/ui/gfx/compositor/test_texture.h
new file mode 100644
index 0000000..95a6f2a
--- /dev/null
+++ b/ui/gfx/compositor/test_texture.h
@@ -0,0 +1,50 @@
+// Copyright (c) 2011 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.
+
+#ifndef UI_GFX_COMPOSITOR_TEST_TEXTURE_H_
+#define UI_GFX_COMPOSITOR_TEST_TEXTURE_H_
+#pragma once
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "ui/gfx/compositor/compositor.h"
+#include "ui/gfx/rect.h"
+
+namespace ui {
+
+// Texture implementation that TextCompositor creates. Doesn't actually draw
+// anything.
+class TestTexture : public ui::Texture {
+ public:
+ TestTexture();
+ virtual ~TestTexture();
+
+ // Number of textures that have been created.
+ static void reset_live_count() { live_count_ = 0; }
+ static int live_count() { return live_count_; }
+
+ // Bounds of the last bitmap passed to SetCanvas.
+ const gfx::Rect& bounds_of_last_paint() const {
+ return bounds_of_last_paint_;
+ }
+
+ // ui::Texture
+ virtual void SetCanvas(const SkCanvas& canvas,
+ const gfx::Point& origin,
+ const gfx::Size& overall_size) OVERRIDE;
+ virtual void Draw(const ui::TextureDrawParams& params,
+ const gfx::Rect& clip_bounds) OVERRIDE;
+
+ private:
+ // Number of live instances.
+ static int live_count_;
+
+ gfx::Rect bounds_of_last_paint_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestTexture);
+};
+
+} // namespace ui
+
+#endif // UI_GFX_COMPOSITOR_TEST_TEXTURE_H_