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
3 // found in the LICENSE file.
5 #include "chrome/test/media_router/media_router_e2e_browsertest.h"
7 #include "base/command_line.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/media/router/media_router.h"
10 #include "chrome/browser/media/router/media_router_factory.h"
11 #include "chrome/browser/media/router/media_router_mojo_impl.h"
12 #include "chrome/browser/media/router/media_source.h"
13 #include "chrome/browser/media/router/media_source_helper.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sessions/session_tab_helper.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/test/test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 // Use the following command to run e2e browser tests:
24 // ./out/Debug/browser_tests --user-data-dir=<empty user data dir>
25 // --extension-unpacked=<mr extension dir>
26 // --receiver=<chromecast device name>
27 // --enable-pixel-output-in-tests --run-manual
28 // --gtest_filter=MediaRouterE2EBrowserTest.<test case name>
29 // --enable-logging=stderr
30 // --whitelisted-extension-id=enhhojjnijigcajfphajepfemndkmdlo
31 // --ui-test-action-timeout=200000
32 // --enable-media-router
35 // Command line argument to specify receiver,
36 const char kReceiver
[] = "receiver";
37 // URL to launch Castv2Player_Staging app on Chromecast
38 const char kCastAppPresentationUrl
[] =
39 "https://google.com/cast#__castAppId__=BE6E4473/"
40 "__castClientId__=143692175507258981";
44 namespace media_router
{
46 MediaRouterE2EBrowserTest::MediaRouterE2EBrowserTest()
47 : media_router_(nullptr) {
50 MediaRouterE2EBrowserTest::~MediaRouterE2EBrowserTest() {
53 void MediaRouterE2EBrowserTest::SetUpOnMainThread() {
54 MediaRouterBaseBrowserTest::SetUpOnMainThread();
56 MediaRouterFactory::GetApiForBrowserContext(browser()->profile());
57 DCHECK(media_router_
);
60 void MediaRouterE2EBrowserTest::TearDownOnMainThread() {
61 MediaRouterBaseBrowserTest::TearDownOnMainThread();
62 media_router_
= nullptr;
65 void MediaRouterE2EBrowserTest::OnRouteResponseReceived(
66 const MediaRoute
* route
,
67 const std::string
& presentation_id
,
68 const std::string
& error
) {
70 route_id_
= route
->media_route_id();
73 void MediaRouterE2EBrowserTest::CreateMediaRoute(const MediaSource
& source
,
76 DCHECK(media_router_
);
77 observer_
.reset(new TestMediaSinksObserver(media_router_
, source
));
79 DVLOG(1) << "Receiver name: " << receiver_
;
80 // Wait for MediaSinks compatible with |source| to be discovered.
81 ASSERT_TRUE(ConditionalWait(
82 base::TimeDelta::FromSeconds(30), base::TimeDelta::FromSeconds(1),
83 base::Bind(&MediaRouterE2EBrowserTest::IsSinkDiscovered
,
84 base::Unretained(this))));
86 const auto& sink_map
= observer_
->sink_map
;
87 const auto it
= sink_map
.find(receiver_
);
88 const MediaSink
& sink
= it
->second
;
90 // The callback will set route_id_ when invoked.
91 std::vector
<MediaRouteResponseCallback
> route_response_callbacks
;
92 route_response_callbacks
.push_back(
93 base::Bind(&MediaRouterE2EBrowserTest::OnRouteResponseReceived
,
94 base::Unretained(this)));
95 media_router_
->CreateRoute(source
.id(), sink
.id(), origin
, tab_id
,
96 route_response_callbacks
);
98 // Wait for the route request to be fulfilled (and route to be started).
99 ASSERT_TRUE(ConditionalWait(
100 base::TimeDelta::FromSeconds(30), base::TimeDelta::FromSeconds(1),
101 base::Bind(&MediaRouterE2EBrowserTest::IsRouteCreated
,
102 base::Unretained(this))));
105 void MediaRouterE2EBrowserTest::StopMediaRoute() {
106 ASSERT_FALSE(route_id_
.empty());
108 media_router_
->CloseRoute(route_id_
);
114 void MediaRouterE2EBrowserTest::ParseCommandLine() {
115 MediaRouterBaseBrowserTest::ParseCommandLine();
116 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
118 receiver_
= command_line
->GetSwitchValueASCII(kReceiver
);
119 ASSERT_FALSE(receiver_
.empty());
122 bool MediaRouterE2EBrowserTest::IsSinkDiscovered() const {
123 return ContainsKey(observer_
->sink_map
, receiver_
);
126 bool MediaRouterE2EBrowserTest::IsRouteCreated() const {
127 return !route_id_
.empty();
132 IN_PROC_BROWSER_TEST_F(MediaRouterE2EBrowserTest
, MANUAL_TabMirroring
) {
133 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
134 EXPECT_EQ(1, browser()->tab_strip_model()->count());
136 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
137 browser(), GURL("about:blank"), 1);
138 int tab_id
= SessionTabHelper::IdForTab(
139 browser()->tab_strip_model()->GetActiveWebContents());
141 // Wait for 30 seconds to make sure the route is stable.
142 CreateMediaRoute(MediaSourceForTab(tab_id
), GURL("http://origin/"), tab_id
);
143 Wait(base::TimeDelta::FromSeconds(30));
145 // Wait for 10 seconds to make sure route has been stopped.
147 Wait(base::TimeDelta::FromSeconds(10));
150 IN_PROC_BROWSER_TEST_F(MediaRouterE2EBrowserTest
, MANUAL_CastApp
) {
151 // Wait for 30 seconds to make sure the route is stable.
152 CreateMediaRoute(MediaSourceForPresentationUrl(kCastAppPresentationUrl
),
153 GURL("http://origin/"), kInvalidTabId
);
154 Wait(base::TimeDelta::FromSeconds(30));
156 // Wait for 10 seconds to make sure route has been stopped.
158 Wait(base::TimeDelta::FromSeconds(10));
161 } // namespace media_router