Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / guest_view / mime_handler_view / mime_handler_view_browsertest.cc
blobec1c0ca9e86871e5f663330d45dbe4df1bdb69c1
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/base_paths.h"
6 #include "base/files/file_util.h"
7 #include "base/path_service.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/test/base/ui_test_utils.h"
10 #include "components/guest_view/browser/test_guest_view_manager.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "extensions/browser/api/extensions_api_client.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h"
15 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
16 #include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view_guest.h"
17 #include "extensions/test/result_catcher.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
20 using extensions::ExtensionsAPIClient;
21 using extensions::MimeHandlerViewGuest;
22 using extensions::TestMimeHandlerViewGuest;
23 using guest_view::GuestViewManager;
24 using guest_view::GuestViewManagerDelegate;
25 using guest_view::TestGuestViewManager;
26 using guest_view::TestGuestViewManagerFactory;
28 // The test extension id is set by the key value in the manifest.
29 const char* kExtensionId = "oickdpebdnfbgkcaoklfcdhjniefkcji";
31 class MimeHandlerViewTest : public ExtensionApiTest {
32 public:
33 MimeHandlerViewTest() {
34 GuestViewManager::set_factory_for_testing(&factory_);
37 ~MimeHandlerViewTest() override {}
39 // TODO(paulmeyer): This function is implemented over and over by the
40 // different GuestView test classes. It really needs to be refactored out to
41 // some kind of GuestViewTest base class.
42 TestGuestViewManager* GetGuestViewManager() {
43 TestGuestViewManager* manager = static_cast<TestGuestViewManager*>(
44 TestGuestViewManager::FromBrowserContext(browser()->profile()));
45 // TestGuestViewManager::WaitForSingleGuestCreated can and will get called
46 // before a guest is created. Since GuestViewManager is usually not created
47 // until the first guest is created, this means that |manager| will be
48 // nullptr if trying to use the manager to wait for the first guest. Because
49 // of this, the manager must be created here if it does not already exist.
50 if (!manager) {
51 manager = static_cast<TestGuestViewManager*>(
52 GuestViewManager::CreateWithDelegate(
53 browser()->profile(),
54 ExtensionsAPIClient::Get()->CreateGuestViewManagerDelegate(
55 browser()->profile())));
57 return manager;
60 const extensions::Extension* LoadTestExtension() {
61 const extensions::Extension* extension =
62 LoadExtension(test_data_dir_.AppendASCII("mime_handler_view"));
63 if (!extension)
64 return nullptr;
66 CHECK_EQ(std::string(kExtensionId), extension->id());
68 return extension;
71 void RunTestWithUrl(const GURL& url) {
72 // Use the testing subclass of MimeHandlerViewGuest.
73 GetGuestViewManager()->RegisterTestGuestViewType<MimeHandlerViewGuest>(
74 base::Bind(&TestMimeHandlerViewGuest::Create));
76 const extensions::Extension* extension = LoadTestExtension();
77 ASSERT_TRUE(extension);
79 extensions::ResultCatcher catcher;
80 ui_test_utils::NavigateToURL(browser(), url);
82 if (!catcher.GetNextResult())
83 FAIL() << catcher.message();
86 void RunTest(const std::string& path) {
87 ASSERT_TRUE(StartEmbeddedTestServer());
88 embedded_test_server()->ServeFilesFromDirectory(
89 test_data_dir_.AppendASCII("mime_handler_view"));
91 RunTestWithUrl(embedded_test_server()->GetURL("/" + path));
94 private:
95 TestGuestViewManagerFactory factory_;
98 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, PostMessage) {
99 RunTest("test_postmessage.html");
102 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, Basic) {
103 RunTest("testBasic.csv");
106 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, Embedded) {
107 RunTest("test_embedded.html");
110 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, Iframe) {
111 RunTest("test_iframe.html");
114 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, Abort) {
115 RunTest("testAbort.csv");
118 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, NonAsciiHeaders) {
119 RunTest("testNonAsciiHeaders.csv");
122 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, DataUrl) {
123 const char* kDataUrlCsv = "data:text/csv;base64,Y29udGVudCB0byByZWFkCg==";
124 RunTestWithUrl(GURL(kDataUrlCsv));
127 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, EmbeddedDataUrlObject) {
128 RunTest("test_embedded_data_url_object.html");
131 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, EmbeddedDataUrlEmbed) {
132 RunTest("test_embedded_data_url_embed.html");
135 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, EmbeddedDataUrlLong) {
136 RunTest("test_embedded_data_url_long.html");
139 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, ResizeBeforeAttach) {
140 // Delay the creation of the guest's WebContents in order to delay the guest's
141 // attachment to the embedder. This will allow us to resize the <object> tag
142 // after the guest is created, but before it is attached in
143 // "test_resize_before_attach.html".
144 TestMimeHandlerViewGuest::DelayNextCreateWebContents(500);
145 RunTest("test_resize_before_attach.html");
147 // Wait for the guest to attach.
148 content::WebContents* guest_web_contents =
149 GetGuestViewManager()->WaitForSingleGuestCreated();
150 TestMimeHandlerViewGuest* guest = static_cast<TestMimeHandlerViewGuest*>(
151 MimeHandlerViewGuest::FromWebContents(guest_web_contents));
152 guest->WaitForGuestAttached();
154 // Ensure that the guest has the correct size after it has attached.
155 auto guest_size = guest->size();
156 CHECK_EQ(guest_size.width(), 500);
157 CHECK_EQ(guest_size.height(), 400);