File: simple_grid_layout_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (66 lines) | stat: -rw-r--r-- 2,565 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/bubble/simple_grid_layout.h"

#include <memory>

#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/test/test_views.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"

namespace ash {

class SimpleGridLayoutTest : public testing::Test {
 public:
  void SetUp() override { host_ = std::make_unique<views::View>(); }

  std::unique_ptr<views::View> host_;
};

TEST_F(SimpleGridLayoutTest, ChildIgnoredByLayout) {
  // Create a SimpleGrid host view for 2 columns.
  views::LayoutManagerBase* layout =
      host_->SetLayoutManager(std::make_unique<SimpleGridLayout>(2, 0, 0));

  // Add a child view of size 20 and verify that the Grid has a correct size.
  views::StaticSizedView* v1 = host_->AddChildView(
      std::make_unique<views::StaticSizedView>(gfx::Size(20, 20)));
  EXPECT_EQ(gfx::Size(40, 20), layout->GetPreferredSize(host_.get()));

  // Add a child view of size 10 and verify that the Grid assumes all views are
  // size 20. as the first child.
  views::StaticSizedView* v2 = host_->AddChildView(
      std::make_unique<views::StaticSizedView>(gfx::Size(10, 10)));
  EXPECT_EQ(gfx::Size(40, 20), layout->GetPreferredSize(host_.get()));

  // Add a third child view of size 10 and verify that the grid is sozed
  // correctly for 2 rows.
  views::StaticSizedView* v3 = host_->AddChildView(
      std::make_unique<views::StaticSizedView>(gfx::Size(10, 10)));
  EXPECT_EQ(gfx::Size(40, 40), layout->GetPreferredSize(host_.get()));

  // Set the first view to be ignored by layout. Expect the size to change
  // accordingly to consider the child views sized at 10.
  v1->SetProperty(views::kViewIgnoredByLayoutKey, true);
  EXPECT_EQ(gfx::Size(20, 10), layout->GetPreferredSize(host_.get()));

  // Layout the views. Verify the coordinates for all views.
  views::test::RunScheduledLayout(host_.get());
  EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v1->bounds());
  EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v2->bounds());
  EXPECT_EQ(gfx::Rect(10, 0, 10, 10), v3->bounds());

  // Change the host size view and run a layout again. Verify the layout did not
  // changed.
  host_->SetBounds(0, 0, 30, 30);
  views::test::RunScheduledLayout(host_.get());
  EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v1->bounds());
  EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v2->bounds());
  EXPECT_EQ(gfx::Rect(10, 0, 10, 10), v3->bounds());
}

}  // namespace ash