File: tab_strip_layout_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (376 lines) | stat: -rw-r--r-- 12,453 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/tabs/tab_strip_layout.h"

#include <stddef.h>

#include <optional>
#include <string>
#include <vector>

#include "base/strings/string_number_conversions.h"
#include "chrome/browser/ui/tabs/tab_types.h"
#include "chrome/browser/ui/views/tabs/tab_layout_state.h"
#include "chrome/browser/ui/views/tabs/tab_width_constraints.h"
#include "components/tabs/public/split_tab_id.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"

namespace {

// Returns a string with the width of each gfx::Rect in `tab_bounds`, separated
// by spaces.
std::string TabWidthsAsString(const std::vector<gfx::Rect>& tab_bounds) {
  std::string result;
  for (const auto& bounds : tab_bounds) {
    if (!result.empty()) {
      result += " ";
    }
    result += base::NumberToString(bounds.width());
  }
  return result;
}

// Returns a string with the x-coordinate of each gfx::Rect in `tab_bounds`,
// separated by spaces.
std::string TabXPositionsAsString(const std::vector<gfx::Rect>& tab_bounds) {
  std::string result;
  for (const auto& bounds : tab_bounds) {
    if (!result.empty()) {
      result += " ";
    }
    result += base::NumberToString(bounds.x());
  }
  return result;
}

struct TestCase {
  int num_pinned_tabs = 0;
  int num_tabs = 0;
  int active_index = 0;
  int tabstrip_width = 0;
  std::set<int> split_tabs;
};

constexpr int kStandardWidth = 256;
constexpr int kStandardSplitWidth = 137;
constexpr int kTabHeight = 41;
constexpr int kMinActiveWidth = 56;
constexpr int kMinActiveSplitWidth = 52;
constexpr int kMinInactiveWidth = 32;
constexpr int kPinnedWidth = 64;
constexpr int kPinnedSplitWidth = 55;
constexpr int kTabOverlap = 18;

std::vector<gfx::Rect> CalculateTabBounds(TestCase test_case) {
  TabSizeInfo size_info;
  size_info.pinned_tab_width = kPinnedWidth;
  size_info.min_active_width = kMinActiveWidth;
  size_info.min_inactive_width = kMinInactiveWidth;
  size_info.standard_width = kStandardWidth;

  TabSizeInfo split_size_info;
  split_size_info.pinned_tab_width = kPinnedSplitWidth;
  split_size_info.min_active_width = kMinActiveSplitWidth;
  split_size_info.min_inactive_width = kMinInactiveWidth;
  split_size_info.standard_width = kStandardSplitWidth;

  std::optional<split_tabs::SplitTabId> split_tab_id =
      split_tabs::SplitTabId::GenerateNew();

  std::vector<TabWidthConstraints> tab_states;
  for (int tab_index = 0; tab_index < test_case.num_tabs; tab_index++) {
    const bool is_split = test_case.split_tabs.contains(tab_index);
    TabLayoutState ideal_animation_state = TabLayoutState(
        TabOpen::kOpen,
        tab_index < test_case.num_pinned_tabs ? TabPinned::kPinned
                                              : TabPinned::kUnpinned,
        tab_index == test_case.active_index ? TabActive::kActive
                                            : TabActive::kInactive,
        is_split ? split_tab_id : std::nullopt);
    tab_states.emplace_back(ideal_animation_state,
                            is_split ? split_size_info : size_info);
  }

  return CalculateTabBounds(tab_states, test_case.tabstrip_width).first;
}

void ExpectTabsNarrowerThanTabStrip(const std::vector<gfx::Rect>& bounds,
                                    int tabstrip_width) {
  EXPECT_LT(bounds.back().right(), tabstrip_width);
}

void ExpectTabsFillTabStrip(const std::vector<gfx::Rect>& bounds,
                            int tabstrip_width) {
  EXPECT_EQ(bounds.back().right(), tabstrip_width);
}

void ExpectTabsWiderThanTabStrip(const std::vector<gfx::Rect>& bounds,
                                 int tabstrip_width) {
  EXPECT_GT(bounds.back().right(), tabstrip_width);
}

}  // namespace

// These tests verify that layout behaves correctly in various situations. In
// particular we want layout to adhere to the following constraints:
// * Tabs are the standard size given by TabSizeInfo when there's room.
// * Tabs are never smaller than the minimum sizes given by TabSizeInfo, even if
//   there isn't enough room.
// * Pinned tabs are always the width given by TabSizeInfo.
// * Remainder pixels (leftover when the available width is distributed evenly)
//   are distributed from left to right.
// * And otherwise tabs shrink to fit the available width.

TEST(TabStripLayoutTest, Basics) {
  TestCase test_case;
  test_case.tabstrip_width = 1000;
  test_case.num_tabs = 3;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("256 256 256", TabWidthsAsString(bounds));
  EXPECT_EQ("0 238 476", TabXPositionsAsString(bounds));
  for (const auto& b : bounds) {
    EXPECT_EQ(0, b.y());
    EXPECT_EQ(kTabHeight, b.height());
  }
  ExpectTabsNarrowerThanTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, AllPinnedTabs) {
  TestCase test_case;
  test_case.tabstrip_width = 1000;
  test_case.num_pinned_tabs = test_case.num_tabs = 3;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 64 64", TabWidthsAsString(bounds));
  EXPECT_EQ("0 46 92", TabXPositionsAsString(bounds));
  ExpectTabsNarrowerThanTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, MixedPinnedAndNormalTabs) {
  TestCase test_case;
  test_case.tabstrip_width = 1000;
  test_case.num_tabs = 3;
  test_case.num_pinned_tabs = 1;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 256 256", TabWidthsAsString(bounds));
  EXPECT_EQ("0 46 284", TabXPositionsAsString(bounds));
  ExpectTabsNarrowerThanTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, SplitPinnedTabs) {
  TestCase test_case;
  test_case.tabstrip_width = 1000;
  test_case.num_tabs = 2;
  test_case.num_pinned_tabs = 2;
  test_case.split_tabs = {0, 1};

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("55 55", TabWidthsAsString(bounds));
  EXPECT_EQ("0 37", TabXPositionsAsString(bounds));
  ExpectTabsNarrowerThanTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, MiddleWidth) {
  TestCase test_case;
  test_case.tabstrip_width = 598;
  test_case.num_tabs = 4;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("163 163 163 163", TabWidthsAsString(bounds));
  EXPECT_EQ("0 145 290 435", TabXPositionsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, MiddleWidthAndPinnedTab) {
  TestCase test_case;
  test_case.tabstrip_width = 400;
  test_case.num_tabs = 3;
  test_case.num_pinned_tabs = 1;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 186 186", TabWidthsAsString(bounds));
  EXPECT_EQ("0 46 214", TabXPositionsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, MiddleWidthRounded) {
  TestCase test_case;
  test_case.tabstrip_width = 600;
  test_case.num_tabs = 4;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("164 164 163 163", TabWidthsAsString(bounds));
  EXPECT_EQ("0 146 292 437", TabXPositionsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, MiddleWidthRoundedAndPinnedTab) {
  TestCase test_case;
  test_case.tabstrip_width = 401;
  test_case.num_tabs = 3;
  test_case.num_pinned_tabs = 1;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 187 186", TabWidthsAsString(bounds));
  EXPECT_EQ("0 46 215", TabXPositionsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, MiddleWidthRoundedAndSplitTab) {
  TestCase test_case;
  test_case.tabstrip_width = 602;
  test_case.num_tabs = 4;
  test_case.split_tabs = {0, 1};

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("117 117 211 211", TabWidthsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, MiddleWidthAndMinWidthSplitTab) {
  TestCase test_case;
  test_case.tabstrip_width = 138;
  test_case.num_tabs = 4;
  test_case.split_tabs = {0, 1};
  test_case.active_index = 2;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("44 44 56 48", TabWidthsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, BelowMinActiveWidth) {
  TestCase test_case;
  test_case.tabstrip_width = 196;
  test_case.num_tabs = 6;
  test_case.active_index = 3;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("46 46 46 56 46 46", TabWidthsAsString(bounds));
  EXPECT_EQ("0 28 56 84 122 150", TabXPositionsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, BelowMinActiveWidthRounded) {
  TestCase test_case;
  test_case.tabstrip_width = 200;
  test_case.num_tabs = 6;
  test_case.active_index = 3;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("47 47 47 56 47 46", TabWidthsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, BelowMinActiveWidthActivePinnedTab) {
  TestCase test_case;
  test_case.tabstrip_width = 249;
  test_case.num_tabs = 6;
  test_case.num_pinned_tabs = 1;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 55 55 55 55 55", TabWidthsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, BelowMinActiveWidthInactivePinnedTab) {
  TestCase test_case;
  test_case.tabstrip_width = 250;
  test_case.num_tabs = 6;
  test_case.num_pinned_tabs = 1;
  test_case.active_index = 2;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 55 56 55 55 55", TabWidthsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, BelowMinActiveWidthActivePinnedTabRounded) {
  TestCase test_case;
  test_case.tabstrip_width = 250;
  test_case.num_tabs = 6;
  test_case.num_pinned_tabs = 1;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 56 55 55 55 55", TabWidthsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, BelowMinActiveWidthSplitTab) {
  TestCase test_case;
  test_case.tabstrip_width = 200;
  test_case.num_tabs = 6;
  test_case.split_tabs = {0, 1};
  test_case.active_index = 2;

  // Can't avoid rounding with split tabs unless there is a large number of tabs
  // because regular tabs grow faster.
  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("45 45 56 48 48 48", TabWidthsAsString(bounds));
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, NotEnoughSpace) {
  TestCase test_case;
  test_case.tabstrip_width = 10;
  test_case.num_tabs = 3;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("56 32 32", TabWidthsAsString(bounds));
  ExpectTabsWiderThanTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, NotEnoughSpaceOneTab) {
  TestCase test_case;
  test_case.tabstrip_width = 15;
  test_case.num_tabs = 1;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("56", TabWidthsAsString(bounds));
  EXPECT_EQ("0", TabXPositionsAsString(bounds));
  ExpectTabsWiderThanTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, NotEnoughSpaceAllPinnedTabs) {
  TestCase test_case;
  test_case.tabstrip_width = 10;
  test_case.num_tabs = 3;
  test_case.num_pinned_tabs = 3;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 64 64", TabWidthsAsString(bounds));
  ExpectTabsWiderThanTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, NotEnoughSpaceMixedPinnedAndNormalTabs) {
  TestCase test_case;
  test_case.tabstrip_width = 10;
  test_case.num_tabs = 3;
  test_case.num_pinned_tabs = 1;

  auto bounds = CalculateTabBounds(test_case);
  EXPECT_EQ("64 32 32", TabWidthsAsString(bounds));
  ExpectTabsWiderThanTabStrip(bounds, test_case.tabstrip_width);
}

TEST(TabStripLayoutTest, ExactlyEnoughSpaceAllPinnedTabs) {
  TestCase test_case;
  test_case.num_tabs = 2;
  test_case.num_pinned_tabs = 2;
  test_case.tabstrip_width = 2 * kPinnedWidth - kTabOverlap;

  // We want to check the case where the necessary strip width equals the
  // available width.
  auto bounds = CalculateTabBounds(test_case);

  EXPECT_EQ("64 64", TabWidthsAsString(bounds));

  // Validate that the tabstrip width is indeeed exactly enough to hold two
  // pinned tabs.
  ExpectTabsFillTabStrip(bounds, test_case.tabstrip_width);
}