cast_media_blocker_browsertest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include <memory>
  5. #include "base/logging.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_buildflags.h"
  12. #include "content/public/browser/media_session.h"
  13. #include "content/public/browser/web_contents.h"
  14. #include "content/public/test/browser_test.h"
  15. #include "content/public/test/browser_test_utils.h"
  16. #include "media/base/test_data_util.h"
  17. #include "url/gurl.h"
  18. #include "url/url_constants.h"
  19. namespace chromecast {
  20. namespace shell {
  21. // TODO(crbug.com/1057860): Move relevant tests to components/browsertests so
  22. // there is common coverage of MediaBlocker across platforms.
  23. class CastMediaBlockerBrowserTest : public CastBrowserTest {
  24. public:
  25. CastMediaBlockerBrowserTest() {}
  26. CastMediaBlockerBrowserTest(const CastMediaBlockerBrowserTest&) = delete;
  27. CastMediaBlockerBrowserTest& operator=(const CastMediaBlockerBrowserTest&) =
  28. delete;
  29. protected:
  30. // CastBrowserTest implementation.
  31. void TearDownOnMainThread() override {
  32. blocker_.reset();
  33. CastBrowserTest::TearDownOnMainThread();
  34. }
  35. void PlayMedia(const std::string& tag, const std::string& media_file) {
  36. base::StringPairs query_params;
  37. query_params.push_back(std::make_pair(tag, media_file));
  38. query_params.push_back(std::make_pair("loop", "true"));
  39. std::string query = ::media::GetURLQueryString(query_params);
  40. GURL gurl = content::GetFileUrlWithQuery(
  41. ::media::GetTestDataFilePath("player.html"), query);
  42. web_contents_ = NavigateToURL(gurl);
  43. EXPECT_TRUE(WaitForLoadStop(web_contents_));
  44. blocker_ = std::make_unique<CastMediaBlocker>(web_contents_);
  45. }
  46. void BlockAndTestPlayerState(const std::string& media_type, bool blocked) {
  47. blocker_->BlockMediaLoading(blocked);
  48. // Changing states is not instant, but should be timely (< 0.5s).
  49. for (size_t i = 0; i < 5; i++) {
  50. LOG(INFO) << "Checking media blocking, re-try = " << i;
  51. base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
  52. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  53. FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(100));
  54. run_loop.Run();
  55. const std::string command =
  56. "document.getElementsByTagName(\"" + media_type + "\")[0].paused";
  57. const std::string js =
  58. "window.domAutomationController.send(" + command + ");";
  59. bool paused;
  60. ASSERT_TRUE(ExecuteScriptAndExtractBool(web_contents_, js, &paused));
  61. if (paused == blocked) {
  62. SUCCEED() << "Media element has been successfullly "
  63. << (blocked ? "blocked" : "unblocked");
  64. return;
  65. }
  66. }
  67. FAIL() << "Could not successfullly " << (blocked ? "block" : "unblock")
  68. << " media element";
  69. }
  70. private:
  71. content::WebContents* web_contents_;
  72. std::unique_ptr<CastMediaBlocker> blocker_;
  73. };
  74. IN_PROC_BROWSER_TEST_F(CastMediaBlockerBrowserTest, Audio_BlockUnblock) {
  75. PlayMedia("audio", "bear-audio-10s-CBR-has-TOC.mp3");
  76. BlockAndTestPlayerState("audio", true);
  77. BlockAndTestPlayerState("audio", false);
  78. }
  79. #if !BUILDFLAG(IS_CAST_AUDIO_ONLY)
  80. IN_PROC_BROWSER_TEST_F(CastMediaBlockerBrowserTest, Video_BlockUnblock) {
  81. PlayMedia("video", "tulip2.webm");
  82. BlockAndTestPlayerState("video", true);
  83. BlockAndTestPlayerState("video", false);
  84. }
  85. #endif
  86. } // namespace shell
  87. } // namespace chromecast