Chromium Code Reviews| Index: ash/common/system/tray/size_range_layout_unittest.cc |
| diff --git a/ash/common/system/tray/size_range_layout_unittest.cc b/ash/common/system/tray/size_range_layout_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c444c4c739181d30f0821674e586692ec354ca1d |
| --- /dev/null |
| +++ b/ash/common/system/tray/size_range_layout_unittest.cc |
| @@ -0,0 +1,162 @@ |
| +// Copyright 2016 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 "ash/common/system/tray/size_range_layout.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/views/test/test_layout_manager.h" |
| +#include "ui/views/view.h" |
| + |
| +namespace ash { |
| + |
| +class SizeRangeLayoutTest : public testing::Test { |
| + public: |
| + SizeRangeLayoutTest(); |
| + |
| + // Wrapper function to access the minimum preferred size of |layout|. |
| + gfx::Size GetMinSize(const SizeRangeLayout* layout) const; |
| + |
| + // Wrapper function to access the maximum preferred size of |layout|. |
| + gfx::Size GetMaxSize(const SizeRangeLayout* layout) const; |
| + |
| + protected: |
| + views::View host_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(SizeRangeLayoutTest); |
| +}; |
| + |
| +SizeRangeLayoutTest::SizeRangeLayoutTest() {} |
| + |
| +gfx::Size SizeRangeLayoutTest::GetMinSize(const SizeRangeLayout* layout) const { |
| + return layout->min_size_; |
| +} |
| + |
| +gfx::Size SizeRangeLayoutTest::GetMaxSize(const SizeRangeLayout* layout) const { |
| + return layout->max_size_; |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, SizeRangeForDefaultConstruction) { |
| + SizeRangeLayout layout; |
| + EXPECT_EQ(SizeRangeLayout::MinSize(), GetMinSize(&layout)); |
| + EXPECT_EQ(SizeRangeLayout::MaxSize(), GetMaxSize(&layout)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, SizeRangeForExplicitConstruction) { |
| + const gfx::Size kSmallSize = gfx::Size(13, 14); |
| + const gfx::Size kLargeSize = gfx::Size(25, 26); |
| + |
| + SizeRangeLayout layout(kSmallSize, kLargeSize); |
| + EXPECT_EQ(kSmallSize, GetMinSize(&layout)); |
| + EXPECT_EQ(kLargeSize, GetMaxSize(&layout)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, InvalidSizeRangeForExplicitConstruction) { |
| + const gfx::Size kInvalidSmallSize(-1, 2); |
| + const gfx::Size kExpectedMinSize(0, 2); |
| + const gfx::Size kInvalidLargeSize(5, -6); |
| + const gfx::Size kExpectedMaxSize(5, 0); |
| + |
| + SizeRangeLayout layout(kInvalidSmallSize, kInvalidLargeSize); |
| + EXPECT_EQ(kExpectedMinSize, GetMinSize(&layout)); |
| + EXPECT_EQ(kExpectedMaxSize, GetMaxSize(&layout)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, SizeRangeForExplicitSetSize) { |
| + const gfx::Size kSize = gfx::Size(13, 14); |
| + |
| + SizeRangeLayout layout; |
| + EXPECT_NE(kSize, GetMinSize(&layout)); |
| + EXPECT_NE(kSize, GetMaxSize(&layout)); |
| + |
| + layout.SetSize(kSize); |
| + EXPECT_EQ(kSize, GetMinSize(&layout)); |
| + EXPECT_EQ(kSize, GetMaxSize(&layout)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, InvalidSizeRangesForExplicitSetSize) { |
| + const gfx::Size kInvalidSize(-7, 8); |
| + const gfx::Size kExpectedSize(0, 8); |
| + |
| + SizeRangeLayout layout; |
| + layout.SetSize(kInvalidSize); |
| + EXPECT_EQ(kExpectedSize, GetMinSize(&layout)); |
| + EXPECT_EQ(kExpectedSize, GetMaxSize(&layout)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, GetPreferredSizeWithoutLayoutManager) { |
| + SizeRangeLayout layout; |
| + layout.SetLayoutManager(nullptr); |
| + EXPECT_EQ(gfx::Size(), layout.GetPreferredSize(&host_)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, InternalLayoutManagerPreferredSizeIsUsed) { |
| + const gfx::Size kSize(7, 8); |
| + std::unique_ptr<views::test::TestLayoutManager> child_layout = |
| + base::MakeUnique<views::test::TestLayoutManager>(); |
| + child_layout->set_preferred_size(kSize); |
| + |
| + SizeRangeLayout layout; |
| + EXPECT_NE(kSize, layout.GetPreferredSize(&host_)); |
| + |
| + layout.SetLayoutManager(std::move(child_layout)); |
| + EXPECT_EQ(kSize, layout.GetPreferredSize(&host_)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, SmallPreferredSizeIsClamped) { |
| + const gfx::Size kMinSize(10, 10); |
| + const gfx::Size kMaxSize(20, 20); |
| + const gfx::Size kLayoutPreferredSize(5, 5); |
| + std::unique_ptr<views::test::TestLayoutManager> child_layout = |
| + base::MakeUnique<views::test::TestLayoutManager>(); |
| + child_layout->set_preferred_size(kLayoutPreferredSize); |
| + |
| + SizeRangeLayout layout; |
| + layout.SetLayoutManager(std::move(child_layout)); |
| + layout.SetMinSize(kMinSize); |
| + layout.SetMaxSize(kMaxSize); |
| + EXPECT_EQ(kMinSize, layout.GetPreferredSize(&host_)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, LargePreferredSizeIsClamped) { |
| + const gfx::Size kMinSize(10, 10); |
| + const gfx::Size kMaxSize(20, 20); |
| + const gfx::Size kLayoutPreferredSize(25, 25); |
| + std::unique_ptr<views::test::TestLayoutManager> child_layout = |
| + base::MakeUnique<views::test::TestLayoutManager>(); |
| + child_layout->set_preferred_size(kLayoutPreferredSize); |
| + |
| + SizeRangeLayout layout; |
| + layout.SetLayoutManager(std::move(child_layout)); |
| + layout.SetMinSize(kMinSize); |
| + layout.SetMaxSize(kMaxSize); |
| + EXPECT_EQ(kMaxSize, layout.GetPreferredSize(&host_)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, MaxSizeTakesPrecedenceOverMinSize) { |
|
tdanderson
2016/10/19 18:30:59
Does max size win here because it was set after th
bruthig
2016/10/20 04:40:12
MaxSize wins because of the implementation of Size
tdanderson
2016/10/20 18:16:48
Works for me, thanks.
|
| + const gfx::Size kMinSize(10, 10); |
| + const gfx::Size kMaxSize(5, 5); |
| + |
| + SizeRangeLayout layout; |
| + layout.SetMinSize(kMinSize); |
| + layout.SetMaxSize(kMaxSize); |
| + EXPECT_EQ(kMaxSize, layout.GetPreferredSize(&host_)); |
| +} |
| + |
| +TEST_F(SizeRangeLayoutTest, |
| + InternalLayoutManagerPreferredHeightForWidthIsUsed) { |
| + const int kWidth = 5; |
| + const int kHeight = 9; |
| + std::unique_ptr<views::test::TestLayoutManager> child_layout = |
| + base::MakeUnique<views::test::TestLayoutManager>(); |
| + child_layout->set_preferred_height_for_width(kHeight); |
| + |
| + SizeRangeLayout layout; |
| + EXPECT_NE(kHeight, layout.GetPreferredHeightForWidth(&host_, kWidth)); |
| + |
| + layout.SetLayoutManager(std::move(child_layout)); |
| + EXPECT_EQ(kHeight, layout.GetPreferredHeightForWidth(&host_, kWidth)); |
| +} |
| + |
| +} // namespace ash |