[email protected] | f861719 | 2010-07-05 07:57:10 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 5 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 6 | #include "views/grid_layout.h" |
| 7 | #include "views/view.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 9 | using views::ColumnSet; |
| 10 | using views::GridLayout; |
| 11 | using views::View; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | |
| 13 | static void ExpectViewBoundsEquals(int x, int y, int w, int h, |
| 14 | const View* view) { |
[email protected] | 6f3bb6c | 2008-09-17 22:25:33 | [diff] [blame] | 15 | EXPECT_EQ(x, view->x()); |
| 16 | EXPECT_EQ(y, view->y()); |
| 17 | EXPECT_EQ(w, view->width()); |
| 18 | EXPECT_EQ(h, view->height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | class SettableSizeView : public View { |
| 22 | public: |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 23 | explicit SettableSizeView(const gfx::Size& pref) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | pref_ = pref; |
| 25 | } |
| 26 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 27 | virtual gfx::Size GetPreferredSize() { |
| 28 | return pref_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | private: |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 32 | gfx::Size pref_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | }; |
| 34 | |
[email protected] | f861719 | 2010-07-05 07:57:10 | [diff] [blame] | 35 | // A view with fixed circumference that trades height for width. |
| 36 | class FlexibleView : public View { |
| 37 | public: |
| 38 | explicit FlexibleView(int circumference) { |
| 39 | circumference_ = circumference; |
| 40 | } |
| 41 | |
| 42 | virtual gfx::Size GetPreferredSize() { |
| 43 | return gfx::Size(0, circumference_ / 2); |
| 44 | } |
| 45 | |
| 46 | virtual int GetHeightForWidth(int width) { |
| 47 | return std::max(0, circumference_ / 2 - width); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | int circumference_; |
| 52 | }; |
| 53 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | class GridLayoutTest : public testing::Test { |
| 55 | public: |
| 56 | virtual void SetUp() { |
| 57 | layout = new GridLayout(&host); |
| 58 | } |
| 59 | |
| 60 | virtual void TearDown() { |
| 61 | delete layout; |
| 62 | } |
| 63 | |
| 64 | virtual void RemoveAll() { |
| 65 | for (int i = host.GetChildViewCount() - 1; i >= 0; i--) { |
| 66 | host.RemoveChildView(host.GetChildViewAt(i)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void GetPreferredSize() { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 71 | pref = layout->GetPreferredSize(&host); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 74 | gfx::Size pref; |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 75 | gfx::Rect bounds; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | View host; |
| 77 | GridLayout* layout; |
| 78 | }; |
| 79 | |
| 80 | class GridLayoutAlignmentTest : public testing::Test { |
| 81 | public: |
| 82 | GridLayoutAlignmentTest() : |
| 83 | host(), |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 84 | v1(gfx::Size(10, 20)), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | layout(new GridLayout(&host)) {} |
| 86 | |
| 87 | virtual void SetUp() { |
| 88 | } |
| 89 | |
| 90 | virtual void TearDown() { |
| 91 | delete layout; |
| 92 | } |
| 93 | |
| 94 | virtual void RemoveAll() { |
| 95 | for (int i = host.GetChildViewCount() - 1; i >= 0; i--) { |
| 96 | host.RemoveChildView(host.GetChildViewAt(i)); |
| 97 | } |
| 98 | } |
| 99 | |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 100 | void TestAlignment(GridLayout::Alignment alignment, gfx::Rect* bounds) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 102 | c1->AddColumn(alignment, alignment, 1, GridLayout::USE_PREF, 0, 0); |
| 103 | layout->StartRow(1, 0); |
| 104 | layout->AddView(&v1); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 105 | gfx::Size pref = layout->GetPreferredSize(&host); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 106 | EXPECT_EQ(gfx::Size(10, 20), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | host.SetBounds(0, 0, 100, 100); |
| 108 | layout->Layout(&host); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 109 | *bounds = v1.bounds(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | RemoveAll(); |
| 111 | } |
| 112 | |
| 113 | View host; |
| 114 | SettableSizeView v1; |
| 115 | GridLayout* layout; |
| 116 | }; |
| 117 | |
| 118 | TEST_F(GridLayoutAlignmentTest, Fill) { |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 119 | gfx::Rect bounds; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | TestAlignment(GridLayout::FILL, &bounds); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 121 | EXPECT_EQ(gfx::Rect(0, 0, 100, 100), bounds); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_F(GridLayoutAlignmentTest, Leading) { |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 125 | gfx::Rect bounds; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | TestAlignment(GridLayout::LEADING, &bounds); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 127 | EXPECT_EQ(gfx::Rect(0, 0, 10, 20), bounds); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST_F(GridLayoutAlignmentTest, Center) { |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 131 | gfx::Rect bounds; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | TestAlignment(GridLayout::CENTER, &bounds); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 133 | EXPECT_EQ(gfx::Rect(45, 40, 10, 20), bounds); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST_F(GridLayoutAlignmentTest, Trailing) { |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 137 | gfx::Rect bounds; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | TestAlignment(GridLayout::TRAILING, &bounds); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 139 | EXPECT_EQ(gfx::Rect(90, 80, 10, 20), bounds); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TEST_F(GridLayoutTest, TwoColumns) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 143 | SettableSizeView v1(gfx::Size(10, 20)); |
| 144 | SettableSizeView v2(gfx::Size(20, 20)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 146 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 147 | 0, GridLayout::USE_PREF, 0, 0); |
| 148 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 149 | 0, GridLayout::USE_PREF, 0, 0); |
| 150 | layout->StartRow(0, 0); |
| 151 | layout->AddView(&v1); |
| 152 | layout->AddView(&v2); |
| 153 | |
| 154 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 155 | EXPECT_EQ(gfx::Size(30, 20), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 157 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | layout->Layout(&host); |
| 159 | ExpectViewBoundsEquals(0, 0, 10, 20, &v1); |
| 160 | ExpectViewBoundsEquals(10, 0, 20, 20, &v2); |
| 161 | |
| 162 | RemoveAll(); |
| 163 | } |
| 164 | |
| 165 | TEST_F(GridLayoutTest, ColSpan1) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 166 | SettableSizeView v1(gfx::Size(100, 20)); |
| 167 | SettableSizeView v2(gfx::Size(10, 40)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 169 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 170 | 0, GridLayout::USE_PREF, 0, 0); |
| 171 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 172 | 1, GridLayout::USE_PREF, 0, 0); |
| 173 | layout->StartRow(0, 0); |
| 174 | layout->AddView(&v1, 2, 1); |
| 175 | layout->StartRow(0, 0); |
| 176 | layout->AddView(&v2); |
| 177 | |
| 178 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 179 | EXPECT_EQ(gfx::Size(100, 60), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 180 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 181 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | layout->Layout(&host); |
| 183 | ExpectViewBoundsEquals(0, 0, 100, 20, &v1); |
| 184 | ExpectViewBoundsEquals(0, 20, 10, 40, &v2); |
| 185 | |
| 186 | RemoveAll(); |
| 187 | } |
| 188 | |
| 189 | TEST_F(GridLayoutTest, ColSpan2) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 190 | SettableSizeView v1(gfx::Size(100, 20)); |
| 191 | SettableSizeView v2(gfx::Size(10, 20)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 193 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 194 | 1, GridLayout::USE_PREF, 0, 0); |
| 195 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 196 | 0, GridLayout::USE_PREF, 0, 0); |
| 197 | layout->StartRow(0, 0); |
| 198 | layout->AddView(&v1, 2, 1); |
| 199 | layout->StartRow(0, 0); |
| 200 | layout->SkipColumns(1); |
| 201 | layout->AddView(&v2); |
| 202 | |
| 203 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 204 | EXPECT_EQ(gfx::Size(100, 40), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 206 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | layout->Layout(&host); |
| 208 | ExpectViewBoundsEquals(0, 0, 100, 20, &v1); |
| 209 | ExpectViewBoundsEquals(90, 20, 10, 20, &v2); |
| 210 | |
| 211 | RemoveAll(); |
| 212 | } |
| 213 | |
| 214 | TEST_F(GridLayoutTest, ColSpan3) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 215 | SettableSizeView v1(gfx::Size(100, 20)); |
| 216 | SettableSizeView v2(gfx::Size(10, 20)); |
| 217 | SettableSizeView v3(gfx::Size(10, 20)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 219 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 220 | 0, GridLayout::USE_PREF, 0, 0); |
| 221 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 222 | 0, GridLayout::USE_PREF, 0, 0); |
| 223 | layout->StartRow(0, 0); |
| 224 | layout->AddView(&v1, 2, 1); |
| 225 | layout->StartRow(0, 0); |
| 226 | layout->AddView(&v2); |
| 227 | layout->AddView(&v3); |
| 228 | |
| 229 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 230 | EXPECT_EQ(gfx::Size(100, 40), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 232 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | layout->Layout(&host); |
| 234 | ExpectViewBoundsEquals(0, 0, 100, 20, &v1); |
| 235 | ExpectViewBoundsEquals(0, 20, 10, 20, &v2); |
| 236 | ExpectViewBoundsEquals(50, 20, 10, 20, &v3); |
| 237 | |
| 238 | RemoveAll(); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | TEST_F(GridLayoutTest, ColSpan4) { |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 243 | views::ColumnSet* set = layout->AddColumnSet(0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | |
| 245 | set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0, |
| 246 | GridLayout::USE_PREF, 0, 0); |
| 247 | set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0, |
| 248 | GridLayout::USE_PREF, 0, 0); |
| 249 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 250 | SettableSizeView v1(gfx::Size(10, 10)); |
| 251 | SettableSizeView v2(gfx::Size(10, 10)); |
| 252 | SettableSizeView v3(gfx::Size(25, 20)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | layout->StartRow(0, 0); |
| 254 | layout->AddView(&v1); |
| 255 | layout->AddView(&v2); |
| 256 | layout->StartRow(0, 0); |
| 257 | layout->AddView(&v3, 2, 1); |
| 258 | |
| 259 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 260 | EXPECT_EQ(gfx::Size(25, 30), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 261 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 262 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 263 | layout->Layout(&host); |
| 264 | ExpectViewBoundsEquals(0, 0, 10, 10, &v1); |
| 265 | ExpectViewBoundsEquals(12, 0, 10, 10, &v2); |
| 266 | ExpectViewBoundsEquals(0, 10, 25, 20, &v3); |
| 267 | |
| 268 | RemoveAll(); |
| 269 | } |
| 270 | |
| 271 | TEST_F(GridLayoutTest, SameSizeColumns) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 272 | SettableSizeView v1(gfx::Size(50, 20)); |
| 273 | SettableSizeView v2(gfx::Size(10, 10)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 275 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 276 | 0, GridLayout::USE_PREF, 0, 0); |
| 277 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 278 | 0, GridLayout::USE_PREF, 0, 0); |
| 279 | c1->LinkColumnSizes(0, 1, -1); |
| 280 | layout->StartRow(0, 0); |
| 281 | layout->AddView(&v1); |
| 282 | layout->AddView(&v2); |
| 283 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 284 | gfx::Size pref = layout->GetPreferredSize(&host); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 285 | EXPECT_EQ(gfx::Size(100, 20), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 287 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | layout->Layout(&host); |
| 289 | ExpectViewBoundsEquals(0, 0, 50, 20, &v1); |
| 290 | ExpectViewBoundsEquals(50, 0, 10, 10, &v2); |
| 291 | |
| 292 | RemoveAll(); |
| 293 | } |
| 294 | |
| 295 | TEST_F(GridLayoutTest, HorizontalResizeTest1) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 296 | SettableSizeView v1(gfx::Size(50, 20)); |
| 297 | SettableSizeView v2(gfx::Size(10, 10)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 298 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 299 | c1->AddColumn(GridLayout::FILL, GridLayout::LEADING, |
| 300 | 1, GridLayout::USE_PREF, 0, 0); |
| 301 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 302 | 0, GridLayout::USE_PREF, 0, 0); |
| 303 | layout->StartRow(0, 0); |
| 304 | layout->AddView(&v1); |
| 305 | layout->AddView(&v2); |
| 306 | |
| 307 | host.SetBounds(0, 0, 110, 20); |
| 308 | layout->Layout(&host); |
| 309 | ExpectViewBoundsEquals(0, 0, 100, 20, &v1); |
| 310 | ExpectViewBoundsEquals(100, 0, 10, 10, &v2); |
| 311 | |
| 312 | RemoveAll(); |
| 313 | } |
| 314 | |
| 315 | TEST_F(GridLayoutTest, HorizontalResizeTest2) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 316 | SettableSizeView v1(gfx::Size(50, 20)); |
| 317 | SettableSizeView v2(gfx::Size(10, 10)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 318 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 319 | c1->AddColumn(GridLayout::FILL, GridLayout::LEADING, |
| 320 | 1, GridLayout::USE_PREF, 0, 0); |
| 321 | c1->AddColumn(GridLayout::TRAILING, GridLayout::LEADING, |
| 322 | 1, GridLayout::USE_PREF, 0, 0); |
| 323 | layout->StartRow(0, 0); |
| 324 | layout->AddView(&v1); |
| 325 | layout->AddView(&v2); |
| 326 | |
| 327 | host.SetBounds(0, 0, 120, 20); |
| 328 | layout->Layout(&host); |
| 329 | ExpectViewBoundsEquals(0, 0, 80, 20, &v1); |
| 330 | ExpectViewBoundsEquals(110, 0, 10, 10, &v2); |
| 331 | |
| 332 | RemoveAll(); |
| 333 | } |
| 334 | |
| 335 | TEST_F(GridLayoutTest, TestVerticalResize1) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 336 | SettableSizeView v1(gfx::Size(50, 20)); |
| 337 | SettableSizeView v2(gfx::Size(10, 10)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 339 | c1->AddColumn(GridLayout::FILL, GridLayout::FILL, |
| 340 | 1, GridLayout::USE_PREF, 0, 0); |
| 341 | layout->StartRow(1, 0); |
| 342 | layout->AddView(&v1); |
| 343 | layout->StartRow(0, 0); |
| 344 | layout->AddView(&v2); |
| 345 | |
| 346 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 347 | EXPECT_EQ(gfx::Size(50, 30), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | |
| 349 | host.SetBounds(0, 0, 50, 100); |
| 350 | layout->Layout(&host); |
| 351 | ExpectViewBoundsEquals(0, 0, 50, 90, &v1); |
| 352 | ExpectViewBoundsEquals(0, 90, 50, 10, &v2); |
| 353 | |
| 354 | RemoveAll(); |
| 355 | } |
| 356 | |
| 357 | TEST_F(GridLayoutTest, Insets) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 358 | SettableSizeView v1(gfx::Size(10, 20)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | ColumnSet* c1 = layout->AddColumnSet(0); |
| 360 | layout->SetInsets(1, 2, 3, 4); |
| 361 | c1->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 362 | 0, GridLayout::USE_PREF, 0, 0); |
| 363 | layout->StartRow(0, 0); |
| 364 | layout->AddView(&v1); |
| 365 | |
| 366 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 367 | EXPECT_EQ(gfx::Size(16, 24), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 368 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 369 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 370 | layout->Layout(&host); |
| 371 | ExpectViewBoundsEquals(2, 1, 10, 20, &v1); |
| 372 | |
| 373 | RemoveAll(); |
| 374 | } |
| 375 | |
| 376 | TEST_F(GridLayoutTest, FixedSize) { |
| 377 | layout->SetInsets(2, 2, 2, 2); |
| 378 | |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 379 | views::ColumnSet* set = layout->AddColumnSet(0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 380 | |
| 381 | int column_count = 4; |
| 382 | int title_width = 100; |
| 383 | int row_count = 2; |
| 384 | int pref_width = 10; |
| 385 | int pref_height = 20; |
| 386 | |
| 387 | for (int i = 0; i < column_count; ++i) { |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 388 | set->AddColumn(views::GridLayout::CENTER, |
| 389 | views::GridLayout::CENTER, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | 0, |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 391 | views::GridLayout::FIXED, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | title_width, |
| 393 | title_width); |
| 394 | } |
| 395 | |
| 396 | for (int row = 0; row < row_count; ++row) { |
| 397 | layout->StartRow(0, 0); |
| 398 | for (int col = 0; col < column_count; ++col) { |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 399 | layout->AddView(new SettableSizeView(gfx::Size(pref_width, pref_height))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | layout->Layout(&host); |
| 404 | |
| 405 | for (int i = 0; i < column_count; ++i) { |
| 406 | for (int row = 0; row < row_count; ++row) { |
| 407 | View* view = host.GetChildViewAt(row * column_count + i); |
[email protected] | 72d1e59 | 2009-03-10 17:39:46 | [diff] [blame] | 408 | ExpectViewBoundsEquals( |
| 409 | 2 + title_width * i + (title_width - pref_width) / 2, |
| 410 | 2 + pref_height * row, |
| 411 | pref_width, |
| 412 | pref_height, view); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 417 | EXPECT_EQ(gfx::Size(column_count * title_width + 4, |
| 418 | row_count * pref_height + 4), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | TEST_F(GridLayoutTest, RowSpanWithPaddingRow) { |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 422 | views::ColumnSet* set = layout->AddColumnSet(0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 423 | |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 424 | set->AddColumn(views::GridLayout::CENTER, |
| 425 | views::GridLayout::CENTER, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | 0, |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 427 | views::GridLayout::FIXED, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 428 | 10, |
| 429 | 10); |
| 430 | |
| 431 | layout->StartRow(0, 0); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 432 | layout->AddView(new SettableSizeView(gfx::Size(10, 10)), 1, 2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 433 | layout->AddPaddingRow(0, 10); |
| 434 | } |
| 435 | |
| 436 | TEST_F(GridLayoutTest, RowSpan) { |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 437 | views::ColumnSet* set = layout->AddColumnSet(0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 438 | |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 439 | set->AddColumn(views::GridLayout::LEADING, |
| 440 | views::GridLayout::LEADING, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | 0, |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 442 | views::GridLayout::USE_PREF, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 443 | 0, |
| 444 | 0); |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 445 | set->AddColumn(views::GridLayout::LEADING, |
| 446 | views::GridLayout::LEADING, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | 0, |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 448 | views::GridLayout::USE_PREF, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 449 | 0, |
| 450 | 0); |
| 451 | |
| 452 | layout->StartRow(0, 0); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 453 | layout->AddView(new SettableSizeView(gfx::Size(20, 10))); |
| 454 | layout->AddView(new SettableSizeView(gfx::Size(20, 40)), 1, 2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 455 | layout->StartRow(1, 0); |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 456 | views::View* s3 = new SettableSizeView(gfx::Size(20, 10)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | layout->AddView(s3); |
| 458 | |
| 459 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 460 | EXPECT_EQ(gfx::Size(40, 40), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 461 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 462 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 463 | layout->Layout(&host); |
| 464 | ExpectViewBoundsEquals(0, 10, 20, 10, s3); |
| 465 | } |
| 466 | |
| 467 | TEST_F(GridLayoutTest, RowSpan2) { |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 468 | views::ColumnSet* set = layout->AddColumnSet(0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | |
| 470 | set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 471 | 0, GridLayout::USE_PREF, 0, 0); |
| 472 | set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 473 | 0,GridLayout::USE_PREF, 0, 0); |
| 474 | |
| 475 | layout->StartRow(0, 0); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 476 | layout->AddView(new SettableSizeView(gfx::Size(20, 20))); |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 477 | views::View* s3 = new SettableSizeView(gfx::Size(64, 64)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 478 | layout->AddView(s3, 1, 3); |
| 479 | |
| 480 | layout->AddPaddingRow(0, 10); |
| 481 | |
| 482 | layout->StartRow(0, 0); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 483 | layout->AddView(new SettableSizeView(gfx::Size(10, 20))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | |
| 485 | GetPreferredSize(); |
[email protected] | f71e726 | 2009-05-14 19:13:13 | [diff] [blame] | 486 | EXPECT_EQ(gfx::Size(84, 64), pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 487 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 488 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 489 | layout->Layout(&host); |
| 490 | ExpectViewBoundsEquals(20, 0, 64, 64, s3); |
| 491 | } |
| 492 | |
| 493 | TEST_F(GridLayoutTest, FixedViewWidth) { |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 494 | views::ColumnSet* set = layout->AddColumnSet(0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 495 | |
| 496 | set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 497 | 0, GridLayout::USE_PREF, 0, 0); |
| 498 | set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 499 | 0,GridLayout::USE_PREF, 0, 0); |
| 500 | |
| 501 | layout->StartRow(0, 0); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 502 | View* view = new SettableSizeView(gfx::Size(30, 40)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 503 | layout->AddView(view, 1, 1, GridLayout::LEADING, GridLayout::LEADING, 10, 0); |
| 504 | |
| 505 | GetPreferredSize(); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 506 | EXPECT_EQ(10, pref.width()); |
| 507 | EXPECT_EQ(40, pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 508 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 509 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | layout->Layout(&host); |
| 511 | ExpectViewBoundsEquals(0, 0, 10, 40, view); |
| 512 | } |
| 513 | |
| 514 | TEST_F(GridLayoutTest, FixedViewHeight) { |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 515 | views::ColumnSet* set = layout->AddColumnSet(0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | |
| 517 | set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 518 | 0, GridLayout::USE_PREF, 0, 0); |
| 519 | set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, |
| 520 | 0,GridLayout::USE_PREF, 0, 0); |
| 521 | |
| 522 | layout->StartRow(0, 0); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 523 | View* view = new SettableSizeView(gfx::Size(30, 40)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 524 | layout->AddView(view, 1, 1, GridLayout::LEADING, GridLayout::LEADING, 0, 10); |
| 525 | |
| 526 | GetPreferredSize(); |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 527 | EXPECT_EQ(30, pref.width()); |
| 528 | EXPECT_EQ(10, pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | |
[email protected] | 154f8bc | 2008-10-15 18:02:30 | [diff] [blame] | 530 | host.SetBounds(0, 0, pref.width(), pref.height()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | layout->Layout(&host); |
| 532 | ExpectViewBoundsEquals(0, 0, 30, 10, view); |
| 533 | } |
[email protected] | a665d47 | 2010-02-24 22:17:03 | [diff] [blame] | 534 | |
| 535 | // Make sure that for views that span columns the underlying columns are resized |
| 536 | // based on the resize percent of the column. |
| 537 | TEST_F(GridLayoutTest, ColumnSpanResizing) { |
| 538 | views::ColumnSet* set = layout->AddColumnSet(0); |
| 539 | |
| 540 | set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 541 | 2, views::GridLayout::USE_PREF, 0, 0); |
| 542 | set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 543 | 4, views::GridLayout::USE_PREF, 0, 0); |
| 544 | |
| 545 | layout->StartRow(0, 0); |
| 546 | // span_view spans two columns and is twice as big the views added below. |
| 547 | View* span_view = new SettableSizeView(gfx::Size(12, 40)); |
| 548 | layout->AddView(span_view, 2, 1, GridLayout::LEADING, GridLayout::LEADING); |
| 549 | |
| 550 | layout->StartRow(0, 0); |
| 551 | View* view1 = new SettableSizeView(gfx::Size(2, 40)); |
| 552 | View* view2 = new SettableSizeView(gfx::Size(4, 40)); |
| 553 | layout->AddView(view1); |
| 554 | layout->AddView(view2); |
| 555 | |
| 556 | host.SetBounds(0, 0, 12, 80); |
| 557 | layout->Layout(&host); |
| 558 | |
| 559 | ExpectViewBoundsEquals(0, 0, 12, 40, span_view); |
| 560 | |
| 561 | // view1 should be 4 pixels wide |
| 562 | // column_pref + (remaining_width * column_resize / total_column_resize) = |
| 563 | // 2 + (6 * 2 / 6). |
| 564 | ExpectViewBoundsEquals(0, 40, 4, 40, view1); |
| 565 | |
| 566 | // And view2 should be 8 pixels wide: |
| 567 | // 4 + (6 * 4 / 6). |
| 568 | ExpectViewBoundsEquals(4, 40, 8, 40, view2); |
| 569 | } |
[email protected] | f861719 | 2010-07-05 07:57:10 | [diff] [blame] | 570 | |
| 571 | // Check that GetPreferredSize() takes resizing of columns into account when |
| 572 | // there is additional space in the case we have column sets of different |
| 573 | // preferred sizes. |
| 574 | TEST_F(GridLayoutTest, ColumnResizingOnGetPreferredSize) { |
| 575 | views::ColumnSet* set = layout->AddColumnSet(0); |
| 576 | set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 577 | 1, views::GridLayout::USE_PREF, 0, 0); |
| 578 | |
| 579 | set = layout->AddColumnSet(1); |
| 580 | set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 581 | 1, views::GridLayout::USE_PREF, 0, 0); |
| 582 | |
| 583 | set = layout->AddColumnSet(2); |
| 584 | set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
| 585 | 1, views::GridLayout::USE_PREF, 0, 0); |
| 586 | |
| 587 | // Make a row containing a flexible view that trades width for height. |
| 588 | layout->StartRow(0, 0); |
| 589 | View* view1 = new FlexibleView(100); |
| 590 | layout->AddView(view1, 1, 1, GridLayout::FILL, GridLayout::LEADING); |
| 591 | |
| 592 | // The second row contains a view of fixed size that will enforce a column |
| 593 | // width of 20 pixels. |
| 594 | layout->StartRow(0, 1); |
| 595 | View* view2 = new SettableSizeView(gfx::Size(20, 20)); |
| 596 | layout->AddView(view2, 1, 1, GridLayout::FILL, GridLayout::LEADING); |
| 597 | |
| 598 | // Add another flexible view in row three in order to ensure column set |
| 599 | // ordering doesn't influence sizing behaviour. |
| 600 | layout->StartRow(0, 2); |
| 601 | View* view3 = new FlexibleView(40); |
| 602 | layout->AddView(view3, 1, 1, GridLayout::FILL, GridLayout::LEADING); |
| 603 | |
| 604 | // We expect a height of 50: 30 from the variable width view in the first row |
| 605 | // plus 20 from the statically sized view in the second row. The flexible |
| 606 | // view in the third row should contribute no height. |
| 607 | EXPECT_EQ(gfx::Size(20, 50), layout->GetPreferredSize(&host)); |
| 608 | } |