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/three_view_layout.h" |
| 6 #include "base/memory/ptr_util.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/gfx/geometry/insets.h" |
| 9 #include "ui/gfx/geometry/rect_conversions.h" |
| 10 #include "ui/views/test/test_layout_manager.h" |
| 11 #include "ui/views/test/test_views.h" |
| 12 #include "ui/views/view.h" |
| 13 |
| 14 namespace ash { |
| 15 |
| 16 class ThreeViewLayoutTest : public testing::Test { |
| 17 public: |
| 18 ThreeViewLayoutTest(); |
| 19 |
| 20 protected: |
| 21 // Returns the bounds of |child| in the coordinate space of |host_|. |
| 22 gfx::Rect GetBoundsInHost(const views::View* child) const; |
| 23 |
| 24 // Wrapper functions to access the internals of |layout_|. |
| 25 views::View* GetContainer(ThreeViewLayout::Container container) const; |
| 26 |
| 27 // The test target. |
| 28 ThreeViewLayout* layout_; |
| 29 |
| 30 // The View that the test target is installed on. |
| 31 std::unique_ptr<views::View> host_; |
| 32 |
| 33 private: |
| 34 DISALLOW_COPY_AND_ASSIGN(ThreeViewLayoutTest); |
| 35 }; |
| 36 |
| 37 ThreeViewLayoutTest::ThreeViewLayoutTest() |
| 38 : layout_(new ThreeViewLayout()), host_(new views::View) { |
| 39 host_->SetLayoutManager(layout_); |
| 40 } |
| 41 |
| 42 gfx::Rect ThreeViewLayoutTest::GetBoundsInHost(const views::View* child) const { |
| 43 gfx::RectF rect_f(child->bounds()); |
| 44 views::View::ConvertRectToTarget(child, host_.get(), &rect_f); |
| 45 return ToNearestRect(rect_f); |
| 46 } |
| 47 |
| 48 views::View* ThreeViewLayoutTest::GetContainer( |
| 49 ThreeViewLayout::Container container) const { |
| 50 return layout_->GetContainer(container); |
| 51 } |
| 52 |
| 53 TEST_F(ThreeViewLayoutTest, PaddingBetweenContainers) { |
| 54 const int kPaddingBetweenContainers = 3; |
| 55 const int kViewWidth = 10; |
| 56 const int kViewHeight = 10; |
| 57 const gfx::Size kViewSize(kViewWidth, kViewHeight); |
| 58 const int kStartChildExpectedX = 0; |
| 59 const int kCenterChildExpectedX = |
| 60 kStartChildExpectedX + kViewWidth + kPaddingBetweenContainers; |
| 61 const int kEndChildExpectedX = |
| 62 kCenterChildExpectedX + kViewWidth + kPaddingBetweenContainers; |
| 63 layout_ = new ThreeViewLayout(kPaddingBetweenContainers); |
| 64 host_->SetLayoutManager(layout_); |
| 65 |
| 66 host_->SetBounds(0, 0, 100, 10); |
| 67 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 68 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 69 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 70 |
| 71 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 72 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 73 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 74 |
| 75 host_->Layout(); |
| 76 |
| 77 EXPECT_EQ(kStartChildExpectedX, GetBoundsInHost(start_child).x()); |
| 78 EXPECT_EQ(kCenterChildExpectedX, GetBoundsInHost(center_child).x()); |
| 79 EXPECT_EQ(kEndChildExpectedX, GetBoundsInHost(end_child).x()); |
| 80 } |
| 81 |
| 82 TEST_F(ThreeViewLayoutTest, VerticalOrientation) { |
| 83 const int kViewWidth = 10; |
| 84 const int kViewHeight = 10; |
| 85 const gfx::Size kViewSize(kViewWidth, kViewHeight); |
| 86 |
| 87 layout_ = new ThreeViewLayout(ThreeViewLayout::Orientation::VERTICAL); |
| 88 host_->SetLayoutManager(layout_); |
| 89 |
| 90 host_->SetBounds(0, 0, 10, 100); |
| 91 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 92 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 93 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 94 |
| 95 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 96 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 97 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 98 |
| 99 host_->Layout(); |
| 100 |
| 101 EXPECT_EQ(0, GetBoundsInHost(start_child).y()); |
| 102 EXPECT_EQ(kViewWidth, GetBoundsInHost(center_child).y()); |
| 103 EXPECT_EQ(kViewWidth * 2, GetBoundsInHost(end_child).y()); |
| 104 } |
| 105 |
| 106 TEST_F(ThreeViewLayoutTest, ContainerViewsRemovedFromHost) { |
| 107 EXPECT_NE(0, host_->child_count()); |
| 108 host_->SetLayoutManager(nullptr); |
| 109 EXPECT_EQ(0, host_->child_count()); |
| 110 } |
| 111 |
| 112 TEST_F(ThreeViewLayoutTest, MinCrossAxisSize) { |
| 113 const int kMinCrossAxisSize = 15; |
| 114 EXPECT_EQ(0, layout_->GetPreferredSize(host_.get()).height()); |
| 115 layout_->SetMinCrossAxisSize(kMinCrossAxisSize); |
| 116 EXPECT_EQ(kMinCrossAxisSize, layout_->GetPreferredSize(host_.get()).height()); |
| 117 EXPECT_EQ(kMinCrossAxisSize, |
| 118 layout_->GetPreferredHeightForWidth(host_.get(), 0)); |
| 119 } |
| 120 |
| 121 TEST_F(ThreeViewLayoutTest, MainAxisMinSize) { |
| 122 host_->SetBounds(0, 0, 100, 10); |
| 123 const gfx::Size kMinSize(15, 10); |
| 124 layout_->SetMinSize(ThreeViewLayout::Container::START, kMinSize); |
| 125 views::View* child = new views::StaticSizedView(gfx::Size(10, 10)); |
| 126 layout_->AddView(ThreeViewLayout::Container::CENTER, child); |
| 127 host_->Layout(); |
| 128 |
| 129 EXPECT_EQ(kMinSize.width(), GetBoundsInHost(child).x()); |
| 130 } |
| 131 |
| 132 TEST_F(ThreeViewLayoutTest, MainAxisMaxSize) { |
| 133 host_->SetBounds(0, 0, 100, 10); |
| 134 const gfx::Size kMaxSize(10, 10); |
| 135 |
| 136 layout_->SetMaxSize(ThreeViewLayout::Container::START, kMaxSize); |
| 137 views::View* start_child = new views::StaticSizedView(gfx::Size(20, 20)); |
| 138 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 139 |
| 140 views::View* center_child = new views::StaticSizedView(gfx::Size(10, 10)); |
| 141 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 142 |
| 143 host_->Layout(); |
| 144 |
| 145 EXPECT_EQ(kMaxSize.width(), GetBoundsInHost(center_child).x()); |
| 146 } |
| 147 |
| 148 TEST_F(ThreeViewLayoutTest, ViewsAddedToCorrectContainers) { |
| 149 views::View* start_child = new views::StaticSizedView(); |
| 150 views::View* center_child = new views::StaticSizedView(); |
| 151 views::View* end_child = new views::StaticSizedView(); |
| 152 |
| 153 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 154 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 155 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 156 |
| 157 EXPECT_TRUE( |
| 158 GetContainer(ThreeViewLayout::Container::START)->Contains(start_child)); |
| 159 EXPECT_EQ(1, GetContainer(ThreeViewLayout::Container::START)->child_count()); |
| 160 |
| 161 EXPECT_TRUE( |
| 162 GetContainer(ThreeViewLayout::Container::CENTER)->Contains(center_child)); |
| 163 EXPECT_EQ(1, GetContainer(ThreeViewLayout::Container::CENTER)->child_count()); |
| 164 |
| 165 EXPECT_TRUE( |
| 166 GetContainer(ThreeViewLayout::Container::END)->Contains(end_child)); |
| 167 EXPECT_EQ(1, GetContainer(ThreeViewLayout::Container::END)->child_count()); |
| 168 } |
| 169 |
| 170 TEST_F(ThreeViewLayoutTest, MultipleViewsAddedToTheSameContainer) { |
| 171 views::View* child1 = new views::StaticSizedView(); |
| 172 views::View* child2 = new views::StaticSizedView(); |
| 173 |
| 174 layout_->AddView(ThreeViewLayout::Container::START, child1); |
| 175 layout_->AddView(ThreeViewLayout::Container::START, child2); |
| 176 |
| 177 EXPECT_TRUE( |
| 178 GetContainer(ThreeViewLayout::Container::START)->Contains(child1)); |
| 179 EXPECT_TRUE( |
| 180 GetContainer(ThreeViewLayout::Container::START)->Contains(child2)); |
| 181 } |
| 182 |
| 183 TEST_F(ThreeViewLayoutTest, ViewsRemovedOnSetViewToTheSameContainer) { |
| 184 views::View* child1 = new views::StaticSizedView(); |
| 185 views::View* child2 = new views::StaticSizedView(); |
| 186 |
| 187 layout_->AddView(ThreeViewLayout::Container::START, child1); |
| 188 EXPECT_TRUE( |
| 189 GetContainer(ThreeViewLayout::Container::START)->Contains(child1)); |
| 190 |
| 191 layout_->SetView(ThreeViewLayout::Container::START, child2); |
| 192 EXPECT_TRUE( |
| 193 GetContainer(ThreeViewLayout::Container::START)->Contains(child2)); |
| 194 EXPECT_EQ(1, GetContainer(ThreeViewLayout::Container::START)->child_count()); |
| 195 } |
| 196 |
| 197 TEST_F(ThreeViewLayoutTest, Insets) { |
| 198 const int kInset = 3; |
| 199 const int kViewHeight = 10; |
| 200 const int kExpectedViewHeight = kViewHeight - 2 * kInset; |
| 201 const gfx::Size kStartViewSize(10, kViewHeight); |
| 202 const gfx::Size kCenterViewSize(100, kViewHeight); |
| 203 const gfx::Size kEndViewSize(10, kViewHeight); |
| 204 const int kHostWidth = 100; |
| 205 |
| 206 host_->SetBounds(0, 0, kHostWidth, kViewHeight); |
| 207 layout_->SetInsets(gfx::Insets(kInset)); |
| 208 views::View* start_child = new views::StaticSizedView(kStartViewSize); |
| 209 views::View* center_child = new views::StaticSizedView(kCenterViewSize); |
| 210 views::View* end_child = new views::StaticSizedView(kEndViewSize); |
| 211 |
| 212 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 213 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 214 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 215 |
| 216 layout_->SetFlexForContainer(ThreeViewLayout::Container::CENTER, 1.f); |
| 217 host_->Layout(); |
| 218 |
| 219 EXPECT_EQ( |
| 220 gfx::Rect(kInset, kInset, kStartViewSize.width(), kExpectedViewHeight), |
| 221 GetBoundsInHost(start_child)); |
| 222 EXPECT_EQ(gfx::Rect(kInset + kStartViewSize.width(), kInset, |
| 223 kHostWidth - kStartViewSize.width() - |
| 224 kEndViewSize.width() - 2 * kInset, |
| 225 kExpectedViewHeight), |
| 226 GetBoundsInHost(center_child)); |
| 227 EXPECT_EQ(gfx::Rect(kHostWidth - kEndViewSize.width() - kInset, kInset, |
| 228 kEndViewSize.width(), kExpectedViewHeight), |
| 229 GetBoundsInHost(end_child)); |
| 230 } |
| 231 |
| 232 TEST_F(ThreeViewLayoutTest, InvisibleContainerDoesntTakeUpSpace) { |
| 233 const int kViewWidth = 10; |
| 234 const int kViewHeight = 10; |
| 235 const gfx::Size kViewSize(kViewWidth, kViewHeight); |
| 236 |
| 237 host_->SetBounds(0, 0, 30, 10); |
| 238 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 239 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 240 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 241 |
| 242 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 243 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 244 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 245 |
| 246 layout_->SetContainerVisible(ThreeViewLayout::Container::START, false); |
| 247 host_->Layout(); |
| 248 |
| 249 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), GetBoundsInHost(start_child)); |
| 250 EXPECT_EQ(0, GetBoundsInHost(center_child).x()); |
| 251 EXPECT_EQ(kViewWidth, GetBoundsInHost(end_child).x()); |
| 252 |
| 253 layout_->SetContainerVisible(ThreeViewLayout::Container::START, true); |
| 254 host_->Layout(); |
| 255 |
| 256 EXPECT_EQ(0, GetBoundsInHost(start_child).x()); |
| 257 EXPECT_EQ(kViewWidth, GetBoundsInHost(center_child).x()); |
| 258 EXPECT_EQ(kViewWidth * 2, GetBoundsInHost(end_child).x()); |
| 259 } |
| 260 |
| 261 TEST_F(ThreeViewLayoutTest, NonZeroFlex) { |
| 262 const int kHostWidth = 100; |
| 263 const gfx::Size kDefaultViewSize(10, 10); |
| 264 const gfx::Size kCenterViewSize(100, 10); |
| 265 const gfx::Size kExpectedCenterViewSize( |
| 266 kHostWidth - 2 * kDefaultViewSize.width(), 10); |
| 267 host_->SetBounds(0, 0, kHostWidth, 10); |
| 268 views::View* start_child = new views::StaticSizedView(kDefaultViewSize); |
| 269 views::View* center_child = new views::StaticSizedView(kCenterViewSize); |
| 270 views::View* end_child = new views::StaticSizedView(kDefaultViewSize); |
| 271 |
| 272 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 273 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 274 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 275 |
| 276 layout_->SetFlexForContainer(ThreeViewLayout::Container::CENTER, 1.f); |
| 277 host_->Layout(); |
| 278 |
| 279 EXPECT_EQ(kDefaultViewSize, GetBoundsInHost(start_child).size()); |
| 280 EXPECT_EQ(kExpectedCenterViewSize, GetBoundsInHost(center_child).size()); |
| 281 EXPECT_EQ(kDefaultViewSize, GetBoundsInHost(end_child).size()); |
| 282 } |
| 283 |
| 284 TEST_F(ThreeViewLayoutTest, NonZeroFlexTakesPrecedenceOverMinSize) { |
| 285 const int kHostWidth = 25; |
| 286 const gfx::Size kViewSize(10, 10); |
| 287 const gfx::Size kMinCenterSize = kViewSize; |
| 288 const gfx::Size kExpectedCenterSize(kHostWidth - 2 * kViewSize.width(), 10); |
| 289 host_->SetBounds(0, 0, kHostWidth, 10); |
| 290 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 291 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 292 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 293 |
| 294 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 295 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 296 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 297 |
| 298 layout_->SetFlexForContainer(ThreeViewLayout::Container::CENTER, 1.f); |
| 299 layout_->SetMinSize(ThreeViewLayout::Container::CENTER, kMinCenterSize); |
| 300 host_->Layout(); |
| 301 |
| 302 EXPECT_EQ(kViewSize, GetBoundsInHost(start_child).size()); |
| 303 EXPECT_EQ( |
| 304 kExpectedCenterSize, |
| 305 GetBoundsInHost(GetContainer(ThreeViewLayout::Container::CENTER)).size()); |
| 306 EXPECT_EQ(kViewSize, GetBoundsInHost(end_child).size()); |
| 307 } |
| 308 |
| 309 TEST_F(ThreeViewLayoutTest, NonZeroFlexTakesPrecedenceOverMaxSize) { |
| 310 const int kHostWidth = 100; |
| 311 const gfx::Size kViewSize(10, 10); |
| 312 const gfx::Size kMaxCenterSize(20, 10); |
| 313 const gfx::Size kExpectedCenterSize(kHostWidth - 2 * kViewSize.width(), 10); |
| 314 host_->SetBounds(0, 0, kHostWidth, 10); |
| 315 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 316 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 317 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 318 |
| 319 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 320 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 321 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 322 |
| 323 layout_->SetFlexForContainer(ThreeViewLayout::Container::CENTER, 1.f); |
| 324 layout_->SetMaxSize(ThreeViewLayout::Container::CENTER, kMaxCenterSize); |
| 325 host_->Layout(); |
| 326 |
| 327 EXPECT_EQ(kViewSize, GetBoundsInHost(start_child).size()); |
| 328 EXPECT_EQ( |
| 329 kExpectedCenterSize, |
| 330 GetBoundsInHost(GetContainer(ThreeViewLayout::Container::CENTER)).size()); |
| 331 EXPECT_EQ(kViewSize, GetBoundsInHost(end_child).size()); |
| 332 } |
| 333 |
| 334 } // namespace ash |
OLD | NEW |