chromium/content/browser/web_contents/web_contents_impl_unittest.cc

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

#include <stdint.h>

#include <tuple>
#include <utility>

#include "base/check.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/download/public/common/download_url_parameters.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/media/audio_stream_monitor.h"
#include "content/browser/media/media_web_contents_observer.h"
#include "content/browser/renderer_host/navigation_entry_impl.h"
#include "content/browser/renderer_host/navigator.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_frame_proxy_host.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/storage_partition_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/common/frame.mojom.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/global_request_id.h"
#include "content/public/browser/javascript_dialog_manager.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/ssl_host_state_delegate.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/content_features.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/url_utils.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/fake_local_frame.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/scoped_web_ui_controller_factory_registration.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_utils.h"
#include "content/test/navigation_simulator_impl.h"
#include "content/test/test_content_browser_client.h"
#include "content/test/test_content_client.h"
#include "content/test/test_page_broadcast.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "net/base/network_handle.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "services/network/public/cpp/web_sandbox_flags.h"
#include "services/network/test/test_network_context.h"
#include "skia/ext/skia_utils_base.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/chrome_debug_urls.h"
#include "third_party/blink/public/common/input/synthetic_web_input_event_builders.h"
#include "third_party/blink/public/common/security/protocol_handler_security_level.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "third_party/blink/public/mojom/image_downloader/image_downloader.mojom.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
#include "third_party/blink/public/mojom/page/page_visibility_state.mojom.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "url/gurl.h"
#include "url/url_constants.h"

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/lacros/lacros_test_helper.h"
#endif

namespace content {
namespace {

class WebContentsImplTest : public RenderViewHostImplTestHarness {};

class TestWebContentsObserver : public WebContentsObserver {};

class MockWebContentsDelegate : public WebContentsDelegate {};

// Pretends to be a normal browser that receives toggles and transitions to/from
// a fullscreened state.
class FakeFullscreenDelegate : public WebContentsDelegate {};

class FakeWebContentsDelegate : public WebContentsDelegate {};

class FakeImageDownloader : public blink::mojom::ImageDownloader {};

class MockPageBroadcast : public TestPageBroadcast {};

class TestColorProviderSource : public ui::ColorProviderSource {};

class MockNetworkContext : public network::TestNetworkContext {};

}  // namespace

TEST_F(WebContentsImplTest, SetMainFrameMimeType) {}

TEST_F(WebContentsImplTest, UpdateTitle) {}

TEST_F(WebContentsImplTest, UpdateTitleBeforeFirstNavigation) {}

TEST_F(WebContentsImplTest, UpdateTitleWhileFirstNavigationIsPending) {}

TEST_F(WebContentsImplTest, DontUsePendingEntryUrlAsTitle) {}

TEST_F(WebContentsImplTest, UpdateAndUseTitleFromFirstNavigationPendingEntry) {}

TEST_F(WebContentsImplTest,
       UpdateAndDontUseTitleFromPendingEntryForSecondNavigation) {}

// Stub out local frame mojo binding. Intercepts calls to EnableViewSourceMode
// and marks the message as received. This class attaches to the first
// RenderFrameHostImpl created.
class EnableViewSourceLocalFrame : public content::FakeLocalFrame,
                                   public WebContentsObserver {};

// Browser initiated navigations to view-source URLs of WebUI pages should work.
TEST_F(WebContentsImplTest, DirectNavigationToViewSourceWebUI) {}

// Test simple same-SiteInstance navigation.
TEST_F(WebContentsImplTest, SimpleNavigation) {}

// Test that we reject NavigateToEntry if the url is over kMaxURLChars.
TEST_F(WebContentsImplTest, NavigateToExcessivelyLongURL) {}

// Test that we reject NavigateToEntry if the url is invalid.
TEST_F(WebContentsImplTest, NavigateToInvalidURL) {}

// Test that we reject NavigateToEntry if the url is a renderer debug URL
// inside a view-source: URL. This verifies that the navigation is not allowed
// to proceed after the view-source: URL rewriting logic has run.
TEST_F(WebContentsImplTest, NavigateToViewSourceRendererDebugURL) {}

// Test that navigating across a site boundary creates a new RenderViewHost
// with a new SiteInstance.  Going back should do the same.
TEST_F(WebContentsImplTest, CrossSiteBoundaries) {}

// Test that navigating across a site boundary after a crash creates a new
// RFH without requiring a cross-site transition (i.e., PENDING state).
TEST_F(WebContentsImplTest, CrossSiteBoundariesAfterCrash) {}

// Test that opening a new contents in the same SiteInstance and then navigating
// both contentses to a new site will place both contentses in a single
// SiteInstance.
TEST_F(WebContentsImplTest, NavigateTwoTabsCrossSite) {}

// The embedder can request sites for certain urls not be be assigned to the
// SiteInstance by adding their schemes as empty document schemes,
// allowing to reuse the renderer backing certain chrome urls for subsequent
// navigation. The test verifies that the override is honored.
TEST_F(WebContentsImplTest, NavigateFromSitelessUrl) {}

// Regression test for http://crbug.com/386542 - variation of
// NavigateFromSitelessUrl in which the original navigation is a session
// restore.
TEST_F(WebContentsImplTest, NavigateFromRestoredSitelessUrl) {}

// Complement for NavigateFromRestoredSitelessUrl, verifying that when a regular
// tab is restored, the SiteInstance will change upon navigation.
TEST_F(WebContentsImplTest, NavigateFromRestoredRegularUrl) {}

// Test that we can find an opener RVH even if it's pending.
// http://crbug.com/176252.
TEST_F(WebContentsImplTest, FindOpenerRVHWhenPending) {}

// Tests that WebContentsImpl uses the current URL, not the SiteInstance's site,
// to determine whether a navigation is cross-site.
TEST_F(WebContentsImplTest, CrossSiteComparesAgainstCurrentPage) {}

// Test that the onbeforeunload and onunload handlers run when navigating
// across site boundaries.
TEST_F(WebContentsImplTest, CrossSiteUnloadHandlers) {}

// Test that during a slow cross-site navigation, the original renderer can
// navigate to a different URL and have it displayed, canceling the slow
// navigation.
TEST_F(WebContentsImplTest, CrossSiteNavigationPreempted) {}

// Tests that if we go back twice (same-site then cross-site), and the cross-
// site RFH commits first, we ignore the now-swapped-out RFH's commit.
TEST_F(WebContentsImplTest, CrossSiteNavigationBackOldNavigationIgnored) {}

// Test that during a slow cross-site navigation, a sub-frame navigation in the
// original renderer will not cancel the slow navigation (bug 42029).
TEST_F(WebContentsImplTest, CrossSiteNavigationNotPreemptedByFrame) {}

// Test that a cross-site navigation is not preempted if the previous
// renderer sends a FrameNavigate message just before being told to stop.
// We should only preempt the cross-site navigation if the previous renderer
// has started a new navigation. See http://crbug.com/79176.
TEST_F(WebContentsImplTest, CrossSiteNotPreemptedDuringBeforeUnload) {}

// Test that NavigationEntries have the correct page state after going
// forward and back.  Prevents regression for bug 1116137.
TEST_F(WebContentsImplTest, NavigationEntryContentState) {}

// Test that NavigationEntries have the correct page state and SiteInstance
// state after opening a new window to about:blank.  Prevents regression for
// bugs b/1116137 and http://crbug.com/111975.
TEST_F(WebContentsImplTest, NavigationEntryContentStateNewWindow) {}

namespace {

void ExpectTrue(bool value) {}

void ExpectFalse(bool value) {}

}  // namespace

// Tests that fullscreen is exited throughout the object hierarchy when
// navigating to a new page.
TEST_F(WebContentsImplTest, NavigationExitsFullscreen) {}

// Tests that fullscreen is exited throughout the object hierarchy when
// instructing NavigationController to GoBack() or GoForward().
TEST_F(WebContentsImplTest, HistoryNavigationExitsFullscreen) {}

// Tests that fullscreen is exited throughout the object hierarchy on a renderer
// crash.
TEST_F(WebContentsImplTest, CrashExitsFullscreen) {}

TEST_F(WebContentsImplTest,
       FailEnterFullscreenWhenNoUserActivationNoOrientationChange) {}

// Regression test for http://crbug.com/168611 - the URLs passed by the
// DidFinishLoad and DidFailLoadWithError IPCs should get filtered.
TEST_F(WebContentsImplTest, FilterURLs) {}

// Test that if a pending contents is deleted before it is shown, we don't
// crash.
TEST_F(WebContentsImplTest, PendingContentsDestroyed) {}

TEST_F(WebContentsImplTest, PendingContentsShown) {}

TEST_F(WebContentsImplTest, CaptureHoldsWakeLock) {}

TEST_F(WebContentsImplTest, CapturerOverridesPreferredSize) {}

TEST_F(WebContentsImplTest, UpdateWebContentsVisibility) {}

TEST_F(WebContentsImplTest, VideoPictureInPictureStaysVisibleIfHidden) {}

TEST_F(WebContentsImplTest, VisibilityIsUpdatedIfVideoPictureInPictureChanges) {}

TEST_F(WebContentsImplTest, DocumentPictureInPictureStaysVisibleIfHidden) {}

TEST_F(WebContentsImplTest,
       VisibilityIsUpdatedIfDocumentPictureInPictureChanges) {}

namespace {

void HideOrOccludeWithCapturerTest(WebContentsImpl* contents,
                                   Visibility hidden_or_occluded) {}

}  // namespace

TEST_F(WebContentsImplTest, HideWithCapturer) {}

TEST_F(WebContentsImplTest, OccludeWithCapturer) {}

TEST_F(WebContentsImplTest, HiddenCapture) {}

TEST_F(WebContentsImplTest, NonActivityCaptureDoesNotCountAsActivity) {}

// Tests that GetLastActiveTimeTicks starts with a real, non-zero time and
// updates on activity.
TEST_F(WebContentsImplTest, GetLastActiveTimeTicks) {}

class ContentsZoomChangedDelegate : public WebContentsDelegate {};

// Tests that some mouseehweel events get turned into browser zoom requests.
TEST_F(WebContentsImplTest, HandleWheelEvent) {}

// Tests that GetRelatedActiveContentsCount is shared between related
// SiteInstances and includes WebContents that have not navigated yet.
TEST_F(WebContentsImplTest, ActiveContentsCountBasic) {}

// Tests that GetRelatedActiveContentsCount is preserved correctly across
// same-site and cross-site navigations.
TEST_F(WebContentsImplTest, ActiveContentsCountNavigate) {}

// Tests that GetRelatedActiveContentsCount tracks BrowsingInstance changes
// from WebUI.
TEST_F(WebContentsImplTest, ActiveContentsCountChangeBrowsingInstance) {}

class LoadingWebContentsObserver : public WebContentsObserver {};

// Subclass of WebContentsImplTest for cases that need out-of-process iframes.
class WebContentsImplTestWithSiteIsolation : public WebContentsImplTest {};

// Ensure that DidStartLoading/DidStopLoading events balance out properly with
// interleaving cross-process navigations in multiple subframes.
// See https://crbug.com/448601 for details of the underlying issue. The
// sequence of events that reproduce it are as follows:
// * Navigate top-level frame with one subframe.
// * Subframe navigates more than once before the top-level frame has had a
//   chance to complete the load.
// The subframe navigations cause the loading_frames_in_progress_ to drop down
// to 0, while the loading_progresses_ map is not reset.
TEST_F(WebContentsImplTestWithSiteIsolation, StartStopEventsBalance) {}

// Tests that WebContentsImpl::ShouldShowLoadingUI only reports main
// frame loads. Browser-initiated navigation of subframes is only possible in
// --site-per-process mode within unit tests.
TEST_F(WebContentsImplTestWithSiteIsolation, ShouldShowLoadingUI) {}

// Ensure that WebContentsImpl does not stop loading too early when there still
// is a pending renderer. This can happen if a same-process non user-initiated
// navigation completes while there is an ongoing cross-process navigation.
// TODO(clamy): Rewrite that test when the renderer-initiated non-user-initiated
// navigation no longer kills the speculative RenderFrameHost. See
// https://crbug.com/889039.
TEST_F(WebContentsImplTest, DISABLED_NoEarlyStop) {}

TEST_F(WebContentsImplTest, MediaWakeLock) {}

// Test that the WebContentsObserver is notified when text is copied to the
// clipboard for a given RenderFrameHost.
TEST_F(WebContentsImplTest, OnTextCopiedToClipboard) {}

TEST_F(WebContentsImplTest, ThemeColorChangeDependingOnFirstVisiblePaint) {}

TEST_F(WebContentsImplTest, ParseDownloadHeaders) {}

// CHECKs should occur when `WebContentsImpl::DidLoadResourceFromMemoryCache()`
// is called with RequestDestinations that can only correspond to navigations.
TEST_F(WebContentsImplTest, DidLoadResourceFromMemoryCache_NavigationCheck) {}

// Regression test for crbug.com/347934841#comment3 to ensure that if
// `WebContentsImpl::DidLoadResourceFromMemoryCache()` is called with a
// `request_destination` parameter value of
// `network::mojom::RequestDestination::kObject` or
// `network::mojom::RequestDestination::kEmbed`, a CHECK does not occur since
// those can correspond to both navigations and resource loads.
TEST_F(WebContentsImplTest,
       DidLoadResourceFromMemoryCache_BypassNavigationCheck) {}

namespace {

class TestJavaScriptDialogManager : public JavaScriptDialogManager {};

}  // namespace

TEST_F(WebContentsImplTest, ResetJavaScriptDialogOnUserNavigate) {}

TEST_F(WebContentsImplTest, StartingSandboxFlags) {}

TEST_F(WebContentsImplTest, DidFirstVisuallyNonEmptyPaint) {}

TEST_F(WebContentsImplTest, DidChangeVerticalScrollDirection) {}

TEST_F(WebContentsImplTest, HandleContextMenuDelegate) {}

TEST_F(WebContentsImplTest, RegisterProtocolHandlerDifferentOrigin) {}

TEST_F(WebContentsImplTest, RegisterProtocolHandlerDataURL) {}

TEST_F(WebContentsImplTest, RegisterProtocolHandlerInvalidURLSyntax) {}

TEST_F(WebContentsImplTest, Bluetooth) {}

TEST_F(WebContentsImplTest, BadDownloadImageResponseFromRenderer) {}

TEST_F(WebContentsImplTest,
       GetCaptureHandleConfigBeforeSetIsCalledReturnsEmptyConfig) {}

TEST_F(WebContentsImplTest, SetAndGetCaptureHandleConfig) {}

TEST_F(WebContentsImplTest, NoOnCaptureHandleConfigUpdateCallIfResettingEmpty) {}

TEST_F(WebContentsImplTest,
       OnCaptureHandleConfigUpdateCalledWhenHandleChanges) {}

TEST_F(WebContentsImplTest,
       OnCaptureHandleConfigUpdateNotCalledWhenResettingAnIdenticalHandle) {}

TEST_F(WebContentsImplTest,
       OnCaptureHandleConfigUpdateCalledWhenClearingTheConfig) {}

TEST_F(WebContentsImplTest,
       CrossDocumentMainPageNavigationClearsCaptureHandleConfig) {}

TEST_F(WebContentsImplTest,
       SameDocumentMainPageNavigationDoesNotClearCaptureHandleConfig) {}

TEST_F(WebContentsImplTest,
       CrossDocumentChildPageNavigationDoesNotClearCaptureHandleConfig) {}

class TestCanonicalUrlLocalFrame : public content::FakeLocalFrame,
                                   public WebContentsObserver {};

TEST_F(WebContentsImplTest, CanonicalUrlSchemeHttpsIsAllowed) {}

TEST_F(WebContentsImplTest, CanonicalUrlSchemeChromeIsNotAllowed) {}

TEST_F(WebContentsImplTest, RequestMediaAccessPermissionNoDelegate) {}

TEST_F(WebContentsImplTest, IgnoreInputEvents) {}

TEST_F(WebContentsImplTest, OnColorProviderChangedTriggersPageBroadcast) {}

TEST_F(WebContentsImplTest, InvalidNetworkHandleAsDefault) {}

TEST_F(WebContentsImplTest, CreateWebContentsWithNetworkHandle) {}

TEST_F(WebContentsImplTest, CreateWebContentsWithOpenerAndNetworkHandle) {}

TEST_F(WebContentsImplTest, BadDownloadImageFromAXNodeId) {}

}  // namespace content