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 // Confirms there are no crashes when destroying the host view before the layout |
| 54 // manager. |
| 55 TEST_F(ThreeViewLayoutTest, SafeDestruction) { |
| 56 views::View* child_view = new views::View(); |
| 57 layout_->AddView(ThreeViewLayout::Container::END, child_view); |
| 58 host_.reset(); |
| 59 } |
| 60 |
| 61 TEST_F(ThreeViewLayoutTest, PaddingBetweenContainers) { |
| 62 const int kPaddingBetweenContainers = 3; |
| 63 const int kViewWidth = 10; |
| 64 const int kViewHeight = 10; |
| 65 const gfx::Size kViewSize(kViewWidth, kViewHeight); |
| 66 const int kStartChildExpectedX = 0; |
| 67 const int kCenterChildExpectedX = |
| 68 kStartChildExpectedX + kViewWidth + kPaddingBetweenContainers; |
| 69 const int kEndChildExpectedX = |
| 70 kCenterChildExpectedX + kViewWidth + kPaddingBetweenContainers; |
| 71 layout_ = new ThreeViewLayout(kPaddingBetweenContainers); |
| 72 host_->SetLayoutManager(layout_); |
| 73 |
| 74 host_->SetBounds(0, 0, 100, 10); |
| 75 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 76 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 77 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 78 |
| 79 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 80 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 81 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 82 |
| 83 host_->Layout(); |
| 84 |
| 85 EXPECT_EQ(kStartChildExpectedX, GetBoundsInHost(start_child).x()); |
| 86 EXPECT_EQ(kCenterChildExpectedX, GetBoundsInHost(center_child).x()); |
| 87 EXPECT_EQ(kEndChildExpectedX, GetBoundsInHost(end_child).x()); |
| 88 } |
| 89 |
| 90 TEST_F(ThreeViewLayoutTest, VerticalOrientation) { |
| 91 const int kViewWidth = 10; |
| 92 const int kViewHeight = 10; |
| 93 const gfx::Size kViewSize(kViewWidth, kViewHeight); |
| 94 |
| 95 layout_ = new ThreeViewLayout(ThreeViewLayout::Orientation::VERTICAL); |
| 96 host_->SetLayoutManager(layout_); |
| 97 |
| 98 host_->SetBounds(0, 0, 10, 100); |
| 99 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 100 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 101 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 102 |
| 103 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 104 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 105 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 106 |
| 107 host_->Layout(); |
| 108 |
| 109 EXPECT_EQ(0, GetBoundsInHost(start_child).y()); |
| 110 EXPECT_EQ(kViewWidth, GetBoundsInHost(center_child).y()); |
| 111 EXPECT_EQ(kViewWidth * 2, GetBoundsInHost(end_child).y()); |
| 112 } |
| 113 |
| 114 TEST_F(ThreeViewLayoutTest, ContainerViewsRemovedFromHost) { |
| 115 EXPECT_NE(0, host_->child_count()); |
| 116 host_->SetLayoutManager(nullptr); |
| 117 EXPECT_EQ(0, host_->child_count()); |
| 118 } |
| 119 |
| 120 TEST_F(ThreeViewLayoutTest, MinCrossAxisSize) { |
| 121 const int kMinCrossAxisSize = 15; |
| 122 EXPECT_EQ(0, layout_->GetPreferredSize(host_.get()).height()); |
| 123 layout_->SetMinCrossAxisSize(kMinCrossAxisSize); |
| 124 EXPECT_EQ(kMinCrossAxisSize, layout_->GetPreferredSize(host_.get()).height()); |
| 125 EXPECT_EQ(kMinCrossAxisSize, |
| 126 layout_->GetPreferredHeightForWidth(host_.get(), 0)); |
| 127 } |
| 128 |
| 129 TEST_F(ThreeViewLayoutTest, MainAxisMinSize) { |
| 130 host_->SetBounds(0, 0, 100, 10); |
| 131 const gfx::Size kMinSize(15, 10); |
| 132 layout_->SetMinSize(ThreeViewLayout::Container::START, kMinSize); |
| 133 views::View* child = new views::StaticSizedView(gfx::Size(10, 10)); |
| 134 layout_->AddView(ThreeViewLayout::Container::CENTER, child); |
| 135 host_->Layout(); |
| 136 |
| 137 EXPECT_EQ(kMinSize.width(), GetBoundsInHost(child).x()); |
| 138 } |
| 139 |
| 140 TEST_F(ThreeViewLayoutTest, MainAxisMaxSize) { |
| 141 host_->SetBounds(0, 0, 100, 10); |
| 142 const gfx::Size kMaxSize(10, 10); |
| 143 |
| 144 layout_->SetMaxSize(ThreeViewLayout::Container::START, kMaxSize); |
| 145 views::View* start_child = new views::StaticSizedView(gfx::Size(20, 20)); |
| 146 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 147 |
| 148 views::View* center_child = new views::StaticSizedView(gfx::Size(10, 10)); |
| 149 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 150 |
| 151 host_->Layout(); |
| 152 |
| 153 EXPECT_EQ(kMaxSize.width(), GetBoundsInHost(center_child).x()); |
| 154 } |
| 155 |
| 156 TEST_F(ThreeViewLayoutTest, ViewsAddedToCorrectContainers) { |
| 157 views::View* start_child = new views::StaticSizedView(); |
| 158 views::View* center_child = new views::StaticSizedView(); |
| 159 views::View* end_child = new views::StaticSizedView(); |
| 160 |
| 161 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 162 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 163 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 164 |
| 165 EXPECT_TRUE( |
| 166 GetContainer(ThreeViewLayout::Container::START)->Contains(start_child)); |
| 167 EXPECT_EQ(1, GetContainer(ThreeViewLayout::Container::START)->child_count()); |
| 168 |
| 169 EXPECT_TRUE( |
| 170 GetContainer(ThreeViewLayout::Container::CENTER)->Contains(center_child)); |
| 171 EXPECT_EQ(1, GetContainer(ThreeViewLayout::Container::CENTER)->child_count()); |
| 172 |
| 173 EXPECT_TRUE( |
| 174 GetContainer(ThreeViewLayout::Container::END)->Contains(end_child)); |
| 175 EXPECT_EQ(1, GetContainer(ThreeViewLayout::Container::END)->child_count()); |
| 176 } |
| 177 |
| 178 TEST_F(ThreeViewLayoutTest, MultipleViewsAddedToTheSameContainer) { |
| 179 views::View* child1 = new views::StaticSizedView(); |
| 180 views::View* child2 = new views::StaticSizedView(); |
| 181 |
| 182 layout_->AddView(ThreeViewLayout::Container::START, child1); |
| 183 layout_->AddView(ThreeViewLayout::Container::START, child2); |
| 184 |
| 185 EXPECT_TRUE( |
| 186 GetContainer(ThreeViewLayout::Container::START)->Contains(child1)); |
| 187 EXPECT_TRUE( |
| 188 GetContainer(ThreeViewLayout::Container::START)->Contains(child2)); |
| 189 } |
| 190 |
| 191 TEST_F(ThreeViewLayoutTest, ViewsRemovedOnSetViewToTheSameContainer) { |
| 192 views::View* child1 = new views::StaticSizedView(); |
| 193 views::View* child2 = new views::StaticSizedView(); |
| 194 |
| 195 layout_->AddView(ThreeViewLayout::Container::START, child1); |
| 196 EXPECT_TRUE( |
| 197 GetContainer(ThreeViewLayout::Container::START)->Contains(child1)); |
| 198 |
| 199 layout_->SetView(ThreeViewLayout::Container::START, child2); |
| 200 EXPECT_TRUE( |
| 201 GetContainer(ThreeViewLayout::Container::START)->Contains(child2)); |
| 202 EXPECT_EQ(1, GetContainer(ThreeViewLayout::Container::START)->child_count()); |
| 203 } |
| 204 |
| 205 TEST_F(ThreeViewLayoutTest, Insets) { |
| 206 const int kInset = 3; |
| 207 const int kViewHeight = 10; |
| 208 const int kExpectedViewHeight = kViewHeight - 2 * kInset; |
| 209 const gfx::Size kStartViewSize(10, kViewHeight); |
| 210 const gfx::Size kCenterViewSize(100, kViewHeight); |
| 211 const gfx::Size kEndViewSize(10, kViewHeight); |
| 212 const int kHostWidth = 100; |
| 213 |
| 214 host_->SetBounds(0, 0, kHostWidth, kViewHeight); |
| 215 layout_->SetInsets(gfx::Insets(kInset)); |
| 216 views::View* start_child = new views::StaticSizedView(kStartViewSize); |
| 217 views::View* center_child = new views::StaticSizedView(kCenterViewSize); |
| 218 views::View* end_child = new views::StaticSizedView(kEndViewSize); |
| 219 |
| 220 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 221 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 222 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 223 |
| 224 layout_->SetFlexForContainer(ThreeViewLayout::Container::CENTER, 1.f); |
| 225 host_->Layout(); |
| 226 |
| 227 EXPECT_EQ( |
| 228 gfx::Rect(kInset, kInset, kStartViewSize.width(), kExpectedViewHeight), |
| 229 GetBoundsInHost(start_child)); |
| 230 EXPECT_EQ(gfx::Rect(kInset + kStartViewSize.width(), kInset, |
| 231 kHostWidth - kStartViewSize.width() - |
| 232 kEndViewSize.width() - 2 * kInset, |
| 233 kExpectedViewHeight), |
| 234 GetBoundsInHost(center_child)); |
| 235 EXPECT_EQ(gfx::Rect(kHostWidth - kEndViewSize.width() - kInset, kInset, |
| 236 kEndViewSize.width(), kExpectedViewHeight), |
| 237 GetBoundsInHost(end_child)); |
| 238 } |
| 239 |
| 240 TEST_F(ThreeViewLayoutTest, InvisibleContainerDoesntTakeUpSpace) { |
| 241 const int kViewWidth = 10; |
| 242 const int kViewHeight = 10; |
| 243 const gfx::Size kViewSize(kViewWidth, kViewHeight); |
| 244 |
| 245 host_->SetBounds(0, 0, 30, 10); |
| 246 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 247 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 248 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 249 |
| 250 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 251 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 252 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 253 |
| 254 layout_->SetContainerVisible(ThreeViewLayout::Container::START, false); |
| 255 host_->Layout(); |
| 256 |
| 257 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), GetBoundsInHost(start_child)); |
| 258 EXPECT_EQ(0, GetBoundsInHost(center_child).x()); |
| 259 EXPECT_EQ(kViewWidth, GetBoundsInHost(end_child).x()); |
| 260 |
| 261 layout_->SetContainerVisible(ThreeViewLayout::Container::START, true); |
| 262 host_->Layout(); |
| 263 |
| 264 EXPECT_EQ(0, GetBoundsInHost(start_child).x()); |
| 265 EXPECT_EQ(kViewWidth, GetBoundsInHost(center_child).x()); |
| 266 EXPECT_EQ(kViewWidth * 2, GetBoundsInHost(end_child).x()); |
| 267 } |
| 268 |
| 269 TEST_F(ThreeViewLayoutTest, NonZeroFlex) { |
| 270 const int kHostWidth = 100; |
| 271 const gfx::Size kDefaultViewSize(10, 10); |
| 272 const gfx::Size kCenterViewSize(100, 10); |
| 273 const gfx::Size kExpectedCenterViewSize( |
| 274 kHostWidth - 2 * kDefaultViewSize.width(), 10); |
| 275 host_->SetBounds(0, 0, kHostWidth, 10); |
| 276 views::View* start_child = new views::StaticSizedView(kDefaultViewSize); |
| 277 views::View* center_child = new views::StaticSizedView(kCenterViewSize); |
| 278 views::View* end_child = new views::StaticSizedView(kDefaultViewSize); |
| 279 |
| 280 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 281 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 282 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 283 |
| 284 layout_->SetFlexForContainer(ThreeViewLayout::Container::CENTER, 1.f); |
| 285 host_->Layout(); |
| 286 |
| 287 EXPECT_EQ(kDefaultViewSize, GetBoundsInHost(start_child).size()); |
| 288 EXPECT_EQ(kExpectedCenterViewSize, GetBoundsInHost(center_child).size()); |
| 289 EXPECT_EQ(kDefaultViewSize, GetBoundsInHost(end_child).size()); |
| 290 } |
| 291 |
| 292 TEST_F(ThreeViewLayoutTest, NonZeroFlexTakesPrecedenceOverMinSize) { |
| 293 const int kHostWidth = 25; |
| 294 const gfx::Size kViewSize(10, 10); |
| 295 const gfx::Size kMinCenterSize = kViewSize; |
| 296 const gfx::Size kExpectedCenterSize(kHostWidth - 2 * kViewSize.width(), 10); |
| 297 host_->SetBounds(0, 0, kHostWidth, 10); |
| 298 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 299 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 300 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 301 |
| 302 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 303 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 304 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 305 |
| 306 layout_->SetFlexForContainer(ThreeViewLayout::Container::CENTER, 1.f); |
| 307 layout_->SetMinSize(ThreeViewLayout::Container::CENTER, kMinCenterSize); |
| 308 host_->Layout(); |
| 309 |
| 310 EXPECT_EQ(kViewSize, GetBoundsInHost(start_child).size()); |
| 311 EXPECT_EQ( |
| 312 kExpectedCenterSize, |
| 313 GetBoundsInHost(GetContainer(ThreeViewLayout::Container::CENTER)).size()); |
| 314 EXPECT_EQ(kViewSize, GetBoundsInHost(end_child).size()); |
| 315 } |
| 316 |
| 317 TEST_F(ThreeViewLayoutTest, NonZeroFlexTakesPrecedenceOverMaxSize) { |
| 318 const int kHostWidth = 100; |
| 319 const gfx::Size kViewSize(10, 10); |
| 320 const gfx::Size kMaxCenterSize(20, 10); |
| 321 const gfx::Size kExpectedCenterSize(kHostWidth - 2 * kViewSize.width(), 10); |
| 322 host_->SetBounds(0, 0, kHostWidth, 10); |
| 323 views::View* start_child = new views::StaticSizedView(kViewSize); |
| 324 views::View* center_child = new views::StaticSizedView(kViewSize); |
| 325 views::View* end_child = new views::StaticSizedView(kViewSize); |
| 326 |
| 327 layout_->AddView(ThreeViewLayout::Container::START, start_child); |
| 328 layout_->AddView(ThreeViewLayout::Container::CENTER, center_child); |
| 329 layout_->AddView(ThreeViewLayout::Container::END, end_child); |
| 330 |
| 331 layout_->SetFlexForContainer(ThreeViewLayout::Container::CENTER, 1.f); |
| 332 layout_->SetMaxSize(ThreeViewLayout::Container::CENTER, kMaxCenterSize); |
| 333 host_->Layout(); |
| 334 |
| 335 EXPECT_EQ(kViewSize, GetBoundsInHost(start_child).size()); |
| 336 EXPECT_EQ( |
| 337 kExpectedCenterSize, |
| 338 GetBoundsInHost(GetContainer(ThreeViewLayout::Container::CENTER)).size()); |
| 339 EXPECT_EQ(kViewSize, GetBoundsInHost(end_child).size()); |
| 340 } |
| 341 |
| 342 } // namespace ash |
OLD | NEW |