Index: ui/gfx/compositor/layer_unittest.cc |
=================================================================== |
--- ui/gfx/compositor/layer_unittest.cc (revision 0) |
+++ ui/gfx/compositor/layer_unittest.cc (revision 0) |
@@ -0,0 +1,162 @@ |
+// 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 "base/basictypes.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gfx/canvas_skia.h" |
+#include "ui/gfx/compositor/layer.h" |
+#include "ui/gfx/compositor/test_compositor_host.h" |
+ |
+namespace ui { |
+ |
+namespace { |
+ |
+class LayerTest : public testing::Test { |
+ public: |
+ LayerTest() {} |
+ virtual ~LayerTest() {} |
+ |
+ // Overridden from testing::Test: |
+ virtual void SetUp() OVERRIDE { |
+ const gfx::Rect host_bounds(10, 10, 500, 500); |
+ window_.reset(TestCompositorHost::Create(host_bounds)); |
+ window_->Show(); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ } |
+ |
+ Compositor* GetCompositor() { |
+ return window_->GetCompositor(); |
+ } |
+ |
+ Layer* CreateLayer() { |
+ return new Layer(GetCompositor()); |
+ } |
+ |
+ Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) { |
+ Layer* layer = CreateLayer(); |
+ layer->SetBounds(bounds); |
+ PaintColorToLayer(layer, color); |
+ 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) { |
+ window_->GetCompositor()->NotifyStart(); |
+ DrawLayerChildren(root); |
+ window_->GetCompositor()->NotifyEnd(); |
+ } |
+ |
+ void DrawLayerChildren(Layer* layer) { |
+ layer->Draw(); |
+ std::vector<Layer*>::const_iterator it = layer->children().begin(); |
+ while (it != layer->children().end()) { |
+ DrawLayerChildren(*it); |
+ ++it; |
+ } |
+ } |
+ |
+ void RunPendingMessages() { |
+ MessageLoop main_message_loop(MessageLoop::TYPE_UI); |
+ MessageLoopForUI::current()->Run(NULL); |
+ } |
+ |
+ private: |
+ scoped_ptr<TestCompositorHost> window_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(LayerTest); |
+}; |
+ |
+} |
+ |
+TEST_F(LayerTest, Draw) { |
+ scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED, |
+ gfx::Rect(20, 20, 50, 50))); |
+ DrawTree(layer.get()); |
+} |
+ |
+// Create this hierarchy: |
+// L1 - red |
+// +-- L2 - blue |
+// | +-- L3 - yellow |
+// +-- L4 - magenta |
+// |
+TEST_F(LayerTest, Hierarchy) { |
+ scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, |
+ gfx::Rect(20, 20, 400, 400))); |
+ scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE, |
+ gfx::Rect(10, 10, 350, 350))); |
+ scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW, |
+ gfx::Rect(5, 5, 25, 25))); |
+ scoped_ptr<Layer> l4(CreateColorLayer(SK_ColorMAGENTA, |
+ gfx::Rect(300, 300, 100, 100))); |
+ |
+ l1->Add(l2.get()); |
+ l1->Add(l4.get()); |
+ l2->Add(l3.get()); |
+ |
+ DrawTree(l1.get()); |
+} |
+ |
+// L1 |
+// +-- L2 |
+TEST_F(LayerTest, ConvertPointToLayer_Simple) { |
+ scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, |
+ gfx::Rect(20, 20, 400, 400))); |
+ scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE, |
+ gfx::Rect(10, 10, 350, 350))); |
+ l1->Add(l2.get()); |
+ DrawTree(l1.get()); |
+ |
+ gfx::Point point1_in_l2_coords(5, 5); |
+ Layer::ConvertPointToLayer(l2.get(), l1.get(), &point1_in_l2_coords); |
+ gfx::Point point1_in_l1_coords(15, 15); |
+ EXPECT_EQ(point1_in_l1_coords, point1_in_l2_coords); |
+ |
+ gfx::Point point2_in_l1_coords(5, 5); |
+ Layer::ConvertPointToLayer(l1.get(), l2.get(), &point2_in_l1_coords); |
+ gfx::Point point2_in_l2_coords(-5, -5); |
+ EXPECT_EQ(point2_in_l2_coords, point2_in_l1_coords); |
+} |
+ |
+// L1 |
+// +-- L2 |
+// +-- L3 |
+TEST_F(LayerTest, ConvertPointToLayer_Medium) { |
+ scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, |
+ gfx::Rect(20, 20, 400, 400))); |
+ scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE, |
+ gfx::Rect(10, 10, 350, 350))); |
+ scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW, |
+ gfx::Rect(10, 10, 100, 100))); |
+ l1->Add(l2.get()); |
+ l2->Add(l3.get()); |
+ DrawTree(l1.get()); |
+ |
+ gfx::Point point1_in_l3_coords(5, 5); |
+ Layer::ConvertPointToLayer(l3.get(), l1.get(), &point1_in_l3_coords); |
+ gfx::Point point1_in_l1_coords(25, 25); |
+ EXPECT_EQ(point1_in_l1_coords, point1_in_l3_coords); |
+ |
+ gfx::Point point2_in_l1_coords(5, 5); |
+ Layer::ConvertPointToLayer(l1.get(), l3.get(), &point2_in_l1_coords); |
+ gfx::Point point2_in_l3_coords(-15, -15); |
+ EXPECT_EQ(point2_in_l3_coords, point2_in_l1_coords); |
+} |
+ |
+} // namespace ui |
+ |
Property changes on: ui\gfx\compositor\layer_unittest.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |