blob: 21c282cdd6dc197ee0539a59ed92914d766755fb [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A collection of functions designed for use with content_shell based browser
// tests internal to the content/ module.
// Note: If a function here also works with browser_tests, it should be in
// the content public API.
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "cc/surfaces/surface_id.h"
#include "content/public/browser/resource_dispatcher_host_delegate.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/test/browser_test_utils.h"
#include "url/gurl.h"
namespace cc {
class SurfaceManager;
namespace content {
class FrameTreeNode;
class MessageLoopRunner;
class RenderFrameHost;
class RenderWidgetHostViewChildFrame;
class Shell;
class SiteInstance;
class ToRenderFrameHost;
// Navigates the frame represented by |node| to |url|, blocking until the
// navigation finishes.
void NavigateFrameToURL(FrameTreeNode* node, const GURL& url);
// Sets the DialogManager to proceed by default or not when showing a
// BeforeUnload dialog.
void SetShouldProceedOnBeforeUnload(Shell* shell, bool proceed);
// Extends the ToRenderFrameHost mechanism to FrameTreeNodes.
RenderFrameHost* ConvertToRenderFrameHost(FrameTreeNode* frame_tree_node);
// Creates compact textual representations of the state of the frame tree that
// is appropriate for use in assertions.
// The diagrams show frame tree structure, the SiteInstance of current frames,
// presence of pending frames, and the SiteInstances of any and all proxies.
// They look like this:
// Site A (D pending) -- proxies for B C
// |--Site B --------- proxies for A C
// +--Site C --------- proxies for B A
// |--Site A ---- proxies for B
// +--Site A ---- proxies for B
// +--Site A -- proxies for B
// Where A =
// B = (no process)
// C =
// D =
// SiteInstances are assigned single-letter names (A, B, C) which are remembered
// across invocations of the pretty-printer.
class FrameTreeVisualizer {
// Formats and returns a diagram for the provided FrameTreeNode.
std::string DepictFrameTree(FrameTreeNode* root);
// Assign or retrive the abbreviated short name (A, B, C) for a site instance.
std::string GetName(SiteInstance* site_instance);
// Elements are site instance ids. The index of the SiteInstance in the vector
// determines the abbreviated name (0->A, 1->B) for that SiteInstance.
std::vector<int> seen_site_instance_ids_;
// Uses to open a popup from the frame |opener| with the specified
// |url| and |name|. Waits for the navigation to |url| to finish and then
// returns the new popup's Shell. Note that since this navigation to |url| is
// renderer-initiated, it won't cause a process swap unless used in
// --site-per-process mode.
Shell* OpenPopup(const ToRenderFrameHost& opener,
const GURL& url,
const std::string& name);
// This class can be used to stall any resource request, based on an URL match.
// There is no explicit way to resume the request; it should be used carefully.
// Note: This class likely doesn't work with PlzNavigate.
// TODO(nasko): Reimplement this class using NavigationThrottle, once it has
// the ability to defer navigation requests.
class NavigationStallDelegate : public ResourceDispatcherHostDelegate {
explicit NavigationStallDelegate(const GURL& url);
// ResourceDispatcherHostDelegate
void RequestBeginning(
net::URLRequest* request,
content::ResourceContext* resource_context,
content::AppCacheService* appcache_service,
ResourceType resource_type,
ScopedVector<content::ResourceThrottle>* throttles) override;
GURL url_;
// Helper class to assist with hit testing surfaces in multiple processes.
// WaitForSurfaceReady() will only return after a Surface from |target_view|
// has been composited in the top-level frame's Surface. At that point,
// browser process hit testing to target_view's Surface can succeed.
class SurfaceHitTestReadyNotifier {
SurfaceHitTestReadyNotifier(RenderWidgetHostViewChildFrame* target_view);
~SurfaceHitTestReadyNotifier() {}
void WaitForSurfaceReady();
bool ContainsSurfaceId(cc::SurfaceId container_surface_id);
cc::SurfaceManager* surface_manager_;
cc::SurfaceId root_surface_id_;
RenderWidgetHostViewChildFrame* target_view_;
// Helper for mocking choosing a file via a file dialog.
class FileChooserDelegate : public WebContentsDelegate {
// Constructs a WebContentsDelegate that mocks a file dialog.
// The mocked file dialog will always reply that the user selected |file|.
FileChooserDelegate(const base::FilePath& file);
// Implementation of WebContentsDelegate::RunFileChooser.
void RunFileChooser(RenderFrameHost* render_frame_host,
const FileChooserParams& params) override;
// Whether the file dialog was shown.
bool file_chosen() { return file_chosen_; }
base::FilePath file_;
bool file_chosen_;
// This class is a TestNavigationManager that only monitors notifications within
// the given frame tree node.
class FrameTestNavigationManager : public TestNavigationManager {
FrameTestNavigationManager(int frame_tree_node_id,
WebContents* web_contents,
const GURL& url);
// TestNavigationManager:
bool ShouldMonitorNavigation(NavigationHandle* handle) override;
// Notifications are filtered so only this frame is monitored.
int filtering_frame_tree_node_id_;
} // namespace content