OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/common/system/tray/size_range_layout.h" | |
6 #include "base/memory/ptr_util.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 #include "ui/views/test/test_layout_manager.h" | |
9 #include "ui/views/view.h" | |
10 | |
11 namespace ash { | |
12 | |
13 class SizeRangeLayoutTest : public testing::Test { | |
14 public: | |
15 SizeRangeLayoutTest(); | |
16 | |
17 // Wrapper function to access the minimum preferred size of |layout|. | |
18 gfx::Size GetMinSize(const SizeRangeLayout* layout) const; | |
19 | |
20 // Wrapper function to access the maximum preferred size of |layout|. | |
21 gfx::Size GetMaxSize(const SizeRangeLayout* layout) const; | |
22 | |
23 protected: | |
24 views::View host_; | |
25 | |
26 const gfx::Size kAbsoluteMinSize; | |
27 const gfx::Size kAbsoluteMaxSize; | |
28 | |
29 private: | |
30 DISALLOW_COPY_AND_ASSIGN(SizeRangeLayoutTest); | |
31 }; | |
32 | |
33 SizeRangeLayoutTest::SizeRangeLayoutTest() | |
34 : kAbsoluteMinSize(SizeRangeLayout::kAbsoluteMinSize, | |
35 SizeRangeLayout::kAbsoluteMinSize), | |
36 kAbsoluteMaxSize(SizeRangeLayout::kAbsoluteMaxSize, | |
37 SizeRangeLayout::kAbsoluteMaxSize) {} | |
38 | |
39 gfx::Size SizeRangeLayoutTest::GetMinSize(const SizeRangeLayout* layout) const { | |
40 return layout->min_size_; | |
41 } | |
42 | |
43 gfx::Size SizeRangeLayoutTest::GetMaxSize(const SizeRangeLayout* layout) const { | |
44 return layout->max_size_; | |
45 } | |
46 | |
47 TEST_F(SizeRangeLayoutTest, SizeRangeForDefaultConstruction) { | |
48 SizeRangeLayout layout; | |
49 EXPECT_EQ(kAbsoluteMinSize, GetMinSize(&layout)); | |
50 EXPECT_EQ(kAbsoluteMaxSize, GetMaxSize(&layout)); | |
51 } | |
52 | |
53 TEST_F(SizeRangeLayoutTest, SizeRangeForExplicitConstruction) { | |
54 const gfx::Size kSmallSize = gfx::Size(13, 14); | |
55 const gfx::Size kLargeSize = gfx::Size(25, 26); | |
56 | |
57 SizeRangeLayout layout(kSmallSize, kLargeSize); | |
58 EXPECT_EQ(kSmallSize, GetMinSize(&layout)); | |
59 EXPECT_EQ(kLargeSize, GetMaxSize(&layout)); | |
60 } | |
61 | |
62 TEST_F(SizeRangeLayoutTest, InvalidMinSizeForExplicitConstruction) { | |
63 const gfx::Size kInvalidSmallSize(-1, 2); | |
64 const gfx::Size kExpectedMinSize(0, 2); | |
65 | |
66 SizeRangeLayout layout(kInvalidSmallSize, kAbsoluteMaxSize); | |
67 EXPECT_EQ(kExpectedMinSize, GetMinSize(&layout)); | |
68 } | |
69 | |
70 TEST_F(SizeRangeLayoutTest, InvalidMaxSizeForExplicitConstruction) { | |
71 const gfx::Size kInvalidSmallSize(-1, 2); | |
72 const gfx::Size kExpectedMinSize(0, 2); | |
73 | |
74 SizeRangeLayout layout(kInvalidSmallSize, kAbsoluteMaxSize); | |
75 EXPECT_EQ(kExpectedMinSize, GetMinSize(&layout)); | |
76 } | |
77 | |
78 TEST_F(SizeRangeLayoutTest, MaxSizeSmallerThanMinSizeConstruction) { | |
79 const gfx::Size kMinSize(10, 11); | |
80 const gfx::Size kMaxSize(5, 6); | |
81 | |
82 SizeRangeLayout layout(kMinSize, kMaxSize); | |
83 EXPECT_EQ(kMaxSize, GetMinSize(&layout)); | |
84 EXPECT_EQ(kMaxSize, GetMaxSize(&layout)); | |
85 } | |
86 | |
87 TEST_F(SizeRangeLayoutTest, SizeRangeForExplicitSetSize) { | |
88 const gfx::Size kSize = gfx::Size(13, 14); | |
89 | |
90 SizeRangeLayout layout; | |
91 EXPECT_NE(kSize, GetMinSize(&layout)); | |
92 EXPECT_NE(kSize, GetMaxSize(&layout)); | |
93 | |
94 layout.SetSize(kSize); | |
95 EXPECT_EQ(kSize, GetMinSize(&layout)); | |
96 EXPECT_EQ(kSize, GetMaxSize(&layout)); | |
97 } | |
98 | |
99 TEST_F(SizeRangeLayoutTest, InvalidSizeRangesForExplicitSetSize) { | |
100 const gfx::Size kInvalidSize(-7, 8); | |
101 const gfx::Size kExpectedSize(0, 8); | |
102 | |
103 SizeRangeLayout layout; | |
104 layout.SetSize(kInvalidSize); | |
105 EXPECT_EQ(kExpectedSize, GetMinSize(&layout)); | |
106 EXPECT_EQ(kExpectedSize, GetMaxSize(&layout)); | |
107 } | |
108 | |
109 TEST_F(SizeRangeLayoutTest, InternalLayoutManagerPreferredSizeIsUsed) { | |
110 const gfx::Size kSize(7, 8); | |
111 std::unique_ptr<views::test::TestLayoutManager> child_layout = | |
112 base::MakeUnique<views::test::TestLayoutManager>(); | |
113 child_layout->set_preferred_size(kSize); | |
114 | |
115 SizeRangeLayout layout; | |
116 EXPECT_NE(kSize, layout.GetPreferredSize(&host_)); | |
117 | |
118 layout.SetLayoutManager(std::move(child_layout)); | |
119 EXPECT_EQ(kSize, layout.GetPreferredSize(&host_)); | |
120 } | |
121 | |
122 TEST_F(SizeRangeLayoutTest, SmallPreferredSizeIsClamped) { | |
123 const gfx::Size kMinSize(10, 10); | |
124 const gfx::Size kMaxSize(20, 20); | |
125 const gfx::Size kLayoutPreferredSize(5, 5); | |
126 std::unique_ptr<views::test::TestLayoutManager> child_layout = | |
127 base::MakeUnique<views::test::TestLayoutManager>(); | |
128 child_layout->set_preferred_size(kLayoutPreferredSize); | |
129 | |
130 SizeRangeLayout layout; | |
131 layout.SetLayoutManager(std::move(child_layout)); | |
132 layout.SetMinSize(kMinSize); | |
133 layout.SetMaxSize(kMaxSize); | |
134 EXPECT_EQ(kMinSize, layout.GetPreferredSize(&host_)); | |
135 } | |
136 | |
137 TEST_F(SizeRangeLayoutTest, LargePreferredSizeIsClamped) { | |
138 const gfx::Size kMinSize(10, 10); | |
139 const gfx::Size kMaxSize(20, 20); | |
140 const gfx::Size kLayoutPreferredSize(25, 25); | |
141 std::unique_ptr<views::test::TestLayoutManager> child_layout = | |
142 base::MakeUnique<views::test::TestLayoutManager>(); | |
143 child_layout->set_preferred_size(kLayoutPreferredSize); | |
144 | |
145 SizeRangeLayout layout; | |
146 layout.SetLayoutManager(std::move(child_layout)); | |
147 layout.SetMinSize(kMinSize); | |
148 layout.SetMaxSize(kMaxSize); | |
149 EXPECT_EQ(kMaxSize, layout.GetPreferredSize(&host_)); | |
150 } | |
151 | |
152 TEST_F(SizeRangeLayoutTest, MaxSizeLargerThanMinSizeUpdatesMinSize) { | |
153 const gfx::Size kMinSize(10, 10); | |
154 const gfx::Size kMaxSize(5, 5); | |
155 | |
156 SizeRangeLayout layout; | |
157 layout.SetMinSize(kMinSize); | |
158 EXPECT_EQ(kMinSize, GetMinSize(&layout)); | |
159 layout.SetMaxSize(kMaxSize); | |
160 EXPECT_EQ(kMaxSize, GetMinSize(&layout)); | |
161 } | |
162 | |
163 TEST_F(SizeRangeLayoutTest, MinSizeSmallerThanMaxSizeUpdatesMaxSize) { | |
164 const gfx::Size kMinSize(10, 10); | |
165 const gfx::Size kMaxSize(5, 5); | |
166 | |
167 SizeRangeLayout layout; | |
168 layout.SetMaxSize(kMaxSize); | |
169 EXPECT_EQ(kMaxSize, GetMaxSize(&layout)); | |
170 layout.SetMinSize(kMinSize); | |
171 EXPECT_EQ(kMinSize, GetMaxSize(&layout)); | |
172 } | |
173 | |
174 TEST_F(SizeRangeLayoutTest, | |
175 InternalLayoutManagerPreferredHeightForWidthIsUsed) { | |
176 const int kWidth = 5; | |
177 const int kHeight = 9; | |
178 std::unique_ptr<views::test::TestLayoutManager> child_layout = | |
179 base::MakeUnique<views::test::TestLayoutManager>(); | |
180 child_layout->set_preferred_height_for_width(kHeight); | |
181 | |
182 SizeRangeLayout layout; | |
183 EXPECT_NE(kHeight, layout.GetPreferredHeightForWidth(&host_, kWidth)); | |
184 | |
185 layout.SetLayoutManager(std::move(child_layout)); | |
186 EXPECT_EQ(kHeight, layout.GetPreferredHeightForWidth(&host_, kWidth)); | |
187 } | |
188 | |
189 } // namespace ash | |
OLD | NEW |