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

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 comments, add no-sinks integration test, update test data json format 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 class PresentationRequestCallbacks {
40 public:
41 explicit PresentationRequestCallbacks(
42 const content::PresentationError& expected_error)
43 : expected_error_(expected_error) {}
44
45 void Success(const content::PresentationSessionInfo&, const MediaRoute::Id&) {
46 }
47
48 void Error(const content::PresentationError& error) {
49 EXPECT_EQ(expected_error_.error_type, error.error_type);
50 EXPECT_EQ(expected_error_.message, error.message);
51 }
52
53 private:
54 content::PresentationError expected_error_;
55 };
56
37 class MockRoutesUpdatedCallback { 57 class MockRoutesUpdatedCallback {
38 public: 58 public:
39 MOCK_METHOD2(OnRoutesUpdated, 59 MOCK_METHOD2(OnRoutesUpdated,
40 void(const std::vector<MediaRoute>& routes, 60 void(const std::vector<MediaRoute>& routes,
41 const std::vector<MediaRoute::Id>& joinable_route_ids)); 61 const std::vector<MediaRoute::Id>& joinable_route_ids));
42 }; 62 };
43 63
44 class MediaRouterUITest : public ::testing::Test { 64 class MediaRouterUITest : public ::testing::Test {
45 public: 65 public:
46 ~MediaRouterUITest() override { 66 ~MediaRouterUITest() override {
47 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)) 67 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_))
48 .Times(AnyNumber()); 68 .Times(AnyNumber());
49 EXPECT_CALL(mock_router_, UnregisterMediaRoutesObserver(_)) 69 EXPECT_CALL(mock_router_, UnregisterMediaRoutesObserver(_))
50 .Times(AnyNumber()); 70 .Times(AnyNumber());
51 } 71 }
52 72
53 void CreateMediaRouterUI(Profile* profile) { 73 void CreateMediaRouterUI(Profile* profile) {
54 initiator_.reset(content::WebContents::Create( 74 initiator_.reset(content::WebContents::Create(
55 content::WebContents::CreateParams(profile))); 75 content::WebContents::CreateParams(profile)));
56 SessionTabHelper::CreateForWebContents(initiator_.get()); 76 SessionTabHelper::CreateForWebContents(initiator_.get());
57 web_contents_.reset(content::WebContents::Create( 77 web_contents_.reset(content::WebContents::Create(
58 content::WebContents::CreateParams(profile))); 78 content::WebContents::CreateParams(profile)));
59 web_ui_.set_web_contents(web_contents_.get()); 79 web_ui_.set_web_contents(web_contents_.get());
60 media_router_ui_.reset(new MediaRouterUI(&web_ui_)); 80 media_router_ui_.reset(new MediaRouterUI(&web_ui_));
61 message_handler_.reset( 81 message_handler_.reset(
62 new MediaRouterWebUIMessageHandler(media_router_ui_.get())); 82 new MediaRouterWebUIMessageHandler(media_router_ui_.get()));
63 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) 83 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_))
64 .WillRepeatedly(Return(true)); 84 .WillRepeatedly(Invoke([this](MediaSinksObserver* observer) {
85 this->media_sinks_observers_.push_back(observer);
86 return true;
87 }));
65 EXPECT_CALL(mock_router_, RegisterMediaRoutesObserver(_)) 88 EXPECT_CALL(mock_router_, RegisterMediaRoutesObserver(_))
66 .Times(AnyNumber()); 89 .Times(AnyNumber());
67 media_router_ui_->InitForTest(&mock_router_, initiator_.get(), 90 media_router_ui_->InitForTest(&mock_router_, initiator_.get(),
68 message_handler_.get()); 91 message_handler_.get(),
92 std::move(create_session_request_));
69 message_handler_->SetWebUIForTest(&web_ui_); 93 message_handler_->SetWebUIForTest(&web_ui_);
70 } 94 }
71 95
72 protected: 96 protected:
73 MockMediaRouter mock_router_; 97 MockMediaRouter mock_router_;
74 content::TestBrowserThreadBundle thread_bundle_; 98 content::TestBrowserThreadBundle thread_bundle_;
75 TestingProfile profile_; 99 TestingProfile profile_;
76 std::unique_ptr<content::WebContents> initiator_; 100 std::unique_ptr<content::WebContents> initiator_;
77 content::TestWebUI web_ui_; 101 content::TestWebUI web_ui_;
78 std::unique_ptr<content::WebContents> web_contents_; 102 std::unique_ptr<content::WebContents> web_contents_;
103 std::unique_ptr<CreatePresentationConnectionRequest> create_session_request_;
79 std::unique_ptr<MediaRouterUI> media_router_ui_; 104 std::unique_ptr<MediaRouterUI> media_router_ui_;
80 std::unique_ptr<MediaRouterWebUIMessageHandler> message_handler_; 105 std::unique_ptr<MediaRouterWebUIMessageHandler> message_handler_;
106 std::vector<MediaSinksObserver*> media_sinks_observers_;
81 }; 107 };
82 108
83 TEST_F(MediaRouterUITest, RouteCreationTimeoutForTab) { 109 TEST_F(MediaRouterUITest, RouteCreationTimeoutForTab) {
84 CreateMediaRouterUI(&profile_); 110 CreateMediaRouterUI(&profile_);
85 std::vector<MediaRouteResponseCallback> callbacks; 111 std::vector<MediaRouteResponseCallback> callbacks;
86 EXPECT_CALL( 112 EXPECT_CALL(
87 mock_router_, 113 mock_router_,
88 CreateRoute(_, _, _, _, _, base::TimeDelta::FromSeconds(60), false)) 114 CreateRoute(_, _, _, _, _, base::TimeDelta::FromSeconds(60), false))
89 .WillOnce(SaveArg<4>(&callbacks)); 115 .WillOnce(SaveArg<4>(&callbacks));
90 media_router_ui_->CreateRoute("sinkId", MediaCastMode::TAB_MIRROR); 116 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())); 425 EXPECT_EQ("", MediaRouterUI::GetExtensionName(url, registry.get()));
400 } 426 }
401 427
402 TEST_F(MediaRouterUITest, GetExtensionNameEmptyWhenNotExtensionURL) { 428 TEST_F(MediaRouterUITest, GetExtensionNameEmptyWhenNotExtensionURL) {
403 GURL url = GURL("https://www.google.com"); 429 GURL url = GURL("https://www.google.com");
404 std::unique_ptr<extensions::ExtensionRegistry> registry = 430 std::unique_ptr<extensions::ExtensionRegistry> registry =
405 base::WrapUnique(new extensions::ExtensionRegistry(nullptr)); 431 base::WrapUnique(new extensions::ExtensionRegistry(nullptr));
406 432
407 EXPECT_EQ("", MediaRouterUI::GetExtensionName(url, registry.get())); 433 EXPECT_EQ("", MediaRouterUI::GetExtensionName(url, registry.get()));
408 } 434 }
435
436 TEST_F(MediaRouterUITest, NotFoundErrorOnCloseWithNoSinks) {
437 content::PresentationError expected_error(
438 content::PresentationErrorType::PRESENTATION_ERROR_NO_AVAILABLE_SCREENS,
439 "No screens found.");
440 PresentationRequestCallbacks request_callbacks(expected_error);
441 create_session_request_.reset(new CreatePresentationConnectionRequest(
442 RenderFrameHostId(0, 0), std::string("http://google.com/presentation"),
443 GURL("http://google.com"),
444 base::Bind(&PresentationRequestCallbacks::Success,
445 base::Unretained(&request_callbacks)),
446 base::Bind(&PresentationRequestCallbacks::Error,
447 base::Unretained(&request_callbacks))));
448 CreateMediaRouterUI(&profile_);
449 // Destroying the UI should return the expected error from above to the error
450 // callback.
451 media_router_ui_.reset();
452 }
453
454 TEST_F(MediaRouterUITest, NotFoundErrorOnCloseWithNoCompatibleSinks) {
455 content::PresentationError expected_error(
456 content::PresentationErrorType::PRESENTATION_ERROR_NO_AVAILABLE_SCREENS,
457 "No screens found.");
458 PresentationRequestCallbacks request_callbacks(expected_error);
459 std::string presentation_url("http://google.com/presentation");
460 create_session_request_.reset(new CreatePresentationConnectionRequest(
461 RenderFrameHostId(0, 0), presentation_url, GURL("http://google.com"),
462 base::Bind(&PresentationRequestCallbacks::Success,
463 base::Unretained(&request_callbacks)),
464 base::Bind(&PresentationRequestCallbacks::Error,
465 base::Unretained(&request_callbacks))));
466 CreateMediaRouterUI(&profile_);
467
468 // Send a sink to the UI that is compatible with sources other than the
469 // presentation url to cause a NotFoundError.
470 std::vector<MediaSink> sinks;
471 sinks.emplace_back("sink id", "sink name", MediaSink::GENERIC);
472 std::vector<GURL> origins;
473 for (auto& observer : media_sinks_observers_) {
474 if (observer->source().id() != presentation_url) {
475 observer->OnSinksUpdated(sinks, origins);
476 }
477 }
478 // Destroying the UI should return the expected error from above to the error
479 // callback.
480 media_router_ui_.reset();
481 }
482
483 TEST_F(MediaRouterUITest, AbortErrorOnClose) {
484 content::PresentationError expected_error(
485 content::PresentationErrorType::
486 PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED,
487 "Dialog closed.");
488 PresentationRequestCallbacks request_callbacks(expected_error);
489 std::string presentation_url("http://google.com/presentation");
490 create_session_request_.reset(new CreatePresentationConnectionRequest(
491 RenderFrameHostId(0, 0), presentation_url, GURL("http://google.com"),
492 base::Bind(&PresentationRequestCallbacks::Success,
493 base::Unretained(&request_callbacks)),
494 base::Bind(&PresentationRequestCallbacks::Error,
495 base::Unretained(&request_callbacks))));
496 CreateMediaRouterUI(&profile_);
497
498 // Send a sink to the UI that is compatible with the presentation url to avoid
499 // a NotFoundError.
500 std::vector<MediaSink> sinks;
501 sinks.emplace_back("sink id", "sink name", MediaSink::GENERIC);
502 std::vector<GURL> origins;
503 for (auto& observer : media_sinks_observers_) {
504 if (observer->source().id() == presentation_url) {
505 observer->OnSinksUpdated(sinks, origins);
506 }
507 }
508 // Destroying the UI should return the expected error from above to the error
509 // callback.
510 media_router_ui_.reset();
511 }
409 } // namespace media_router 512 } // namespace media_router
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/media_router/media_router_ui.cc ('k') | chrome/test/media_router/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698