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/auto_reset.h" | |
6 #include "base/command_line.h" | |
7 #include "base/macros.h" | 5 #include "base/macros.h" |
8 #include "chrome/browser/extensions/browser_action_test_util.h" | 6 #include "chrome/browser/extensions/browser_action_test_util.h" |
9 #include "chrome/browser/extensions/extension_action_test_util.h" | 7 #include "chrome/browser/extensions/extension_action_test_util.h" |
10 #include "chrome/browser/extensions/test_extension_system.h" | |
11 #include "chrome/browser/ui/browser_commands.h" | 8 #include "chrome/browser/ui/browser_commands.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | |
14 #include "chrome/browser/ui/toolbar/media_router_action.h" | 10 #include "chrome/browser/ui/toolbar/media_router_action.h" |
15 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | 11 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
16 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im
pl.h" | 12 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im
pl.h" |
17 #include "chrome/browser/ui/webui/media_router/media_router_test.h" | 13 #include "chrome/browser/ui/webui/media_router/media_router_test.h" |
18 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
19 #include "content/public/browser/site_instance.h" | 15 #include "content/public/browser/site_instance.h" |
20 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/gfx/color_palette.h" | 19 #include "ui/gfx/color_palette.h" |
(...skipping 21 matching lines...) Expand all Loading... |
45 ToolbarActionsBar* toolbar_actions_bar) | 41 ToolbarActionsBar* toolbar_actions_bar) |
46 : MediaRouterAction(browser, toolbar_actions_bar), | 42 : MediaRouterAction(browser, toolbar_actions_bar), |
47 controller_(nullptr), | 43 controller_(nullptr), |
48 platform_delegate_(nullptr) {} | 44 platform_delegate_(nullptr) {} |
49 ~TestMediaRouterAction() override {} | 45 ~TestMediaRouterAction() override {} |
50 | 46 |
51 void SetMediaRouterDialogController( | 47 void SetMediaRouterDialogController( |
52 MediaRouterDialogControllerImpl* controller) { | 48 MediaRouterDialogControllerImpl* controller) { |
53 DCHECK(controller); | 49 DCHECK(controller); |
54 controller_ = controller; | 50 controller_ = controller; |
55 controller_->SetMediaRouterAction(GetWeakPtr()); | |
56 } | 51 } |
57 | 52 |
58 private: | 53 private: |
59 MediaRouterDialogControllerImpl* GetMediaRouterDialogController() | 54 MediaRouterDialogControllerImpl* GetMediaRouterDialogController() |
60 override { | 55 override { |
61 return controller_; | 56 return controller_; |
62 } | 57 } |
63 | 58 |
64 MediaRouterActionPlatformDelegate* GetPlatformDelegate() override { | 59 MediaRouterActionPlatformDelegate* GetPlatformDelegate() override { |
65 return platform_delegate_; | 60 return platform_delegate_; |
66 } | 61 } |
67 | 62 |
68 void MaybeRemoveAction() override { | |
69 if (GetMediaRouterDialogController()) | |
70 MediaRouterAction::MaybeRemoveAction(); | |
71 } | |
72 | |
73 MediaRouterDialogControllerImpl* controller_; | 63 MediaRouterDialogControllerImpl* controller_; |
74 MediaRouterActionPlatformDelegate* platform_delegate_; | 64 MediaRouterActionPlatformDelegate* platform_delegate_; |
75 }; | 65 }; |
76 | 66 |
77 class MediaRouterActionUnitTest : public MediaRouterTest { | 67 class MediaRouterActionUnitTest : public MediaRouterTest { |
78 public: | 68 public: |
79 MediaRouterActionUnitTest() | 69 MediaRouterActionUnitTest() |
80 : toolbar_model_(nullptr), | 70 : toolbar_model_(nullptr), |
81 fake_issue_notification_(media_router::Issue( | 71 fake_issue_notification_(media_router::Issue( |
82 "title notification", | 72 "title notification", |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 gfx::kPlaceholderColor)), | 107 gfx::kPlaceholderColor)), |
118 warning_icon_( | 108 warning_icon_( |
119 gfx::CreateVectorIcon(gfx::VectorIconId::MEDIA_ROUTER_WARNING, | 109 gfx::CreateVectorIcon(gfx::VectorIconId::MEDIA_ROUTER_WARNING, |
120 gfx::kPlaceholderColor)) {} | 110 gfx::kPlaceholderColor)) {} |
121 | 111 |
122 ~MediaRouterActionUnitTest() override {} | 112 ~MediaRouterActionUnitTest() override {} |
123 | 113 |
124 // MediaRouterTest: | 114 // MediaRouterTest: |
125 void SetUp() override { | 115 void SetUp() override { |
126 MediaRouterTest::SetUp(); | 116 MediaRouterTest::SetUp(); |
127 static_cast<extensions::TestExtensionSystem*>( | |
128 extensions::ExtensionSystem::Get(profile())) | |
129 ->CreateExtensionService(base::CommandLine::ForCurrentProcess(), | |
130 base::FilePath(), false); | |
131 toolbar_model_ = extensions::extension_action_test_util:: | 117 toolbar_model_ = extensions::extension_action_test_util:: |
132 CreateToolbarModelForProfile(profile()); | 118 CreateToolbarModelForProfileWithoutWaitingForReady(profile()); |
133 | 119 |
134 // browser() will only be valid once BrowserWithTestWindowTest::SetUp() | 120 // browser() will only be valid once BrowserWithTestWindowTest::SetUp() |
135 // has run. | 121 // has run. |
136 browser_action_test_util_.reset( | 122 browser_action_test_util_.reset( |
137 new BrowserActionTestUtil(browser(), false)); | 123 new BrowserActionTestUtil(browser(), false)); |
138 action_.reset( | 124 action_.reset( |
139 new TestMediaRouterAction( | 125 new TestMediaRouterAction( |
140 browser(), | 126 browser(), |
141 browser_action_test_util_->GetToolbarActionsBar())); | 127 browser_action_test_util_->GetToolbarActionsBar())); |
142 delegate_.reset(new MockToolbarActionViewDelegate()); | |
143 | |
144 action()->SetDelegate(delegate_.get()); | |
145 | 128 |
146 local_display_route_list_.push_back( | 129 local_display_route_list_.push_back( |
147 media_router::MediaRoute("routeId1", fake_source1_, "sinkId1", | 130 media_router::MediaRoute("routeId1", fake_source1_, "sinkId1", |
148 "description", true, std::string(), true)); | 131 "description", true, std::string(), true)); |
149 non_local_display_route_list_.push_back( | 132 non_local_display_route_list_.push_back( |
150 media_router::MediaRoute("routeId2", fake_source1_, "sinkId2", | 133 media_router::MediaRoute("routeId2", fake_source1_, "sinkId2", |
151 "description", false, std::string(), true)); | 134 "description", false, std::string(), true)); |
152 non_local_display_route_list_.push_back( | 135 non_local_display_route_list_.push_back( |
153 media_router::MediaRoute("routeId3", fake_source2_, "sinkId3", | 136 media_router::MediaRoute("routeId3", fake_source2_, "sinkId3", |
154 "description", true, std::string(), false)); | 137 "description", true, std::string(), false)); |
155 } | 138 } |
156 | 139 |
157 void TearDown() override { | 140 void TearDown() override { |
158 delegate_.reset(); | 141 browser_action_test_util_.reset(); |
159 action_.reset(); | 142 action_.reset(); |
160 browser_action_test_util_.reset(); | |
161 MediaRouterTest::TearDown(); | 143 MediaRouterTest::TearDown(); |
162 } | 144 } |
163 | 145 |
164 bool ActionExists() { | |
165 return toolbar_model_->HasComponentAction( | |
166 ComponentToolbarActionsFactory::kMediaRouterActionId); | |
167 } | |
168 | |
169 void ResetTestMediaRouterAction() { | |
170 action_.reset(); | |
171 } | |
172 | |
173 TestMediaRouterAction* action() { return action_.get(); } | 146 TestMediaRouterAction* action() { return action_.get(); } |
174 ToolbarActionsModel* toolbar_model() { return toolbar_model_; } | |
175 const media_router::Issue* fake_issue_notification() { | 147 const media_router::Issue* fake_issue_notification() { |
176 return &fake_issue_notification_; | 148 return &fake_issue_notification_; |
177 } | 149 } |
178 const media_router::Issue* fake_issue_warning() { | 150 const media_router::Issue* fake_issue_warning() { |
179 return &fake_issue_warning_; | 151 return &fake_issue_warning_; |
180 } | 152 } |
181 const media_router::Issue* fake_issue_fatal() { | 153 const media_router::Issue* fake_issue_fatal() { |
182 return &fake_issue_fatal_; | 154 return &fake_issue_fatal_; |
183 } | 155 } |
184 const gfx::Image active_icon() { return active_icon_; } | 156 const gfx::Image active_icon() { return active_icon_; } |
(...skipping 11 matching lines...) Expand all Loading... |
196 const std::vector<media_router::MediaRoute::Id>& empty_route_id_list() const { | 168 const std::vector<media_router::MediaRoute::Id>& empty_route_id_list() const { |
197 return empty_route_id_list_; | 169 return empty_route_id_list_; |
198 } | 170 } |
199 | 171 |
200 private: | 172 private: |
201 // A BrowserActionTestUtil object constructed with the associated | 173 // A BrowserActionTestUtil object constructed with the associated |
202 // ToolbarActionsBar. | 174 // ToolbarActionsBar. |
203 std::unique_ptr<BrowserActionTestUtil> browser_action_test_util_; | 175 std::unique_ptr<BrowserActionTestUtil> browser_action_test_util_; |
204 | 176 |
205 std::unique_ptr<TestMediaRouterAction> action_; | 177 std::unique_ptr<TestMediaRouterAction> action_; |
206 std::unique_ptr<MockToolbarActionViewDelegate> delegate_; | |
207 | 178 |
208 // The associated ToolbarActionsModel (owned by the keyed service setup). | 179 // The associated ToolbarActionsModel (owned by the keyed service setup). |
209 ToolbarActionsModel* toolbar_model_; | 180 ToolbarActionsModel* toolbar_model_; |
210 | 181 |
211 // Fake Issues. | 182 // Fake Issues. |
212 const media_router::Issue fake_issue_notification_; | 183 const media_router::Issue fake_issue_notification_; |
213 const media_router::Issue fake_issue_warning_; | 184 const media_router::Issue fake_issue_warning_; |
214 const media_router::Issue fake_issue_fatal_; | 185 const media_router::Issue fake_issue_fatal_; |
215 | 186 |
216 // Fake Sources, used for the Routes. | 187 // Fake Sources, used for the Routes. |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 active_icon(), action()->GetIcon(nullptr, gfx::Size()))); | 313 active_icon(), action()->GetIcon(nullptr, gfx::Size()))); |
343 | 314 |
344 // Update |current_icon_| when the local route is closed. | 315 // Update |current_icon_| when the local route is closed. |
345 action()->OnRoutesUpdated(non_local_display_route_list(), | 316 action()->OnRoutesUpdated(non_local_display_route_list(), |
346 empty_route_id_list()); | 317 empty_route_id_list()); |
347 EXPECT_TRUE(gfx::test::AreImagesEqual( | 318 EXPECT_TRUE(gfx::test::AreImagesEqual( |
348 idle_icon(), action()->GetIcon(nullptr, gfx::Size()))); | 319 idle_icon(), action()->GetIcon(nullptr, gfx::Size()))); |
349 } | 320 } |
350 | 321 |
351 TEST_F(MediaRouterActionUnitTest, IconPressedState) { | 322 TEST_F(MediaRouterActionUnitTest, IconPressedState) { |
352 base::AutoReset<bool> disable_animations( | |
353 &ToolbarActionsBar::disable_animations_for_testing_, true); | |
354 | |
355 // Start with one window with one tab. | 323 // Start with one window with one tab. |
356 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | 324 EXPECT_EQ(0, browser()->tab_strip_model()->count()); |
357 chrome::NewTab(browser()); | 325 chrome::NewTab(browser()); |
358 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 326 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
359 | 327 |
360 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); | 328 // Create a reference to initiator contents. |
361 MediaRouterDialogControllerImpl::CreateForWebContents(initiator); | 329 WebContents* initiator_ = |
362 MediaRouterDialogControllerImpl* dialog_controller = | 330 browser()->tab_strip_model()->GetActiveWebContents(); |
363 MediaRouterDialogControllerImpl::FromWebContents(initiator); | 331 |
364 ASSERT_TRUE(dialog_controller); | 332 MediaRouterDialogControllerImpl::CreateForWebContents(initiator_); |
| 333 MediaRouterDialogControllerImpl* dialog_controller_ = |
| 334 MediaRouterDialogControllerImpl::FromWebContents(initiator_); |
| 335 ASSERT_TRUE(dialog_controller_); |
365 | 336 |
366 // Sets the controller to use for TestMediaRouterAction. | 337 // Sets the controller to use for TestMediaRouterAction. |
367 action()->SetMediaRouterDialogController(dialog_controller); | 338 action()->SetMediaRouterDialogController(dialog_controller_); |
368 | 339 |
369 // Add the icon to the toolbar and make it persist. | 340 // Create a ToolbarActionViewDelegate to use for MediaRouterAction. |
370 toolbar_model()->AddComponentAction( | 341 std::unique_ptr<MockToolbarActionViewDelegate> mock_delegate( |
371 ComponentToolbarActionsFactory::kMediaRouterActionId); | 342 new MockToolbarActionViewDelegate()); |
372 action()->ToggleVisibilityPreference(); | |
373 EXPECT_TRUE(ActionExists()); | |
374 | 343 |
| 344 EXPECT_CALL(*mock_delegate, GetCurrentWebContents()).WillOnce( |
| 345 testing::Return(initiator_)); |
| 346 EXPECT_CALL(*mock_delegate, OnPopupClosed()).Times(1); |
| 347 action()->SetDelegate(mock_delegate.get()); |
| 348 |
| 349 EXPECT_CALL(*mock_delegate, OnPopupShown(true)).Times(1); |
375 action()->ExecuteAction(true); | 350 action()->ExecuteAction(true); |
376 EXPECT_TRUE(dialog_controller->IsShowingMediaRouterDialog()); | |
377 | 351 |
378 // Pressing the icon while the popup is shown should close the popup | 352 // Pressing the icon while the popup is shown should close the popup |
| 353 EXPECT_CALL(*mock_delegate, OnPopupClosed()).Times(1); |
379 action()->ExecuteAction(true); | 354 action()->ExecuteAction(true); |
380 EXPECT_FALSE(dialog_controller->IsShowingMediaRouterDialog()); | |
381 | 355 |
382 dialog_controller->CreateMediaRouterDialog(); | 356 EXPECT_CALL(*mock_delegate, OnPopupShown(true)).Times(1); |
383 EXPECT_TRUE(dialog_controller->IsShowingMediaRouterDialog()); | 357 dialog_controller_->CreateMediaRouterDialog(); |
384 | 358 |
385 dialog_controller->HideMediaRouterDialog(); | 359 EXPECT_CALL(*mock_delegate, OnPopupClosed()).Times(1); |
386 EXPECT_FALSE(dialog_controller->IsShowingMediaRouterDialog()); | 360 dialog_controller_->HideMediaRouterDialog(); |
387 | |
388 // Make the icon go away. | |
389 action()->ToggleVisibilityPreference(); | |
390 EXPECT_FALSE(ActionExists()); | |
391 } | 361 } |
392 | |
393 TEST_F(MediaRouterActionUnitTest, EphemeralIcon) { | |
394 // We'll be using the action created by the toolbar model in this test, | |
395 // so we remove the test action here. | |
396 ResetTestMediaRouterAction(); | |
397 | |
398 base::AutoReset<bool> disable_animations( | |
399 &ToolbarActionsBar::disable_animations_for_testing_, true); | |
400 | |
401 // Start with one window with one tab. | |
402 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | |
403 chrome::NewTab(browser()); | |
404 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
405 | |
406 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); | |
407 MediaRouterDialogControllerImpl::CreateForWebContents(initiator); | |
408 MediaRouterDialogControllerImpl* dialog_controller = | |
409 MediaRouterDialogControllerImpl::FromWebContents(initiator); | |
410 ASSERT_TRUE(dialog_controller); | |
411 | |
412 EXPECT_FALSE(ActionExists()); | |
413 // Show the popup. The icon should become visible. | |
414 dialog_controller->ShowMediaRouterDialog(); | |
415 EXPECT_TRUE(ActionExists()); | |
416 // Hide the popup. The icon should become hidden. | |
417 dialog_controller->HideMediaRouterDialog(); | |
418 EXPECT_FALSE(ActionExists()); | |
419 | |
420 // Show the popup. | |
421 dialog_controller->ShowMediaRouterDialog(); | |
422 // Add a local display route. | |
423 dialog_controller->action_for_test() | |
424 ->OnRoutesUpdated(local_display_route_list(), empty_route_id_list()); | |
425 EXPECT_TRUE(ActionExists()); | |
426 // Hide the popup while there's still a local media route. The icon should not | |
427 // be hidden as long as the media route exists. | |
428 dialog_controller->HideMediaRouterDialog(); | |
429 EXPECT_TRUE(ActionExists()); | |
430 // Remove the local route. Now the icon should be hidden. | |
431 dialog_controller->action_for_test()->OnRoutesUpdated( | |
432 std::vector<media_router::MediaRoute>(), | |
433 empty_route_id_list()); | |
434 EXPECT_FALSE(ActionExists()); | |
435 } | |
436 | |
437 TEST_F(MediaRouterActionUnitTest, ToggleIconVisibilityPreference) { | |
438 // We'll be using the action created by the toolbar model in this test, | |
439 // so we remove the test action here. | |
440 ResetTestMediaRouterAction(); | |
441 | |
442 base::AutoReset<bool> disable_animations( | |
443 &ToolbarActionsBar::disable_animations_for_testing_, true); | |
444 | |
445 // Start with one window with one tab. | |
446 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | |
447 chrome::NewTab(browser()); | |
448 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
449 | |
450 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); | |
451 MediaRouterDialogControllerImpl::CreateForWebContents(initiator); | |
452 MediaRouterDialogControllerImpl* dialog_controller = | |
453 MediaRouterDialogControllerImpl::FromWebContents(initiator); | |
454 ASSERT_TRUE(dialog_controller); | |
455 | |
456 EXPECT_FALSE(ActionExists()); | |
457 // Show the popup. The icon should become visible. | |
458 dialog_controller->ShowMediaRouterDialog(); | |
459 EXPECT_TRUE(ActionExists()); | |
460 // Turn on the settings to always show the icon. | |
461 dialog_controller->action_for_test()->ToggleVisibilityPreference(); | |
462 // Hide the popup. The icon should stay visible. | |
463 dialog_controller->HideMediaRouterDialog(); | |
464 EXPECT_TRUE(ActionExists()); | |
465 | |
466 // Close the tab and open another. | |
467 browser()->tab_strip_model()->CloseAllTabs(); | |
468 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | |
469 chrome::NewTab(browser()); | |
470 // The icon should persist. | |
471 EXPECT_TRUE(ActionExists()); | |
472 // Get the dialog controller for the new tab. | |
473 dialog_controller = MediaRouterDialogControllerImpl::FromWebContents( | |
474 browser()->tab_strip_model()->GetActiveWebContents()); | |
475 // Turn off the setting to always show the icon. | |
476 // The icon should get hidden now. | |
477 dialog_controller->action_for_test()->ToggleVisibilityPreference(); | |
478 EXPECT_FALSE(ActionExists()); | |
479 } | |
OLD | NEW |