chromium/chrome/browser/media/media_engagement_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 "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/mei_preload_component_installer.h"
#include "chrome/browser/media/media_engagement_contents_observer.h"
#include "chrome/browser/media/media_engagement_preloaded_list.h"
#include "chrome/browser/media/media_engagement_service.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/preloading/prefetch/no_state_prefetch/no_state_prefetch_manager_factory.h"
#include "chrome/browser/preloading/prefetch/no_state_prefetch/no_state_prefetch_test_utils.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sessions/session_restore_test_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/recently_audible_helper.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/component_updater/component_updater_service.h"
#include "components/content_settings/core/common/features.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_handle.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_manager.h"
#include "components/no_state_prefetch/common/no_state_prefetch_final_status.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/prerender_test_util.h"
#include "media/base/media_switches.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/mojom/autoplay/autoplay.mojom-test-utils.h"
#include "third_party/blink/public/mojom/autoplay/autoplay.mojom.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/sessions/session_service_factory.h"
#include "chrome/browser/sessions/session_service_test_helper.h"
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

namespace {

const char* kMediaEngagementTestDataPath =;

const std::u16string kReadyTitle =;

// Watches WasRecentlyAudible changes on a WebContents, blocking until the
// tab is audible. The audio stream monitor runs at 15Hz so we need have
// a slight delay to ensure it has run.
// TODO: Clean this up to use the callbacks available on
// RecentlyAudibleHelper rather than busy-looping.
class WasRecentlyAudibleWatcher {};

}  // namespace

// Class used to test the Media Engagement service.
class MediaEngagementBrowserTest : public InProcessBrowserTest {};

// Class used to test the MEI preload component.
class MediaEngagementPreloadBrowserTest : public InProcessBrowserTest {};

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest, RecordEngagement) {}

// Flaky tests on CrOS: http://crbug.com/1020131.
#if BUILDFLAG(IS_CHROMEOS_ASH)
#define MAYBE_RecordEngagement_AudioOnly
#else
#define MAYBE_RecordEngagement_AudioOnly
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_RecordEngagement_AudioOnly) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       DoNotRecordEngagement_NotTime) {}

// Flaky tests on CrOS: http://crbug.com/1019671.
#if BUILDFLAG(IS_CHROMEOS_ASH)
#define MAYBE_DoNotRecordEngagement_NotTime_AudioOnly
#else
#define MAYBE_DoNotRecordEngagement_NotTime_AudioOnly
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_DoNotRecordEngagement_NotTime_AudioOnly) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       DoNotRecordEngagement_TabMuted) {}

// Flaky tests on CrOS: http://crbug.com/1019671.
#if BUILDFLAG(IS_CHROMEOS_ASH)
#define MAYBE_DoNotRecordEngagement_TabMuted_AudioOnly
#else
#define MAYBE_DoNotRecordEngagement_TabMuted_AudioOnly
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_DoNotRecordEngagement_TabMuted_AudioOnly) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       DoNotRecordEngagement_PlayerMuted) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       DoNotRecordEngagement_PlayerMuted_AudioOnly) {}

// TODO(crbug.com/40748282) Re-enable test
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       DISABLED_DoNotRecordEngagement_PlaybackStopped) {}

// Flaky tests on CrOS: http://crbug.com/1019671.
#if BUILDFLAG(IS_CHROMEOS_ASH)
#define MAYBE_DoNotRecordEngagement_PlaybackStopped_AudioOnly
#else
#define MAYBE_DoNotRecordEngagement_PlaybackStopped_AudioOnly
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_DoNotRecordEngagement_PlaybackStopped_AudioOnly) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       RecordEngagement_NotVisible) {}

// Flaky tests on CrOS: http://crbug.com/1019671.
#if BUILDFLAG(IS_CHROMEOS_ASH)
#define MAYBE_RecordEngagement_NotVisible_AudioOnly
#else
#define MAYBE_RecordEngagement_NotVisible_AudioOnly
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_RecordEngagement_NotVisible_AudioOnly) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       DoNotRecordEngagement_FrameSize) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       DoNotRecordEngagement_NoAudioTrack) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       DoNotRecordEngagement_SilentAudioTrack) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest, RecordVisitOnBrowserClose) {}

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
    BUILDFLAG(IS_MAC)
// Flaky timeout. https://crbug.com/1014229
#define MAYBE_RecordSingleVisitOnSameOrigin
#else
#define MAYBE_RecordSingleVisitOnSameOrigin
#endif

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_RecordSingleVisitOnSameOrigin) {}

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// Flaky: https://crbug.com/1115238
#define MAYBE_RecordVisitOnNewOrigin
#else
#define MAYBE_RecordVisitOnNewOrigin
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_RecordVisitOnNewOrigin) {}

// Flaky tests on CrOS: http://crbug.com/1019671.
#if BUILDFLAG(IS_CHROMEOS_ASH)
#define MAYBE_DoNotRecordEngagement_SilentAudioTrack_AudioOnly
#else
#define MAYBE_DoNotRecordEngagement_SilentAudioTrack_AudioOnly
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_DoNotRecordEngagement_SilentAudioTrack_AudioOnly) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest, IFrameDelegation) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest, IFrameDelegation_AudioOnly) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       ClearBrowsingHistoryBeforePlayback) {}

// Flaky tests on CrOS: http://crbug.com/1019671.
#if BUILDFLAG(IS_CHROMEOS_ASH)
#define MAYBE_MultipleElements
#else
#define MAYBE_MultipleElements
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest, MAYBE_MultipleElements) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       RecordAudibleBasedOnShortTime) {}

#if BUILDFLAG(IS_MAC)
// https://crbug.com/1222896
#define MAYBE_SessionNewTabNavigateSameURL
#else
#define MAYBE_SessionNewTabNavigateSameURL
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_SessionNewTabNavigateSameURL) {}

#if BUILDFLAG(IS_MAC)
// https://crbug.com/1222896
#define MAYBE_SessionNewTabSameURL
#else
#define MAYBE_SessionNewTabSameURL
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest, MAYBE_SessionNewTabSameURL) {}

#if BUILDFLAG(IS_MAC)
// https://crbug.com/1222896
#define MAYBE_SessionNewTabSameOrigin
#else
#define MAYBE_SessionNewTabSameOrigin
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_SessionNewTabSameOrigin) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest, SessionNewTabCrossOrigin) {}

#if BUILDFLAG(IS_MAC)
// TODO(crbug.com/40939686) Flaky on Mac.
#define MAYBE_SessionMultipleTabsClosingParent
#else
#define MAYBE_SessionMultipleTabsClosingParent
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_SessionMultipleTabsClosingParent) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementPreloadBrowserTest,
                       EnsureSingletonListIsLoaded) {}

#if BUILDFLAG(IS_MAC)
// https://crbug.com/1222896
#define MAYBE_SessionNewTabNavigateSameURLWithOpener_Typed
#else
#define MAYBE_SessionNewTabNavigateSameURLWithOpener_Typed
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementBrowserTest,
                       MAYBE_SessionNewTabNavigateSameURLWithOpener_Typed) {}

class MediaEngagementPreThirdPartyCookieDeprecationBrowserTest
    : public MediaEngagementBrowserTest {};

#if BUILDFLAG(IS_WIN)
#define MAYBE_Ignored
#else
#define MAYBE_Ignored
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementPreThirdPartyCookieDeprecationBrowserTest,
                       MAYBE_Ignored) {}

class MediaEngagementSessionRestoreBrowserTest
    : public MediaEngagementBrowserTest {};

IN_PROC_BROWSER_TEST_F(MediaEngagementSessionRestoreBrowserTest,
                       RestoredSession_NoPlayback_NoMEI) {}

IN_PROC_BROWSER_TEST_F(MediaEngagementSessionRestoreBrowserTest,
                       RestoredSession_Playback_MEI) {}

class MockAutoplayConfigurationClient
    : public blink::mojom::AutoplayConfigurationClientInterceptorForTesting {};

class MediaEngagementContentsObserverMPArchBrowserTest
    : public MediaEngagementBrowserTest {};

class MediaEngagementContentsObserverPrerenderBrowserTest
    : public MediaEngagementContentsObserverMPArchBrowserTest {};

// Flaky on Linux: http://crbug.com/325530046
#if BUILDFLAG(IS_LINUX)
#define MAYBE_DoNotSendEngagementLevelToRenderFrameInPrerendering
#else
#define MAYBE_DoNotSendEngagementLevelToRenderFrameInPrerendering
#endif
IN_PROC_BROWSER_TEST_F(
    MediaEngagementContentsObserverPrerenderBrowserTest,
    MAYBE_DoNotSendEngagementLevelToRenderFrameInPrerendering) {}

class MediaEngagementContentsObserverFencedFrameBrowserTest
    : public MediaEngagementContentsObserverMPArchBrowserTest {};

// TODO(crbug.com/349253812): Flaky on Linux.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_SendEngagementLevelToRenderFrameOnFencedFrame
#else
#define MAYBE_SendEngagementLevelToRenderFrameOnFencedFrame
#endif
IN_PROC_BROWSER_TEST_F(MediaEngagementContentsObserverFencedFrameBrowserTest,
                       MAYBE_SendEngagementLevelToRenderFrameOnFencedFrame) {}