| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/command_line.h" |
| 5 #include "base/macros.h" | 6 #include "base/macros.h" |
| 6 #include "chrome/browser/extensions/browser_action_test_util.h" | 7 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 7 #include "chrome/browser/extensions/extension_action_test_util.h" | 8 #include "chrome/browser/extensions/extension_action_test_util.h" |
| 9 #include "chrome/browser/extensions/test_extension_system.h" |
| 8 #include "chrome/browser/ui/browser_commands.h" | 10 #include "chrome/browser/ui/browser_commands.h" |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 10 #include "chrome/browser/ui/toolbar/media_router_action.h" | 13 #include "chrome/browser/ui/toolbar/media_router_action.h" |
| 11 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | 14 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
| 12 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im
pl.h" | 15 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im
pl.h" |
| 13 #include "chrome/browser/ui/webui/media_router/media_router_test.h" | 16 #include "chrome/browser/ui/webui/media_router/media_router_test.h" |
| 14 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
| 15 #include "content/public/browser/site_instance.h" | 18 #include "content/public/browser/site_instance.h" |
| 16 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/gfx/color_palette.h" | 22 #include "ui/gfx/color_palette.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 41 ToolbarActionsBar* toolbar_actions_bar) | 44 ToolbarActionsBar* toolbar_actions_bar) |
| 42 : MediaRouterAction(browser, toolbar_actions_bar), | 45 : MediaRouterAction(browser, toolbar_actions_bar), |
| 43 controller_(nullptr), | 46 controller_(nullptr), |
| 44 platform_delegate_(nullptr) {} | 47 platform_delegate_(nullptr) {} |
| 45 ~TestMediaRouterAction() override {} | 48 ~TestMediaRouterAction() override {} |
| 46 | 49 |
| 47 void SetMediaRouterDialogController( | 50 void SetMediaRouterDialogController( |
| 48 MediaRouterDialogControllerImpl* controller) { | 51 MediaRouterDialogControllerImpl* controller) { |
| 49 DCHECK(controller); | 52 DCHECK(controller); |
| 50 controller_ = controller; | 53 controller_ = controller; |
| 54 controller_->SetMediaRouterAction(GetWeakPtr()); |
| 51 } | 55 } |
| 52 | 56 |
| 53 private: | 57 private: |
| 54 MediaRouterDialogControllerImpl* GetMediaRouterDialogController() | 58 MediaRouterDialogControllerImpl* GetMediaRouterDialogController() |
| 55 override { | 59 override { |
| 56 return controller_; | 60 return controller_; |
| 57 } | 61 } |
| 58 | 62 |
| 59 MediaRouterActionPlatformDelegate* GetPlatformDelegate() override { | 63 MediaRouterActionPlatformDelegate* GetPlatformDelegate() override { |
| 60 return platform_delegate_; | 64 return platform_delegate_; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 gfx::kPlaceholderColor)), | 111 gfx::kPlaceholderColor)), |
| 108 warning_icon_( | 112 warning_icon_( |
| 109 gfx::CreateVectorIcon(gfx::VectorIconId::MEDIA_ROUTER_WARNING, | 113 gfx::CreateVectorIcon(gfx::VectorIconId::MEDIA_ROUTER_WARNING, |
| 110 gfx::kPlaceholderColor)) {} | 114 gfx::kPlaceholderColor)) {} |
| 111 | 115 |
| 112 ~MediaRouterActionUnitTest() override {} | 116 ~MediaRouterActionUnitTest() override {} |
| 113 | 117 |
| 114 // MediaRouterTest: | 118 // MediaRouterTest: |
| 115 void SetUp() override { | 119 void SetUp() override { |
| 116 MediaRouterTest::SetUp(); | 120 MediaRouterTest::SetUp(); |
| 121 static_cast<extensions::TestExtensionSystem*>( |
| 122 extensions::ExtensionSystem::Get(profile())) |
| 123 ->CreateExtensionService(base::CommandLine::ForCurrentProcess(), |
| 124 base::FilePath(), false); |
| 117 toolbar_model_ = extensions::extension_action_test_util:: | 125 toolbar_model_ = extensions::extension_action_test_util:: |
| 118 CreateToolbarModelForProfileWithoutWaitingForReady(profile()); | 126 CreateToolbarModelForProfile(profile()); |
| 119 | 127 |
| 120 // browser() will only be valid once BrowserWithTestWindowTest::SetUp() | 128 // browser() will only be valid once BrowserWithTestWindowTest::SetUp() |
| 121 // has run. | 129 // has run. |
| 122 browser_action_test_util_.reset( | 130 browser_action_test_util_.reset( |
| 123 new BrowserActionTestUtil(browser(), false)); | 131 new BrowserActionTestUtil(browser(), false)); |
| 124 action_.reset( | 132 action_.reset( |
| 125 new TestMediaRouterAction( | 133 new TestMediaRouterAction( |
| 126 browser(), | 134 browser(), |
| 127 browser_action_test_util_->GetToolbarActionsBar())); | 135 browser_action_test_util_->GetToolbarActionsBar())); |
| 128 | 136 |
| 129 local_display_route_list_.push_back( | 137 local_display_route_list_.push_back( |
| 130 media_router::MediaRoute("routeId1", fake_source1_, "sinkId1", | 138 media_router::MediaRoute("routeId1", fake_source1_, "sinkId1", |
| 131 "description", true, std::string(), true)); | 139 "description", true, std::string(), true)); |
| 132 non_local_display_route_list_.push_back( | 140 non_local_display_route_list_.push_back( |
| 133 media_router::MediaRoute("routeId2", fake_source1_, "sinkId2", | 141 media_router::MediaRoute("routeId2", fake_source1_, "sinkId2", |
| 134 "description", false, std::string(), true)); | 142 "description", false, std::string(), true)); |
| 135 non_local_display_route_list_.push_back( | 143 non_local_display_route_list_.push_back( |
| 136 media_router::MediaRoute("routeId3", fake_source2_, "sinkId3", | 144 media_router::MediaRoute("routeId3", fake_source2_, "sinkId3", |
| 137 "description", true, std::string(), false)); | 145 "description", true, std::string(), false)); |
| 138 } | 146 } |
| 139 | 147 |
| 140 void TearDown() override { | 148 void TearDown() override { |
| 149 action_.reset(); |
| 141 browser_action_test_util_.reset(); | 150 browser_action_test_util_.reset(); |
| 142 action_.reset(); | |
| 143 MediaRouterTest::TearDown(); | 151 MediaRouterTest::TearDown(); |
| 144 } | 152 } |
| 145 | 153 |
| 146 TestMediaRouterAction* action() { return action_.get(); } | 154 TestMediaRouterAction* action() { return action_.get(); } |
| 155 bool ActionExists() { |
| 156 return toolbar_model_->HasComponentAction( |
| 157 ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 158 } |
| 147 const media_router::Issue* fake_issue_notification() { | 159 const media_router::Issue* fake_issue_notification() { |
| 148 return &fake_issue_notification_; | 160 return &fake_issue_notification_; |
| 149 } | 161 } |
| 150 const media_router::Issue* fake_issue_warning() { | 162 const media_router::Issue* fake_issue_warning() { |
| 151 return &fake_issue_warning_; | 163 return &fake_issue_warning_; |
| 152 } | 164 } |
| 153 const media_router::Issue* fake_issue_fatal() { | 165 const media_router::Issue* fake_issue_fatal() { |
| 154 return &fake_issue_fatal_; | 166 return &fake_issue_fatal_; |
| 155 } | 167 } |
| 156 const gfx::Image active_icon() { return active_icon_; } | 168 const gfx::Image active_icon() { return active_icon_; } |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 EXPECT_TRUE(gfx::test::AreImagesEqual( | 330 EXPECT_TRUE(gfx::test::AreImagesEqual( |
| 319 idle_icon(), action()->GetIcon(nullptr, gfx::Size()))); | 331 idle_icon(), action()->GetIcon(nullptr, gfx::Size()))); |
| 320 } | 332 } |
| 321 | 333 |
| 322 TEST_F(MediaRouterActionUnitTest, IconPressedState) { | 334 TEST_F(MediaRouterActionUnitTest, IconPressedState) { |
| 323 // Start with one window with one tab. | 335 // Start with one window with one tab. |
| 324 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | 336 EXPECT_EQ(0, browser()->tab_strip_model()->count()); |
| 325 chrome::NewTab(browser()); | 337 chrome::NewTab(browser()); |
| 326 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 338 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 327 | 339 |
| 328 // Create a reference to initiator contents. | 340 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); |
| 329 WebContents* initiator_ = | 341 MediaRouterDialogControllerImpl::CreateForWebContents(initiator); |
| 330 browser()->tab_strip_model()->GetActiveWebContents(); | 342 MediaRouterDialogControllerImpl* dialog_controller = |
| 331 | 343 MediaRouterDialogControllerImpl::FromWebContents(initiator); |
| 332 MediaRouterDialogControllerImpl::CreateForWebContents(initiator_); | 344 ASSERT_TRUE(dialog_controller); |
| 333 MediaRouterDialogControllerImpl* dialog_controller_ = | |
| 334 MediaRouterDialogControllerImpl::FromWebContents(initiator_); | |
| 335 ASSERT_TRUE(dialog_controller_); | |
| 336 | 345 |
| 337 // Sets the controller to use for TestMediaRouterAction. | 346 // Sets the controller to use for TestMediaRouterAction. |
| 338 action()->SetMediaRouterDialogController(dialog_controller_); | 347 action()->SetMediaRouterDialogController(dialog_controller); |
| 339 | 348 |
| 340 // Create a ToolbarActionViewDelegate to use for MediaRouterAction. | 349 // Create a ToolbarActionViewDelegate to use for MediaRouterAction. |
| 341 std::unique_ptr<MockToolbarActionViewDelegate> mock_delegate( | 350 std::unique_ptr<MockToolbarActionViewDelegate> mock_delegate( |
| 342 new MockToolbarActionViewDelegate()); | 351 new MockToolbarActionViewDelegate()); |
| 343 | 352 |
| 344 EXPECT_CALL(*mock_delegate, GetCurrentWebContents()).WillOnce( | 353 EXPECT_CALL(*mock_delegate, GetCurrentWebContents()).WillOnce( |
| 345 testing::Return(initiator_)); | 354 testing::Return(initiator)); |
| 346 EXPECT_CALL(*mock_delegate, OnPopupClosed()).Times(1); | |
| 347 action()->SetDelegate(mock_delegate.get()); | 355 action()->SetDelegate(mock_delegate.get()); |
| 348 | 356 |
| 349 EXPECT_CALL(*mock_delegate, OnPopupShown(true)).Times(1); | |
| 350 action()->ExecuteAction(true); | 357 action()->ExecuteAction(true); |
| 358 EXPECT_TRUE(dialog_controller->IsShowingMediaRouterDialog()); |
| 351 | 359 |
| 352 // Pressing the icon while the popup is shown should close the popup | 360 // Pressing the icon while the popup is shown should close the popup |
| 353 EXPECT_CALL(*mock_delegate, OnPopupClosed()).Times(1); | |
| 354 action()->ExecuteAction(true); | 361 action()->ExecuteAction(true); |
| 362 EXPECT_FALSE(dialog_controller->IsShowingMediaRouterDialog()); |
| 355 | 363 |
| 356 EXPECT_CALL(*mock_delegate, OnPopupShown(true)).Times(1); | 364 dialog_controller->CreateMediaRouterDialog(); |
| 357 dialog_controller_->CreateMediaRouterDialog(); | 365 EXPECT_TRUE(dialog_controller->IsShowingMediaRouterDialog()); |
| 358 | 366 |
| 359 EXPECT_CALL(*mock_delegate, OnPopupClosed()).Times(1); | 367 dialog_controller->HideMediaRouterDialog(); |
| 360 dialog_controller_->HideMediaRouterDialog(); | 368 EXPECT_FALSE(dialog_controller->IsShowingMediaRouterDialog()); |
| 361 } | 369 } |
| 370 |
| 371 TEST_F(MediaRouterActionUnitTest, EphemeralIcon) { |
| 372 base::AutoReset<bool> disable_animations( |
| 373 &ToolbarActionsBar::disable_animations_for_testing_, true); |
| 374 |
| 375 // Start with one window with one tab. |
| 376 EXPECT_EQ(0, browser()->tab_strip_model()->count()); |
| 377 chrome::NewTab(browser()); |
| 378 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 379 |
| 380 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); |
| 381 MediaRouterDialogControllerImpl::CreateForWebContents(initiator); |
| 382 MediaRouterDialogControllerImpl* dialog_controller = |
| 383 MediaRouterDialogControllerImpl::FromWebContents(initiator); |
| 384 ASSERT_TRUE(dialog_controller); |
| 385 |
| 386 EXPECT_FALSE(ActionExists()); |
| 387 // Show the popup. The icon should become visible. |
| 388 dialog_controller->ShowMediaRouterDialog(); |
| 389 EXPECT_TRUE(ActionExists()); |
| 390 // Hide the popup. The icon should become hidden. |
| 391 dialog_controller->HideMediaRouterDialog(); |
| 392 EXPECT_FALSE(ActionExists()); |
| 393 |
| 394 // Show the popup. |
| 395 dialog_controller->ShowMediaRouterDialog(); |
| 396 // Add a local display route. |
| 397 dialog_controller->action_for_test() |
| 398 ->OnRoutesUpdated(local_display_route_list(), empty_route_id_list()); |
| 399 EXPECT_TRUE(ActionExists()); |
| 400 // Hide the popup while there's still a local media route. The icon should not |
| 401 // be hidden as long as the media route exists. |
| 402 dialog_controller->HideMediaRouterDialog(); |
| 403 EXPECT_TRUE(ActionExists()); |
| 404 // Remove the local route. Now the icon should be hidden. |
| 405 dialog_controller->action_for_test()->OnRoutesUpdated( |
| 406 std::vector<media_router::MediaRoute>(), |
| 407 empty_route_id_list()); |
| 408 EXPECT_FALSE(ActionExists()); |
| 409 } |
| 410 |
| 411 TEST_F(MediaRouterActionUnitTest, ToggleIconVisibilityPreference) { |
| 412 base::AutoReset<bool> disable_animations( |
| 413 &ToolbarActionsBar::disable_animations_for_testing_, true); |
| 414 |
| 415 // Start with one window with one tab. |
| 416 EXPECT_EQ(0, browser()->tab_strip_model()->count()); |
| 417 chrome::NewTab(browser()); |
| 418 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 419 |
| 420 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); |
| 421 MediaRouterDialogControllerImpl::CreateForWebContents(initiator); |
| 422 MediaRouterDialogControllerImpl* dialog_controller = |
| 423 MediaRouterDialogControllerImpl::FromWebContents(initiator); |
| 424 ASSERT_TRUE(dialog_controller); |
| 425 |
| 426 EXPECT_FALSE(ActionExists()); |
| 427 // Show the popup. The icon should become visible. |
| 428 dialog_controller->ShowMediaRouterDialog(); |
| 429 EXPECT_TRUE(ActionExists()); |
| 430 // Turn on the settings to always show the icon. |
| 431 dialog_controller->action_for_test()->ToggleVisibilityPreference(); |
| 432 // Hide the popup. The icon should stay visible. |
| 433 dialog_controller->HideMediaRouterDialog(); |
| 434 EXPECT_TRUE(ActionExists()); |
| 435 |
| 436 // Close the tab and open another. |
| 437 browser()->tab_strip_model()->CloseAllTabs(); |
| 438 EXPECT_EQ(0, browser()->tab_strip_model()->count()); |
| 439 chrome::NewTab(browser()); |
| 440 // The icon should persist. |
| 441 EXPECT_TRUE(ActionExists()); |
| 442 // Turn off the settings to always show the icon. |
| 443 // The icon should get hidden now. |
| 444 MediaRouterDialogControllerImpl::FromWebContents( |
| 445 browser()->tab_strip_model()->GetActiveWebContents()) |
| 446 ->action_for_test()->ToggleVisibilityPreference(); |
| 447 EXPECT_FALSE(ActionExists()); |
| 448 } |
| OLD | NEW |