- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / layout / box_layout_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "ui/views/layout/box_layout.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/views/test/test_views.h"
9 #include "ui/views/view.h"
10
11 namespace views {
12
13 namespace {
14
15 class BoxLayoutTest : public testing::Test {
16  public:
17   virtual void SetUp() OVERRIDE {
18     host_.reset(new View);
19   }
20
21   scoped_ptr<View> host_;
22   scoped_ptr<BoxLayout> layout_;
23 };
24
25 }  // namespace
26
27 TEST_F(BoxLayoutTest, Empty) {
28   layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 20));
29   EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get()));
30 }
31
32 TEST_F(BoxLayoutTest, AlignmentHorizontal) {
33   layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
34   View* v1 = new StaticSizedView(gfx::Size(10, 20));
35   host_->AddChildView(v1);
36   View* v2 = new StaticSizedView(gfx::Size(10, 10));
37   host_->AddChildView(v2);
38   EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get()));
39   host_->SetBounds(0, 0, 20, 20);
40   layout_->Layout(host_.get());
41   EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1->bounds());
42   EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2->bounds());
43 }
44
45 TEST_F(BoxLayoutTest, AlignmentVertical) {
46   layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0));
47   View* v1 = new StaticSizedView(gfx::Size(20, 10));
48   host_->AddChildView(v1);
49   View* v2 = new StaticSizedView(gfx::Size(10, 10));
50   host_->AddChildView(v2);
51   EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get()));
52   host_->SetBounds(0, 0, 20, 20);
53   layout_->Layout(host_.get());
54   EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds());
55   EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2->bounds());
56 }
57
58 TEST_F(BoxLayoutTest, Spacing) {
59   layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 7, 7, 8));
60   View* v1 = new StaticSizedView(gfx::Size(10, 20));
61   host_->AddChildView(v1);
62   View* v2 = new StaticSizedView(gfx::Size(10, 20));
63   host_->AddChildView(v2);
64   EXPECT_EQ(gfx::Size(42, 34), layout_->GetPreferredSize(host_.get()));
65   host_->SetBounds(0, 0, 100, 100);
66   layout_->Layout(host_.get());
67   EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1->bounds());
68   EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds());
69 }
70
71 TEST_F(BoxLayoutTest, Overflow) {
72   layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
73   View* v1 = new StaticSizedView(gfx::Size(20, 20));
74   host_->AddChildView(v1);
75   View* v2 = new StaticSizedView(gfx::Size(10, 20));
76   host_->AddChildView(v2);
77   host_->SetBounds(0, 0, 10, 10);
78   layout_->Layout(host_.get());
79   EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
80   EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
81 }
82
83 TEST_F(BoxLayoutTest, NoSpace) {
84   layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
85   View* childView = new StaticSizedView(gfx::Size(20, 20));
86   host_->AddChildView(childView);
87   host_->SetBounds(0, 0, 10, 10);
88   layout_->Layout(host_.get());
89   EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds());
90 }
91
92 TEST_F(BoxLayoutTest, InvisibleChild) {
93   layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
94   View* v1 = new StaticSizedView(gfx::Size(20, 20));
95   v1->SetVisible(false);
96   host_->AddChildView(v1);
97   View* v2 = new StaticSizedView(gfx::Size(10, 10));
98   host_->AddChildView(v2);
99   EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get()));
100   host_->SetBounds(0, 0, 30, 30);
101   layout_->Layout(host_.get());
102   EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds());
103 }
104
105 TEST_F(BoxLayoutTest, DistributeEmptySpace) {
106   layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
107   layout_->set_spread_blank_space(true);
108
109   View* v1 = new StaticSizedView(gfx::Size(20, 20));
110   host_->AddChildView(v1);
111   View* v2 = new StaticSizedView(gfx::Size(10, 10));
112   host_->AddChildView(v2);
113   EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get()));
114
115   host_->SetBounds(0, 0, 100, 40);
116   layout_->Layout(host_.get());
117   EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString());
118   EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString());
119 }
120
121 TEST_F(BoxLayoutTest, UseHeightForWidth) {
122   layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0));
123   View* v1 = new StaticSizedView(gfx::Size(20, 10));
124   host_->AddChildView(v1);
125   View* v2 = new ProportionallySizedView(2);
126   host_->AddChildView(v2);
127   EXPECT_EQ(gfx::Size(20, 50), layout_->GetPreferredSize(host_.get()));
128
129   host_->SetBounds(0, 0, 20, 50);
130   layout_->Layout(host_.get());
131   EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds());
132   EXPECT_EQ(gfx::Rect(0, 10, 20, 40), v2->bounds());
133
134   EXPECT_EQ(110, layout_->GetPreferredHeightForWidth(host_.get(), 50));
135 }
136
137 TEST_F(BoxLayoutTest, EmptyPreferredSize) {
138   for (size_t i = 0; i < 2; i++) {
139     BoxLayout::Orientation orientation = i == 0 ? BoxLayout::kHorizontal :
140                                                   BoxLayout::kVertical;
141     host_->RemoveAllChildViews(true);
142     host_->SetLayoutManager(new BoxLayout(orientation, 0, 0, 5));
143     View* v1 = new StaticSizedView(gfx::Size());
144     host_->AddChildView(v1);
145     View* v2 = new StaticSizedView(gfx::Size(10, 10));
146     host_->AddChildView(v2);
147     host_->SizeToPreferredSize();
148     host_->Layout();
149
150     EXPECT_EQ(v2->GetPreferredSize().width(), host_->bounds().width()) << i;
151     EXPECT_EQ(v2->GetPreferredSize().height(), host_->bounds().height()) << i;
152     EXPECT_EQ(v1->GetPreferredSize().width(), v1->bounds().width()) << i;
153     EXPECT_EQ(v1->GetPreferredSize().height(), v1->bounds().height()) << i;
154     EXPECT_EQ(v2->GetPreferredSize().width(), v2->bounds().width()) << i;
155     EXPECT_EQ(v2->GetPreferredSize().height(), v2->bounds().height()) << i;
156   }
157 }
158
159 }  // namespace views