1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/views/tabs/stacked_tab_strip_layout.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/views/view.h"
15 #include "ui/views/view_model.h"
19 struct CommonTestData
{
23 const int tab_padding
;
24 const int stacked_offset
;
25 const int pinned_tab_count
;
26 const int active_index
;
27 const std::string start_bounds
;
28 const std::string expected_bounds
;
33 class StackedTabStripLayoutTest
: public testing::Test
{
35 StackedTabStripLayoutTest() {}
38 void Reset(StackedTabStripLayout
* layout
,
43 layout
->Reset(x
, width
, pinned_tab_count
, active_index
);
46 void CreateLayout(const CommonTestData
& data
) {
47 if (!data
.start_bounds
.empty())
48 PrepareChildViewsFromString(data
.start_bounds
);
50 PrepareChildViewsFromString(data
.expected_bounds
);
51 layout_
.reset(new StackedTabStripLayout(
52 gfx::Size(data
.tab_size
, 10), data
.tab_padding
,
53 data
.stacked_offset
, 4, &view_model_
));
54 if (data
.start_bounds
.empty()) {
55 PrepareChildViewsFromString(data
.expected_bounds
);
56 layout_
->Reset(data
.initial_x
, data
.width
, data
.pinned_tab_count
,
59 ASSERT_NO_FATAL_FAILURE(SetBoundsFromString(data
.start_bounds
));
60 layout_
->Reset(data
.initial_x
, data
.width
, data
.pinned_tab_count
,
62 ASSERT_NO_FATAL_FAILURE(SetBoundsFromString(data
.start_bounds
));
66 void AddViewToViewModel(int index
) {
67 views::View
* child_view
= new views::View
;
68 view_
.AddChildView(child_view
);
69 view_model_
.Add(child_view
, index
);
72 void PrepareChildViewsFromString(const std::string
& bounds
) {
73 std::vector
<base::StringPiece
> positions
= base::SplitStringPiece(
74 bounds
, " ", base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
75 PrepareChildViews(static_cast<int>(positions
.size()));
78 void PrepareChildViews(int count
) {
80 view_
.RemoveAllChildViews(true);
81 for (int i
= 0; i
< count
; ++i
)
82 AddViewToViewModel(i
);
85 void SetBoundsFromString(const std::string
& bounds
) {
86 std::vector
<base::StringPiece
> positions
= base::SplitStringPiece(
87 bounds
, " ", base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
88 PrepareChildViews(static_cast<int>(positions
.size()));
89 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
91 gfx::Rect
bounds(view_model_
.ideal_bounds(i
));
92 ASSERT_TRUE(base::StringToInt(positions
[i
], &x
));
94 view_model_
.set_ideal_bounds(i
, bounds
);
98 std::string
BoundsString() const {
100 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
103 result
+= base::IntToString(view_model_
.ideal_bounds(i
).x());
108 std::string
BoundsString2(int active_index
) const {
110 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
113 if (i
== active_index
)
115 result
+= base::IntToString(view_model_
.ideal_bounds(i
).x());
116 if (i
== active_index
)
122 void Validate(int active_index
, int max_width
) {
123 // Make sure none of the tabs are more than 90 apart
124 // (tab_size(100) + padding (-10)).
125 for (int j
= 1; j
< view_model_
.view_size(); ++j
)
126 EXPECT_LE(ideal_x(j
) - ideal_x(j
- 1), max_width
- 100);
129 int ideal_x(int index
) const {
130 return view_model_
.ideal_bounds(index
).x();
133 content::TestBrowserThreadBundle thread_bundle_
;
134 scoped_ptr
<StackedTabStripLayout
> layout_
;
135 views::ViewModel view_model_
;
140 DISALLOW_COPY_AND_ASSIGN(StackedTabStripLayoutTest
);
144 TEST_F(StackedTabStripLayoutTest
, ValidateInitialLayout
) {
145 StackedTabStripLayout
layout(gfx::Size(100, 10), -10, 2, 4, &view_model_
);
146 PrepareChildViews(12);
148 for (int i
= 120; i
< 600; ++i
) {
149 for (int j
= 0; j
< 12; ++j
) {
150 Reset(&layout
, 0, i
, 0, j
);
152 if (HasNonfatalFailure())
158 // Ensure initial layout is correct.
159 TEST_F(StackedTabStripLayoutTest
, InitialLayout
) {
160 struct CommonTestData test_data
[] = {
161 { 0, 198, 100, -10, 1, 0, 9, "",
162 "0 0 0 0 0 0 1 2 3 4 94 95 96 97 98 98 98 98" },
163 { 0, 198, 100, -10, 1, 0, 0, "", "0 90 94 95 96 97 98 98 98" },
164 { 0, 300, 100, -10, 1, 0, 0, "",
165 "0 90 180 196 197 198 199 200 200 200 200" },
166 { 0, 300, 100, -10, 1, 0, 10, "", "0 0 0 0 1 2 3 4 20 110 200" },
167 { 0, 300, 100, -10, 1, 0, 1, "", "0 90 180 196 197 198 199 200 200" },
168 { 0, 643, 160, -27, 6, 0, 0, "", "0 133 266 399" },
169 { 0, 300, 100, -10, 1, 0, 7, "", "0 1 2 3 4 20 110 200" },
170 { 0, 300, 100, -10, 1, 0, 6, "", "0 1 2 3 4 20 110 200" },
171 { 0, 300, 100, -10, 1, 0, 4, "", "0 1 2 3 4 94 184 199 200" },
173 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
174 CreateLayout(test_data
[i
]);
175 EXPECT_EQ(test_data
[i
].expected_bounds
, BoundsString()) << " at " << i
;
179 // Assertions for dragging from an existing configuration.
180 TEST_F(StackedTabStripLayoutTest
, DragActiveTabExisting
) {
182 struct CommonTestData common_data
;
186 // The following set of tests create 6 tabs, the first two are pinned and
187 // the 2nd tab is selected.
189 // 1 pixel to the right, should push only pinned tabs and first non-pinned
191 { { 10, 240, 100, -10, 2, 2, 1, "0 5 10 100 138 140",
192 "1 6 11 101 138 140" }, 1 },
193 // Push enough to collapse the 4th tab.
194 { { 10, 240, 100, -10, 2, 2, 1, "0 5 10 100 138 140",
195 "36 41 46 136 138 140" }, 36 },
196 // 1 past collapsing the 4th.
197 { { 10, 240, 100, -10, 2, 2, 1, "0 5 10 100 138 140",
198 "37 42 47 136 138 140" }, 37 },
199 // Collapse the third.
200 { { 10, 240, 100, -10, 2, 2, 1, "0 5 10 100 138 140",
201 "124 129 134 136 138 140" }, 124 },
202 // One past collapsing the third.
203 { { 10, 240, 100, -10, 2, 2, 1, "0 5 10 100 138 140",
204 "124 129 134 136 138 140" }, 125 },
207 // The following set of tests create 6 tabs, the first two are pinned and
208 // the 5th is selected.
210 // 1 pixel to the right, should expose part of a tab.
211 { { 10, 240, 100, -10, 2, 2, 4, "0 5 10 90 130 140", "0 5 10 90 131 140" },
213 // Push the tab as far to the right as it'll go.
214 { { 10, 240, 100, -10, 2, 2, 4, "0 5 10 90 130 140", "0 5 10 90 138 140" },
216 // One past as far to the right as it'll go. Should expose more of the tab
218 { { 10, 240, 100, -10, 2, 2, 4, "0 5 10 90 130 140", "0 5 10 91 138 140" },
220 // Enough so that the pinned tabs start pulling in.
221 { { 10, 240, 100, -10, 2, 2, 4, "0 5 10 90 130 140", "1 6 11 101 138 140" },
223 // One more than last.
224 { { 10, 240, 100, -10, 2, 2, 4, "0 5 10 90 130 140", "2 7 12 102 138 140" },
226 // Enough to collapse the fourth as small it can get.
227 { { 10, 240, 100, -10, 2, 2, 4, "0 5 10 90 130 140",
228 "36 41 46 136 138 140" }, 54 },
229 // Enough to collapse the third as small it can get.
230 { { 10, 240, 100, -10, 2, 2, 4, "0 5 10 90 130 140",
231 "124 129 134 136 138 140" }, 142 },
232 // One more than last, shouldn't change anything.
233 { { 10, 240, 100, -10, 2, 2, 4, "0 5 10 90 130 140",
234 "124 129 134 136 138 140" }, 143 },
237 // The following set of tests create 3 tabs with the second selected.
239 // Drags in 2, pulling the rightmost tab along.
240 { { 0, 240, 100, -10, 2, 0, 1, "0 90 140", "2 92 140" }, 2 },
241 // Drags the rightmost tab as far to right as possible.
242 { { 0, 240, 100, -10, 2, 0, 1, "0 90 140", "48 138 140" }, 48 },
243 // Drags so much that the left most tabs pulls in.
244 { { 0, 240, 100, -10, 2, 0, 1, "0 90 140", "135 138 140" }, 135 },
245 // Drags so far that no more tabs pull in.
246 { { 0, 240, 100, -10, 2, 0, 1, "0 90 140", "136 138 140" }, 200 },
247 // Drags to the left most position before the right tabs start pulling in.
248 { { 0, 240, 100, -10, 2, 0, 1, "0 90 140", "0 50 140" }, -40 },
249 // Drags 1 beyond the left most position, which should pull in the right
251 { { 0, 240, 100, -10, 2, 0, 1, "0 90 140", "0 49 139" }, -41 },
252 // Drags to the left as far as the tab goes.
253 { { 0, 240, 100, -10, 2, 0, 1, "0 90 140", "0 2 92" }, -88 },
254 // Drags one past as far to the left as the tab goes. Should keep pulling
255 // in the rightmost tab.
256 { { 0, 240, 100, -10, 2, 0, 1, "0 90 140", "0 2 91" }, -89 },
259 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
260 CreateLayout(test_data
[i
].common_data
);
261 layout_
->DragActiveTab(test_data
[i
].delta
);
262 EXPECT_EQ(test_data
[i
].common_data
.expected_bounds
, BoundsString()) <<
267 // Assertions for SizeToFit().
268 TEST_F(StackedTabStripLayoutTest
, SizeToFit
) {
269 struct CommonTestData test_data
[] = {
270 // Dragged to the right.
271 { 10, 240, 100, -10, 2, 2, 1, "0 5 10 100 138 140", "1 6 11 101 138 140"},
272 { 10, 240, 100, -10, 2, 2, 1, "0 5 10 100 138 140",
273 "124 129 134 136 138 140" },
275 // Dragged to the left.
276 { 0, 240, 100, -10, 2, 0, 1, "0 50 140", "0 49 139" },
278 // Dragged to the left.
279 { 0, 240, 100, -10, 2, 0, 1, "0 49 89 140", "0 49 89 139" },
282 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
283 CreateLayout(test_data
[i
]);
284 SetBoundsFromString(test_data
[i
].expected_bounds
);
285 layout_
->SizeToFit();
286 // NOTE: because of the way the code is structured this asserts on
287 // |start_bound|, not |expected_bounds|.
288 EXPECT_EQ(test_data
[i
].start_bounds
, BoundsString()) << " at " << i
;
292 // Assertions for AddTab().
293 TEST_F(StackedTabStripLayoutTest
, AddTab
) {
295 CommonTestData common_data
;
300 // Adding a background tab test cases.
301 { { 0, 300, 100, -10, 2, 0, 1, "0 90 180 198 200", "0 16 106 196 198 200"},
303 { { 0, 300, 100, -10, 2, 0, 1, "0 90 180 198 200", "0 2 4 20 110 200"},
305 { { 0, 300, 100, -10, 2, 0, 1, "0 90 180 198 200", "0 90 180 196 198 200"},
307 { { 0, 300, 100, -10, 2, 0, 1, "0 90 180 198 200", "0 2 4 94 184 200"},
310 { { 4, 200, 100, -10, 2, 1, 2, "0 4 10 100", "0 0 8 10 100"},
312 { { 4, 200, 100, -10, 2, 1, 2, "0 4 10 100", "0 0 8 98 100"},
314 { { 4, 200, 100, -10, 2, 1, 2, "0 4 10 100", "0 0 8 98 100"},
316 { { 0, 200, 100, -10, 2, 0, 2, "0 2 10 100", "0 4 94 98 100"},
319 { { 0, 200, 100, -10, 2, 0, 0, "0 90 92 92 94 96 98 100",
320 "0 0 0 2 4 6 8 98 100"},
322 { { 0, 200, 100, -10, 2, 0, 7, "0 2 4 6 8 8 10 100",
323 "0 0 2 4 6 8 96 98 100"},
325 { { 0, 200, 100, -10, 2, 0, 7, "0 2 4 6 8 8 10 100",
326 "0 2 4 6 8 94 96 98 100"},
328 { { 0, 200, 100, -10, 2, 0, 2, "0 2 10 100", "0 2 10 98 100"},
330 { { 0, 200, 100, -10, 2, 0, 2, "0 2 10 100", "0 2 4 10 100"},
332 { { 0, 200, 100, -10, 2, 0, 2, "0 2 10 100", "0 90 96 98 100"},
335 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
336 CreateLayout(test_data
[i
].common_data
);
338 if (test_data
[i
].add_active
)
339 add_types
|= StackedTabStripLayout::kAddTypeActive
;
340 if (test_data
[i
].add_pinned
)
341 add_types
|= StackedTabStripLayout::kAddTypePinned
;
342 AddViewToViewModel(test_data
[i
].add_index
);
343 layout_
->AddTab(test_data
[i
].add_index
, add_types
,
344 test_data
[i
].common_data
.initial_x
+
345 (test_data
[i
].add_pinned
? 4 : 0));
346 EXPECT_EQ(test_data
[i
].common_data
.expected_bounds
, BoundsString()) <<
351 // Assertions around removing tabs.
352 TEST_F(StackedTabStripLayoutTest
, RemoveTab
) {
353 // TODO: add coverage of removing pinned tabs!
355 struct CommonTestData common_data
;
356 const int remove_index
;
357 const int x_after_remove
;
359 { { 0, 882, 220, -29, 2, 0, 4, "0 23 214 405 596 602",
360 "0 191 382 573 662" }, 1, 0 },
362 // Remove before active.
363 { { 0, 200, 100, -10, 2, 0, 4, "0 2 4 6 8 10 80 98 100",
364 "0 2 6 8 10 80 98 100" },
367 // Stacked tabs on both sides.
368 { { 0, 200, 100, -10, 2, 0, 4, "0 2 4 6 8 10 80 98 100",
369 "0 2 4 6 10 80 98 100" },
373 { { 8, 200, 100, -10, 2, 1, 0, "0 8 94 96 98 100", "0 86 88 90 100" },
375 { { 16, 200, 100, -10, 2, 2, 0, "0 8 16 94 96 98 100", "8 8 86 88 90 100" },
377 { { 16, 200, 100, -10, 2, 2, 0, "0 8 16 94 96 98 100", "0 8 86 88 90 100" },
380 // Remove from ideal layout.
381 { { 0, 200, 100, -10, 2, 0, 0, "0 90 94 96 98 100", "0 90 96 98 100" },
383 { { 0, 200, 100, -10, 2, 0, 0, "0 90 94 96 98 100", "0 90 96 98 100" },
385 { { 0, 200, 100, -10, 2, 0, 0, "0 90 94 96 98 100", "0 90 96 98 100" },
387 { { 0, 200, 100, -10, 2, 0, 0, "0 90 94 96 98 100", "0 90 94 98 100" },
389 { { 0, 200, 100, -10, 2, 0, 0, "0 90 94 96 98 100", "0 90 94 96 100" },
392 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
393 CreateLayout(test_data
[i
].common_data
);
394 int old_x
= view_model_
.ideal_bounds(test_data
[i
].remove_index
).x();
395 view_model_
.Remove(test_data
[i
].remove_index
);
396 layout_
->RemoveTab(test_data
[i
].remove_index
, test_data
[i
].x_after_remove
,
398 EXPECT_EQ(test_data
[i
].common_data
.expected_bounds
, BoundsString()) <<
403 // Assertions for SetWidth().
404 TEST_F(StackedTabStripLayoutTest
, SetWidth
) {
406 CommonTestData common_data
;
409 { { 0, 500, 100, -10, 2, 0, 4, "0 90 180 270 360 400",
410 "0 90 180 196 198 200"}, 300 },
412 // Verifies a bug in AdjustTrailingStackedTabs().
413 { { 0, 103, 100, -10, 2, 0, 0, "", "0 2"}, 102 },
415 { { 8, 250, 100, -10, 2, 2, 2, "0 4 8 98 148 150", "0 4 8 98 160 250"},
417 { { 8, 250, 100, -10, 2, 2, 2, "0 4 8 98 148 150", "0 4 8 96 98 100"},
420 { { 0, 250, 100, -10, 2, 0, 2, "0 40 90 120 150", "0 40 90 98 100"}, 200 },
421 { { 0, 250, 100, -10, 2, 0, 2, "0 2 60 150", "0 2 60 100"}, 200 },
422 { { 0, 250, 100, -10, 2, 0, 2, "0 40 120 150", "0 40 98 100"}, 200 },
424 { { 0, 200, 100, -10, 2, 0, 2, "0 2 10 100", "0 2 60 150"}, 250 },
425 { { 0, 200, 100, -10, 2, 0, 2, "0 2 4 10 100", "0 2 20 110 200"}, 300 },
427 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
428 CreateLayout(test_data
[i
].common_data
);
429 layout_
->SetWidth(test_data
[i
].new_width
);
430 EXPECT_EQ(test_data
[i
].common_data
.expected_bounds
, BoundsString()) <<
435 // Assertions for SetActiveIndex().
436 TEST_F(StackedTabStripLayoutTest
, SetActiveIndex
) {
438 CommonTestData common_data
;
441 { { 0, 250, 100, -10, 2, 0, 2, "0 4 8 98 148 150", "0 90 144 146 148 150"},
443 { { 0, 250, 100, -10, 2, 0, 2, "0 4 8 98 148 150", "0 2 4 58 148 150"}, 4 },
444 { { 0, 250, 100, -10, 2, 0, 2, "0 4 8 98 148 150", "0 2 4 6 60 150"}, 5 },
445 { { 4, 250, 100, -10, 2, 1, 2, "0 4 8 98 148 150", "0 4 94 146 148 150"},
448 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
449 CreateLayout(test_data
[i
].common_data
);
450 layout_
->SetActiveIndex(test_data
[i
].new_index
);
451 EXPECT_EQ(test_data
[i
].common_data
.expected_bounds
, BoundsString()) <<
456 // Makes sure don't crash when resized and only one tab.
457 TEST_F(StackedTabStripLayoutTest
, EmptyTest
) {
458 StackedTabStripLayout
layout(gfx::Size(160, 10), -27, 6, 4, &view_model_
);
459 PrepareChildViews(1);
460 layout
.AddTab(0, StackedTabStripLayout::kAddTypeActive
, 0);
461 layout
.SetWidth(100);
464 layout
.SetWidth(500);
467 // Assertions around removing tabs.
468 TEST_F(StackedTabStripLayoutTest
, MoveTab
) {
469 // TODO: add coverage of removing pinned tabs!
471 struct CommonTestData common_data
;
474 const int new_active_index
;
475 const int new_start_x
;
476 const int new_pinned_tab_count
;
479 { { 10, 300, 100, -10, 2, 2, 0, "", "0 5 10 100 190 198 200" },
483 { { 0, 300, 100, -10, 2, 0, 4, "", "0 5 95 185 196 198 200" },
485 { { 0, 300, 100, -10, 2, 1, 2, "", "0 5 10 100 190 198 200" },
488 { { 0, 200, 100, -10, 2, 0, 4, "0 2 4 6 96 98 100", "0 2 4 6 96 98 100" },
490 { { 0, 200, 100, -10, 2, 0, 4, "0 2 4 6 96 98 100", "0 2 4 6 8 10 100" },
493 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
494 CreateLayout(test_data
[i
].common_data
);
495 view_model_
.MoveViewOnly(test_data
[i
].from
, test_data
[i
].to
);
496 for (int j
= 0; j
< test_data
[i
].new_pinned_tab_count
; ++j
) {
499 view_model_
.set_ideal_bounds(j
, bounds
);
501 layout_
->MoveTab(test_data
[i
].from
, test_data
[i
].to
,
502 test_data
[i
].new_active_index
, test_data
[i
].new_start_x
,
503 test_data
[i
].new_pinned_tab_count
);
504 EXPECT_EQ(test_data
[i
].common_data
.expected_bounds
, BoundsString()) <<
509 // Assertions around IsStacked().
510 TEST_F(StackedTabStripLayoutTest
, IsStacked
) {
511 // A single tab with enough space should never be stacked.
512 PrepareChildViews(1);
513 layout_
.reset(new StackedTabStripLayout(
514 gfx::Size(100, 10), -10, 2, 4, &view_model_
));
515 Reset(layout_
.get(), 0, 400, 0, 0);
516 EXPECT_FALSE(layout_
->IsStacked(0));
518 // First tab active, remaining tabs stacked.
519 PrepareChildViews(8);
520 Reset(layout_
.get(), 0, 400, 0, 0);
521 EXPECT_FALSE(layout_
->IsStacked(0));
522 EXPECT_TRUE(layout_
->IsStacked(7));
524 // Last tab active, preceeding tabs stacked.
525 layout_
->SetActiveIndex(7);
526 EXPECT_FALSE(layout_
->IsStacked(7));
527 EXPECT_TRUE(layout_
->IsStacked(0));
530 // Assertions around SetXAndPinnedCount.
531 TEST_F(StackedTabStripLayoutTest
, SetXAndPinnedCount
) {
532 // Verifies we don't crash when transitioning to all pinned tabs.
533 PrepareChildViews(1);
534 layout_
.reset(new StackedTabStripLayout(
535 gfx::Size(100, 10), -10, 2, 4, &view_model_
));
536 Reset(layout_
.get(), 0, 400, 0, 0);
537 layout_
->SetXAndPinnedCount(0, 1);
540 // Assertions around SetXAndPinnedCount.
541 TEST_F(StackedTabStripLayoutTest
, SetActiveTabLocation
) {
543 struct CommonTestData common_data
;
546 // Active tab is the first tab, can't be moved.
547 { { 0, 300, 100, -10, 2, 0, 0, "", "0 90 180 194 196 198 200" }, 50 },
549 // Active tab is pinned; should result in nothing.
550 { { 0, 300, 100, -10, 2, 2, 1, "", "0 0 0 90 180 198 200" }, 199 },
552 // Location is too far to the right, ends up being pushed in.
553 { { 0, 300, 100, -10, 2, 0, 3, "", "0 14 104 194 196 198 200" }, 199 },
555 // Location can be honored.
556 { { 0, 300, 100, -10, 2, 0, 3, "", "0 2 4 40 130 198 200" }, 40 },
558 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
559 CreateLayout(test_data
[i
].common_data
);
560 layout_
->SetActiveTabLocation(test_data
[i
].location
);
561 EXPECT_EQ(test_data
[i
].common_data
.expected_bounds
, BoundsString()) <<