Chromium Code Reviews| Index: extensions/browser/guest_view/mime_handler_view/mime_handler_view_browsertest.cc |
| diff --git a/extensions/browser/guest_view/mime_handler_view/mime_handler_view_browsertest.cc b/extensions/browser/guest_view/mime_handler_view/mime_handler_view_browsertest.cc |
| index ec1c0ca9e86871e5f663330d45dbe4df1bdb69c1..52f51d2ea1e9164fab64247b1069c70ef35d0167 100644 |
| --- a/extensions/browser/guest_view/mime_handler_view/mime_handler_view_browsertest.cc |
| +++ b/extensions/browser/guest_view/mime_handler_view/mime_handler_view_browsertest.cc |
| @@ -4,10 +4,13 @@ |
| #include "base/base_paths.h" |
| #include "base/files/file_util.h" |
| +#include "base/macros.h" |
| #include "base/path_service.h" |
| +#include "base/run_loop.h" |
| #include "chrome/browser/extensions/extension_apitest.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| #include "components/guest_view/browser/test_guest_view_manager.h" |
| +#include "content/public/browser/browser_thread.h" |
| #include "content/public/test/browser_test_utils.h" |
| #include "extensions/browser/api/extensions_api_client.h" |
| #include "extensions/browser/extension_registry.h" |
| @@ -16,6 +19,8 @@ |
| #include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view_guest.h" |
| #include "extensions/test/result_catcher.h" |
| #include "net/test/embedded_test_server/embedded_test_server.h" |
| +#include "net/url_request/url_request_filter.h" |
| +#include "net/url_request/url_request_interceptor.h" |
| using extensions::ExtensionsAPIClient; |
| using extensions::MimeHandlerViewGuest; |
| @@ -28,6 +33,76 @@ using guest_view::TestGuestViewManagerFactory; |
| // The test extension id is set by the key value in the manifest. |
| const char* kExtensionId = "oickdpebdnfbgkcaoklfcdhjniefkcji"; |
| +// Counts the number of URL requests made for a given URL. |
| +class URLRequestCounter { |
| + public: |
| + URLRequestCounter(const GURL& url) : url_(url), count_(0) { |
|
mmenke
2016/08/30 14:51:12
explicit
raymes
2016/08/31 00:25:48
Done.
|
| + base::RunLoop run_loop; |
| + content::BrowserThread::PostTaskAndReply( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&URLRequestCounter::AddInterceptor, base::Unretained(this)), |
| + run_loop.QuitClosure()); |
| + run_loop.Run(); |
| + } |
| + |
| + ~URLRequestCounter() { |
| + base::RunLoop run_loop; |
| + content::BrowserThread::PostTaskAndReply( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&URLRequestCounter::RemoveInterceptor, |
| + base::Unretained(this)), |
| + run_loop.QuitClosure()); |
|
mmenke
2016/08/30 14:51:12
Should include bind.h and location.h (locations.h?
raymes
2016/08/31 00:25:48
Done.
|
| + run_loop.Run(); |
| + } |
| + |
| + int GetCount() { |
| + // Do a round-trip to the IO thread to guarantee that the UI thread has |
| + // been notified of all the requests triggered by the IO thread. |
| + base::RunLoop run_loop; |
| + content::BrowserThread::PostTaskAndReply( |
| + content::BrowserThread::IO, FROM_HERE, base::Bind(&base::DoNothing), |
| + run_loop.QuitClosure()); |
| + run_loop.Run(); |
| + return count_; |
| + } |
| + |
| + private: |
| + class SimpleRequestInterceptor : public net::URLRequestInterceptor { |
|
mmenke
2016/08/30 14:51:13
Suggest a short comment on what this does.
raymes
2016/08/31 00:25:48
Done.
|
| + public: |
| + SimpleRequestInterceptor(const base::Closure& callback) |
|
mmenke
2016/08/30 14:51:13
explicit
raymes
2016/08/31 00:25:48
Done.
|
| + : callback_(callback) {} |
| + |
| + // URLRequestInterceptor implementation: |
| + net::URLRequestJob* MaybeInterceptRequest( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const override { |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + callback_); |
| + return nullptr; |
| + } |
| + |
| + private: |
| + const base::Closure callback_; |
| + }; |
| + |
| + void RequestFired() { ++count_; } |
|
mmenke
2016/08/30 14:51:12
Suggest adding DCHECK_CURRENTLY_ONs to all methods
raymes
2016/08/31 00:25:48
Done.
|
| + |
| + void AddInterceptor() { |
| + net::URLRequestFilter::GetInstance()->AddUrlInterceptor( |
| + url_, base::MakeUnique<SimpleRequestInterceptor>(base::Bind( |
| + &URLRequestCounter::RequestFired, base::Unretained(this)))); |
| + } |
| + |
| + void RemoveInterceptor() { |
| + net::URLRequestFilter::GetInstance()->RemoveUrlHandler(url_); |
| + } |
| + |
| + const GURL url_; |
| + int count_; |
|
mmenke
2016/08/30 14:51:13
Should mention count is only used on the UI thread
raymes
2016/08/31 00:25:48
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(URLRequestCounter); |
| +}; |
| + |
| class MimeHandlerViewTest : public ExtensionApiTest { |
| public: |
| MimeHandlerViewTest() { |
| @@ -36,6 +111,14 @@ class MimeHandlerViewTest : public ExtensionApiTest { |
| ~MimeHandlerViewTest() override {} |
| + void SetUpOnMainThread() override { |
| + ExtensionApiTest::SetUpOnMainThread(); |
| + |
| + ASSERT_TRUE(StartEmbeddedTestServer()); |
| + embedded_test_server()->ServeFilesFromDirectory( |
| + test_data_dir_.AppendASCII("mime_handler_view")); |
| + } |
| + |
| // TODO(paulmeyer): This function is implemented over and over by the |
| // different GuestView test classes. It really needs to be refactored out to |
| // some kind of GuestViewTest base class. |
| @@ -84,10 +167,6 @@ class MimeHandlerViewTest : public ExtensionApiTest { |
| } |
| void RunTest(const std::string& path) { |
| - ASSERT_TRUE(StartEmbeddedTestServer()); |
| - embedded_test_server()->ServeFilesFromDirectory( |
| - test_data_dir_.AppendASCII("mime_handler_view")); |
| - |
| RunTestWithUrl(embedded_test_server()->GetURL("/" + path)); |
| } |
| @@ -156,3 +235,11 @@ IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, ResizeBeforeAttach) { |
| CHECK_EQ(guest_size.width(), 500); |
| CHECK_EQ(guest_size.height(), 400); |
| } |
| + |
| +// Regression test for crbug.com/587709. |
| +IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, SingleRequest) { |
| + GURL url(embedded_test_server()->GetURL("/testBasic.csv")); |
| + URLRequestCounter request_counter(url); |
| + RunTest("testBasic.csv"); |
| + EXPECT_EQ(1, request_counter.GetCount()); |
| +} |