OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/views/toolbar/browser_actions_container.h" | 5 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
9 #include "chrome/browser/extensions/browser_action_test_util.h" | 9 #include "chrome/browser/extensions/browser_action_test_util.h" |
10 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
11 #include "chrome/browser/extensions/extension_toolbar_model.h" | 11 #include "chrome/browser/extensions/extension_toolbar_model.h" |
12 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
13 #include "chrome/browser/ui/browser_window_testing_views.h" | 13 #include "chrome/browser/ui/browser_window_testing_views.h" |
| 14 #include "chrome/browser/ui/views/frame/browser_view.h" |
14 #include "chrome/browser/ui/views/toolbar/browser_action_view.h" | 15 #include "chrome/browser/ui/views/toolbar/browser_action_view.h" |
15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 16 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
16 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
17 #include "extensions/browser/extension_prefs.h" | 18 #include "extensions/browser/extension_prefs.h" |
18 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
19 #include "ui/gfx/geometry/point.h" | 20 #include "ui/gfx/geometry/point.h" |
20 #include "ui/views/view.h" | 21 #include "ui/views/view.h" |
21 | 22 |
22 using extensions::Extension; | 23 using extensions::Extension; |
23 | 24 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 std::string id = browser_actions_bar()->GetExtensionId(0); | 76 std::string id = browser_actions_bar()->GetExtensionId(0); |
76 UnloadExtension(id); | 77 UnloadExtension(id); |
77 EXPECT_EQ(0, browser_actions_bar()->NumberOfBrowserActions()); | 78 EXPECT_EQ(0, browser_actions_bar()->NumberOfBrowserActions()); |
78 } | 79 } |
79 | 80 |
80 // Test moving various browser actions. This is not to check the logic of the | 81 // Test moving various browser actions. This is not to check the logic of the |
81 // move (that's in the toolbar model tests), but just to check our ui. | 82 // move (that's in the toolbar model tests), but just to check our ui. |
82 IN_PROC_BROWSER_TEST_F(BrowserActionsContainerTest, | 83 IN_PROC_BROWSER_TEST_F(BrowserActionsContainerTest, |
83 MoveBrowserActions) { | 84 MoveBrowserActions) { |
84 // Load three extensions with browser actions. | 85 // Load three extensions with browser actions. |
85 const Extension* extension_a = | 86 const extensions::Extension* extension_a = |
86 LoadExtension(test_data_dir_.AppendASCII("api_test") | 87 LoadExtension(test_data_dir_.AppendASCII("api_test") |
87 .AppendASCII("browser_action") | 88 .AppendASCII("browser_action") |
88 .AppendASCII("basics")); | 89 .AppendASCII("basics")); |
89 ASSERT_TRUE(extension_a); | 90 ASSERT_TRUE(extension_a); |
90 const Extension* extension_b = | 91 const extensions::Extension* extension_b = |
91 LoadExtension(test_data_dir_.AppendASCII("api_test") | 92 LoadExtension(test_data_dir_.AppendASCII("api_test") |
92 .AppendASCII("browser_action") | 93 .AppendASCII("browser_action") |
93 .AppendASCII("add_popup")); | 94 .AppendASCII("add_popup")); |
94 ASSERT_TRUE(extension_b); | 95 ASSERT_TRUE(extension_b); |
95 const Extension* extension_c = | 96 const extensions::Extension* extension_c = |
96 LoadExtension(test_data_dir_.AppendASCII("api_test") | 97 LoadExtension(test_data_dir_.AppendASCII("api_test") |
97 .AppendASCII("browser_action") | 98 .AppendASCII("browser_action") |
98 .AppendASCII("remove_popup")); | 99 .AppendASCII("remove_popup")); |
99 ASSERT_TRUE(extension_c); | 100 ASSERT_TRUE(extension_c); |
100 | 101 |
101 EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions()); | 102 EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions()); |
102 EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions()); | 103 EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions()); |
103 | 104 |
104 extensions::ExtensionToolbarModel* model = | 105 extensions::ExtensionToolbarModel* model = |
105 extensions::ExtensionToolbarModel::Get(profile()); | 106 extensions::ExtensionToolbarModel::Get(profile()); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 action_view = container->GetBrowserActionViewAt(0); | 284 action_view = container->GetBrowserActionViewAt(0); |
284 EXPECT_FALSE(container->CanStartDragForView(action_view, point, point)); | 285 EXPECT_FALSE(container->CanStartDragForView(action_view, point, point)); |
285 | 286 |
286 // We should go back to normal after leaving highlight mode. | 287 // We should go back to normal after leaving highlight mode. |
287 model->StopHighlighting(); | 288 model->StopHighlighting(); |
288 EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions()); | 289 EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions()); |
289 EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions()); | 290 EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions()); |
290 action_view = container->GetBrowserActionViewAt(0); | 291 action_view = container->GetBrowserActionViewAt(0); |
291 EXPECT_TRUE(container->CanStartDragForView(action_view, point, point)); | 292 EXPECT_TRUE(container->CanStartDragForView(action_view, point, point)); |
292 } | 293 } |
| 294 |
| 295 // Test the behavior of the overflow container for Extension Actions. |
| 296 class BrowserActionsContainerOverflowTest : public BrowserActionsContainerTest { |
| 297 public: |
| 298 BrowserActionsContainerOverflowTest() : main_bar_(NULL), model_(NULL) { |
| 299 } |
| 300 virtual ~BrowserActionsContainerOverflowTest() { |
| 301 } |
| 302 |
| 303 protected: |
| 304 // Returns true if the order of the BrowserActionViews in |main_bar_| |
| 305 // and |overflow_bar_| match. |
| 306 bool ViewOrdersMatch(); |
| 307 |
| 308 // Returns Success if the visible count matches |expected_visible|. This means |
| 309 // that the number of visible browser actions in |main_bar_| is |
| 310 // |expected_visible| and shows the first icons, and that the overflow bar |
| 311 // shows all (and only) the remainder. |
| 312 testing::AssertionResult VerifyVisibleCount(size_t expected_visible); |
| 313 |
| 314 // Accessors. |
| 315 BrowserActionsContainer* main_bar() { return main_bar_; } |
| 316 BrowserActionsContainer* overflow_bar() { return overflow_bar_.get(); } |
| 317 extensions::ExtensionToolbarModel* model() { return model_; } |
| 318 |
| 319 private: |
| 320 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE; |
| 321 virtual void SetUpOnMainThread() OVERRIDE; |
| 322 virtual void TearDownOnMainThread() OVERRIDE; |
| 323 |
| 324 // The main BrowserActionsContainer (owned by the browser view). |
| 325 BrowserActionsContainer* main_bar_; |
| 326 |
| 327 // The overflow BrowserActionsContainer. We manufacture this so that we don't |
| 328 // have to open the wrench menu. |
| 329 scoped_ptr<BrowserActionsContainer> overflow_bar_; |
| 330 |
| 331 // The associated toolbar model. |
| 332 extensions::ExtensionToolbarModel* model_; |
| 333 |
| 334 // Enable the feature redesign switch. |
| 335 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_; |
| 336 |
| 337 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainerOverflowTest); |
| 338 }; |
| 339 |
| 340 void BrowserActionsContainerOverflowTest::SetUpCommandLine( |
| 341 base::CommandLine* command_line) { |
| 342 BrowserActionsContainerTest::SetUpCommandLine(command_line); |
| 343 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride( |
| 344 extensions::FeatureSwitch::extension_action_redesign(), |
| 345 true)); |
| 346 } |
| 347 |
| 348 void BrowserActionsContainerOverflowTest::SetUpOnMainThread() { |
| 349 BrowserActionsContainerTest::SetUpOnMainThread(); |
| 350 main_bar_ = BrowserView::GetBrowserViewForBrowser(browser()) |
| 351 ->toolbar()->browser_actions(); |
| 352 overflow_bar_.reset(new BrowserActionsContainer(browser(), NULL, main_bar_)); |
| 353 overflow_bar_->set_owned_by_client(); |
| 354 model_ = extensions::ExtensionToolbarModel::Get(profile()); |
| 355 } |
| 356 |
| 357 void BrowserActionsContainerOverflowTest::TearDownOnMainThread() { |
| 358 overflow_bar_.reset(); |
| 359 enable_redesign_.reset(); |
| 360 BrowserActionsContainerTest::TearDownOnMainThread(); |
| 361 } |
| 362 |
| 363 bool BrowserActionsContainerOverflowTest::ViewOrdersMatch() { |
| 364 if (main_bar_->num_browser_actions() != |
| 365 overflow_bar_->num_browser_actions()) |
| 366 return false; |
| 367 for (size_t i = 0; i < main_bar_->num_browser_actions(); ++i) { |
| 368 if (main_bar_->GetBrowserActionViewAt(i)->extension() != |
| 369 overflow_bar_->GetBrowserActionViewAt(i)->extension()) |
| 370 return false; |
| 371 } |
| 372 return true; |
| 373 } |
| 374 |
| 375 testing::AssertionResult |
| 376 BrowserActionsContainerOverflowTest::VerifyVisibleCount( |
| 377 size_t expected_visible) { |
| 378 // Views order should always match (as it is based directly off the model). |
| 379 if (!ViewOrdersMatch()) |
| 380 return testing::AssertionFailure() << "View orders don't match"; |
| 381 |
| 382 // Loop through and check each browser action for proper visibility (which |
| 383 // implicitly also guarantees that the proper number are visible). |
| 384 for (size_t i = 0; i < overflow_bar_->num_browser_actions(); ++i) { |
| 385 bool visible = i < expected_visible; |
| 386 if (main_bar_->GetBrowserActionViewAt(i)->visible() != visible) { |
| 387 return testing::AssertionFailure() << "Index " << i << |
| 388 " has improper visibility in main: " << !visible; |
| 389 } |
| 390 if (overflow_bar_->GetBrowserActionViewAt(i)->visible() == visible) { |
| 391 return testing::AssertionFailure() << "Index " << i << |
| 392 " has improper visibility in overflow: " << visible; |
| 393 } |
| 394 } |
| 395 return testing::AssertionSuccess(); |
| 396 } |
| 397 |
| 398 // Test the basic functionality of the BrowserActionsContainer in overflow mode. |
| 399 IN_PROC_BROWSER_TEST_F(BrowserActionsContainerOverflowTest, |
| 400 TestBasicActionOverflow) { |
| 401 // Load three extensions with browser actions. |
| 402 // TODO(devlin): Make a method to load these, and generate them rather than |
| 403 // using files. |
| 404 base::FilePath test_data_path = |
| 405 test_data_dir_.AppendASCII("api_test").AppendASCII("browser_action"); |
| 406 const extensions::Extension* extension_a = |
| 407 LoadExtension(test_data_path.AppendASCII("basics")); |
| 408 const extensions::Extension* extension_b = |
| 409 LoadExtension(test_data_path.AppendASCII("add_popup")); |
| 410 const extensions::Extension* extension_c = |
| 411 LoadExtension(test_data_path.AppendASCII("remove_popup")); |
| 412 |
| 413 // Since the overflow bar isn't attached to a view, we have to kick it in |
| 414 // order to retrigger layout each time we change the number of icons in the |
| 415 // bar. |
| 416 overflow_bar()->Layout(); |
| 417 |
| 418 // Sanity checks: |
| 419 // All extensions loaded. |
| 420 ASSERT_TRUE(extension_a); |
| 421 ASSERT_TRUE(extension_b); |
| 422 ASSERT_TRUE(extension_c); |
| 423 |
| 424 // All actions are showing, and are in the installation order. |
| 425 EXPECT_EQ(-1, model()->GetVisibleIconCount()); |
| 426 ASSERT_EQ(3u, main_bar()->num_browser_actions()); |
| 427 EXPECT_EQ(extension_a, main_bar()->GetBrowserActionViewAt(0)->extension()); |
| 428 EXPECT_EQ(extension_b, main_bar()->GetBrowserActionViewAt(1)->extension()); |
| 429 EXPECT_EQ(extension_c, main_bar()->GetBrowserActionViewAt(2)->extension()); |
| 430 EXPECT_TRUE(VerifyVisibleCount(3u)); |
| 431 |
| 432 // Reduce the visible count to 2. Order should be unchanged (A B C), but |
| 433 // only A and B should be visible on the main bar. |
| 434 model()->SetVisibleIconCountForTest(2u); |
| 435 overflow_bar()->Layout(); // Kick. |
| 436 EXPECT_EQ(extension_a, main_bar()->GetBrowserActionViewAt(0)->extension()); |
| 437 EXPECT_EQ(extension_b, main_bar()->GetBrowserActionViewAt(1)->extension()); |
| 438 EXPECT_EQ(extension_c, main_bar()->GetBrowserActionViewAt(2)->extension()); |
| 439 EXPECT_TRUE(VerifyVisibleCount(2u)); |
| 440 |
| 441 // Move extension C to the first position. Order should now be C A B, with |
| 442 // C and A visible in the main bar. |
| 443 model()->MoveExtensionIcon(extension_c, 0); |
| 444 overflow_bar()->Layout(); // Kick. |
| 445 EXPECT_EQ(extension_c, main_bar()->GetBrowserActionViewAt(0)->extension()); |
| 446 EXPECT_EQ(extension_a, main_bar()->GetBrowserActionViewAt(1)->extension()); |
| 447 EXPECT_EQ(extension_b, main_bar()->GetBrowserActionViewAt(2)->extension()); |
| 448 EXPECT_TRUE(VerifyVisibleCount(2u)); |
| 449 |
| 450 // Hide action A. This results in it being sent to overflow, and reducing the |
| 451 // visible size to 1, so the order should be C A B, with only C visible in the |
| 452 // main bar. |
| 453 extensions::ExtensionActionAPI::SetBrowserActionVisibility( |
| 454 extensions::ExtensionPrefs::Get(profile()), |
| 455 extension_a->id(), |
| 456 false); |
| 457 overflow_bar()->Layout(); // Kick. |
| 458 EXPECT_EQ(extension_c, main_bar()->GetBrowserActionViewAt(0)->extension()); |
| 459 EXPECT_EQ(extension_a, main_bar()->GetBrowserActionViewAt(1)->extension()); |
| 460 EXPECT_EQ(extension_b, main_bar()->GetBrowserActionViewAt(2)->extension()); |
| 461 EXPECT_TRUE(VerifyVisibleCount(1u)); |
| 462 } |
OLD | NEW |