| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #include "base/macros.h" | |
| 6 #include "base/run_loop.h" | |
| 7 #include "base/threading/platform_thread.h" | |
| 8 #include "base/threading/thread_task_runner_handle.h" | |
| 9 #include "chromecast/browser/cast_media_blocker.h" | |
| 10 #include "chromecast/browser/test/cast_browser_test.h" | |
| 11 #include "chromecast/chromecast_features.h" | |
| 12 #include "content/public/browser/media_session.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 #include "content/public/test/browser_test_utils.h" | |
| 15 #include "media/base/test_data_util.h" | |
| 16 #include "url/gurl.h" | |
| 17 #include "url/url_constants.h" | |
| 18 | |
| 19 namespace chromecast { | |
| 20 namespace shell { | |
| 21 | |
| 22 class CastMediaBlockerBrowserTest : public CastBrowserTest { | |
| 23 public: | |
| 24 CastMediaBlockerBrowserTest() {} | |
| 25 | |
| 26 protected: | |
| 27 // CastBrowserTest implementation. | |
| 28 void TearDownOnMainThread() override { | |
| 29 blocker_.reset(); | |
| 30 | |
| 31 CastBrowserTest::TearDownOnMainThread(); | |
| 32 } | |
| 33 | |
| 34 void PlayMedia(const std::string& tag, const std::string& media_file) { | |
| 35 base::StringPairs query_params; | |
| 36 query_params.push_back(std::make_pair(tag, media_file)); | |
| 37 query_params.push_back(std::make_pair("loop", "true")); | |
| 38 | |
| 39 std::string query = media::GetURLQueryString(query_params); | |
| 40 GURL gurl = content::GetFileUrlWithQuery( | |
| 41 media::GetTestDataFilePath("player.html"), query); | |
| 42 | |
| 43 web_contents_ = NavigateToURL(gurl); | |
| 44 WaitForLoadStop(web_contents_); | |
| 45 | |
| 46 blocker_ = base::MakeUnique<CastMediaBlocker>( | |
| 47 content::MediaSession::Get(web_contents_), web_contents_); | |
| 48 } | |
| 49 | |
| 50 void BlockAndTestPlayerState(const std::string& media_type, bool blocked) { | |
| 51 blocker_->BlockMediaLoading(blocked); | |
| 52 | |
| 53 // Changing states is not instant, but should be timely (< 0.5s). | |
| 54 for (size_t i = 0; i < 5; i++) { | |
| 55 base::RunLoop run_loop; | |
| 56 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 57 FROM_HERE, run_loop.QuitClosure(), | |
| 58 base::TimeDelta::FromMilliseconds(50)); | |
| 59 base::MessageLoop::ScopedNestableTaskAllower allow_nested( | |
| 60 base::MessageLoop::current()); | |
| 61 run_loop.Run(); | |
| 62 | |
| 63 const std::string command = | |
| 64 "document.getElementsByTagName(\"" + media_type + "\")[0].paused"; | |
| 65 const std::string js = | |
| 66 "window.domAutomationController.send(" + command + ");"; | |
| 67 | |
| 68 bool paused; | |
| 69 ASSERT_TRUE(ExecuteScriptAndExtractBool(web_contents_, js, &paused)); | |
| 70 | |
| 71 if (paused == blocked) { | |
| 72 SUCCEED() << "Media element has been successfullly " | |
| 73 << (blocked ? "blocked" : "unblocked"); | |
| 74 return; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 FAIL() << "Could not successfullly " << (blocked ? "block" : "unblock") | |
| 79 << " media element"; | |
| 80 } | |
| 81 | |
| 82 private: | |
| 83 content::WebContents* web_contents_; | |
| 84 std::unique_ptr<CastMediaBlocker> blocker_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(CastMediaBlockerBrowserTest); | |
| 87 }; | |
| 88 | |
| 89 IN_PROC_BROWSER_TEST_F(CastMediaBlockerBrowserTest, Audio_BlockUnblock) { | |
| 90 PlayMedia("audio", "bear-audio-10s-CBR-has-TOC.mp3"); | |
| 91 | |
| 92 BlockAndTestPlayerState("audio", true); | |
| 93 BlockAndTestPlayerState("audio", false); | |
| 94 } | |
| 95 | |
| 96 #if !BUILDFLAG(IS_CAST_AUDIO_ONLY) | |
| 97 IN_PROC_BROWSER_TEST_F(CastMediaBlockerBrowserTest, Video_BlockUnblock) { | |
| 98 PlayMedia("video", "tulip2.webm"); | |
| 99 | |
| 100 BlockAndTestPlayerState("video", true); | |
| 101 BlockAndTestPlayerState("video", false); | |
| 102 } | |
| 103 #endif | |
| 104 | |
| 105 } // namespace shell | |
| 106 } // namespace chromecast | |
| OLD | NEW |