OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "base/basictypes.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "ui/gfx/canvas_skia.h" |
| 8 #include "ui/gfx/compositor/layer.h" |
| 9 #include "ui/gfx/compositor/test_compositor_host.h" |
| 10 |
| 11 namespace ui { |
| 12 |
| 13 namespace { |
| 14 |
| 15 class LayerTest : public testing::Test { |
| 16 public: |
| 17 LayerTest() {} |
| 18 virtual ~LayerTest() {} |
| 19 |
| 20 // Overridden from testing::Test: |
| 21 virtual void SetUp() OVERRIDE { |
| 22 const gfx::Rect host_bounds(10, 10, 500, 500); |
| 23 window_.reset(TestCompositorHost::Create(host_bounds)); |
| 24 window_->Show(); |
| 25 } |
| 26 |
| 27 virtual void TearDown() OVERRIDE { |
| 28 } |
| 29 |
| 30 Compositor* GetCompositor() { |
| 31 return window_->GetCompositor(); |
| 32 } |
| 33 |
| 34 Layer* CreateLayer() { |
| 35 return new Layer(GetCompositor()); |
| 36 } |
| 37 |
| 38 Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) { |
| 39 Layer* layer = CreateLayer(); |
| 40 layer->SetBounds(bounds); |
| 41 PaintColorToLayer(layer, color); |
| 42 return layer; |
| 43 } |
| 44 |
| 45 gfx::Canvas* CreateCanvasForLayer(const Layer* layer) { |
| 46 return gfx::Canvas::CreateCanvas(layer->bounds().width(), |
| 47 layer->bounds().height(), |
| 48 false); |
| 49 } |
| 50 |
| 51 void PaintColorToLayer(Layer* layer, SkColor color) { |
| 52 scoped_ptr<gfx::Canvas> canvas(CreateCanvasForLayer(layer)); |
| 53 canvas->FillRectInt(color, 0, 0, layer->bounds().width(), |
| 54 layer->bounds().height()); |
| 55 layer->SetCanvas(*canvas->AsCanvasSkia(), layer->bounds().origin()); |
| 56 } |
| 57 |
| 58 void DrawTree(Layer* root) { |
| 59 window_->GetCompositor()->NotifyStart(); |
| 60 DrawLayerChildren(root); |
| 61 window_->GetCompositor()->NotifyEnd(); |
| 62 } |
| 63 |
| 64 void DrawLayerChildren(Layer* layer) { |
| 65 layer->Draw(); |
| 66 std::vector<Layer*>::const_iterator it = layer->children().begin(); |
| 67 while (it != layer->children().end()) { |
| 68 DrawLayerChildren(*it); |
| 69 ++it; |
| 70 } |
| 71 } |
| 72 |
| 73 void RunPendingMessages() { |
| 74 MessageLoop main_message_loop(MessageLoop::TYPE_UI); |
| 75 MessageLoopForUI::current()->Run(NULL); |
| 76 } |
| 77 |
| 78 private: |
| 79 scoped_ptr<TestCompositorHost> window_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(LayerTest); |
| 82 }; |
| 83 |
| 84 } |
| 85 |
| 86 TEST_F(LayerTest, Draw) { |
| 87 scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED, |
| 88 gfx::Rect(20, 20, 50, 50))); |
| 89 DrawTree(layer.get()); |
| 90 } |
| 91 |
| 92 // Create this hierarchy: |
| 93 // L1 - red |
| 94 // +-- L2 - blue |
| 95 // | +-- L3 - yellow |
| 96 // +-- L4 - magenta |
| 97 // |
| 98 TEST_F(LayerTest, Hierarchy) { |
| 99 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, |
| 100 gfx::Rect(20, 20, 400, 400))); |
| 101 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE, |
| 102 gfx::Rect(10, 10, 350, 350))); |
| 103 scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW, |
| 104 gfx::Rect(5, 5, 25, 25))); |
| 105 scoped_ptr<Layer> l4(CreateColorLayer(SK_ColorMAGENTA, |
| 106 gfx::Rect(300, 300, 100, 100))); |
| 107 |
| 108 l1->Add(l2.get()); |
| 109 l1->Add(l4.get()); |
| 110 l2->Add(l3.get()); |
| 111 |
| 112 DrawTree(l1.get()); |
| 113 } |
| 114 |
| 115 // L1 |
| 116 // +-- L2 |
| 117 TEST_F(LayerTest, ConvertPointToLayer_Simple) { |
| 118 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, |
| 119 gfx::Rect(20, 20, 400, 400))); |
| 120 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE, |
| 121 gfx::Rect(10, 10, 350, 350))); |
| 122 l1->Add(l2.get()); |
| 123 DrawTree(l1.get()); |
| 124 |
| 125 gfx::Point point1_in_l2_coords(5, 5); |
| 126 Layer::ConvertPointToLayer(l2.get(), l1.get(), &point1_in_l2_coords); |
| 127 gfx::Point point1_in_l1_coords(15, 15); |
| 128 EXPECT_EQ(point1_in_l1_coords, point1_in_l2_coords); |
| 129 |
| 130 gfx::Point point2_in_l1_coords(5, 5); |
| 131 Layer::ConvertPointToLayer(l1.get(), l2.get(), &point2_in_l1_coords); |
| 132 gfx::Point point2_in_l2_coords(-5, -5); |
| 133 EXPECT_EQ(point2_in_l2_coords, point2_in_l1_coords); |
| 134 } |
| 135 |
| 136 // L1 |
| 137 // +-- L2 |
| 138 // +-- L3 |
| 139 TEST_F(LayerTest, ConvertPointToLayer_Medium) { |
| 140 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, |
| 141 gfx::Rect(20, 20, 400, 400))); |
| 142 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE, |
| 143 gfx::Rect(10, 10, 350, 350))); |
| 144 scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW, |
| 145 gfx::Rect(10, 10, 100, 100))); |
| 146 l1->Add(l2.get()); |
| 147 l2->Add(l3.get()); |
| 148 DrawTree(l1.get()); |
| 149 |
| 150 gfx::Point point1_in_l3_coords(5, 5); |
| 151 Layer::ConvertPointToLayer(l3.get(), l1.get(), &point1_in_l3_coords); |
| 152 gfx::Point point1_in_l1_coords(25, 25); |
| 153 EXPECT_EQ(point1_in_l1_coords, point1_in_l3_coords); |
| 154 |
| 155 gfx::Point point2_in_l1_coords(5, 5); |
| 156 Layer::ConvertPointToLayer(l1.get(), l3.get(), &point2_in_l1_coords); |
| 157 gfx::Point point2_in_l3_coords(-15, -15); |
| 158 EXPECT_EQ(point2_in_l3_coords, point2_in_l1_coords); |
| 159 } |
| 160 |
| 161 } // namespace ui |
| 162 |
OLD | NEW |