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 "testing/gtest/include/gtest/gtest.h" | |
6 #include "views/layout/box_layout.h" | |
7 #include "views/view.h" | |
8 | |
9 class StaticSizedView : public views::View { | |
10 public: | |
11 explicit StaticSizedView(const gfx::Size& size) | |
12 : size_(size) { } | |
13 | |
14 virtual gfx::Size GetPreferredSize() { | |
15 return size_; | |
16 } | |
17 | |
18 private: | |
19 gfx::Size size_; | |
20 }; | |
21 | |
22 class BoxLayoutTest : public testing::Test { | |
23 public: | |
24 virtual void SetUp() { | |
25 host_.reset(new views::View); | |
26 } | |
27 | |
28 scoped_ptr<views::View> host_; | |
29 scoped_ptr<views::BoxLayout> layout_; | |
30 }; | |
31 | |
32 TEST_F(BoxLayoutTest, Empty) { | |
33 layout_.reset( | |
34 new views::BoxLayout(views::BoxLayout::kHorizontal, 10, 10, 20)); | |
35 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | |
36 } | |
37 | |
38 TEST_F(BoxLayoutTest, AlignmentHorizontal) { | |
39 layout_.reset(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | |
40 views::View* v1 = new StaticSizedView(gfx::Size(10, 20)); | |
41 host_->AddChildView(v1); | |
42 views::View* v2 = new StaticSizedView(gfx::Size(10, 10)); | |
43 host_->AddChildView(v2); | |
44 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | |
45 host_->SetBounds(0, 0, 20, 20); | |
46 layout_->Layout(host_.get()); | |
47 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1->bounds()); | |
48 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2->bounds()); | |
49 } | |
50 | |
51 TEST_F(BoxLayoutTest, AlignmentVertical) { | |
52 layout_.reset(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | |
53 views::View* v1 = new StaticSizedView(gfx::Size(20, 10)); | |
54 host_->AddChildView(v1); | |
55 views::View* v2 = new StaticSizedView(gfx::Size(10, 10)); | |
56 host_->AddChildView(v2); | |
57 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | |
58 host_->SetBounds(0, 0, 20, 20); | |
59 layout_->Layout(host_.get()); | |
60 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); | |
61 EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2->bounds()); | |
62 } | |
63 | |
64 TEST_F(BoxLayoutTest, Spacing) { | |
65 layout_.reset(new views::BoxLayout(views::BoxLayout::kHorizontal, 7, 7, 8)); | |
66 views::View* v1 = new StaticSizedView(gfx::Size(10, 20)); | |
67 host_->AddChildView(v1); | |
68 views::View* v2 = new StaticSizedView(gfx::Size(10, 20)); | |
69 host_->AddChildView(v2); | |
70 EXPECT_EQ(gfx::Size(42, 34), layout_->GetPreferredSize(host_.get())); | |
71 host_->SetBounds(0, 0, 100, 100); | |
72 layout_->Layout(host_.get()); | |
73 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1->bounds()); | |
74 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds()); | |
75 } | |
76 | |
77 TEST_F(BoxLayoutTest, Overflow) { | |
78 layout_.reset(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | |
79 views::View* v1 = new StaticSizedView(gfx::Size(20, 20)); | |
80 host_->AddChildView(v1); | |
81 views::View* v2 = new StaticSizedView(gfx::Size(10, 20)); | |
82 host_->AddChildView(v2); | |
83 host_->SetBounds(0, 0, 10, 10); | |
84 layout_->Layout(host_.get()); | |
85 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); | |
86 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); | |
87 } | |
88 | |
89 TEST_F(BoxLayoutTest, NoSpace) { | |
90 layout_.reset( | |
91 new views::BoxLayout(views::BoxLayout::kHorizontal, 10, 10, 10)); | |
92 views::View* childView = new StaticSizedView(gfx::Size(20, 20)); | |
93 host_->AddChildView(childView); | |
94 host_->SetBounds(0, 0, 10, 10); | |
95 layout_->Layout(host_.get()); | |
96 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds()); | |
97 } | |
98 | |
99 TEST_F(BoxLayoutTest, InvisibleChild) { | |
100 layout_.reset( | |
101 new views::BoxLayout(views::BoxLayout::kHorizontal, 10, 10, 10)); | |
102 views::View* v1 = new StaticSizedView(gfx::Size(20, 20)); | |
103 v1->SetVisible(false); | |
104 host_->AddChildView(v1); | |
105 views::View* v2 = new StaticSizedView(gfx::Size(10, 10)); | |
106 host_->AddChildView(v2); | |
107 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get())); | |
108 host_->SetBounds(0, 0, 30, 30); | |
109 layout_->Layout(host_.get()); | |
110 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); | |
111 } | |
OLD | NEW |