chromium/content/browser/media/session/media_session_browsertest.cc

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/browser/media_session.h"

#include <optional>

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/media_start_stop_observer.h"
#include "content/public/test/test_media_session_client.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "media/base/media_switches.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/http_request.h"
#include "services/media_session/public/cpp/features.h"
#include "services/media_session/public/cpp/test/audio_focus_test_util.h"
#include "services/media_session/public/cpp/test/mock_media_session.h"

namespace {
int hidden_metadata_placeholder_thumbnail_size =;
}  // namespace

namespace content {

namespace {

const char kMediaSessionImageTestURL[] =;
const char kMediaSessionImageTestPageVideoElement[] =;

const char kMediaSessionTestImagePath[] =;

class MediaImageGetterHelper {};

// Integration tests for content::MediaSession that do not take into
// consideration the implementation details contrary to
// MediaSessionImplBrowserTest.
class MediaSessionBrowserTestBase : public ContentBrowserTest {};

class MediaSessionBrowserTest : public MediaSessionBrowserTestBase {};

class MediaSessionBrowserTestWithoutInternalMediaSession
    : public MediaSessionBrowserTestBase {};

}  // anonymous namespace

IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTestWithoutInternalMediaSession,
                       MediaSessionNoOpWhenDisabled) {}

IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, SimplePlayPause) {}

IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, MultiplePlayersPlayPause) {}

// Flaky on Mac. See https://crbug.com/980663
#if BUILDFLAG(IS_MAC)
#define MAYBE_WebContents_Muted
#else
#define MAYBE_WebContents_Muted
#endif
IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, MAYBE_WebContents_Muted) {}

#if !BUILDFLAG(IS_ANDROID)
// On Android, System Audio Focus would break this test.

IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, MultipleTabsPlayPause) {}
#endif  // BUILDFLAG(IS_ANDROID)

IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, GetMediaImageBitmap) {}

// We hide the media image from CrOS' media controls by replacing the image in
// the MediaSessionImpl with a placeholder image. These changes are gated to
// only affect ChromeOS, hence why the testing for this is also ChromeOS only.
#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, HideMediaMetadataImageInCrOS) {
  client_.SetShouldHideMetadata(true);

  ASSERT_TRUE(embedded_test_server()->Start());

  // We don't expect a media image because of the way the image will be replaced
  // with its placeholder in MediaSessionImpl.
  MediaSession* media_session =
      SetupMediaImageTest(/*expect_media_image=*/false);
  ASSERT_NE(nullptr, media_session);

  MediaImageGetterHelper helper(media_session, CreateTestImageWithSize(1), 0,
                                10);

  helper.Wait();

  EXPECT_EQ(hidden_metadata_placeholder_thumbnail_size,
            helper.bitmap().width());
  EXPECT_EQ(hidden_metadata_placeholder_thumbnail_size,
            helper.bitmap().height());

  // As we are replacing the image, we should not visit the original's URL.
  EXPECT_FALSE(WasURLVisited(GetTestImageURL()));
}
#else  // !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
                       DontHideMediaMetadataImageInNonCrOS) {}
#endif

IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
                       GetMediaImageBitmap_ImageTooSmall) {}

IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
                       GetMediaImageBitmap_ImageTooSmall_BeforeDownload) {}

IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest,
                       GetMediaImageBitmap_InvalidImage) {}

// Regression test of crbug.com/1195769.
IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, ChangeMediaElementDocument) {}

}  // namespace content