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
& error
) {
69 route_id_
= route
->media_route_id();
72 void MediaRouterE2EBrowserTest::CreateMediaRoute(const MediaSource
& source
,
75 DCHECK(media_router_
);
76 observer_
.reset(new TestMediaSinksObserver(media_router_
, source
));
78 DVLOG(1) << "Receiver name: " << receiver_
;
79 // Wait for MediaSinks compatible with |source| to be discovered.
80 ConditionalWait(base::TimeDelta::FromSeconds(30),
81 base::TimeDelta::FromSeconds(1),
82 base::Bind(&MediaRouterE2EBrowserTest::IsSinkDiscovered
,
83 base::Unretained(this)));
85 const auto& sink_map
= observer_
->sink_map
;
86 const auto it
= sink_map
.find(receiver_
);
87 const MediaSink
& sink
= it
->second
;
89 // The callback will set route_id_ when invoked.
90 std::vector
<MediaRouteResponseCallback
> route_response_callbacks
;
91 route_response_callbacks
.push_back(
92 base::Bind(&MediaRouterE2EBrowserTest::OnRouteResponseReceived
,
93 base::Unretained(this)));
94 media_router_
->CreateRoute(source
.id(), sink
.id(), origin
, tab_id
,
95 route_response_callbacks
);
97 // Wait for the route request to be fulfilled (and route to be started).
98 ConditionalWait(base::TimeDelta::FromSeconds(30),
99 base::TimeDelta::FromSeconds(1),
100 base::Bind(&MediaRouterE2EBrowserTest::IsRouteCreated
,
101 base::Unretained(this)));
104 void MediaRouterE2EBrowserTest::StopMediaRoute() {
105 ASSERT_FALSE(route_id_
.empty());
107 media_router_
->CloseRoute(route_id_
);
113 void MediaRouterE2EBrowserTest::ParseCommandLine() {
114 MediaRouterBaseBrowserTest::ParseCommandLine();
115 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
117 receiver_
= command_line
->GetSwitchValueASCII(kReceiver
);
118 ASSERT_FALSE(receiver_
.empty());
121 bool MediaRouterE2EBrowserTest::IsSinkDiscovered() const {
122 return ContainsKey(observer_
->sink_map
, receiver_
);
125 bool MediaRouterE2EBrowserTest::IsRouteCreated() const {
126 return !route_id_
.empty();
131 IN_PROC_BROWSER_TEST_F(MediaRouterE2EBrowserTest
, MANUAL_TabMirroring
) {
132 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
133 EXPECT_EQ(1, browser()->tab_strip_model()->count());
135 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
136 browser(), GURL("about:blank"), 1);
137 int tab_id
= SessionTabHelper::IdForTab(
138 browser()->tab_strip_model()->GetActiveWebContents());
140 // Wait for 30 seconds to make sure the route is stable.
141 CreateMediaRoute(MediaSourceForTab(tab_id
), GURL("http://origin/"), tab_id
);
142 Wait(base::TimeDelta::FromSeconds(30));
144 // Wait for 10 seconds to make sure route has been stopped.
146 Wait(base::TimeDelta::FromSeconds(10));
149 IN_PROC_BROWSER_TEST_F(MediaRouterE2EBrowserTest
, MANUAL_CastApp
) {
150 // Wait for 30 seconds to make sure the route is stable.
151 CreateMediaRoute(MediaSourceForPresentationUrl(kCastAppPresentationUrl
),
152 GURL("http://origin/"), kInvalidTabId
);
153 Wait(base::TimeDelta::FromSeconds(30));
155 // Wait for 10 seconds to make sure route has been stopped.
157 Wait(base::TimeDelta::FromSeconds(10));
160 } // namespace media_router