| 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/grid_layout.h" | 5 #include "ui/views/layout/grid_layout.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/views/view.h" | 9 #include "ui/views/view.h" |
| 10 | 10 |
| 11 namespace views { | 11 namespace views { |
| 12 | 12 |
| 13 void ExpectViewBoundsEquals(int x, int y, int w, int h, | 13 void ExpectViewBoundsEquals(int x, int y, int w, int h, |
| 14 const View* view) { | 14 const View* view) { |
| 15 EXPECT_EQ(x, view->x()); | 15 EXPECT_EQ(x, view->x()); |
| 16 EXPECT_EQ(y, view->y()); | 16 EXPECT_EQ(y, view->y()); |
| 17 EXPECT_EQ(w, view->width()); | 17 EXPECT_EQ(w, view->width()); |
| 18 EXPECT_EQ(h, view->height()); | 18 EXPECT_EQ(h, view->height()); |
| 19 } | 19 } |
| 20 | 20 |
| 21 class SettableSizeView : public View { | 21 class SettableSizeView : public View { |
| 22 public: | 22 public: |
| 23 explicit SettableSizeView(const gfx::Size& pref) { | 23 explicit SettableSizeView(const gfx::Size& pref) { |
| 24 pref_ = pref; | 24 pref_ = pref; |
| 25 } | 25 } |
| 26 | 26 |
| 27 gfx::Size GetPreferredSize() const override { return pref_; } | 27 gfx::Size GetPreferredSize() const override { return pref_; } |
| 28 | 28 |
| 29 void set_pref(const gfx::Size& pref) { pref_ = pref; } |
| 30 |
| 29 private: | 31 private: |
| 30 gfx::Size pref_; | 32 gfx::Size pref_; |
| 33 |
| 34 DISALLOW_COPY_AND_ASSIGN(SettableSizeView); |
| 31 }; | 35 }; |
| 32 | 36 |
| 33 // A view with fixed circumference that trades height for width. | 37 // A view with fixed circumference that trades height for width. |
| 34 class FlexibleView : public View { | 38 class FlexibleView : public View { |
| 35 public: | 39 public: |
| 36 explicit FlexibleView(int circumference) { | 40 explicit FlexibleView(int circumference) { |
| 37 circumference_ = circumference; | 41 circumference_ = circumference; |
| 38 } | 42 } |
| 39 | 43 |
| 40 gfx::Size GetPreferredSize() const override { | 44 gfx::Size GetPreferredSize() const override { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 EXPECT_EQ(gfx::Size(30, 20), pref); | 141 EXPECT_EQ(gfx::Size(30, 20), pref); |
| 138 | 142 |
| 139 host.SetBounds(0, 0, pref.width(), pref.height()); | 143 host.SetBounds(0, 0, pref.width(), pref.height()); |
| 140 layout.Layout(&host); | 144 layout.Layout(&host); |
| 141 ExpectViewBoundsEquals(0, 0, 10, 20, &v1); | 145 ExpectViewBoundsEquals(0, 0, 10, 20, &v1); |
| 142 ExpectViewBoundsEquals(10, 0, 20, 20, &v2); | 146 ExpectViewBoundsEquals(10, 0, 20, 20, &v2); |
| 143 | 147 |
| 144 RemoveAll(); | 148 RemoveAll(); |
| 145 } | 149 } |
| 146 | 150 |
| 151 // Test linked column sizes, and the column size limit. |
| 152 TEST_F(GridLayoutTest, LinkedSizes) { |
| 153 SettableSizeView v1(gfx::Size(10, 20)); |
| 154 SettableSizeView v2(gfx::Size(20, 20)); |
| 155 SettableSizeView v3(gfx::Size(0, 20)); |
| 156 ColumnSet* c1 = layout.AddColumnSet(0); |
| 157 |
| 158 // Fill widths. |
| 159 c1->AddColumn(GridLayout::FILL, GridLayout::LEADING, 0, GridLayout::USE_PREF, |
| 160 0, 0); |
| 161 c1->AddColumn(GridLayout::FILL, GridLayout::LEADING, 0, GridLayout::USE_PREF, |
| 162 0, 0); |
| 163 c1->AddColumn(GridLayout::FILL, GridLayout::LEADING, 0, GridLayout::USE_PREF, |
| 164 0, 0); |
| 165 |
| 166 layout.StartRow(0, 0); |
| 167 layout.AddView(&v1); |
| 168 layout.AddView(&v2); |
| 169 layout.AddView(&v3); |
| 170 |
| 171 // Link all the columns. |
| 172 c1->LinkColumnSizes(0, 1, 2, -1); |
| 173 GetPreferredSize(); |
| 174 |
| 175 // |v1| and |v3| should obtain the same width as |v2|, since |v2| is largest. |
| 176 EXPECT_EQ(gfx::Size(20 + 20 + 20, 20), pref); |
| 177 host.SetBounds(0, 0, pref.width(), pref.height()); |
| 178 layout.Layout(&host); |
| 179 ExpectViewBoundsEquals(0, 0, 20, 20, &v1); |
| 180 ExpectViewBoundsEquals(20, 0, 20, 20, &v2); |
| 181 ExpectViewBoundsEquals(40, 0, 20, 20, &v3); |
| 182 |
| 183 // If the limit is zero, behaves as though the columns are not linked. |
| 184 c1->set_linked_column_size_limit(0); |
| 185 GetPreferredSize(); |
| 186 EXPECT_EQ(gfx::Size(10 + 20 + 0, 20), pref); |
| 187 host.SetBounds(0, 0, pref.width(), pref.height()); |
| 188 layout.Layout(&host); |
| 189 ExpectViewBoundsEquals(0, 0, 10, 20, &v1); |
| 190 ExpectViewBoundsEquals(10, 0, 20, 20, &v2); |
| 191 ExpectViewBoundsEquals(30, 0, 0, 20, &v3); |
| 192 |
| 193 // Set a size limit. |
| 194 c1->set_linked_column_size_limit(40); |
| 195 v1.set_pref(gfx::Size(35, 20)); |
| 196 GetPreferredSize(); |
| 197 |
| 198 // |v1| now dominates, but it is still below the limit. |
| 199 EXPECT_EQ(gfx::Size(35 + 35 + 35, 20), pref); |
| 200 host.SetBounds(0, 0, pref.width(), pref.height()); |
| 201 layout.Layout(&host); |
| 202 ExpectViewBoundsEquals(0, 0, 35, 20, &v1); |
| 203 ExpectViewBoundsEquals(35, 0, 35, 20, &v2); |
| 204 ExpectViewBoundsEquals(70, 0, 35, 20, &v3); |
| 205 |
| 206 // Go over the limit. |v1| shouldn't influence size at all, but the others |
| 207 // should still be linked to the next largest width. |
| 208 v1.set_pref(gfx::Size(45, 20)); |
| 209 GetPreferredSize(); |
| 210 EXPECT_EQ(gfx::Size(45 + 20 + 20, 20), pref); |
| 211 host.SetBounds(0, 0, pref.width(), pref.height()); |
| 212 layout.Layout(&host); |
| 213 ExpectViewBoundsEquals(0, 0, 45, 20, &v1); |
| 214 ExpectViewBoundsEquals(45, 0, 20, 20, &v2); |
| 215 ExpectViewBoundsEquals(65, 0, 20, 20, &v3); |
| 216 |
| 217 RemoveAll(); |
| 218 } |
| 219 |
| 147 TEST_F(GridLayoutTest, ColSpan1) { | 220 TEST_F(GridLayoutTest, ColSpan1) { |
| 148 SettableSizeView v1(gfx::Size(100, 20)); | 221 SettableSizeView v1(gfx::Size(100, 20)); |
| 149 SettableSizeView v2(gfx::Size(10, 40)); | 222 SettableSizeView v2(gfx::Size(10, 40)); |
| 150 ColumnSet* c1 = layout.AddColumnSet(0); | 223 ColumnSet* c1 = layout.AddColumnSet(0); |
| 151 c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, | 224 c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 152 0, GridLayout::USE_PREF, 0, 0); | 225 0, GridLayout::USE_PREF, 0, 0); |
| 153 c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, | 226 c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 154 1, GridLayout::USE_PREF, 0, 0); | 227 1, GridLayout::USE_PREF, 0, 0); |
| 155 layout.StartRow(0, 0); | 228 layout.StartRow(0, 0); |
| 156 layout.AddView(&v1, 2, 1); | 229 layout.AddView(&v1, 2, 1); |
| (...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 EXPECT_EQ(gfx::Size(10, 20), pref); | 731 EXPECT_EQ(gfx::Size(10, 20), pref); |
| 659 | 732 |
| 660 layout.set_minimum_size(gfx::Size(40, 40)); | 733 layout.set_minimum_size(gfx::Size(40, 40)); |
| 661 GetPreferredSize(); | 734 GetPreferredSize(); |
| 662 EXPECT_EQ(gfx::Size(40, 40), pref); | 735 EXPECT_EQ(gfx::Size(40, 40), pref); |
| 663 | 736 |
| 664 RemoveAll(); | 737 RemoveAll(); |
| 665 } | 738 } |
| 666 | 739 |
| 667 } // namespace views | 740 } // namespace views |
| OLD | NEW |