| 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 <memory> | |
| 6 | |
| 7 #include "ash/common/system/tray/tri_view.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "ui/gfx/geometry/insets.h" | |
| 11 #include "ui/gfx/geometry/rect_conversions.h" | |
| 12 #include "ui/views/layout/box_layout.h" | |
| 13 #include "ui/views/test/test_layout_manager.h" | |
| 14 #include "ui/views/test/test_views.h" | |
| 15 #include "ui/views/view.h" | |
| 16 | |
| 17 namespace ash { | |
| 18 namespace { | |
| 19 | |
| 20 // Returns a layout manager that will size views according to their preferred | |
| 21 // size. | |
| 22 std::unique_ptr<views::LayoutManager> CreatePreferredSizeLayoutManager() { | |
| 23 auto layout = base::MakeUnique<views::BoxLayout>( | |
| 24 views::BoxLayout::kHorizontal, 0, 0, 0); | |
| 25 layout->set_cross_axis_alignment( | |
| 26 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); | |
| 27 return std::move(layout); | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 class TriViewTest : public testing::Test { | |
| 33 public: | |
| 34 TriViewTest(); | |
| 35 | |
| 36 protected: | |
| 37 // Convenience function to get the minimum height of |container|. | |
| 38 int GetMinHeight(TriView::Container container) const; | |
| 39 | |
| 40 // Returns the bounds of |child| in the coordinate space of | |
| 41 // |tri_view_|. | |
| 42 gfx::Rect GetBoundsInHost(const views::View* child) const; | |
| 43 | |
| 44 // Wrapper functions to access the internals of |tri_view_|. | |
| 45 views::View* GetContainer(TriView::Container container) const; | |
| 46 | |
| 47 // The test target. | |
| 48 std::unique_ptr<TriView> tri_view_; | |
| 49 | |
| 50 private: | |
| 51 DISALLOW_COPY_AND_ASSIGN(TriViewTest); | |
| 52 }; | |
| 53 | |
| 54 TriViewTest::TriViewTest() : tri_view_(base::MakeUnique<TriView>()) {} | |
| 55 | |
| 56 int TriViewTest::GetMinHeight(TriView::Container container) const { | |
| 57 return tri_view_->GetMinSize(container).height(); | |
| 58 } | |
| 59 | |
| 60 gfx::Rect TriViewTest::GetBoundsInHost(const views::View* child) const { | |
| 61 gfx::RectF rect_f(child->bounds()); | |
| 62 views::View::ConvertRectToTarget(child, tri_view_.get(), &rect_f); | |
| 63 return ToNearestRect(rect_f); | |
| 64 } | |
| 65 | |
| 66 views::View* TriViewTest::GetContainer(TriView::Container container) const { | |
| 67 return tri_view_->GetContainer(container); | |
| 68 } | |
| 69 | |
| 70 TEST_F(TriViewTest, PaddingBetweenContainers) { | |
| 71 const int kPaddingBetweenContainers = 3; | |
| 72 const int kViewWidth = 10; | |
| 73 const int kViewHeight = 10; | |
| 74 const gfx::Size kViewSize(kViewWidth, kViewHeight); | |
| 75 const int kStartChildExpectedX = 0; | |
| 76 const int kCenterChildExpectedX = | |
| 77 kStartChildExpectedX + kViewWidth + kPaddingBetweenContainers; | |
| 78 const int kEndChildExpectedX = | |
| 79 kCenterChildExpectedX + kViewWidth + kPaddingBetweenContainers; | |
| 80 | |
| 81 tri_view_ = base::MakeUnique<TriView>(kPaddingBetweenContainers); | |
| 82 tri_view_->SetBounds(0, 0, 100, 10); | |
| 83 | |
| 84 views::View* start_child = new views::StaticSizedView(kViewSize); | |
| 85 views::View* center_child = new views::StaticSizedView(kViewSize); | |
| 86 views::View* end_child = new views::StaticSizedView(kViewSize); | |
| 87 | |
| 88 tri_view_->AddView(TriView::Container::START, start_child); | |
| 89 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 90 tri_view_->AddView(TriView::Container::END, end_child); | |
| 91 | |
| 92 tri_view_->Layout(); | |
| 93 | |
| 94 EXPECT_EQ(kStartChildExpectedX, GetBoundsInHost(start_child).x()); | |
| 95 EXPECT_EQ(kCenterChildExpectedX, GetBoundsInHost(center_child).x()); | |
| 96 EXPECT_EQ(kEndChildExpectedX, GetBoundsInHost(end_child).x()); | |
| 97 } | |
| 98 | |
| 99 TEST_F(TriViewTest, VerticalOrientation) { | |
| 100 const int kViewWidth = 10; | |
| 101 const int kViewHeight = 10; | |
| 102 const gfx::Size kViewSize(kViewWidth, kViewHeight); | |
| 103 | |
| 104 tri_view_ = base::MakeUnique<TriView>(TriView::Orientation::VERTICAL); | |
| 105 tri_view_->SetBounds(0, 0, 10, 100); | |
| 106 | |
| 107 views::View* start_child = new views::StaticSizedView(kViewSize); | |
| 108 views::View* center_child = new views::StaticSizedView(kViewSize); | |
| 109 views::View* end_child = new views::StaticSizedView(kViewSize); | |
| 110 | |
| 111 tri_view_->AddView(TriView::Container::START, start_child); | |
| 112 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 113 tri_view_->AddView(TriView::Container::END, end_child); | |
| 114 | |
| 115 tri_view_->Layout(); | |
| 116 | |
| 117 EXPECT_EQ(0, GetBoundsInHost(start_child).y()); | |
| 118 EXPECT_EQ(kViewWidth, GetBoundsInHost(center_child).y()); | |
| 119 EXPECT_EQ(kViewWidth * 2, GetBoundsInHost(end_child).y()); | |
| 120 } | |
| 121 | |
| 122 TEST_F(TriViewTest, MainAxisMinSize) { | |
| 123 tri_view_->SetBounds(0, 0, 100, 10); | |
| 124 const gfx::Size kMinSize(15, 10); | |
| 125 tri_view_->SetMinSize(TriView::Container::START, kMinSize); | |
| 126 views::View* child = new views::StaticSizedView(gfx::Size(10, 10)); | |
| 127 tri_view_->AddView(TriView::Container::CENTER, child); | |
| 128 | |
| 129 tri_view_->Layout(); | |
| 130 | |
| 131 EXPECT_EQ(kMinSize.width(), GetBoundsInHost(child).x()); | |
| 132 } | |
| 133 | |
| 134 TEST_F(TriViewTest, MainAxisMaxSize) { | |
| 135 tri_view_->SetBounds(0, 0, 100, 10); | |
| 136 const gfx::Size kMaxSize(10, 10); | |
| 137 | |
| 138 tri_view_->SetMaxSize(TriView::Container::START, kMaxSize); | |
| 139 views::View* start_child = new views::StaticSizedView(gfx::Size(20, 20)); | |
| 140 tri_view_->AddView(TriView::Container::START, start_child); | |
| 141 | |
| 142 views::View* center_child = new views::StaticSizedView(gfx::Size(10, 10)); | |
| 143 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 144 | |
| 145 tri_view_->Layout(); | |
| 146 | |
| 147 EXPECT_EQ(kMaxSize.width(), GetBoundsInHost(center_child).x()); | |
| 148 } | |
| 149 | |
| 150 TEST_F(TriViewTest, ViewsAddedToCorrectContainers) { | |
| 151 views::View* start_child = new views::StaticSizedView(); | |
| 152 views::View* center_child = new views::StaticSizedView(); | |
| 153 views::View* end_child = new views::StaticSizedView(); | |
| 154 | |
| 155 tri_view_->AddView(TriView::Container::START, start_child); | |
| 156 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 157 tri_view_->AddView(TriView::Container::END, end_child); | |
| 158 | |
| 159 EXPECT_TRUE(GetContainer(TriView::Container::START)->Contains(start_child)); | |
| 160 EXPECT_EQ(1, GetContainer(TriView::Container::START)->child_count()); | |
| 161 | |
| 162 EXPECT_TRUE(GetContainer(TriView::Container::CENTER)->Contains(center_child)); | |
| 163 EXPECT_EQ(1, GetContainer(TriView::Container::CENTER)->child_count()); | |
| 164 | |
| 165 EXPECT_TRUE(GetContainer(TriView::Container::END)->Contains(end_child)); | |
| 166 EXPECT_EQ(1, GetContainer(TriView::Container::END)->child_count()); | |
| 167 } | |
| 168 | |
| 169 TEST_F(TriViewTest, MultipleViewsAddedToTheSameContainer) { | |
| 170 views::View* child1 = new views::StaticSizedView(); | |
| 171 views::View* child2 = new views::StaticSizedView(); | |
| 172 | |
| 173 tri_view_->AddView(TriView::Container::START, child1); | |
| 174 tri_view_->AddView(TriView::Container::START, child2); | |
| 175 | |
| 176 EXPECT_TRUE(GetContainer(TriView::Container::START)->Contains(child1)); | |
| 177 EXPECT_TRUE(GetContainer(TriView::Container::START)->Contains(child2)); | |
| 178 } | |
| 179 | |
| 180 TEST_F(TriViewTest, ViewsRemovedOnRemoveAllChildren) { | |
| 181 views::View* child1 = new views::StaticSizedView(); | |
| 182 views::View* child2 = new views::StaticSizedView(); | |
| 183 | |
| 184 tri_view_->AddView(TriView::Container::START, child1); | |
| 185 tri_view_->AddView(TriView::Container::START, child2); | |
| 186 | |
| 187 EXPECT_TRUE(GetContainer(TriView::Container::START)->Contains(child1)); | |
| 188 EXPECT_TRUE(GetContainer(TriView::Container::START)->Contains(child2)); | |
| 189 EXPECT_EQ(2, GetContainer(TriView::Container::START)->child_count()); | |
| 190 | |
| 191 tri_view_->RemoveAllChildren(TriView::Container::START, false); | |
| 192 | |
| 193 EXPECT_FALSE(GetContainer(TriView::Container::START)->Contains(child1)); | |
| 194 EXPECT_FALSE(GetContainer(TriView::Container::START)->Contains(child2)); | |
| 195 EXPECT_EQ(0, GetContainer(TriView::Container::START)->child_count()); | |
| 196 | |
| 197 delete child1; | |
| 198 delete child2; | |
| 199 } | |
| 200 | |
| 201 TEST_F(TriViewTest, Insets) { | |
| 202 const int kInset = 3; | |
| 203 const int kViewHeight = 10; | |
| 204 const int kExpectedViewHeight = kViewHeight - 2 * kInset; | |
| 205 const gfx::Size kStartViewSize(10, kViewHeight); | |
| 206 const gfx::Size kCenterViewSize(100, kViewHeight); | |
| 207 const gfx::Size kEndViewSize(10, kViewHeight); | |
| 208 const int kHostWidth = 100; | |
| 209 | |
| 210 tri_view_->SetBounds(0, 0, kHostWidth, kViewHeight); | |
| 211 tri_view_->SetInsets(gfx::Insets(kInset)); | |
| 212 | |
| 213 views::View* start_child = new views::StaticSizedView(kStartViewSize); | |
| 214 views::View* center_child = new views::StaticSizedView(kCenterViewSize); | |
| 215 views::View* end_child = new views::StaticSizedView(kEndViewSize); | |
| 216 | |
| 217 tri_view_->AddView(TriView::Container::START, start_child); | |
| 218 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 219 tri_view_->AddView(TriView::Container::END, end_child); | |
| 220 | |
| 221 tri_view_->SetFlexForContainer(TriView::Container::CENTER, 1.f); | |
| 222 tri_view_->Layout(); | |
| 223 | |
| 224 EXPECT_EQ( | |
| 225 gfx::Rect(kInset, kInset, kStartViewSize.width(), kExpectedViewHeight), | |
| 226 GetBoundsInHost(start_child)); | |
| 227 EXPECT_EQ(gfx::Rect(kInset + kStartViewSize.width(), kInset, | |
| 228 kHostWidth - kStartViewSize.width() - | |
| 229 kEndViewSize.width() - 2 * kInset, | |
| 230 kExpectedViewHeight), | |
| 231 GetBoundsInHost(center_child)); | |
| 232 EXPECT_EQ(gfx::Rect(kHostWidth - kEndViewSize.width() - kInset, kInset, | |
| 233 kEndViewSize.width(), kExpectedViewHeight), | |
| 234 GetBoundsInHost(end_child)); | |
| 235 } | |
| 236 | |
| 237 TEST_F(TriViewTest, InvisibleContainerDoesntTakeUpSpace) { | |
| 238 const int kViewWidth = 10; | |
| 239 const int kViewHeight = 10; | |
| 240 const gfx::Size kViewSize(kViewWidth, kViewHeight); | |
| 241 | |
| 242 tri_view_->SetBounds(0, 0, 30, 10); | |
| 243 | |
| 244 views::View* start_child = new views::StaticSizedView(kViewSize); | |
| 245 views::View* center_child = new views::StaticSizedView(kViewSize); | |
| 246 views::View* end_child = new views::StaticSizedView(kViewSize); | |
| 247 | |
| 248 tri_view_->AddView(TriView::Container::START, start_child); | |
| 249 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 250 tri_view_->AddView(TriView::Container::END, end_child); | |
| 251 | |
| 252 tri_view_->SetContainerVisible(TriView::Container::START, false); | |
| 253 tri_view_->Layout(); | |
| 254 | |
| 255 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), GetBoundsInHost(start_child)); | |
| 256 EXPECT_EQ(0, GetBoundsInHost(center_child).x()); | |
| 257 EXPECT_EQ(kViewWidth, GetBoundsInHost(end_child).x()); | |
| 258 | |
| 259 tri_view_->SetContainerVisible(TriView::Container::START, true); | |
| 260 tri_view_->Layout(); | |
| 261 | |
| 262 EXPECT_EQ(0, GetBoundsInHost(start_child).x()); | |
| 263 EXPECT_EQ(kViewWidth, GetBoundsInHost(center_child).x()); | |
| 264 EXPECT_EQ(kViewWidth * 2, GetBoundsInHost(end_child).x()); | |
| 265 } | |
| 266 | |
| 267 TEST_F(TriViewTest, NonZeroFlex) { | |
| 268 const int kHostWidth = 100; | |
| 269 const gfx::Size kDefaultViewSize(10, 10); | |
| 270 const gfx::Size kCenterViewSize(100, 10); | |
| 271 const gfx::Size kExpectedCenterViewSize( | |
| 272 kHostWidth - 2 * kDefaultViewSize.width(), 10); | |
| 273 | |
| 274 tri_view_->SetBounds(0, 0, kHostWidth, 10); | |
| 275 | |
| 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 tri_view_->AddView(TriView::Container::START, start_child); | |
| 281 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 282 tri_view_->AddView(TriView::Container::END, end_child); | |
| 283 | |
| 284 tri_view_->SetFlexForContainer(TriView::Container::CENTER, 1.f); | |
| 285 tri_view_->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(TriViewTest, 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 | |
| 298 tri_view_->SetBounds(0, 0, kHostWidth, 10); | |
| 299 | |
| 300 views::View* start_child = new views::StaticSizedView(kViewSize); | |
| 301 views::View* center_child = new views::StaticSizedView(kViewSize); | |
| 302 views::View* end_child = new views::StaticSizedView(kViewSize); | |
| 303 | |
| 304 tri_view_->AddView(TriView::Container::START, start_child); | |
| 305 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 306 tri_view_->AddView(TriView::Container::END, end_child); | |
| 307 | |
| 308 tri_view_->SetFlexForContainer(TriView::Container::CENTER, 1.f); | |
| 309 tri_view_->SetMinSize(TriView::Container::CENTER, kMinCenterSize); | |
| 310 tri_view_->Layout(); | |
| 311 | |
| 312 EXPECT_EQ(kViewSize, GetBoundsInHost(start_child).size()); | |
| 313 EXPECT_EQ(kExpectedCenterSize, | |
| 314 GetBoundsInHost(GetContainer(TriView::Container::CENTER)).size()); | |
| 315 EXPECT_EQ(kViewSize, GetBoundsInHost(end_child).size()); | |
| 316 } | |
| 317 | |
| 318 TEST_F(TriViewTest, NonZeroFlexTakesPrecedenceOverMaxSize) { | |
| 319 const int kHostWidth = 100; | |
| 320 const gfx::Size kViewSize(10, 10); | |
| 321 const gfx::Size kMaxCenterSize(20, 10); | |
| 322 const gfx::Size kExpectedCenterSize(kHostWidth - 2 * kViewSize.width(), 10); | |
| 323 | |
| 324 tri_view_->SetBounds(0, 0, kHostWidth, 10); | |
| 325 | |
| 326 views::View* start_child = new views::StaticSizedView(kViewSize); | |
| 327 views::View* center_child = new views::StaticSizedView(kViewSize); | |
| 328 views::View* end_child = new views::StaticSizedView(kViewSize); | |
| 329 | |
| 330 tri_view_->AddView(TriView::Container::START, start_child); | |
| 331 tri_view_->AddView(TriView::Container::CENTER, center_child); | |
| 332 tri_view_->AddView(TriView::Container::END, end_child); | |
| 333 | |
| 334 tri_view_->SetFlexForContainer(TriView::Container::CENTER, 1.f); | |
| 335 tri_view_->SetMaxSize(TriView::Container::CENTER, kMaxCenterSize); | |
| 336 tri_view_->Layout(); | |
| 337 | |
| 338 EXPECT_EQ(kViewSize, GetBoundsInHost(start_child).size()); | |
| 339 EXPECT_EQ(kExpectedCenterSize, | |
| 340 GetBoundsInHost(GetContainer(TriView::Container::CENTER)).size()); | |
| 341 EXPECT_EQ(kViewSize, GetBoundsInHost(end_child).size()); | |
| 342 } | |
| 343 | |
| 344 TEST_F(TriViewTest, ChildViewsPreferredSizeChanged) { | |
| 345 const int kHostWidth = 500; | |
| 346 const gfx::Size kMinStartSize(100, 10); | |
| 347 | |
| 348 tri_view_->SetBounds(0, 0, kHostWidth, 10); | |
| 349 tri_view_->SetMinSize(TriView::Container::START, kMinStartSize); | |
| 350 tri_view_->SetContainerLayout(TriView::Container::START, | |
| 351 CreatePreferredSizeLayoutManager()); | |
| 352 tri_view_->SetFlexForContainer(TriView::Container::CENTER, 1.f); | |
| 353 tri_view_->Layout(); | |
| 354 | |
| 355 views::ProportionallySizedView* child_view = | |
| 356 new views::ProportionallySizedView(1); | |
| 357 tri_view_->AddView(TriView::Container::START, child_view); | |
| 358 | |
| 359 child_view->SetPreferredWidth(1); | |
| 360 EXPECT_EQ(child_view->GetPreferredSize(), child_view->size()); | |
| 361 | |
| 362 child_view->SetPreferredWidth(2); | |
| 363 EXPECT_EQ(child_view->GetPreferredSize(), child_view->size()); | |
| 364 } | |
| 365 | |
| 366 TEST_F(TriViewTest, SetMinHeight) { | |
| 367 const int kMinHeight = 10; | |
| 368 | |
| 369 EXPECT_NE(kMinHeight, GetMinHeight(TriView::Container::START)); | |
| 370 EXPECT_NE(kMinHeight, GetMinHeight(TriView::Container::CENTER)); | |
| 371 EXPECT_NE(kMinHeight, GetMinHeight(TriView::Container::END)); | |
| 372 | |
| 373 tri_view_->SetMinHeight(kMinHeight); | |
| 374 | |
| 375 EXPECT_EQ(kMinHeight, GetMinHeight(TriView::Container::START)); | |
| 376 EXPECT_EQ(kMinHeight, GetMinHeight(TriView::Container::CENTER)); | |
| 377 EXPECT_EQ(kMinHeight, GetMinHeight(TriView::Container::END)); | |
| 378 } | |
| 379 | |
| 380 } // namespace ash | |
| OLD | NEW |