Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: chrome/browser/ui/webui/media_router/media_router_ui_unittest.cc

Issue 2108973002: [Media Router] Return NotFoundError on dialog exit if appropriate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address nits, add unit tests, add integration test Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/ui/webui/media_router/media_router_ui.h" 5 #include "chrome/browser/ui/webui/media_router/media_router_ui.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/media/router/create_presentation_connection_request.h"
10 #include "chrome/browser/media/router/media_route.h" 11 #include "chrome/browser/media/router/media_route.h"
11 #include "chrome/browser/media/router/media_source_helper.h" 12 #include "chrome/browser/media/router/media_source_helper.h"
12 #include "chrome/browser/media/router/mock_media_router.h" 13 #include "chrome/browser/media/router/mock_media_router.h"
13 #include "chrome/browser/media/router/route_request_result.h" 14 #include "chrome/browser/media/router/route_request_result.h"
14 #include "chrome/browser/media/router/test_helper.h" 15 #include "chrome/browser/media/router/test_helper.h"
15 #include "chrome/browser/sessions/session_tab_helper.h" 16 #include "chrome/browser/sessions/session_tab_helper.h"
16 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle r.h" 17 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle r.h"
17 #include "chrome/grit/generated_resources.h" 18 #include "chrome/grit/generated_resources.h"
18 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/test/test_browser_thread_bundle.h" 20 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "content/public/test/test_web_ui.h" 21 #include "content/public/test/test_web_ui.h"
21 #include "extensions/browser/extension_registry.h" 22 #include "extensions/browser/extension_registry.h"
22 #include "extensions/common/extension.h" 23 #include "extensions/common/extension.h"
23 #include "extensions/common/extension_builder.h" 24 #include "extensions/common/extension_builder.h"
24 #include "extensions/common/test_util.h" 25 #include "extensions/common/test_util.h"
25 #include "extensions/common/value_builder.h" 26 #include "extensions/common/value_builder.h"
26 #include "testing/gmock/include/gmock/gmock.h" 27 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/base/l10n/l10n_util.h" 29 #include "ui/base/l10n/l10n_util.h"
29 30
30 using testing::_; 31 using testing::_;
31 using testing::AnyNumber; 32 using testing::AnyNumber;
33 using testing::Invoke;
32 using testing::SaveArg; 34 using testing::SaveArg;
33 using testing::Return; 35 using testing::Return;
34 36
35 namespace media_router { 37 namespace media_router {
36 38
39 struct PresentationRequestCallbacks {
imcheng 2016/06/30 18:24:46 This should be a class since we have Success and E
btolsch 2016/06/30 21:22:26 Done.
40 explicit PresentationRequestCallbacks(
41 const content::PresentationError& expected_error)
42 : expected_error(expected_error) {}
43
44 void Success(const content::PresentationSessionInfo&, const MediaRoute::Id&) {
45 }
46
47 void Error(const content::PresentationError& error) {
48 EXPECT_EQ(expected_error.error_type, error.error_type);
49 EXPECT_EQ(expected_error.message, error.message);
50 }
51
52 const content::PresentationError& expected_error;
imcheng 2016/06/30 18:24:46 remove const ref, rename to expected_error_ and mo
btolsch 2016/06/30 21:22:26 Done.
53 };
54
37 class MockRoutesUpdatedCallback { 55 class MockRoutesUpdatedCallback {
38 public: 56 public:
39 MOCK_METHOD2(OnRoutesUpdated, 57 MOCK_METHOD2(OnRoutesUpdated,
40 void(const std::vector<MediaRoute>& routes, 58 void(const std::vector<MediaRoute>& routes,
41 const std::vector<MediaRoute::Id>& joinable_route_ids)); 59 const std::vector<MediaRoute::Id>& joinable_route_ids));
42 }; 60 };
43 61
44 class MediaRouterUITest : public ::testing::Test { 62 class MediaRouterUITest : public ::testing::Test {
45 public: 63 public:
46 ~MediaRouterUITest() override { 64 ~MediaRouterUITest() override {
47 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)) 65 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_))
48 .Times(AnyNumber()); 66 .Times(AnyNumber());
49 EXPECT_CALL(mock_router_, UnregisterMediaRoutesObserver(_)) 67 EXPECT_CALL(mock_router_, UnregisterMediaRoutesObserver(_))
50 .Times(AnyNumber()); 68 .Times(AnyNumber());
51 } 69 }
52 70
53 void CreateMediaRouterUI(Profile* profile) { 71 void CreateMediaRouterUI(Profile* profile) {
54 initiator_.reset(content::WebContents::Create( 72 initiator_.reset(content::WebContents::Create(
55 content::WebContents::CreateParams(profile))); 73 content::WebContents::CreateParams(profile)));
56 SessionTabHelper::CreateForWebContents(initiator_.get()); 74 SessionTabHelper::CreateForWebContents(initiator_.get());
57 web_contents_.reset(content::WebContents::Create( 75 web_contents_.reset(content::WebContents::Create(
58 content::WebContents::CreateParams(profile))); 76 content::WebContents::CreateParams(profile)));
59 web_ui_.set_web_contents(web_contents_.get()); 77 web_ui_.set_web_contents(web_contents_.get());
60 media_router_ui_.reset(new MediaRouterUI(&web_ui_)); 78 media_router_ui_.reset(new MediaRouterUI(&web_ui_));
61 message_handler_.reset( 79 message_handler_.reset(
62 new MediaRouterWebUIMessageHandler(media_router_ui_.get())); 80 new MediaRouterWebUIMessageHandler(media_router_ui_.get()));
63 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) 81 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_))
64 .WillRepeatedly(Return(true)); 82 .WillRepeatedly(Invoke([this](MediaSinksObserver* observer) {
83 this->media_sinks_observers_.push_back(observer);
84 return true;
85 }));
65 EXPECT_CALL(mock_router_, RegisterMediaRoutesObserver(_)) 86 EXPECT_CALL(mock_router_, RegisterMediaRoutesObserver(_))
66 .Times(AnyNumber()); 87 .Times(AnyNumber());
67 media_router_ui_->InitForTest(&mock_router_, initiator_.get(), 88 media_router_ui_->InitForTest(&mock_router_, initiator_.get(),
68 message_handler_.get()); 89 message_handler_.get(),
90 std::move(create_session_request_));
69 message_handler_->SetWebUIForTest(&web_ui_); 91 message_handler_->SetWebUIForTest(&web_ui_);
70 } 92 }
71 93
72 protected: 94 protected:
73 MockMediaRouter mock_router_; 95 MockMediaRouter mock_router_;
74 content::TestBrowserThreadBundle thread_bundle_; 96 content::TestBrowserThreadBundle thread_bundle_;
75 TestingProfile profile_; 97 TestingProfile profile_;
76 std::unique_ptr<content::WebContents> initiator_; 98 std::unique_ptr<content::WebContents> initiator_;
77 content::TestWebUI web_ui_; 99 content::TestWebUI web_ui_;
78 std::unique_ptr<content::WebContents> web_contents_; 100 std::unique_ptr<content::WebContents> web_contents_;
101 std::unique_ptr<CreatePresentationConnectionRequest> create_session_request_;
79 std::unique_ptr<MediaRouterUI> media_router_ui_; 102 std::unique_ptr<MediaRouterUI> media_router_ui_;
80 std::unique_ptr<MediaRouterWebUIMessageHandler> message_handler_; 103 std::unique_ptr<MediaRouterWebUIMessageHandler> message_handler_;
104 std::vector<MediaSinksObserver*> media_sinks_observers_;
81 }; 105 };
82 106
83 TEST_F(MediaRouterUITest, RouteCreationTimeoutForTab) { 107 TEST_F(MediaRouterUITest, RouteCreationTimeoutForTab) {
84 CreateMediaRouterUI(&profile_); 108 CreateMediaRouterUI(&profile_);
85 std::vector<MediaRouteResponseCallback> callbacks; 109 std::vector<MediaRouteResponseCallback> callbacks;
86 EXPECT_CALL( 110 EXPECT_CALL(
87 mock_router_, 111 mock_router_,
88 CreateRoute(_, _, _, _, _, base::TimeDelta::FromSeconds(60), false)) 112 CreateRoute(_, _, _, _, _, base::TimeDelta::FromSeconds(60), false))
89 .WillOnce(SaveArg<4>(&callbacks)); 113 .WillOnce(SaveArg<4>(&callbacks));
90 media_router_ui_->CreateRoute("sinkId", MediaCastMode::TAB_MIRROR); 114 media_router_ui_->CreateRoute("sinkId", MediaCastMode::TAB_MIRROR);
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 EXPECT_EQ("", MediaRouterUI::GetExtensionName(url, registry.get())); 423 EXPECT_EQ("", MediaRouterUI::GetExtensionName(url, registry.get()));
400 } 424 }
401 425
402 TEST_F(MediaRouterUITest, GetExtensionNameEmptyWhenNotExtensionURL) { 426 TEST_F(MediaRouterUITest, GetExtensionNameEmptyWhenNotExtensionURL) {
403 GURL url = GURL("https://www.google.com"); 427 GURL url = GURL("https://www.google.com");
404 std::unique_ptr<extensions::ExtensionRegistry> registry = 428 std::unique_ptr<extensions::ExtensionRegistry> registry =
405 base::WrapUnique(new extensions::ExtensionRegistry(nullptr)); 429 base::WrapUnique(new extensions::ExtensionRegistry(nullptr));
406 430
407 EXPECT_EQ("", MediaRouterUI::GetExtensionName(url, registry.get())); 431 EXPECT_EQ("", MediaRouterUI::GetExtensionName(url, registry.get()));
408 } 432 }
433
434 TEST_F(MediaRouterUITest, NotFoundErrorOnCloseWithNoSinks) {
435 content::PresentationError expected_error(
436 content::PresentationErrorType::PRESENTATION_ERROR_NO_AVAILABLE_SCREENS,
437 "No screens found.");
438 PresentationRequestCallbacks request_callbacks(expected_error);
439 create_session_request_.reset(new CreatePresentationConnectionRequest(
440 RenderFrameHostId(0, 0), std::string("http://google.com/presentation"),
441 GURL("http://google.com"),
442 base::Bind(&PresentationRequestCallbacks::Success,
443 base::Unretained(&request_callbacks)),
444 base::Bind(&PresentationRequestCallbacks::Error,
445 base::Unretained(&request_callbacks))));
446 CreateMediaRouterUI(&profile_);
447 // Destroying the UI should return the expected error from above to the error
448 // callback.
449 media_router_ui_.reset();
450 }
451
452 TEST_F(MediaRouterUITest, NotFoundErrorOnCloseWithNoCompatibleSinks) {
453 content::PresentationError expected_error(
454 content::PresentationErrorType::PRESENTATION_ERROR_NO_AVAILABLE_SCREENS,
455 "No screens found.");
456 PresentationRequestCallbacks request_callbacks(expected_error);
457 std::string presentation_url("http://google.com/presentation");
458 create_session_request_.reset(new CreatePresentationConnectionRequest(
459 RenderFrameHostId(0, 0), presentation_url, GURL("http://google.com"),
460 base::Bind(&PresentationRequestCallbacks::Success,
461 base::Unretained(&request_callbacks)),
462 base::Bind(&PresentationRequestCallbacks::Error,
463 base::Unretained(&request_callbacks))));
464 CreateMediaRouterUI(&profile_);
465
466 // Send a sink to the UI that is compatible with sources other than the
467 // presentation url to cause a NotFoundError.
468 std::vector<MediaSink> sinks;
469 sinks.emplace_back("sink id", "sink name", MediaSink::GENERIC);
470 std::vector<GURL> origins;
471 for (auto& observer : media_sinks_observers_) {
472 if (observer->source().id() != presentation_url) {
473 observer->OnSinksUpdated(sinks, origins);
474 }
475 }
476 // Destroying the UI should return the expected error from above to the error
477 // callback.
478 media_router_ui_.reset();
479 }
480
481 TEST_F(MediaRouterUITest, AbortErrorOnClose) {
482 content::PresentationError expected_error(
483 content::PresentationErrorType::
484 PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED,
485 "Dialog closed.");
486 PresentationRequestCallbacks request_callbacks(expected_error);
487 std::string presentation_url("http://google.com/presentation");
488 create_session_request_.reset(new CreatePresentationConnectionRequest(
489 RenderFrameHostId(0, 0), presentation_url, GURL("http://google.com"),
490 base::Bind(&PresentationRequestCallbacks::Success,
491 base::Unretained(&request_callbacks)),
492 base::Bind(&PresentationRequestCallbacks::Error,
493 base::Unretained(&request_callbacks))));
494 CreateMediaRouterUI(&profile_);
495
496 // Send a sink to the UI that is compatible with the presentation url to avoid
497 // a NotFoundError.
498 std::vector<MediaSink> sinks;
499 sinks.emplace_back("sink id", "sink name", MediaSink::GENERIC);
500 std::vector<GURL> origins;
501 for (auto& observer : media_sinks_observers_) {
502 if (observer->source().id() == presentation_url) {
503 observer->OnSinksUpdated(sinks, origins);
504 }
505 }
506 // Destroying the UI should return the expected error from above to the error
507 // callback.
508 media_router_ui_.reset();
509 }
409 } // namespace media_router 510 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698