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