chromium/content/browser/back_forward_cache_basics_browsertest.cc

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/342213636): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "content/browser/back_forward_cache_browsertest.h"

#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/navigation_handle_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/controllable_http_response.h"

// This file contains back/forward-cache tests that test basic functionality,
// e.g. navigation, different responses and document structures.
// Almost everything in here could have been written as a JS-only WPT. It was
// forked from
// https://source.chromium.org/chromium/chromium/src/+/main:content/browser/back_forward_cache_browsertest.cc;drc=804bb57be3441b6291c11e34d8f901e2b1c0b430
//
// When adding tests here consider adding a WPT intead. See
// third_party/blink/web_tests/external/wpt/html/browsers/browsing-the-web/back-forward-cache/README.md

_;
Each;
ElementsAre;
Not;
UnorderedElementsAreArray;

namespace content {

namespace {
void InsertSubFrameWithUrl(RenderFrameHost* rfh, std::string url) {}
}  // namespace

NotRestoredReason;
NotRestoredReasons;

// Navigate from A to B and go back.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, Basic) {}

// Navigate from A to B and go back.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, BasicDocumentInitiated) {}

// Navigate from back and forward repeatedly.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       NavigateBackForwardRepeatedly) {}

// The current page can't enter the BackForwardCache if another page can script
// it. This can happen when one document opens a popup using window.open() for
// instance. It prevents the BackForwardCache from being used.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, WindowOpen) {}

// Similar with WindowOpen above, but open a cross-site page in the popup.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, WindowOpenCrossSite) {}

// Similar with WindowOpenCrossSite above, but navigate the popup same-site
// instead of cross site.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       WindowOpenCrossSiteNavigateSameSite) {}

// Similar with WindowOpen above, but open a cross-site page that has an iframe
// that is same-site with the opener.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       WindowOpenCrossSiteWithSameSiteChild) {}

// A popup will prevent a page from entering BFCache. Test that after closing a
// popup, the page is not stopped from entering. This tries to close the popup
// at the last moment.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, WindowOpenThenClose) {}

enum class NavigationSiteType {};

class BackForwardCacheBrowserTestWithVaryingNavigationSite
    : public BackForwardCacheBrowserTest,
      public ::testing::WithParamInterface<NavigationSiteType> {};

INSTANTIATE_TEST_SUITE_P();

// Tests that RelatedActiveContents-related metrics is not logged when the
// BrowsingInstance swap didn't happen, but not because of the existence of
// related active contents, but due to other BFCache blockers.
IN_PROC_BROWSER_TEST_P(BackForwardCacheBrowserTestWithVaryingNavigationSite,
                       RelatedActiveContentsLoggingOnPageWithBlockingFeature) {}

// Tests that RelatedActiveContents-related metrics is logged when the
// BrowsingInstance swap didn't happen because it has related active contents
// alongside other BFCache blockers.
IN_PROC_BROWSER_TEST_P(
    BackForwardCacheBrowserTestWithVaryingNavigationSite,
    RelatedActiveContentsLoggingOnPageWithBlockingFeatureAndRAC) {}

// Test that if a page has an openee sharing the same BrowsingInstance but it
// is no longer active (pending deletion, etc) it won't prevent the opener from
// getting BFCached.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       WindowOpen_SameSitePopupPendingDeletion) {}

// Tests that a page that has a same-site same-process but unrelated page (no
// opener relationship) won't be treated as having a potential sync accessor.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       WindowOpen_UnrelatedSameSiteAndProcessTab) {}

// Navigate from A(B) to C and go back.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, BasicIframe) {}

// Similar to BackForwardCacheBrowserTest.SubframeSurviveCache*
// Test case: url_1(b2) -> c3 -> url_1(b2)
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, SubframeSurviveCache1) {}

// Similar to BackForwardCacheBrowserTest.SubframeSurviveCache*
// Test case: url_1(b2) -> b3 -> url_1(b2).
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, SubframeSurviveCache2) {}

// Similar to BackForwardCacheBrowserTest.tSubframeSurviveCache*
// Test case: url_1(b2) -> b3(a4) -> url_1(b2) -> b3(a4)
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, SubframeSurviveCache3) {}

// Similar to BackForwardCacheBrowserTest.SubframeSurviveCache*
// Test case: url_1(b2) -> b3 -> a4 -> b5 -> url_1(b2).
IN_PROC_BROWSER_TEST_F(HighCacheSizeBackForwardCacheBrowserTest,
                       SubframeSurviveCache4) {}

// Check that unload event handlers are not dispatched when the page goes
// into BackForwardCache.
IN_PROC_BROWSER_TEST_F(BackForwardCacheUnloadBrowserTest,
                       ConfirmUnloadEventNotFired) {}

class BackForwardCacheStillLoadingBrowserTest
    : public BackForwardCacheBrowserTest,
      public ::testing::WithParamInterface<TestFrameType> {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(BackForwardCacheStillLoadingBrowserTest,
                       DoesNotCacheIfFrameStillLoading) {}

class BackForwardCacheStillNavigatingBrowserTest
    : public BackForwardCacheBrowserTest,
      public ::testing::WithParamInterface<TestFrameType> {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(BackForwardCacheStillNavigatingBrowserTest,
                       DoesNotCacheNavigatingSubframe) {}

// Check that a frame with an invalid url doesn't affect the back-forward cache
// usage.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       FrameWithInvalidURLDoesntAffectCache) {}

IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, DoesNotCacheIfHttpError) {}

IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       DoesNotCacheIfPageUnreachable) {}

// Tests the events are fired when going back from the cache.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, Events) {}

// Tests the events are fired for subframes when going back from the cache.
// Test case: a(b) -> c -> a(b)
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, EventsForSubframes) {}

// Tests the events are fired when going back from the cache.
// Same as: BackForwardCacheBrowserTest.Events, but with a document-initiated
// navigation. This is a regression test for https://crbug.com/1000324
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       EventsAfterDocumentInitiatedNavigation) {}

// Track the events dispatched when a page is deemed ineligible for back-forward
// cache after we've dispatched the 'pagehide' event with persisted set to true.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       EventsForPageIneligibleAfterPagehidePersisted) {}

// Track the events dispatched when a page is deemed ineligible for back-forward
// cache before we've dispatched the pagehide event on it.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       EventsForPageIneligibleBeforePagehide) {}

enum class StickinessType {};

class BackForwardCacheBrowserTestWithVaryingFrameAndFeatureStickinessType
    : public BackForwardCacheBrowserTest,
      public ::testing::WithParamInterface<
          testing::tuple<TestFrameType, StickinessType>> {};

INSTANTIATE_TEST_SUITE_P();

// Test pagehide's persisted value and whether the page can be BFCached when a
// sticky/non-sticky feature is used on the mainframe/subframe.
//
// TODO(crbug.com/40913015): Flaky on all platforms.
IN_PROC_BROWSER_TEST_P(
    BackForwardCacheBrowserTestWithVaryingFrameAndFeatureStickinessType,
    DISABLED_TestPagehidePersistedValue) {}

IN_PROC_BROWSER_TEST_F(HighCacheSizeBackForwardCacheBrowserTest,
                       CanCacheMultiplesPagesOnSameDomain) {}

IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, Encoding) {}

IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       DoesNotCacheCrossSiteHttpPost) {}

// On windows, the expected value is off by ~20ms. In order to get the
// feature out to canary, the test is disabled for WIN.
// TODO(crbug.com/40657468): Fix this for Win.
// TODO(crbug.com/40767606): Flaky on other platforms.
// Make sure we are exposing the duration between back navigation's
// navigationStart and the page's original navigationStart through pageshow
// event's timeStamp, and that we aren't modifying
// performance.timing.navigationStart.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, DISABLED_NavigationStart) {}

IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       CanUseCacheWhenNavigatingAwayToErrorPage) {}

// RenderFrameHostImpl::coep_reporter() must be preserved when doing a back
// navigation using the BackForwardCache.
// Regression test for https://crbug.com/1102285.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, CoepReporter) {}

// RenderFrameHostImpl::coop_reporter() must be preserved when doing a back
// navigation using the BackForwardCache.
// Regression test for https://crbug.com/1102285.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, CoopReporter) {}

// RenderFrameHostImpl::cross_origin_embedder_policy() must be preserved when
// doing a back navigation using the BackForwardCache.
// Regression test for https://crbug.com/1021846.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, Coep) {}

// Tests that pagehide and visibilitychange handlers of the old RFH are run for
// bfcached pages.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       PagehideAndVisibilitychangeRuns) {}

// Tests that the history value saved in the renderer is updated correctly when
// a page gets restored from the back-forward cache through browser-initiated
// navigation.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       RendererHistory_BrowserInitiated) {}

// Tests that the history value saved in the renderer is updated correctly when
// a page gets restored from the back-forward cache through renderer-initiated
// navigation.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       RendererHistory_RendererInitiated) {}

// Check that an eligible page is cached when navigating to about:blank.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest,
                       NavigatingToAboutBlankDoesNotPreventCaching) {}

// Check that the response 204 No Content doesn't affect back-forward cache.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, NoContent) {}

// A testing subclass that limits the cache size to 1 for ease of testing
// evictions.
class CacheSizeOneBackForwardCacheBrowserTest
    : public BackForwardCacheBrowserTest {};

IN_PROC_BROWSER_TEST_F(CacheSizeOneBackForwardCacheBrowserTest,
                       ReplacedNavigationEntry) {}

// Server redirect happens when doing history navigation, causing a SiteInstance
// swap and a new navigation entry. Ensure that the reasons from the old entry
// are copied to the new one and reported.
IN_PROC_BROWSER_TEST_F(BackForwardCacheBrowserTest, ServerRedirect) {}

class BackForwardCacheBrowsingContextStateBrowserTest
    : public BackForwardCacheBrowserTest,
      public ::testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

// Check that if a RenderViewHost is removed after the page has entered
// the back/forward cache we don't crash.
IN_PROC_BROWSER_TEST_P(BackForwardCacheBrowsingContextStateBrowserTest,
                       SlowUnloadHandlerInIframe) {}

}  // namespace content