File: fill_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 (262 lines) | stat: -rw-r--r-- 8,313 bytes parent folder | download | duplicates (4)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/layout/fill_layout.h"

#include <memory>

#include "base/memory/raw_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/border.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 views {

namespace {

class FillLayoutTest : public testing::Test {
 public:
  static constexpr int kDefaultHostWidth = 100;
  static constexpr int kDefaultHostHeight = 200;

  FillLayoutTest() : host_(std::make_unique<View>()) {
    host_->SetLayoutManager(std::make_unique<FillLayout>());
    SetHostSize(kDefaultHostWidth, kDefaultHostHeight);
  }

  FillLayoutTest(const FillLayoutTest&) = delete;
  FillLayoutTest& operator=(const FillLayoutTest&) = delete;

 protected:
  // Convenience function to get the preferred size from `layout()`.
  gfx::Size GetPreferredSize() {
    return layout()->GetPreferredSize(host_.get());
  }

  // Convenience function to get the preferred height for width from `layout()`.
  int GetPreferredHeightForWidth(int width) {
    return layout()->GetPreferredHeightForWidth(host_.get(), width);
  }

  // Creates a View with the given |width| and |height| and adds it to |host_|.
  StaticSizedView* AddChildView(int width, int height) {
    StaticSizedView* child_view = new StaticSizedView(gfx::Size(width, height));
    child_view->SizeToPreferredSize();
    host_->AddChildViewRaw(child_view);
    return child_view;
  }

  void SetHostSize(int width, int height) {
    host_->SetSize(gfx::Size(width, height));
  }

  void SetHostInsets(const gfx::Insets& insets) {
    host_->SetBorder(CreateEmptyBorder(insets));
  }

  FillLayout* layout() {
    return static_cast<FillLayout*>(host_->GetLayoutManager());
  }

  std::unique_ptr<View> host_;
};

}  // namespace

TEST_F(FillLayoutTest, GetPreferredSizeWithNoChildren) {
  EXPECT_EQ(gfx::Size(0, 0), GetPreferredSize());

  SetHostSize(0, 0);
  EXPECT_EQ(gfx::Size(0, 0), GetPreferredSize());
}

TEST_F(FillLayoutTest, GetPreferredSizeWithOneChild) {
  AddChildView(25, 50);
  EXPECT_EQ(gfx::Size(25, 50), GetPreferredSize());
}

TEST_F(FillLayoutTest, GetPreferredSizeWithInsets) {
  const int kChildWidth = 25;
  const int kChildHeight = 50;
  const int kTopInset = 2;
  const int kLeftInset = 3;
  const int kBottomInset = 8;
  const int kRightInset = 7;

  AddChildView(kChildWidth, kChildHeight);
  SetHostInsets(
      gfx::Insets::TLBR(kTopInset, kLeftInset, kBottomInset, kRightInset));

  EXPECT_EQ(gfx::Size(kChildWidth + kLeftInset + kRightInset,
                      kChildHeight + kTopInset + kBottomInset),
            GetPreferredSize());
}

TEST_F(FillLayoutTest, GetPreferredSizeWithMultipleChildren) {
  AddChildView(10, 50);
  AddChildView(5, 5);
  AddChildView(25, 10);

  EXPECT_EQ(gfx::Size(25, 50), GetPreferredSize());
}

TEST_F(FillLayoutTest, GetPreferredHeightForWidthWithNoChildren) {
  EXPECT_EQ(0, GetPreferredHeightForWidth(0));
  EXPECT_EQ(0, GetPreferredHeightForWidth(100));

  SetHostSize(0, 0);
  EXPECT_EQ(0, GetPreferredHeightForWidth(0));
  EXPECT_EQ(0, GetPreferredHeightForWidth(100));
}

TEST_F(FillLayoutTest, GetPreferredHeightForWidthWithOneChild) {
  AddChildView(25, 50);

  EXPECT_EQ(50, GetPreferredHeightForWidth(0));
  EXPECT_EQ(50, GetPreferredHeightForWidth(25));
  EXPECT_EQ(50, GetPreferredHeightForWidth(100));
}

TEST_F(FillLayoutTest, GetPreferredHeightForWidthWithInsets) {
  const int kChildWidth = 25;
  const int kChildHeight = 50;
  const int kTopInset = 2;
  const int kLeftInset = 3;
  const int kBottomInset = 8;
  const int kRightInset = 7;

  const int kExpectedHeight = kChildHeight + kTopInset + kBottomInset;

  AddChildView(kChildWidth, kChildHeight);
  SetHostInsets(
      gfx::Insets::TLBR(kTopInset, kLeftInset, kBottomInset, kRightInset));

  EXPECT_EQ(kExpectedHeight, GetPreferredHeightForWidth(0));
  EXPECT_EQ(kExpectedHeight, GetPreferredHeightForWidth(25));
  EXPECT_EQ(kExpectedHeight, GetPreferredHeightForWidth(100));
}

TEST_F(FillLayoutTest, GetPreferredHeightForWidthWithMultipleChildren) {
  AddChildView(10, 50);
  AddChildView(5, 5);
  AddChildView(25, 10);

  EXPECT_EQ(50, GetPreferredHeightForWidth(0));
  EXPECT_EQ(50, GetPreferredHeightForWidth(25));
  EXPECT_EQ(50, GetPreferredHeightForWidth(100));
}

TEST_F(FillLayoutTest, LayoutWithNoChildren) {
  test::RunScheduledLayout(host_.get());
  // Makes sure there is no crash.
}

TEST_F(FillLayoutTest, LayoutWithOneChild) {
  View* const child = AddChildView(25, 50);
  test::RunScheduledLayout(host_.get());

  EXPECT_EQ(gfx::Rect(0, 0, kDefaultHostWidth, kDefaultHostHeight),
            child->bounds());
}

TEST_F(FillLayoutTest, LayoutWithInsets) {
  const int kChildWidth = 25;
  const int kChildHeight = 50;
  const int kTopInset = 2;
  const int kLeftInset = 3;
  const int kBottomInset = 8;
  const int kRightInset = 7;

  View* const child = AddChildView(kChildWidth, kChildHeight);
  SetHostInsets(
      gfx::Insets::TLBR(kTopInset, kLeftInset, kBottomInset, kRightInset));
  test::RunScheduledLayout(host_.get());

  EXPECT_EQ(gfx::Rect(kLeftInset, kTopInset,
                      kDefaultHostWidth - kLeftInset - kRightInset,
                      kDefaultHostHeight - kTopInset - kBottomInset),
            child->bounds());
}

TEST_F(FillLayoutTest, LayoutMultipleChildren) {
  View* const child_1 = AddChildView(10, 50);
  View* const child_2 = AddChildView(5, 5);
  View* const child_3 = AddChildView(25, 10);

  const gfx::Rect kExpectedBounds(0, 0, kDefaultHostWidth, kDefaultHostHeight);

  test::RunScheduledLayout(host_.get());

  EXPECT_EQ(kExpectedBounds, child_1->bounds());
  EXPECT_EQ(kExpectedBounds, child_2->bounds());
  EXPECT_EQ(kExpectedBounds, child_3->bounds());
}

TEST_F(FillLayoutTest, LayoutIgnoreView) {
  View* const child_1 = AddChildView(10, 50);
  View* const child_2 = AddChildView(5, 5);
  View* const child_3 = AddChildView(25, 10);

  child_3->SetProperty(kViewIgnoredByLayoutKey, true);
  EXPECT_EQ(gfx::Size(10, 50), GetPreferredSize());
  test::RunScheduledLayout(host_.get());

  const gfx::Size kExpectedSize(kDefaultHostWidth, kDefaultHostHeight);
  EXPECT_EQ(kExpectedSize, child_1->size());
  EXPECT_EQ(kExpectedSize, child_2->size());
  EXPECT_EQ(gfx::Size(25, 10), child_3->size());
}

TEST_F(FillLayoutTest, LayoutIncludesHiddenView) {
  View* const child_1 = AddChildView(10, 50);
  View* const child_2 = AddChildView(5, 5);
  View* const child_3 = AddChildView(25, 10);
  child_3->SetVisible(false);

  EXPECT_EQ(gfx::Size(25, 50), GetPreferredSize());
  test::RunScheduledLayout(host_.get());

  const gfx::Size kExpectedSize(kDefaultHostWidth, kDefaultHostHeight);
  EXPECT_EQ(kExpectedSize, child_1->size());
  EXPECT_EQ(kExpectedSize, child_2->size());
  EXPECT_EQ(kExpectedSize, child_3->size());
}

TEST_F(FillLayoutTest, MinimumSizeDisabled) {
  StaticSizedView* const child_1 = AddChildView(10, 50);
  StaticSizedView* const child_2 = AddChildView(5, 5);
  StaticSizedView* const child_3 = AddChildView(25, 10);
  child_1->set_minimum_size({1, 3});
  child_2->set_minimum_size({3, 1});
  child_3->set_minimum_size({2, 2});
  EXPECT_EQ(host_->GetPreferredSize({}), host_->GetMinimumSize());
}

TEST_F(FillLayoutTest, MinimumSizeEnabled) {
  layout()->SetMinimumSizeEnabled(true);
  StaticSizedView* const child_1 = AddChildView(10, 50);
  StaticSizedView* const child_2 = AddChildView(5, 5);
  StaticSizedView* const child_3 = AddChildView(25, 10);
  child_1->set_minimum_size({1, 3});
  child_2->set_minimum_size({3, 1});
  child_3->set_minimum_size({2, 2});
  EXPECT_EQ(gfx::Size(3, 3), host_->GetMinimumSize());
}

TEST_F(FillLayoutTest, MinimumSizeExcludesView) {
  layout()->SetMinimumSizeEnabled(true);
  StaticSizedView* const child_1 = AddChildView(10, 50);
  StaticSizedView* const child_2 = AddChildView(5, 5);
  StaticSizedView* const child_3 = AddChildView(25, 10);
  child_1->set_minimum_size({1, 3});
  child_2->set_minimum_size({3, 1});
  child_3->set_minimum_size({2, 2});
  child_2->SetProperty(kViewIgnoredByLayoutKey, true);
  EXPECT_EQ(gfx::Size(2, 3), host_->GetMinimumSize());
}

}  // namespace views