Index: chrome/browser/ui/webui/media_router/media_router_ui_unittest.cc |
diff --git a/chrome/browser/ui/webui/media_router/media_router_ui_unittest.cc b/chrome/browser/ui/webui/media_router/media_router_ui_unittest.cc |
index 2fe3a8390b1cdc492553d7ec28b915738ccdb962..7b20b304bb12838c93b0bca0036d27361867cc8c 100644 |
--- a/chrome/browser/ui/webui/media_router/media_router_ui_unittest.cc |
+++ b/chrome/browser/ui/webui/media_router/media_router_ui_unittest.cc |
@@ -18,8 +18,8 @@ |
#include "chrome/browser/sessions/session_tab_helper.h" |
#include "chrome/browser/ui/webui/media_router/media_router_webui_message_handler.h" |
#include "chrome/grit/generated_resources.h" |
+#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
#include "chrome/test/base/testing_profile.h" |
-#include "content/public/test/test_browser_thread_bundle.h" |
#include "content/public/test/test_web_ui.h" |
#include "extensions/browser/extension_registry.h" |
#include "extensions/common/extension.h" |
@@ -30,11 +30,12 @@ |
#include "testing/gtest/include/gtest/gtest.h" |
#include "ui/base/l10n/l10n_util.h" |
+using content::WebContents; |
using testing::_; |
using testing::AnyNumber; |
using testing::Invoke; |
-using testing::SaveArg; |
using testing::Return; |
+using testing::SaveArg; |
namespace media_router { |
@@ -63,22 +64,33 @@ class MockRoutesUpdatedCallback { |
const std::vector<MediaRoute::Id>& joinable_route_ids)); |
}; |
-class MediaRouterUITest : public ::testing::Test { |
+class MediaRouterUITest : public ChromeRenderViewHostTestHarness { |
public: |
- ~MediaRouterUITest() override { |
+ void TearDown() override { |
EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)) |
.Times(AnyNumber()); |
EXPECT_CALL(mock_router_, UnregisterMediaRoutesObserver(_)) |
.Times(AnyNumber()); |
+ web_ui_contents_.reset(); |
+ create_session_request_.reset(); |
+ media_router_ui_.reset(); |
+ message_handler_.reset(); |
+ ChromeRenderViewHostTestHarness::TearDown(); |
+ } |
+ |
+ void CreateMediaRouterUIForURL(Profile* profile, const GURL& url) { |
+ web_contents()->GetController().LoadURL(url, content::Referrer(), |
+ ui::PAGE_TRANSITION_LINK, ""); |
+ content::RenderFrameHostTester::CommitPendingLoad( |
+ &web_contents()->GetController()); |
+ CreateMediaRouterUI(profile); |
} |
void CreateMediaRouterUI(Profile* profile) { |
- initiator_.reset(content::WebContents::Create( |
- content::WebContents::CreateParams(profile))); |
- SessionTabHelper::CreateForWebContents(initiator_.get()); |
- web_contents_.reset(content::WebContents::Create( |
- content::WebContents::CreateParams(profile))); |
- web_ui_.set_web_contents(web_contents_.get()); |
+ SessionTabHelper::CreateForWebContents(web_contents()); |
+ web_ui_contents_.reset( |
+ WebContents::Create(WebContents::CreateParams(profile))); |
+ web_ui_.set_web_contents(web_ui_contents_.get()); |
media_router_ui_.reset(new MediaRouterUI(&web_ui_)); |
message_handler_.reset( |
new MediaRouterWebUIMessageHandler(media_router_ui_.get())); |
@@ -89,7 +101,7 @@ class MediaRouterUITest : public ::testing::Test { |
})); |
EXPECT_CALL(mock_router_, RegisterMediaRoutesObserver(_)) |
.Times(AnyNumber()); |
- media_router_ui_->InitForTest(&mock_router_, initiator_.get(), |
+ media_router_ui_->InitForTest(&mock_router_, web_contents(), |
message_handler_.get(), |
std::move(create_session_request_)); |
message_handler_->SetWebUIForTest(&web_ui_); |
@@ -104,11 +116,8 @@ class MediaRouterUITest : public ::testing::Test { |
protected: |
MockMediaRouter mock_router_; |
- content::TestBrowserThreadBundle thread_bundle_; |
- TestingProfile profile_; |
- std::unique_ptr<content::WebContents> initiator_; |
content::TestWebUI web_ui_; |
- std::unique_ptr<content::WebContents> web_contents_; |
+ std::unique_ptr<WebContents> web_ui_contents_; |
std::unique_ptr<CreatePresentationConnectionRequest> create_session_request_; |
std::unique_ptr<MediaRouterUI> media_router_ui_; |
std::unique_ptr<MediaRouterWebUIMessageHandler> message_handler_; |
@@ -116,7 +125,7 @@ class MediaRouterUITest : public ::testing::Test { |
}; |
TEST_F(MediaRouterUITest, RouteCreationTimeoutForTab) { |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
std::vector<MediaRouteResponseCallback> callbacks; |
EXPECT_CALL( |
mock_router_, |
@@ -135,7 +144,7 @@ TEST_F(MediaRouterUITest, RouteCreationTimeoutForTab) { |
} |
TEST_F(MediaRouterUITest, RouteCreationTimeoutForDesktop) { |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
std::vector<MediaRouteResponseCallback> callbacks; |
EXPECT_CALL( |
mock_router_, |
@@ -154,7 +163,7 @@ TEST_F(MediaRouterUITest, RouteCreationTimeoutForDesktop) { |
} |
TEST_F(MediaRouterUITest, RouteCreationTimeoutForPresentation) { |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
PresentationRequest presentation_request( |
RenderFrameHostId(0, 0), {GURL("https://presentationurl.com")}, |
GURL("https://frameurl.fakeurl")); |
@@ -178,7 +187,7 @@ TEST_F(MediaRouterUITest, RouteCreationTimeoutForPresentation) { |
} |
TEST_F(MediaRouterUITest, RouteCreationParametersCantBeCreated) { |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
MediaSinkSearchResponseCallback sink_callback; |
EXPECT_CALL(mock_router_, SearchSinks(_, _, _, _, _)) |
.WillOnce(SaveArg<4>(&sink_callback)); |
@@ -193,7 +202,7 @@ TEST_F(MediaRouterUITest, RouteCreationParametersCantBeCreated) { |
} |
TEST_F(MediaRouterUITest, RouteRequestFromIncognito) { |
- CreateMediaRouterUI(profile_.GetOffTheRecordProfile()); |
+ CreateMediaRouterUI(profile()->GetOffTheRecordProfile()); |
PresentationRequest presentation_request(RenderFrameHostId(0, 0), |
{GURL("https://foo.url.com/")}, |
@@ -208,7 +217,7 @@ TEST_F(MediaRouterUITest, RouteRequestFromIncognito) { |
} |
TEST_F(MediaRouterUITest, SortedSinks) { |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
std::vector<MediaSinkWithCastModes> unsorted_sinks; |
std::string sink_id1("sink3"); |
std::string sink_name1("B sink"); |
@@ -237,7 +246,7 @@ TEST_F(MediaRouterUITest, SortedSinks) { |
} |
TEST_F(MediaRouterUITest, SortSinksByIconType) { |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
std::vector<MediaSinkWithCastModes> unsorted_sinks; |
MediaSinkWithCastModes sink1( |
@@ -349,8 +358,8 @@ TEST_F(MediaRouterUITest, |
} |
TEST_F(MediaRouterUITest, UIMediaRoutesObserverAssignsCurrentCastModes) { |
- CreateMediaRouterUI(&profile_); |
- SessionID::id_type tab_id = SessionTabHelper::IdForTab(initiator_.get()); |
+ CreateMediaRouterUI(profile()); |
+ SessionID::id_type tab_id = SessionTabHelper::IdForTab(web_contents()); |
MediaSource media_source_1(MediaSourceForTab(tab_id)); |
MediaSource media_source_2("mediaSource"); |
MediaSource media_source_3(MediaSourceForDesktop()); |
@@ -399,7 +408,7 @@ TEST_F(MediaRouterUITest, UIMediaRoutesObserverAssignsCurrentCastModes) { |
} |
TEST_F(MediaRouterUITest, UIMediaRoutesObserverSkipsUnavailableCastModes) { |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
MediaSource media_source_1("mediaSource1"); |
MediaSource media_source_2("mediaSource2"); |
MediaSource media_source_3(MediaSourceForDesktop()); |
@@ -494,7 +503,7 @@ TEST_F(MediaRouterUITest, NotFoundErrorOnCloseWithNoSinks) { |
base::Unretained(&request_callbacks)), |
base::Bind(&PresentationRequestCallbacks::Error, |
base::Unretained(&request_callbacks)))); |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
// Destroying the UI should return the expected error from above to the error |
// callback. |
media_router_ui_.reset(); |
@@ -512,7 +521,7 @@ TEST_F(MediaRouterUITest, NotFoundErrorOnCloseWithNoCompatibleSinks) { |
base::Unretained(&request_callbacks)), |
base::Bind(&PresentationRequestCallbacks::Error, |
base::Unretained(&request_callbacks)))); |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
// Send a sink to the UI that is compatible with sources other than the |
// presentation url to cause a NotFoundError. |
@@ -542,7 +551,7 @@ TEST_F(MediaRouterUITest, AbortErrorOnClose) { |
base::Unretained(&request_callbacks)), |
base::Bind(&PresentationRequestCallbacks::Error, |
base::Unretained(&request_callbacks)))); |
- CreateMediaRouterUI(&profile_); |
+ CreateMediaRouterUI(profile()); |
// Send a sink to the UI that is compatible with the presentation url to avoid |
// a NotFoundError. |
@@ -560,4 +569,60 @@ TEST_F(MediaRouterUITest, AbortErrorOnClose) { |
// callback. |
media_router_ui_.reset(); |
} |
+ |
+TEST_F(MediaRouterUITest, RecordCastModeSelections) { |
+ const GURL url_1a = GURL("https://www.example.com/watch?v=AAAA"); |
+ const GURL url_1b = GURL("https://www.example.com/watch?v=BBBB"); |
+ const GURL url_2 = GURL("https://example2.com/0000"); |
+ const GURL url_3 = GURL("https://www3.example.com/index.html"); |
+ |
+ CreateMediaRouterUIForURL(profile(), url_1a); |
+ // When the user hasn't selected a cast mode for the hostname, it should |
+ // default to the default cast mode. |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::DEFAULT); |
+ media_router_ui_->RecordCastModeSelection(MediaCastMode::TAB_MIRROR); |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::TAB_MIRROR); |
+ |
+ CreateMediaRouterUIForURL(profile(), url_2); |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::DEFAULT); |
+ |
+ CreateMediaRouterUIForURL(profile(), url_1b); |
+ // |url_1a| and |url_1b| have the same hostname, so the selection made for |
+ // |url_1a| should be retrieved. |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::TAB_MIRROR); |
+ media_router_ui_->RecordCastModeSelection(MediaCastMode::DEFAULT); |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::DEFAULT); |
+ |
+ media_router_ui_->RecordCastModeSelection(MediaCastMode::TAB_MIRROR); |
+ CreateMediaRouterUIForURL(profile(), url_3); |
+ // |url_1a| and |url_3| have the same domain "example.com" but different |
+ // hostnames, so their preferences should be separate. |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::DEFAULT); |
+} |
+ |
+TEST_F(MediaRouterUITest, RecordDesktopMirroringCastModeSelection) { |
+ const GURL url = GURL("https://www.example.com/watch?v=AAAA"); |
+ CreateMediaRouterUIForURL(profile(), url); |
+ |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::DEFAULT); |
+ media_router_ui_->RecordCastModeSelection(MediaCastMode::DESKTOP_MIRROR); |
+ // Selecting desktop mirroring should not change the recorded preferences. |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::DEFAULT); |
+ |
+ media_router_ui_->RecordCastModeSelection(MediaCastMode::TAB_MIRROR); |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::TAB_MIRROR); |
+ media_router_ui_->RecordCastModeSelection(MediaCastMode::DESKTOP_MIRROR); |
+ // Selecting desktop mirroring should not change the recorded preferences. |
+ EXPECT_EQ(media_router_ui_->GetCastModeSelectionForCurrentHost(), |
+ MediaCastMode::TAB_MIRROR); |
+} |
} // namespace media_router |