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