| 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" | 5 #include "ui/views/layout/box_layout.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/views/view.h" | 8 #include "ui/views/view.h" |
| 9 | 9 |
| 10 namespace views { | 10 namespace views { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 class StaticSizedView : public View { | 14 class StaticSizedView : public View { |
| 15 public: | 15 public: |
| 16 explicit StaticSizedView(const gfx::Size& size) | 16 explicit StaticSizedView(const gfx::Size& size) |
| 17 : size_(size) { | 17 : size_(size) { |
| 18 } | 18 } |
| 19 virtual ~StaticSizedView() {} |
| 19 | 20 |
| 20 virtual gfx::Size GetPreferredSize() OVERRIDE{ | 21 virtual gfx::Size GetPreferredSize() OVERRIDE { |
| 21 return size_; | 22 return size_; |
| 22 } | 23 } |
| 23 | 24 |
| 24 private: | 25 private: |
| 25 gfx::Size size_; | 26 gfx::Size size_; |
| 27 |
| 28 DISALLOW_COPY_AND_ASSIGN(StaticSizedView); |
| 29 }; |
| 30 |
| 31 class ProportionallySizedView : public View { |
| 32 public: |
| 33 explicit ProportionallySizedView(int factor) : factor_(factor) {} |
| 34 virtual ~ProportionallySizedView() {} |
| 35 |
| 36 virtual int GetHeightForWidth(int w) OVERRIDE { |
| 37 return w * factor_; |
| 38 } |
| 39 |
| 40 private: |
| 41 // The multiplicative factor between width and height, i.e. |
| 42 // height = width * factor_. |
| 43 int factor_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(ProportionallySizedView); |
| 26 }; | 46 }; |
| 27 | 47 |
| 28 class BoxLayoutTest : public testing::Test { | 48 class BoxLayoutTest : public testing::Test { |
| 29 public: | 49 public: |
| 30 virtual void SetUp() OVERRIDE { | 50 virtual void SetUp() OVERRIDE { |
| 31 host_.reset(new View); | 51 host_.reset(new View); |
| 32 } | 52 } |
| 33 | 53 |
| 34 scoped_ptr<View> host_; | 54 scoped_ptr<View> host_; |
| 35 scoped_ptr<BoxLayout> layout_; | 55 scoped_ptr<BoxLayout> layout_; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 144 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
| 125 host_->AddChildView(v2); | 145 host_->AddChildView(v2); |
| 126 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get())); | 146 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get())); |
| 127 | 147 |
| 128 host_->SetBounds(0, 0, 100, 40); | 148 host_->SetBounds(0, 0, 100, 40); |
| 129 layout_->Layout(host_.get()); | 149 layout_->Layout(host_.get()); |
| 130 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); | 150 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); |
| 131 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString()); | 151 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString()); |
| 132 } | 152 } |
| 133 | 153 |
| 154 TEST_F(BoxLayoutTest, UseHeightForWidth) { |
| 155 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); |
| 156 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 157 host_->AddChildView(v1); |
| 158 View* v2 = new ProportionallySizedView(2); |
| 159 host_->AddChildView(v2); |
| 160 EXPECT_EQ(gfx::Size(20, 50), layout_->GetPreferredSize(host_.get())); |
| 161 |
| 162 host_->SetBounds(0, 0, 20, 50); |
| 163 layout_->Layout(host_.get()); |
| 164 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); |
| 165 EXPECT_EQ(gfx::Rect(0, 10, 20, 40), v2->bounds()); |
| 166 |
| 167 EXPECT_EQ(110, layout_->GetPreferredHeightForWidth(host_.get(), 50)); |
| 168 } |
| 169 |
| 134 } // namespace views | 170 } // namespace views |
| OLD | NEW |