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; |
24 using extensions::ExtensionToolbarModel; | |
25 using testing::AssertionFailure; | |
26 using testing::AssertionSuccess; | |
23 | 27 |
24 class BrowserActionsContainerTest : public ExtensionBrowserTest { | 28 class BrowserActionsContainerTest : public ExtensionBrowserTest { |
25 public: | 29 public: |
26 BrowserActionsContainerTest() { | 30 BrowserActionsContainerTest() { |
27 } | 31 } |
28 virtual ~BrowserActionsContainerTest() {} | 32 virtual ~BrowserActionsContainerTest() {} |
29 | 33 |
30 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { | 34 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { |
31 BrowserActionsContainer::disable_animations_during_testing_ = true; | 35 BrowserActionsContainer::disable_animations_during_testing_ = true; |
32 ExtensionBrowserTest::SetUpCommandLine(command_line); | 36 ExtensionBrowserTest::SetUpCommandLine(command_line); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 action_view = container->GetBrowserActionViewAt(0); | 287 action_view = container->GetBrowserActionViewAt(0); |
284 EXPECT_FALSE(container->CanStartDragForView(action_view, point, point)); | 288 EXPECT_FALSE(container->CanStartDragForView(action_view, point, point)); |
285 | 289 |
286 // We should go back to normal after leaving highlight mode. | 290 // We should go back to normal after leaving highlight mode. |
287 model->StopHighlighting(); | 291 model->StopHighlighting(); |
288 EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions()); | 292 EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions()); |
289 EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions()); | 293 EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions()); |
290 action_view = container->GetBrowserActionViewAt(0); | 294 action_view = container->GetBrowserActionViewAt(0); |
291 EXPECT_TRUE(container->CanStartDragForView(action_view, point, point)); | 295 EXPECT_TRUE(container->CanStartDragForView(action_view, point, point)); |
292 } | 296 } |
297 | |
298 // Test the behavior of the overflow container for Extension Actions. | |
299 class BrowserActionsContainerOverflowTest : public BrowserActionsContainerTest { | |
300 public: | |
301 BrowserActionsContainerOverflowTest() : main_bar_(NULL), model_(NULL) { | |
302 } | |
303 virtual ~BrowserActionsContainerOverflowTest() { | |
304 } | |
305 | |
306 protected: | |
307 // Returns true if the order of the BrowserActionViews in |main_bar_| | |
308 // and |overflow_bar_| match. | |
309 bool ViewOrdersMatch(); | |
310 | |
311 // Returns Success if the visible count matches |expected_visible|. This means | |
312 // that the number of visible browser actions in |main_bar_| is | |
313 // |expected_visible| and shows the first icons, and that the overflow bar | |
314 // shows all (and only) the remainder. | |
315 testing::AssertionResult VerifyVisibleCount(size_t expected_visible); | |
316 | |
317 // Accessors. | |
318 BrowserActionsContainer* main_bar() { return main_bar_; } | |
319 BrowserActionsContainer* overflow_bar() { return overflow_bar_.get(); } | |
320 ExtensionToolbarModel* model() { return model_; } | |
321 | |
322 private: | |
323 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE; | |
324 virtual void SetUpOnMainThread() OVERRIDE; | |
325 virtual void TearDownOnMainThread() OVERRIDE; | |
326 | |
327 // The main BrowserActionsContainer (owned by the browser view). | |
328 BrowserActionsContainer* main_bar_; | |
329 | |
330 // The overflow BrowserActionsContainer. We manufacture this so that we don't | |
331 // have to open the wrench menu. | |
332 scoped_ptr<BrowserActionsContainer> overflow_bar_; | |
333 | |
334 // The associated toolbar model. | |
335 ExtensionToolbarModel* model_; | |
336 | |
337 // Enable the feature redesign switch. | |
338 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_; | |
339 }; | |
340 | |
341 void BrowserActionsContainerOverflowTest::SetUpCommandLine( | |
342 base::CommandLine* command_line) { | |
343 BrowserActionsContainerTest::SetUpCommandLine(command_line); | |
344 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride( | |
345 extensions::FeatureSwitch::extension_action_redesign(), | |
346 true)); | |
347 } | |
348 | |
349 void BrowserActionsContainerOverflowTest::SetUpOnMainThread() { | |
350 BrowserActionsContainerTest::SetUpOnMainThread(); | |
351 main_bar_ = BrowserView::GetBrowserViewForBrowser(browser()) | |
352 ->toolbar()->browser_actions(); | |
353 overflow_bar_.reset(new BrowserActionsContainer(browser(), NULL, main_bar_)); | |
354 overflow_bar_->set_owned_by_client(); | |
355 model_ = ExtensionToolbarModel::Get(profile()); | |
356 } | |
357 | |
358 void BrowserActionsContainerOverflowTest::TearDownOnMainThread() { | |
359 overflow_bar_.reset(); | |
360 enable_redesign_.reset(); | |
361 BrowserActionsContainerTest::TearDownOnMainThread(); | |
362 } | |
363 | |
364 bool BrowserActionsContainerOverflowTest::ViewOrdersMatch() { | |
365 if (main_bar_->num_browser_actions() != | |
366 overflow_bar_->num_browser_actions()) | |
367 return false; | |
368 for (size_t i = 0; i < main_bar_->num_browser_actions(); ++i) { | |
369 if (main_bar_->GetBrowserActionViewAt(i)->extension() != | |
370 overflow_bar_->GetBrowserActionViewAt(i)->extension()) | |
371 return false; | |
372 } | |
373 return true; | |
374 } | |
375 | |
376 testing::AssertionResult | |
377 BrowserActionsContainerOverflowTest::VerifyVisibleCount( | |
378 size_t expected_visible) { | |
379 // Views order should always match (as it is based directly off the model). | |
380 if (!ViewOrdersMatch()) | |
381 return AssertionFailure() << "View orders don't match"; | |
382 | |
383 // Loop through and check each browser action for proper visibility (which | |
384 // implicitly also guarantees that the proper number are visible). | |
385 for (size_t i = 0; i < overflow_bar_->num_browser_actions(); ++i) { | |
386 bool visible = i < expected_visible; | |
387 if (main_bar_->GetBrowserActionViewAt(i)->visible() != visible) { | |
388 return AssertionFailure() << "Index " << i << | |
389 " has improper visibility in main: " << visible; | |
Finnur
2014/08/22 16:54:58
Don't you want main_bar_->GetBrowserActionViewAt(i
Devlin
2014/08/22 17:00:15
Yeah, probably. :) Done. (For ease, just made th
| |
390 } | |
391 if (overflow_bar_->GetBrowserActionViewAt(i)->visible() == visible) { | |
392 return AssertionFailure() << "Index " << i << | |
393 " has improper visibility in overflow: " << visible; | |
394 } | |
395 } | |
396 return AssertionSuccess(); | |
397 } | |
398 | |
399 // Test the basic functionality of the BrowserActionsContainer in overflow mode. | |
400 IN_PROC_BROWSER_TEST_F(BrowserActionsContainerOverflowTest, | |
401 TestBasicActionOverflow) { | |
402 // Load three extensions with browser actions. | |
403 // TODO(devlin): Make a method to load these, and generate them rather than | |
404 // using files. | |
Finnur
2014/08/22 16:54:58
Yup, I like that idea.
Devlin
2014/08/22 17:00:15
Mmkay, will do in a follow up :)
| |
405 const Extension* extension_a = | |
406 LoadExtension(test_data_dir_.AppendASCII("api_test") | |
407 .AppendASCII("browser_action") | |
408 .AppendASCII("basics")); | |
409 const Extension* extension_b = | |
410 LoadExtension(test_data_dir_.AppendASCII("api_test") | |
411 .AppendASCII("browser_action") | |
412 .AppendASCII("add_popup")); | |
413 const Extension* extension_c = | |
414 LoadExtension(test_data_dir_.AppendASCII("api_test") | |
415 .AppendASCII("browser_action") | |
416 .AppendASCII("remove_popup")); | |
417 | |
418 // Since the overflow bar isn't attached to a view, we kinda have to kick it | |
419 // from time to time. | |
420 overflow_bar()->Layout(); | |
421 | |
422 // Sanity checks: | |
423 // All extensions loaded. | |
424 ASSERT_TRUE(extension_a); | |
425 ASSERT_TRUE(extension_b); | |
426 ASSERT_TRUE(extension_c); | |
427 | |
428 // All actions are showing, and are in the installation order. | |
429 EXPECT_EQ(-1, model()->GetVisibleIconCount()); | |
430 ASSERT_EQ(3u, main_bar()->num_browser_actions()); | |
431 EXPECT_EQ(extension_a, main_bar()->GetBrowserActionViewAt(0)->extension()); | |
432 EXPECT_EQ(extension_b, main_bar()->GetBrowserActionViewAt(1)->extension()); | |
433 EXPECT_EQ(extension_c, main_bar()->GetBrowserActionViewAt(2)->extension()); | |
434 EXPECT_TRUE(VerifyVisibleCount(3u)); | |
435 | |
436 // Views should always match. | |
437 EXPECT_TRUE(ViewOrdersMatch()); | |
438 | |
439 // Reduce the visible count to 2. Order should be unchanged (A B C), but | |
440 // only A and B should be visible on the main bar. | |
441 model()->SetVisibleIconCountForTest(2u); | |
442 overflow_bar()->Layout(); // Kick. | |
443 EXPECT_EQ(extension_a, main_bar()->GetBrowserActionViewAt(0)->extension()); | |
444 EXPECT_EQ(extension_b, main_bar()->GetBrowserActionViewAt(1)->extension()); | |
445 EXPECT_EQ(extension_c, main_bar()->GetBrowserActionViewAt(2)->extension()); | |
446 EXPECT_TRUE(ViewOrdersMatch()); | |
Finnur
2014/08/22 16:54:58
This call is redundant, it is implied by calling V
Devlin
2014/08/22 17:00:15
Yeah... I went back and forth on this. My basic t
| |
447 EXPECT_TRUE(VerifyVisibleCount(2u)); | |
448 | |
449 // Move extension C to the first position. Order should now be C A B, with | |
450 // C and A visible in the main bar. | |
451 model()->MoveExtensionIcon(extension_c, 0); | |
452 overflow_bar()->Layout(); // Kick. | |
453 EXPECT_EQ(extension_c, main_bar()->GetBrowserActionViewAt(0)->extension()); | |
454 EXPECT_EQ(extension_a, main_bar()->GetBrowserActionViewAt(1)->extension()); | |
455 EXPECT_EQ(extension_b, main_bar()->GetBrowserActionViewAt(2)->extension()); | |
456 EXPECT_TRUE(ViewOrdersMatch()); | |
457 EXPECT_TRUE(VerifyVisibleCount(2u)); | |
458 | |
459 // Hide action A. This results in it being sent to overflow, and reducing the | |
460 // visible size to 1, so the order should be C A B, with only C visible in the | |
461 // main bar. | |
462 extensions::ExtensionActionAPI::SetBrowserActionVisibility( | |
463 extensions::ExtensionPrefs::Get(profile()), | |
464 extension_a->id(), | |
465 false); | |
466 overflow_bar()->Layout(); // Kick. | |
467 EXPECT_EQ(extension_c, main_bar()->GetBrowserActionViewAt(0)->extension()); | |
468 EXPECT_EQ(extension_a, main_bar()->GetBrowserActionViewAt(1)->extension()); | |
469 EXPECT_EQ(extension_b, main_bar()->GetBrowserActionViewAt(2)->extension()); | |
470 EXPECT_TRUE(ViewOrdersMatch()); | |
471 EXPECT_TRUE(VerifyVisibleCount(1u)); | |
472 } | |
OLD | NEW |