| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/base_paths.h" | 5 #include "base/base_paths.h" |
| 6 #include "base/bind.h" |
| 6 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/location.h" |
| 9 #include "base/macros.h" |
| 7 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/run_loop.h" |
| 8 #include "chrome/browser/extensions/extension_apitest.h" | 12 #include "chrome/browser/extensions/extension_apitest.h" |
| 9 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
| 10 #include "components/guest_view/browser/test_guest_view_manager.h" | 14 #include "components/guest_view/browser/test_guest_view_manager.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/test/browser_test_utils.h" | 16 #include "content/public/test/browser_test_utils.h" |
| 12 #include "extensions/browser/api/extensions_api_client.h" | 17 #include "extensions/browser/api/extensions_api_client.h" |
| 13 #include "extensions/browser/extension_registry.h" | 18 #include "extensions/browser/extension_registry.h" |
| 14 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h
" | 19 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h
" |
| 15 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues
t.h" | 20 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues
t.h" |
| 16 #include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view
_guest.h" | 21 #include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view
_guest.h" |
| 17 #include "extensions/test/result_catcher.h" | 22 #include "extensions/test/result_catcher.h" |
| 18 #include "net/test/embedded_test_server/embedded_test_server.h" | 23 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 24 #include "net/url_request/url_request_filter.h" |
| 25 #include "net/url_request/url_request_interceptor.h" |
| 19 | 26 |
| 20 using extensions::ExtensionsAPIClient; | 27 using extensions::ExtensionsAPIClient; |
| 21 using extensions::MimeHandlerViewGuest; | 28 using extensions::MimeHandlerViewGuest; |
| 22 using extensions::TestMimeHandlerViewGuest; | 29 using extensions::TestMimeHandlerViewGuest; |
| 23 using guest_view::GuestViewManager; | 30 using guest_view::GuestViewManager; |
| 24 using guest_view::GuestViewManagerDelegate; | 31 using guest_view::GuestViewManagerDelegate; |
| 25 using guest_view::TestGuestViewManager; | 32 using guest_view::TestGuestViewManager; |
| 26 using guest_view::TestGuestViewManagerFactory; | 33 using guest_view::TestGuestViewManagerFactory; |
| 27 | 34 |
| 28 // The test extension id is set by the key value in the manifest. | 35 // The test extension id is set by the key value in the manifest. |
| 29 const char* kExtensionId = "oickdpebdnfbgkcaoklfcdhjniefkcji"; | 36 const char* kExtensionId = "oickdpebdnfbgkcaoklfcdhjniefkcji"; |
| 30 | 37 |
| 38 // Counts the number of URL requests made for a given URL. |
| 39 class URLRequestCounter { |
| 40 public: |
| 41 explicit URLRequestCounter(const GURL& url) : url_(url), count_(0) { |
| 42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 43 base::RunLoop run_loop; |
| 44 content::BrowserThread::PostTaskAndReply( |
| 45 content::BrowserThread::IO, FROM_HERE, |
| 46 base::Bind(&URLRequestCounter::AddInterceptor, base::Unretained(this)), |
| 47 run_loop.QuitClosure()); |
| 48 run_loop.Run(); |
| 49 } |
| 50 |
| 51 ~URLRequestCounter() { |
| 52 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 53 base::RunLoop run_loop; |
| 54 content::BrowserThread::PostTaskAndReply( |
| 55 content::BrowserThread::IO, FROM_HERE, |
| 56 base::Bind(&URLRequestCounter::RemoveInterceptor, |
| 57 base::Unretained(this)), |
| 58 run_loop.QuitClosure()); |
| 59 run_loop.Run(); |
| 60 } |
| 61 |
| 62 int GetCount() { |
| 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 64 // Do a round-trip to the IO thread to guarantee that the UI thread has |
| 65 // been notified of all the requests triggered by the IO thread. |
| 66 base::RunLoop run_loop; |
| 67 content::BrowserThread::PostTaskAndReply( |
| 68 content::BrowserThread::IO, FROM_HERE, base::Bind(&base::DoNothing), |
| 69 run_loop.QuitClosure()); |
| 70 run_loop.Run(); |
| 71 return count_; |
| 72 } |
| 73 |
| 74 private: |
| 75 // This class runs a callback when a URL request is intercepted. It doesn't |
| 76 // handle the request itself. |
| 77 class SimpleRequestInterceptor : public net::URLRequestInterceptor { |
| 78 public: |
| 79 explicit SimpleRequestInterceptor(const base::Closure& callback) |
| 80 : callback_(callback) {} |
| 81 |
| 82 // URLRequestInterceptor implementation: |
| 83 net::URLRequestJob* MaybeInterceptRequest( |
| 84 net::URLRequest* request, |
| 85 net::NetworkDelegate* network_delegate) const override { |
| 86 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 87 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 88 callback_); |
| 89 return nullptr; |
| 90 } |
| 91 |
| 92 private: |
| 93 const base::Closure callback_; |
| 94 }; |
| 95 |
| 96 void RequestFired() { |
| 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 98 ++count_; |
| 99 } |
| 100 |
| 101 void AddInterceptor() { |
| 102 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 103 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( |
| 104 url_, base::MakeUnique<SimpleRequestInterceptor>(base::Bind( |
| 105 &URLRequestCounter::RequestFired, base::Unretained(this)))); |
| 106 } |
| 107 |
| 108 void RemoveInterceptor() { |
| 109 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 110 net::URLRequestFilter::GetInstance()->RemoveUrlHandler(url_); |
| 111 } |
| 112 |
| 113 const GURL url_; |
| 114 // |count_| is only accessed on the UI thread. |
| 115 int count_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(URLRequestCounter); |
| 118 }; |
| 119 |
| 31 class MimeHandlerViewTest : public ExtensionApiTest { | 120 class MimeHandlerViewTest : public ExtensionApiTest { |
| 32 public: | 121 public: |
| 33 MimeHandlerViewTest() { | 122 MimeHandlerViewTest() { |
| 34 GuestViewManager::set_factory_for_testing(&factory_); | 123 GuestViewManager::set_factory_for_testing(&factory_); |
| 35 } | 124 } |
| 36 | 125 |
| 37 ~MimeHandlerViewTest() override {} | 126 ~MimeHandlerViewTest() override {} |
| 38 | 127 |
| 128 void SetUpOnMainThread() override { |
| 129 ExtensionApiTest::SetUpOnMainThread(); |
| 130 |
| 131 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 132 embedded_test_server()->ServeFilesFromDirectory( |
| 133 test_data_dir_.AppendASCII("mime_handler_view")); |
| 134 } |
| 135 |
| 39 // TODO(paulmeyer): This function is implemented over and over by the | 136 // TODO(paulmeyer): This function is implemented over and over by the |
| 40 // different GuestView test classes. It really needs to be refactored out to | 137 // different GuestView test classes. It really needs to be refactored out to |
| 41 // some kind of GuestViewTest base class. | 138 // some kind of GuestViewTest base class. |
| 42 TestGuestViewManager* GetGuestViewManager() { | 139 TestGuestViewManager* GetGuestViewManager() { |
| 43 TestGuestViewManager* manager = static_cast<TestGuestViewManager*>( | 140 TestGuestViewManager* manager = static_cast<TestGuestViewManager*>( |
| 44 TestGuestViewManager::FromBrowserContext(browser()->profile())); | 141 TestGuestViewManager::FromBrowserContext(browser()->profile())); |
| 45 // TestGuestViewManager::WaitForSingleGuestCreated can and will get called | 142 // TestGuestViewManager::WaitForSingleGuestCreated can and will get called |
| 46 // before a guest is created. Since GuestViewManager is usually not created | 143 // before a guest is created. Since GuestViewManager is usually not created |
| 47 // until the first guest is created, this means that |manager| will be | 144 // 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 | 145 // nullptr if trying to use the manager to wait for the first guest. Because |
| (...skipping 28 matching lines...) Expand all Loading... |
| 77 ASSERT_TRUE(extension); | 174 ASSERT_TRUE(extension); |
| 78 | 175 |
| 79 extensions::ResultCatcher catcher; | 176 extensions::ResultCatcher catcher; |
| 80 ui_test_utils::NavigateToURL(browser(), url); | 177 ui_test_utils::NavigateToURL(browser(), url); |
| 81 | 178 |
| 82 if (!catcher.GetNextResult()) | 179 if (!catcher.GetNextResult()) |
| 83 FAIL() << catcher.message(); | 180 FAIL() << catcher.message(); |
| 84 } | 181 } |
| 85 | 182 |
| 86 void RunTest(const std::string& path) { | 183 void RunTest(const std::string& path) { |
| 87 ASSERT_TRUE(StartEmbeddedTestServer()); | |
| 88 embedded_test_server()->ServeFilesFromDirectory( | |
| 89 test_data_dir_.AppendASCII("mime_handler_view")); | |
| 90 | |
| 91 RunTestWithUrl(embedded_test_server()->GetURL("/" + path)); | 184 RunTestWithUrl(embedded_test_server()->GetURL("/" + path)); |
| 92 } | 185 } |
| 93 | 186 |
| 94 private: | 187 private: |
| 95 TestGuestViewManagerFactory factory_; | 188 TestGuestViewManagerFactory factory_; |
| 96 }; | 189 }; |
| 97 | 190 |
| 98 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, PostMessage) { | 191 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, PostMessage) { |
| 99 RunTest("test_postmessage.html"); | 192 RunTest("test_postmessage.html"); |
| 100 } | 193 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 GetGuestViewManager()->WaitForSingleGuestCreated(); | 242 GetGuestViewManager()->WaitForSingleGuestCreated(); |
| 150 TestMimeHandlerViewGuest* guest = static_cast<TestMimeHandlerViewGuest*>( | 243 TestMimeHandlerViewGuest* guest = static_cast<TestMimeHandlerViewGuest*>( |
| 151 MimeHandlerViewGuest::FromWebContents(guest_web_contents)); | 244 MimeHandlerViewGuest::FromWebContents(guest_web_contents)); |
| 152 guest->WaitForGuestAttached(); | 245 guest->WaitForGuestAttached(); |
| 153 | 246 |
| 154 // Ensure that the guest has the correct size after it has attached. | 247 // Ensure that the guest has the correct size after it has attached. |
| 155 auto guest_size = guest->size(); | 248 auto guest_size = guest->size(); |
| 156 CHECK_EQ(guest_size.width(), 500); | 249 CHECK_EQ(guest_size.width(), 500); |
| 157 CHECK_EQ(guest_size.height(), 400); | 250 CHECK_EQ(guest_size.height(), 400); |
| 158 } | 251 } |
| 252 |
| 253 // Regression test for crbug.com/587709. |
| 254 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, SingleRequest) { |
| 255 GURL url(embedded_test_server()->GetURL("/testBasic.csv")); |
| 256 URLRequestCounter request_counter(url); |
| 257 RunTest("testBasic.csv"); |
| 258 EXPECT_EQ(1, request_counter.GetCount()); |
| 259 } |
| OLD | NEW |