OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_unittest.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_unittest.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 11 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
12 #include "chrome/browser/extensions/extension_action.h" | 12 #include "chrome/browser/extensions/extension_action.h" |
13 #include "chrome/browser/extensions/extension_action_manager.h" | 13 #include "chrome/browser/extensions/extension_action_manager.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/extensions/test_extension_system.h" | 15 #include "chrome/browser/extensions/test_extension_system.h" |
16 #include "chrome/browser/sessions/session_tab_helper.h" | 16 #include "chrome/browser/sessions/session_tab_helper.h" |
17 #include "chrome/browser/ui/layout_constants.h" | 17 #include "chrome/browser/ui/layout_constants.h" |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
19 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" | 19 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
20 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | 20 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
21 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_delegate.h" | 21 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_delegate.h" |
22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
23 #include "extensions/browser/extension_prefs.h" | 23 #include "extensions/browser/extension_prefs.h" |
24 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
25 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
| 26 #include "extensions/common/feature_switch.h" |
26 #include "ui/base/test/material_design_controller_test_api.h" | 27 #include "ui/base/test/material_design_controller_test_api.h" |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 // Verifies that the toolbar order matches for the given |actions_bar|. If the | 31 // Verifies that the toolbar order matches for the given |actions_bar|. If the |
31 // order matches, the return value is empty; otherwise, it contains the error. | 32 // order matches, the return value is empty; otherwise, it contains the error. |
32 std::string VerifyToolbarOrderForBar( | 33 std::string VerifyToolbarOrderForBar( |
33 const ToolbarActionsBar* actions_bar, | 34 const ToolbarActionsBar* actions_bar, |
34 BrowserActionTestUtil* browser_action_test_util, | 35 BrowserActionTestUtil* browser_action_test_util, |
35 const char* expected_names[], | 36 const char* expected_names[], |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 error += base::StringPrintf( | 79 error += base::StringPrintf( |
79 "Incorrect visible count in view: expected %d, found %d.\n", | 80 "Incorrect visible count in view: expected %d, found %d.\n", |
80 static_cast<int>(visible_count), static_cast<int>(view_icon_count)); | 81 static_cast<int>(visible_count), static_cast<int>(view_icon_count)); |
81 | 82 |
82 return error; | 83 return error; |
83 } | 84 } |
84 | 85 |
85 } // namespace | 86 } // namespace |
86 | 87 |
87 ToolbarActionsBarUnitTest::ToolbarActionsBarUnitTest() | 88 ToolbarActionsBarUnitTest::ToolbarActionsBarUnitTest() |
88 : toolbar_model_(nullptr), | 89 : toolbar_model_(nullptr) {} |
89 use_redesign_(false) {} | |
90 | |
91 ToolbarActionsBarUnitTest::ToolbarActionsBarUnitTest(bool use_redesign) | |
92 : toolbar_model_(nullptr), | |
93 use_redesign_(use_redesign) {} | |
94 | 90 |
95 ToolbarActionsBarUnitTest::~ToolbarActionsBarUnitTest() {} | 91 ToolbarActionsBarUnitTest::~ToolbarActionsBarUnitTest() {} |
96 | 92 |
97 void ToolbarActionsBarUnitTest::SetUp() { | 93 void ToolbarActionsBarUnitTest::SetUp() { |
98 redesign_switch_.reset(new extensions::FeatureSwitch::ScopedOverride( | |
99 extensions::FeatureSwitch::extension_action_redesign(), use_redesign_)); | |
100 | |
101 BrowserWithTestWindowTest::SetUp(); | 94 BrowserWithTestWindowTest::SetUp(); |
102 // The toolbar typically displays extension icons, so create some extension | 95 // The toolbar typically displays extension icons, so create some extension |
103 // test infrastructure. | 96 // test infrastructure. |
104 extensions::TestExtensionSystem* extension_system = | 97 extensions::TestExtensionSystem* extension_system = |
105 static_cast<extensions::TestExtensionSystem*>( | 98 static_cast<extensions::TestExtensionSystem*>( |
106 extensions::ExtensionSystem::Get(profile())); | 99 extensions::ExtensionSystem::Get(profile())); |
107 extension_system->CreateExtensionService( | 100 extension_system->CreateExtensionService( |
108 base::CommandLine::ForCurrentProcess(), | 101 base::CommandLine::ForCurrentProcess(), |
109 base::FilePath(), | 102 base::FilePath(), |
110 false); | 103 false); |
111 toolbar_model_ = | 104 toolbar_model_ = |
112 extensions::extension_action_test_util::CreateToolbarModelForProfile( | 105 extensions::extension_action_test_util::CreateToolbarModelForProfile( |
113 profile()); | 106 profile()); |
114 | 107 |
115 material_design_state_.reset( | 108 material_design_state_.reset( |
116 new ui::test::MaterialDesignControllerTestAPI(GetParam())); | 109 new ui::test::MaterialDesignControllerTestAPI(GetParam())); |
117 ToolbarActionsBar::disable_animations_for_testing_ = true; | 110 ToolbarActionsBar::disable_animations_for_testing_ = true; |
118 browser_action_test_util_.reset(new BrowserActionTestUtil(browser(), false)); | 111 browser_action_test_util_.reset(new BrowserActionTestUtil(browser(), false)); |
119 | 112 |
120 if (use_redesign_) { | 113 if (extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) { |
121 overflow_browser_action_test_util_ = | 114 overflow_browser_action_test_util_ = |
122 browser_action_test_util_->CreateOverflowBar(); | 115 browser_action_test_util_->CreateOverflowBar(); |
123 } | 116 } |
124 } | 117 } |
125 | 118 |
126 void ToolbarActionsBarUnitTest::TearDown() { | 119 void ToolbarActionsBarUnitTest::TearDown() { |
127 // Since the profile gets destroyed in BrowserWithTestWindowTest::TearDown(), | 120 // Since the profile gets destroyed in BrowserWithTestWindowTest::TearDown(), |
128 // we need to delete this now. | 121 // we need to delete this now. |
129 browser_action_test_util_.reset(); | 122 browser_action_test_util_.reset(); |
130 overflow_browser_action_test_util_.reset(); | 123 overflow_browser_action_test_util_.reset(); |
131 ToolbarActionsBar::disable_animations_for_testing_ = false; | 124 ToolbarActionsBar::disable_animations_for_testing_ = false; |
132 redesign_switch_.reset(); | |
133 material_design_state_.reset(); | 125 material_design_state_.reset(); |
134 BrowserWithTestWindowTest::TearDown(); | 126 BrowserWithTestWindowTest::TearDown(); |
135 } | 127 } |
136 | 128 |
137 void ToolbarActionsBarUnitTest::ActivateTab(int index) { | 129 void ToolbarActionsBarUnitTest::ActivateTab(int index) { |
138 ASSERT_NE(nullptr, browser()->tab_strip_model()->GetWebContentsAt(index)); | 130 ASSERT_NE(nullptr, browser()->tab_strip_model()->GetWebContentsAt(index)); |
139 browser()->tab_strip_model()->ActivateTabAt(index, true); | 131 browser()->tab_strip_model()->ActivateTabAt(index, true); |
140 } | 132 } |
141 | 133 |
142 scoped_refptr<const extensions::Extension> | 134 scoped_refptr<const extensions::Extension> |
(...skipping 21 matching lines...) Expand all Loading... |
164 const char* expected_names[], | 156 const char* expected_names[], |
165 size_t total_size, | 157 size_t total_size, |
166 size_t visible_count) { | 158 size_t visible_count) { |
167 std::string main_bar_error = | 159 std::string main_bar_error = |
168 VerifyToolbarOrderForBar(toolbar_actions_bar(), | 160 VerifyToolbarOrderForBar(toolbar_actions_bar(), |
169 browser_action_test_util(), | 161 browser_action_test_util(), |
170 expected_names, | 162 expected_names, |
171 total_size, | 163 total_size, |
172 visible_count); | 164 visible_count); |
173 std::string overflow_bar_error; | 165 std::string overflow_bar_error; |
174 if (use_redesign_) { | 166 if (extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) { |
175 overflow_bar_error = | 167 overflow_bar_error = |
176 VerifyToolbarOrderForBar(overflow_bar(), | 168 VerifyToolbarOrderForBar(overflow_bar(), |
177 overflow_browser_action_test_util(), | 169 overflow_browser_action_test_util(), |
178 expected_names, | 170 expected_names, |
179 total_size, | 171 total_size, |
180 total_size - visible_count); | 172 total_size - visible_count); |
181 | 173 |
182 } | 174 } |
183 | 175 |
184 return main_bar_error.empty() && overflow_bar_error.empty() ? | 176 return main_bar_error.empty() && overflow_bar_error.empty() ? |
185 testing::AssertionSuccess() : | 177 testing::AssertionSuccess() : |
186 testing::AssertionFailure() << "main bar error:\n" << main_bar_error << | 178 testing::AssertionFailure() << "main bar error:\n" << main_bar_error << |
187 "overflow bar error:\n" << overflow_bar_error; | 179 "overflow bar error:\n" << overflow_bar_error; |
188 } | 180 } |
189 | 181 |
190 ToolbarActionsBarRedesignUnitTest::ToolbarActionsBarRedesignUnitTest() | |
191 : ToolbarActionsBarUnitTest(true) {} | |
192 | |
193 ToolbarActionsBarRedesignUnitTest::~ToolbarActionsBarRedesignUnitTest() {} | |
194 | |
195 // Note: First argument is optional and intentionally left blank. | 182 // Note: First argument is optional and intentionally left blank. |
196 // (it's a prefix for the generated test cases) | 183 // (it's a prefix for the generated test cases) |
197 INSTANTIATE_TEST_CASE_P( | 184 INSTANTIATE_TEST_CASE_P( |
198 , | 185 , |
199 ToolbarActionsBarUnitTest, | 186 ToolbarActionsBarUnitTest, |
200 testing::Values(ui::MaterialDesignController::NON_MATERIAL, | 187 testing::Values(ui::MaterialDesignController::NON_MATERIAL, |
201 ui::MaterialDesignController::MATERIAL_NORMAL, | 188 ui::MaterialDesignController::MATERIAL_NORMAL, |
202 ui::MaterialDesignController::MATERIAL_HYBRID)); | 189 ui::MaterialDesignController::MATERIAL_HYBRID)); |
203 | 190 |
204 TEST_P(ToolbarActionsBarUnitTest, BasicToolbarActionsBarTest) { | 191 TEST_P(ToolbarActionsBarUnitTest, BasicToolbarActionsBarTest) { |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 } | 389 } |
403 | 390 |
404 toolbar_model()->StopHighlighting(); | 391 toolbar_model()->StopHighlighting(); |
405 { | 392 { |
406 // And, again, back to normal. | 393 // And, again, back to normal. |
407 const char* expected_names[] = {kExtension0, kExtension1, kExtension2}; | 394 const char* expected_names[] = {kExtension0, kExtension1, kExtension2}; |
408 EXPECT_TRUE(VerifyToolbarOrder(expected_names, 3u, 3u)); | 395 EXPECT_TRUE(VerifyToolbarOrder(expected_names, 3u, 3u)); |
409 } | 396 } |
410 } | 397 } |
411 | 398 |
412 // Note: First argument is optional and intentionally left blank. | |
413 // (it's a prefix for the generated test cases) | |
414 INSTANTIATE_TEST_CASE_P( | |
415 , | |
416 ToolbarActionsBarRedesignUnitTest, | |
417 testing::Values(ui::MaterialDesignController::NON_MATERIAL, | |
418 ui::MaterialDesignController::MATERIAL_NORMAL, | |
419 ui::MaterialDesignController::MATERIAL_HYBRID)); | |
420 | |
421 // Test the bounds calculation for different indices. | 399 // Test the bounds calculation for different indices. |
422 TEST_P(ToolbarActionsBarRedesignUnitTest, TestActionFrameBounds) { | 400 TEST_P(ToolbarActionsBarUnitTest, TestActionFrameBounds) { |
423 const int kIconWidth = ToolbarActionsBar::IconWidth(false); | 401 const int kIconWidth = ToolbarActionsBar::IconWidth(false); |
424 const int kIconHeight = ToolbarActionsBar::IconHeight(); | 402 const int kIconHeight = ToolbarActionsBar::IconHeight(); |
425 const int kIconWidthWithPadding = ToolbarActionsBar::IconWidth(true); | 403 const int kIconWidthWithPadding = ToolbarActionsBar::IconWidth(true); |
426 const int kIconSpacing = GetLayoutConstant(TOOLBAR_STANDARD_SPACING); | 404 const int kIconSpacing = GetLayoutConstant(TOOLBAR_STANDARD_SPACING); |
427 const int kIconsPerOverflowRow = 3; | 405 const int kIconsPerOverflowRow = 3; |
428 const int kNumExtensions = 7; | 406 const int kNumExtensions = 7; |
429 const int kSpacing = | 407 const int kSpacing = |
430 toolbar_actions_bar()->platform_settings().item_spacing; | 408 toolbar_actions_bar()->platform_settings().item_spacing; |
431 | 409 |
432 // Initialization: 7 total extensions, with 3 visible per row in overflow. | 410 // Initialization: 7 total extensions, with 3 visible per row in overflow. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 overflow_bar()->GetFrameForIndex(6)); | 455 overflow_bar()->GetFrameForIndex(6)); |
478 | 456 |
479 // Check with > 2 rows. | 457 // Check with > 2 rows. |
480 toolbar_model()->SetVisibleIconCount(0); | 458 toolbar_model()->SetVisibleIconCount(0); |
481 EXPECT_EQ(gfx::Rect(kSpacing, 0, kIconWidth, kIconHeight), | 459 EXPECT_EQ(gfx::Rect(kSpacing, 0, kIconWidth, kIconHeight), |
482 overflow_bar()->GetFrameForIndex(0)); | 460 overflow_bar()->GetFrameForIndex(0)); |
483 EXPECT_EQ(gfx::Rect(kSpacing, kIconHeight * 2, kIconWidth, kIconHeight), | 461 EXPECT_EQ(gfx::Rect(kSpacing, kIconHeight * 2, kIconWidth, kIconHeight), |
484 overflow_bar()->GetFrameForIndex(6)); | 462 overflow_bar()->GetFrameForIndex(6)); |
485 } | 463 } |
486 | 464 |
487 TEST_P(ToolbarActionsBarRedesignUnitTest, TestStartAndEndIndexes) { | 465 TEST_P(ToolbarActionsBarUnitTest, TestStartAndEndIndexes) { |
488 const int kIconWidthWithPadding = ToolbarActionsBar::IconWidth(true); | 466 const int kIconWidthWithPadding = ToolbarActionsBar::IconWidth(true); |
489 const int kIconSpacing = GetLayoutConstant(TOOLBAR_STANDARD_SPACING); | 467 const int kIconSpacing = GetLayoutConstant(TOOLBAR_STANDARD_SPACING); |
490 | 468 |
491 for (int i = 0; i < 3; ++i) { | 469 for (int i = 0; i < 3; ++i) { |
492 CreateAndAddExtension( | 470 CreateAndAddExtension( |
493 base::StringPrintf("extension %d", i), | 471 base::StringPrintf("extension %d", i), |
494 extensions::extension_action_test_util::BROWSER_ACTION); | 472 extensions::extension_action_test_util::BROWSER_ACTION); |
495 } | 473 } |
496 // At the start, all icons should be present on the main bar, and no | 474 // At the start, all icons should be present on the main bar, and no |
497 // overflow should be needed. | 475 // overflow should be needed. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 toolbar_actions_bar()->GetPreferredSize().width()); | 510 toolbar_actions_bar()->GetPreferredSize().width()); |
533 EXPECT_EQ(3u, toolbar_actions_bar()->GetIconCount()); | 511 EXPECT_EQ(3u, toolbar_actions_bar()->GetIconCount()); |
534 EXPECT_EQ(0u, toolbar_actions_bar()->GetStartIndexInBounds()); | 512 EXPECT_EQ(0u, toolbar_actions_bar()->GetStartIndexInBounds()); |
535 EXPECT_EQ(3u, toolbar_actions_bar()->GetEndIndexInBounds()); | 513 EXPECT_EQ(3u, toolbar_actions_bar()->GetEndIndexInBounds()); |
536 EXPECT_EQ(3u, overflow_bar()->GetStartIndexInBounds()); | 514 EXPECT_EQ(3u, overflow_bar()->GetStartIndexInBounds()); |
537 EXPECT_EQ(3u, overflow_bar()->GetEndIndexInBounds()); | 515 EXPECT_EQ(3u, overflow_bar()->GetEndIndexInBounds()); |
538 EXPECT_FALSE(toolbar_actions_bar()->NeedsOverflow()); | 516 EXPECT_FALSE(toolbar_actions_bar()->NeedsOverflow()); |
539 } | 517 } |
540 | 518 |
541 // Tests the logic for determining if the container needs an overflow menu item. | 519 // Tests the logic for determining if the container needs an overflow menu item. |
542 TEST_P(ToolbarActionsBarRedesignUnitTest, TestNeedsOverflow) { | 520 TEST_P(ToolbarActionsBarUnitTest, TestNeedsOverflow) { |
543 CreateAndAddExtension( | 521 CreateAndAddExtension( |
544 "extension 1", | 522 "extension 1", |
545 extensions::extension_action_test_util::BROWSER_ACTION); | 523 extensions::extension_action_test_util::BROWSER_ACTION); |
546 // One extension on the main bar, none overflowed. Overflow not needed. | 524 // One extension on the main bar, none overflowed. Overflow not needed. |
547 EXPECT_EQ(1u, toolbar_actions_bar()->GetIconCount()); | 525 EXPECT_EQ(1u, toolbar_actions_bar()->GetIconCount()); |
548 EXPECT_EQ(0u, overflow_bar()->GetIconCount()); | 526 EXPECT_EQ(0u, overflow_bar()->GetIconCount()); |
549 EXPECT_FALSE(toolbar_actions_bar()->NeedsOverflow()); | 527 EXPECT_FALSE(toolbar_actions_bar()->NeedsOverflow()); |
550 | 528 |
551 // Set one extension in the overflow menu, none on the main bar. Overflow | 529 // Set one extension in the overflow menu, none on the main bar. Overflow |
552 // needed. | 530 // needed. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 extensions::extension_action_test_util::BROWSER_ACTION); | 569 extensions::extension_action_test_util::BROWSER_ACTION); |
592 toolbar_model()->SetVisibleIconCount(0u); | 570 toolbar_model()->SetVisibleIconCount(0u); |
593 { | 571 { |
594 base::RunLoop run_loop; | 572 base::RunLoop run_loop; |
595 toolbar_actions_bar()->PopOutAction(action, true, run_loop.QuitClosure()); | 573 toolbar_actions_bar()->PopOutAction(action, true, run_loop.QuitClosure()); |
596 run_loop.Run(); | 574 run_loop.Run(); |
597 } | 575 } |
598 EXPECT_EQ(1u, toolbar_actions_bar()->GetIconCount()); | 576 EXPECT_EQ(1u, toolbar_actions_bar()->GetIconCount()); |
599 EXPECT_TRUE(toolbar_actions_bar()->NeedsOverflow()); | 577 EXPECT_TRUE(toolbar_actions_bar()->NeedsOverflow()); |
600 } | 578 } |
OLD | NEW |