#include "content/browser/back_forward_cache_browsertest.h"
#include <climits>
#include <optional>
#include <string_view>
#include <unordered_map>
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/metrics_hashes.h"
#include "base/run_loop.h"
#include "base/system/sys_info.h"
#include "base/task/common/task_annotator.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/trace_event/trace_log.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#include "build/chromeos_buildflags.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/browser/bad_message.h"
#include "content/browser/renderer_host/back_forward_cache_can_store_document_result.h"
#include "content/browser/renderer_host/back_forward_cache_disable.h"
#include "content/browser/renderer_host/back_forward_cache_impl.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/should_swap_browsing_instance.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/common/features.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/document_service.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/commit_message_delayer.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/navigation_handle_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_navigation_throttle.h"
#include "content/public/test/test_navigation_throttle_inserter.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/text_input_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/public/test/web_contents_observer_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_javascript_dialog_manager.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "media/base/media_switches.h"
#include "mojo/public/cpp/bindings/message.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/filename_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/common/device_memory/approximated_device_memory.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/features_generated.h"
#include "third_party/blink/public/common/scheduler/web_scheduler_tracked_feature.h"
#include "third_party/blink/public/common/switches.h"
#include "third_party/blink/public/mojom/back_forward_cache_not_restored_reasons.mojom.h"
#include "third_party/blink/public/mojom/frame/back_forward_cache_controller.mojom.h"
#include "third_party/blink/public/mojom/frame/frame.mojom.h"
#include "third_party/blink/public/mojom/render_accessibility.mojom.h"
#include "third_party/blink/public/mojom/script_source_location.mojom.h"
_;
Each;
ElementsAre;
Not;
UnorderedElementsAreArray;
namespace content {
NotRestoredReasons;
NotRestoredReason;
namespace {
class DOMContentLoadedObserver : public WebContentsObserver { … };
}
bool WaitForDOMContentLoaded(RenderFrameHostImpl* rfh) { … }
EvalJsResult GetLocalStorage(RenderFrameHostImpl* rfh, std::string key) { … }
[[nodiscard]] bool WaitForLocalStorage(RenderFrameHostImpl* rfh,
std::string key,
std::string expected_value) { … }
BackForwardCacheBrowserTest::BackForwardCacheBrowserTest() = default;
BackForwardCacheBrowserTest::~BackForwardCacheBrowserTest() { … }
void BackForwardCacheBrowserTest::NotifyNotRestoredReasons(
std::unique_ptr<BackForwardCacheCanStoreTreeResult> tree_result) { … }
void BackForwardCacheBrowserTest::SetUpCommandLine(
base::CommandLine* command_line) { … }
void BackForwardCacheBrowserTest::SetUpInProcessBrowserTestFixture() { … }
void BackForwardCacheBrowserTest::TearDownInProcessBrowserTestFixture() { … }
void BackForwardCacheBrowserTest::SetupFeaturesAndParameters() { … }
void BackForwardCacheBrowserTest::EnableFeatureAndSetParams(
const base::Feature& feature,
std::string param_name,
std::string param_value) { … }
void BackForwardCacheBrowserTest::DisableFeature(const base::Feature& feature) { … }
void BackForwardCacheBrowserTest::SetUpOnMainThread() { … }
void BackForwardCacheBrowserTest::TearDownOnMainThread() { … }
WebContentsImpl* BackForwardCacheBrowserTest::web_contents() const { … }
RenderFrameHostImpl* BackForwardCacheBrowserTest::current_frame_host() { … }
RenderFrameHostManager*
BackForwardCacheBrowserTest::render_frame_host_manager() { … }
std::string BackForwardCacheBrowserTest::DepictFrameTree(FrameTreeNode* node) { … }
bool BackForwardCacheBrowserTest::HistogramContainsIntValue(
base::HistogramBase::Sample sample,
std::vector<base::Bucket> histogram_values) { … }
void BackForwardCacheBrowserTest::EvictByJavaScript(RenderFrameHostImpl* rfh) { … }
void BackForwardCacheBrowserTest::StartRecordingEvents(
RenderFrameHostImpl* rfh) { … }
void BackForwardCacheBrowserTest::MatchEventList(RenderFrameHostImpl* rfh,
base::Value list,
base::Location location) { … }
net::EmbeddedTestServer* BackForwardCacheBrowserTest::CreateHttpsServer() { … }
net::EmbeddedTestServer* BackForwardCacheBrowserTest::https_server() { … }
void BackForwardCacheBrowserTest::DoNotFailForUnexpectedMessagesWhileCached() { … }
RenderFrameHostImpl* BackForwardCacheBrowserTest::NavigateToPageWithImage(
const GURL& page_url) { … }
void BackForwardCacheBrowserTest::AcquireKeyboardLock(
RenderFrameHostImpl* rfh) { … }
void BackForwardCacheBrowserTest::ReleaseKeyboardLock(
RenderFrameHostImpl* rfh) { … }
void BackForwardCacheBrowserTest::NavigateAndBlock(GURL url,
int history_offset) { … }
ReasonsMatcher BackForwardCacheBrowserTest::MatchesNotRestoredReasons(
const std::optional<testing::Matcher<std::string>>& id,
const std::optional<testing::Matcher<std::string>>& name,
const std::optional<testing::Matcher<std::string>>& src,
const std::vector<BlockingDetailsReasonsMatcher>& reasons,
const std::optional<SameOriginMatcher>& same_origin_details) { … }
SameOriginMatcher BackForwardCacheBrowserTest::MatchesSameOriginDetails(
const testing::Matcher<GURL>& url,
const std::vector<ReasonsMatcher>& children) { … }
BlockingDetailsReasonsMatcher
BackForwardCacheBrowserTest::MatchesDetailedReason(
const testing::Matcher<std::string>& name,
const std::optional<SourceLocationMatcher>& source) { … }
BlockingDetailsMatcher BackForwardCacheBrowserTest::MatchesBlockingDetails(
const std::optional<SourceLocationMatcher>& source) { … }
SourceLocationMatcher BackForwardCacheBrowserTest::MatchesSourceLocation(
const testing::Matcher<GURL>& url,
const testing::Matcher<std::string>& function_name,
const testing::Matcher<uint64_t>& line_number,
const testing::Matcher<uint64_t>& column_number) { … }
void BackForwardCacheUnloadBrowserTest::SetUpCommandLine(
base::CommandLine* command_line) { … }
std::initializer_list<RenderFrameHostImpl*> Elements(
std::initializer_list<RenderFrameHostImpl*> t) { … }
class ReadyToCommitNavigationCallback : public WebContentsObserver { … };
class FirstVisuallyNonEmptyPaintObserver : public WebContentsObserver { … };
void WaitForFirstVisuallyNonEmptyPaint(WebContents* contents) { … }
class ThemeColorObserver : public WebContentsObserver { … };
PageLifecycleStateManagerTestDelegate::PageLifecycleStateManagerTestDelegate(
PageLifecycleStateManager* manager)
: … { … }
PageLifecycleStateManagerTestDelegate::
~PageLifecycleStateManagerTestDelegate() { … }
bool PageLifecycleStateManagerTestDelegate::WaitForInBackForwardCacheAck() { … }
void PageLifecycleStateManagerTestDelegate::OnStoreInBackForwardCacheSent(
base::OnceClosure cb) { … }
void PageLifecycleStateManagerTestDelegate::OnDisableJsEvictionSent(
base::OnceClosure cb) { … }
void PageLifecycleStateManagerTestDelegate::OnRestoreFromBackForwardCacheSent(
base::OnceClosure cb) { … }
void PageLifecycleStateManagerTestDelegate::OnLastAcknowledgedStateChanged(
const blink::mojom::PageLifecycleState& old_state,
const blink::mojom::PageLifecycleState& new_state) { … }
void PageLifecycleStateManagerTestDelegate::OnUpdateSentToRenderer(
const blink::mojom::PageLifecycleState& new_state) { … }
void PageLifecycleStateManagerTestDelegate::OnDeleted() { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, VisibleURL) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
CacheSizeLimitedToOneDocumentPerTab) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, ResponseHeaders) { … }
void HighCacheSizeBackForwardCacheBrowserTest::SetUpCommandLine(
base::CommandLine* command_line) { … }
IN_PROC_BROWSER_TEST_F(HighCacheSizeBackForwardCacheBrowserTest,
CacheEvictionWithIncreasedCacheSize) { … }
IN_PROC_BROWSER_TEST_F(HighCacheSizeBackForwardCacheBrowserTest,
EvictedWhileWaitingForBeforeUnload) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
SubframeWithOngoingNavigationNotCached) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, CacheHTTPDocumentOnly) { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_MAC) || BUILDFLAG(IS_IOS)
#define MAYBE_NavigateToTwoPagesOnSameSite …
#else
#define MAYBE_NavigateToTwoPagesOnSameSite …
#endif
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
MAYBE_NavigateToTwoPagesOnSameSite) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
NavigateToTwoPagesOnSameSiteWithSubframes) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheUnloadBrowserTest,
SubframeWithUnloadHandler) { … }
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_DoesNotFireDidFirstVisuallyNonEmptyPaintForSameDocumentNavigation …
#else
#define MAYBE_DoesNotFireDidFirstVisuallyNonEmptyPaintForSameDocumentNavigation …
#endif
IN_PROC_BROWSER_TEST_F(
BackForwardCacheBrowserTest,
MAYBE_DoesNotFireDidFirstVisuallyNonEmptyPaintForSameDocumentNavigation) { … }
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_FiresDidFirstVisuallyNonEmptyPaintWhenRestoredFromCache …
#else
#define MAYBE_FiresDidFirstVisuallyNonEmptyPaintWhenRestoredFromCache …
#endif
IN_PROC_BROWSER_TEST_F(
BackForwardCacheBrowserTest,
MAYBE_FiresDidFirstVisuallyNonEmptyPaintWhenRestoredFromCache) { … }
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_SetsThemeColorWhenRestoredFromCache …
#else
#define MAYBE_SetsThemeColorWhenRestoredFromCache …
#endif
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
MAYBE_SetsThemeColorWhenRestoredFromCache) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
ContentsMimeTypeWhenRestoredFromCache) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
BackForwardCacheEnabledOnLowMemoryDevices) { … }
class BackForwardCacheBrowserTestForLowMemoryDevices
: public BackForwardCacheBrowserTest { … };
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTestForLowMemoryDevices,
DisableBFCacheForLowEndDevices) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTestForLowMemoryDevices,
DisableBFCacheForLowEndDevices_NetworkRequests) { … }
class BackForwardCacheBrowserTestForHighMemoryDevices
: public BackForwardCacheBrowserTest { … };
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTestForHighMemoryDevices,
EnableBFCacheForHighMemoryDevices) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTestForHighMemoryDevices,
EnableBFCacheForHighMemoryDevices_NetworkRequests) { … }
class BackForwardCacheBrowserTestForHighMemoryDevicesWithBFCacheDisabled
: public BackForwardCacheBrowserTest { … };
IN_PROC_BROWSER_TEST_F(
BackForwardCacheBrowserTestForHighMemoryDevicesWithBFCacheDisabled,
HighMemoryDevicesWithBFacheDisabled) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
CanUseCacheWhenPageAlertsInTimeoutLoop) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
DialogsCancelledAndSuppressedWhenCached) { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN)
#define MAYBE_PagehideRunsWhenPageIsHidden …
#else
#define MAYBE_PagehideRunsWhenPageIsHidden …
#endif
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
MAYBE_PagehideRunsWhenPageIsHidden) { … }
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_TextInputStateUpdated …
#else
#define MAYBE_TextInputStateUpdated …
#endif
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
MAYBE_TextInputStateUpdated) { … }
#if (BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID))
#define MAYBE_SubframeTextInputStateUpdated …
#else
#define MAYBE_SubframeTextInputStateUpdated …
#endif
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
MAYBE_SubframeTextInputStateUpdated) { … }
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_FocusSameSiteSubframeOnPagehide …
#else
#define MAYBE_FocusSameSiteSubframeOnPagehide …
#endif
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
MAYBE_FocusSameSiteSubframeOnPagehide) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
FocusCrossSiteSubframeOnPagehide) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
MainDocumentCSPHeadersAreRestored) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, CspSandbox) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, AboutBlankWillNotBeCached) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, RedirectToSelf) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, ReloadDoesntAffectCache) { … }
IN_PROC_BROWSER_TEST_F(
BackForwardCacheBrowserTest,
SameDocumentNavAfterRestoringDocumentLoadedWithUserGesture) { … }
testing::Matcher<BackForwardCacheCanStoreTreeResult> MatchesTreeResult(
testing::Matcher<bool> same_origin,
GURL url) { … }
RenderFrameHostImpl* ChildFrame(RenderFrameHostImpl* rfh, int child_index) { … }
testing::Matcher<BackForwardCacheCanStoreDocumentResult>
BackForwardCacheBrowserTest::MatchesDocumentResult(
testing::Matcher<NotRestoredReasons> not_stored,
BlockListedFeatures block_listed) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, TreeResultFeatureUsage) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
TreeResultEvictionMainFrame) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
TreeResultEvictionSubFrame) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
TreeResultEvictionSubFramesSubframe) { … }
void BackForwardCacheBrowserTest::InstallUnloadHandlerOnMainFrame() { … }
void BackForwardCacheBrowserTest::InstallUnloadHandlerOnSubFrame() { … }
EvalJsResult BackForwardCacheBrowserTest::GetUnloadRunCount() { … }
bool BackForwardCacheBrowserTest::AddBlocklistedFeature(RenderFrameHost* rfh) { … }
void BackForwardCacheBrowserTest::ExpectNotRestoredDueToBlocklistedFeature(
base::Location location) { … }
const ukm::TestAutoSetUkmRecorder& BackForwardCacheBrowserTest::ukm_recorder() { … }
const base::HistogramTester& BackForwardCacheBrowserTest::histogram_tester() { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, UnloadAllowedFlag) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
FrameWithBlocklistedFeatureNotCached) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
SubframeWithBlocklistedFeatureNotCached) { … }
class BackForwardCacheBrowserUnloadHandlerTest
: public BackForwardCacheBrowserTest,
public ::testing::WithParamInterface<
std::tuple<bool, bool, bool, TestFrameType>> { … };
IN_PROC_BROWSER_TEST_P(BackForwardCacheBrowserUnloadHandlerTest,
UnloadHandlerPresent) { … }
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, DisableForRenderFrameHost) { … }
namespace {
enum class SubframeType { … };
}
class BackForwardCacheEvictionDueToSubframeNavigationBrowserTest
: public BackForwardCacheBrowserTest,
public ::testing::WithParamInterface<SubframeType> { … };
IN_PROC_BROWSER_TEST_P(
BackForwardCacheEvictionDueToSubframeNavigationBrowserTest,
SubframePendingCommitShouldPreventCache) { … }
IN_PROC_BROWSER_TEST_P(
BackForwardCacheEvictionDueToSubframeNavigationBrowserTest,
MainFrameCommitFirstAndSubframePendingCommitShouldBeEvicted) { … }
INSTANTIATE_TEST_SUITE_P(…);
namespace {
enum class SubframeNavigationType { … };
}
class BackForwardCacheWithSubframeNavigationBrowserTest
: public BackForwardCacheBrowserTest { … };
class BackForwardCacheWithSubframeNavigationWithParamBrowserTest
: public BackForwardCacheWithSubframeNavigationBrowserTest,
public ::testing::WithParamInterface<SubframeNavigationType> { … };
IN_PROC_BROWSER_TEST_P(
BackForwardCacheWithSubframeNavigationWithParamBrowserTest,
SubframeNavigationWithPendingCommitShouldPreventCache) { … }
IN_PROC_BROWSER_TEST_F(
BackForwardCacheWithSubframeNavigationBrowserTest,
MultipleSubframeNavigationWithBeforeAndPendingCommitShouldPreventCache) { … }
IN_PROC_BROWSER_TEST_F(
BackForwardCacheWithSubframeNavigationBrowserTest,
MultipleSubframeNavigationWithBeforeAndAfterSendingRequestShouldPreventCache) { … }
IN_PROC_BROWSER_TEST_F(
BackForwardCacheWithSubframeNavigationBrowserTest,
SubframeNavigationWithUrlLoaderAfterSendingRequestShouldPreventCache) { … }
IN_PROC_BROWSER_TEST_F(
BackForwardCacheWithSubframeNavigationBrowserTest,
SubframeNavigationWithUrlLoaderBeforeSendingRequestShouldNotPreventCache) { … }
IN_PROC_BROWSER_TEST_F(
BackForwardCacheWithSubframeNavigationBrowserTest,
SubframeNavigationWithoutUrlLoaderBeforeCommitShouldNotPreventCache) { … }
IN_PROC_BROWSER_TEST_P(
BackForwardCacheWithSubframeNavigationWithParamBrowserTest,
SubframeNavigationShouldNotBeResumedWhenUnrelatedPageRestored) { … }
IN_PROC_BROWSER_TEST_P(
BackForwardCacheWithSubframeNavigationWithParamBrowserTest,
EvictBFCachedPageWithDeferredSubframeNavigationBeforeCommit) { … }
INSTANTIATE_TEST_SUITE_P(…);
class BackForwardCacheFencedFrameBrowserTest
: public BackForwardCacheBrowserTest { … };
IN_PROC_BROWSER_TEST_F(BackForwardCacheFencedFrameBrowserTest,
FencedFramePageNotStoredInBackForwardCache) { … }
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
RendererInitiatedNavigateToSameUrl) { … }
}