OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "ui/views/layout/box_layout.h" |
| 6 |
5 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "ui/views/layout/box_layout.h" | |
7 #include "ui/views/view.h" | 8 #include "ui/views/view.h" |
8 | 9 |
9 class StaticSizedView : public views::View { | 10 namespace views { |
| 11 |
| 12 namespace { |
| 13 |
| 14 class StaticSizedView : public View { |
10 public: | 15 public: |
11 explicit StaticSizedView(const gfx::Size& size) | 16 explicit StaticSizedView(const gfx::Size& size) |
12 : size_(size) { } | 17 : size_(size) { |
| 18 } |
13 | 19 |
14 virtual gfx::Size GetPreferredSize() { | 20 virtual gfx::Size GetPreferredSize() OVERRIDE{ |
15 return size_; | 21 return size_; |
16 } | 22 } |
17 | 23 |
18 private: | 24 private: |
19 gfx::Size size_; | 25 gfx::Size size_; |
20 }; | 26 }; |
21 | 27 |
22 class BoxLayoutTest : public testing::Test { | 28 class BoxLayoutTest : public testing::Test { |
23 public: | 29 public: |
24 virtual void SetUp() { | 30 virtual void SetUp() OVERRIDE { |
25 host_.reset(new views::View); | 31 host_.reset(new View); |
26 } | 32 } |
27 | 33 |
28 scoped_ptr<views::View> host_; | 34 scoped_ptr<View> host_; |
29 scoped_ptr<views::BoxLayout> layout_; | 35 scoped_ptr<BoxLayout> layout_; |
30 }; | 36 }; |
31 | 37 |
| 38 } // namespace |
| 39 |
32 TEST_F(BoxLayoutTest, Empty) { | 40 TEST_F(BoxLayoutTest, Empty) { |
33 layout_.reset( | 41 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 20)); |
34 new views::BoxLayout(views::BoxLayout::kHorizontal, 10, 10, 20)); | |
35 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | 42 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); |
36 } | 43 } |
37 | 44 |
38 TEST_F(BoxLayoutTest, AlignmentHorizontal) { | 45 TEST_F(BoxLayoutTest, AlignmentHorizontal) { |
39 layout_.reset(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | 46 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); |
40 views::View* v1 = new StaticSizedView(gfx::Size(10, 20)); | 47 View* v1 = new StaticSizedView(gfx::Size(10, 20)); |
41 host_->AddChildView(v1); | 48 host_->AddChildView(v1); |
42 views::View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 49 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
43 host_->AddChildView(v2); | 50 host_->AddChildView(v2); |
44 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | 51 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); |
45 host_->SetBounds(0, 0, 20, 20); | 52 host_->SetBounds(0, 0, 20, 20); |
46 layout_->Layout(host_.get()); | 53 layout_->Layout(host_.get()); |
47 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1->bounds()); | 54 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1->bounds()); |
48 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2->bounds()); | 55 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2->bounds()); |
49 } | 56 } |
50 | 57 |
51 TEST_F(BoxLayoutTest, AlignmentVertical) { | 58 TEST_F(BoxLayoutTest, AlignmentVertical) { |
52 layout_.reset(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | 59 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); |
53 views::View* v1 = new StaticSizedView(gfx::Size(20, 10)); | 60 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
54 host_->AddChildView(v1); | 61 host_->AddChildView(v1); |
55 views::View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 62 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
56 host_->AddChildView(v2); | 63 host_->AddChildView(v2); |
57 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | 64 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); |
58 host_->SetBounds(0, 0, 20, 20); | 65 host_->SetBounds(0, 0, 20, 20); |
59 layout_->Layout(host_.get()); | 66 layout_->Layout(host_.get()); |
60 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); | 67 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); |
61 EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2->bounds()); | 68 EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2->bounds()); |
62 } | 69 } |
63 | 70 |
64 TEST_F(BoxLayoutTest, Spacing) { | 71 TEST_F(BoxLayoutTest, Spacing) { |
65 layout_.reset(new views::BoxLayout(views::BoxLayout::kHorizontal, 7, 7, 8)); | 72 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 7, 7, 8)); |
66 views::View* v1 = new StaticSizedView(gfx::Size(10, 20)); | 73 View* v1 = new StaticSizedView(gfx::Size(10, 20)); |
67 host_->AddChildView(v1); | 74 host_->AddChildView(v1); |
68 views::View* v2 = new StaticSizedView(gfx::Size(10, 20)); | 75 View* v2 = new StaticSizedView(gfx::Size(10, 20)); |
69 host_->AddChildView(v2); | 76 host_->AddChildView(v2); |
70 EXPECT_EQ(gfx::Size(42, 34), layout_->GetPreferredSize(host_.get())); | 77 EXPECT_EQ(gfx::Size(42, 34), layout_->GetPreferredSize(host_.get())); |
71 host_->SetBounds(0, 0, 100, 100); | 78 host_->SetBounds(0, 0, 100, 100); |
72 layout_->Layout(host_.get()); | 79 layout_->Layout(host_.get()); |
73 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1->bounds()); | 80 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1->bounds()); |
74 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds()); | 81 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds()); |
75 } | 82 } |
76 | 83 |
77 TEST_F(BoxLayoutTest, Overflow) { | 84 TEST_F(BoxLayoutTest, Overflow) { |
78 layout_.reset(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | 85 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); |
79 views::View* v1 = new StaticSizedView(gfx::Size(20, 20)); | 86 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
80 host_->AddChildView(v1); | 87 host_->AddChildView(v1); |
81 views::View* v2 = new StaticSizedView(gfx::Size(10, 20)); | 88 View* v2 = new StaticSizedView(gfx::Size(10, 20)); |
82 host_->AddChildView(v2); | 89 host_->AddChildView(v2); |
83 host_->SetBounds(0, 0, 10, 10); | 90 host_->SetBounds(0, 0, 10, 10); |
84 layout_->Layout(host_.get()); | 91 layout_->Layout(host_.get()); |
85 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); | 92 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); |
86 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); | 93 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); |
87 } | 94 } |
88 | 95 |
89 TEST_F(BoxLayoutTest, NoSpace) { | 96 TEST_F(BoxLayoutTest, NoSpace) { |
90 layout_.reset( | 97 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); |
91 new views::BoxLayout(views::BoxLayout::kHorizontal, 10, 10, 10)); | 98 View* childView = new StaticSizedView(gfx::Size(20, 20)); |
92 views::View* childView = new StaticSizedView(gfx::Size(20, 20)); | |
93 host_->AddChildView(childView); | 99 host_->AddChildView(childView); |
94 host_->SetBounds(0, 0, 10, 10); | 100 host_->SetBounds(0, 0, 10, 10); |
95 layout_->Layout(host_.get()); | 101 layout_->Layout(host_.get()); |
96 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds()); | 102 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds()); |
97 } | 103 } |
98 | 104 |
99 TEST_F(BoxLayoutTest, InvisibleChild) { | 105 TEST_F(BoxLayoutTest, InvisibleChild) { |
100 layout_.reset( | 106 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); |
101 new views::BoxLayout(views::BoxLayout::kHorizontal, 10, 10, 10)); | 107 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
102 views::View* v1 = new StaticSizedView(gfx::Size(20, 20)); | |
103 v1->SetVisible(false); | 108 v1->SetVisible(false); |
104 host_->AddChildView(v1); | 109 host_->AddChildView(v1); |
105 views::View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 110 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
106 host_->AddChildView(v2); | 111 host_->AddChildView(v2); |
107 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get())); | 112 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get())); |
108 host_->SetBounds(0, 0, 30, 30); | 113 host_->SetBounds(0, 0, 30, 30); |
109 layout_->Layout(host_.get()); | 114 layout_->Layout(host_.get()); |
110 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); | 115 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); |
111 } | 116 } |
112 | 117 |
113 TEST_F(BoxLayoutTest, DistributeEmptySpace) { | 118 TEST_F(BoxLayoutTest, DistributeEmptySpace) { |
114 layout_.reset( | 119 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); |
115 new views::BoxLayout(views::BoxLayout::kHorizontal, 10, 10, 10)); | |
116 layout_->set_spread_blank_space(true); | 120 layout_->set_spread_blank_space(true); |
117 | 121 |
118 views::View* v1 = new StaticSizedView(gfx::Size(20, 20)); | 122 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
119 host_->AddChildView(v1); | 123 host_->AddChildView(v1); |
120 views::View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 124 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
121 host_->AddChildView(v2); | 125 host_->AddChildView(v2); |
122 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get())); | 126 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get())); |
123 | 127 |
124 host_->SetBounds(0, 0, 100, 40); | 128 host_->SetBounds(0, 0, 100, 40); |
125 layout_->Layout(host_.get()); | 129 layout_->Layout(host_.get()); |
126 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); | 130 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); |
127 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString()); | 131 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString()); |
128 } | 132 } |
| 133 |
| 134 } // namespace views |
OLD | NEW |