chromium/chrome/browser/guest_view/mime_handler_view/chrome_mime_handler_view_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.

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/guest_view/browser/guest_view_manager.h"
#include "components/guest_view/browser/guest_view_manager_delegate.h"
#include "components/guest_view/browser/test_guest_view_manager.h"
#include "components/javascript_dialogs/app_modal_dialog_controller.h"
#include "components/javascript_dialogs/app_modal_dialog_view.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/hit_test_region_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/update_user_activation_state_interceptor.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_stream_manager.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_attach_helper.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_constants.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view_guest.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/constants.h"
#include "extensions/common/mojom/guest_view.mojom.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_response_headers.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "printing/buildflags/buildflags.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/view.h"
#include "url/url_constants.h"

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#include "chrome/browser/printing/test_print_preview_observer.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
#endif

ExtensionsAPIClient;
MimeHandlerViewGuest;
TestMimeHandlerViewGuest;
GuestViewManager;
TestGuestViewManager;
TestGuestViewManagerFactory;

namespace {
// The value of the data is "content to read\n".
const char kDataUrlCsv[] =;
}  // namespace

class ChromeMimeHandlerViewTest : public extensions::ExtensionApiTest {};

namespace {

class UserActivationUpdateWaiter {};

// A DevToolsAgentHostClient implementation doing nothing.
class StubDevToolsAgentHostClient : public content::DevToolsAgentHostClient {};

}  // namespace

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, Embedded) {}

// This test start with an <object> that has a content frame. Then the content
// frame (plugin frame) is navigated to a cross-origin target page. After the
// navigation is completed, the <object> is set to render MimeHandlerView by
// setting its |data| and |type| attributes accordingly.
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       EmbedWithInitialCrossOriginFrame) {}

// This test verifies that navigations on the plugin frame before setting it to
// load MimeHandlerView do not race with the creation of the guest. The test
// loads a page with an <object> which is first navigated to some cross-origin
// domain and then immediately after load, the page triggers a navigation of its
// own to another cross-origin domain. Meanwhile the embedder sets the <object>
// to load a MimeHandlerView. The test passes if MHV loads. This is to catch the
// potential race between the cross-origin renderer initiated navigation and
// the navigation to "about:blank" started from the browser.
//
// Disabled on all platforms due to flakiness: https://crbug.com/1182355.
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       DISABLED_NavigationRaceFromEmbedder) {}

// TODO(ekaramad): Without proper handling of navigation to 'about:blank', this
// test would be flaky. Use TestNavigationManager class and possibly break the
// test into more sub-tests for various scenarios (https://crbug.com/659750).
// This test verifies that (almost) concurrent navigations in a cross-process
// frame inside an <embed> which is transitioning to a MimeHandlerView will
// not block creation of MimeHandlerView. The test will load some cross-origin
// content in <object> which right after loading will navigate it self to some
// other cross-origin content. On the embedder side, when the first page loads,
// the <object> loads some text/csv content to create a MimeHandlerViewGuest.
// The test passes if MHV loads.
// TODO(crbug.com/40751404): Disabled due to flakes.
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       DISABLED_NavigationRaceFromCrossProcessRenderer) {}

// This test verifies that removing embedder RenderFrame will not crash the
// renderer (for context see https://crbug.com/930803).
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, EmbedderFrameRemovedNoCrash) {}

// TODO(ekaramad): Somehow canceling a first dialog in a setup similar to the
// test below pops up another dialog. This is likely due to the navigation to
// about:blank from both the browser side and the embedder side in the method
// HTMLPlugInElement::RequestObjectInternal. Find out the issue and add another
// test here where the dialog is dismissed and the guest not created.
// (https://crbug.com/659750).
// This test verifies that transitioning a plugin element from text/html to
// application/pdf respects 'beforeunload'. The test specifically checks that
// 'beforeunload' dialog is shown to the user and if the user decides to
// proceed with the transition, MimeHandlerViewGuest is created.
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       EmbedWithInitialFrameAcceptBeforeUnloadDialog) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, PostMessage) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, Basic) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, Iframe) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, NonAsciiHeaders) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, DataUrl) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, EmbeddedDataUrlObject) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, EmbeddedDataUrlEmbed) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, EmbeddedDataUrlLong) {}

// Regression test for crbug.com/587709.
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, SingleRequest) {}

// Test that a mime handler view can keep a background page alive.
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, BackgroundPage) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, TargetBlankAnchor) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, BeforeUnload_NoDialog) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, BeforeUnload_ShowDialog) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       BeforeUnloadEnabled_WithoutUserActivation) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       BeforeUnloadEnabled_WithUserActivation) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       ActivatePostMessageSupportOnce) {}

// This is a minimized repro for a clusterfuzz crasher and is not really related
// to MimeHandlerView. The test verifies that when
// HTMLPlugInElement::PluginWrapper is called for a plugin with no node document
// frame, the renderer does not crash (see https://966371).
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       AdoptNodeInOnLoadDoesNotCrash) {}

// Verifies that sandboxed frames do not create GuestViews (plugins are
// blocked in sandboxed frames).
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, DoNotLoadInSandboxedFrame) {}

// Tests that a MimeHandlerViewGuest auto-rejects pointer lock requests.
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, RejectPointLock) {}

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       GuestDevToolsReloadsEmbedder) {}

// This test verifies that a display:none frame loading a MimeHandlerView type
// will end up creating a MimeHandlerview. NOTE: this is an exception to support
// printing in Google docs (see https://crbug.com/978240).
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest,
                       MimeHandlerViewInDisplayNoneFrameForGoogleApps) {}

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, EmbeddedThenPrint) {}
#endif

IN_PROC_BROWSER_TEST_F(ChromeMimeHandlerViewTest, FrameIterationBeforeAttach) {}