Chromium Code Reviews| 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/files/file_util.h" | 6 #include "base/files/file_util.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/run_loop.h" | |
| 8 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 9 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
| 10 #include "components/guest_view/browser/test_guest_view_manager.h" | 11 #include "components/guest_view/browser/test_guest_view_manager.h" |
| 12 #include "content/public/browser/browser_thread.h" | |
| 11 #include "content/public/test/browser_test_utils.h" | 13 #include "content/public/test/browser_test_utils.h" |
| 12 #include "extensions/browser/api/extensions_api_client.h" | 14 #include "extensions/browser/api/extensions_api_client.h" |
| 13 #include "extensions/browser/extension_registry.h" | 15 #include "extensions/browser/extension_registry.h" |
| 14 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h " | 16 #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" | 17 #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" | 18 #include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view _guest.h" |
| 17 #include "extensions/test/result_catcher.h" | 19 #include "extensions/test/result_catcher.h" |
| 18 #include "net/test/embedded_test_server/embedded_test_server.h" | 20 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 21 #include "net/url_request/url_request_filter.h" | |
| 22 #include "net/url_request/url_request_interceptor.h" | |
| 19 | 23 |
| 20 using extensions::ExtensionsAPIClient; | 24 using extensions::ExtensionsAPIClient; |
| 21 using extensions::MimeHandlerViewGuest; | 25 using extensions::MimeHandlerViewGuest; |
| 22 using extensions::TestMimeHandlerViewGuest; | 26 using extensions::TestMimeHandlerViewGuest; |
| 23 using guest_view::GuestViewManager; | 27 using guest_view::GuestViewManager; |
| 24 using guest_view::GuestViewManagerDelegate; | 28 using guest_view::GuestViewManagerDelegate; |
| 25 using guest_view::TestGuestViewManager; | 29 using guest_view::TestGuestViewManager; |
| 26 using guest_view::TestGuestViewManagerFactory; | 30 using guest_view::TestGuestViewManagerFactory; |
| 27 | 31 |
| 28 // The test extension id is set by the key value in the manifest. | 32 // The test extension id is set by the key value in the manifest. |
| 29 const char* kExtensionId = "oickdpebdnfbgkcaoklfcdhjniefkcji"; | 33 const char* kExtensionId = "oickdpebdnfbgkcaoklfcdhjniefkcji"; |
| 30 | 34 |
| 35 // Counts the number of URL requests made for a given URL. | |
| 36 class URLRequestCounter { | |
| 37 public: | |
| 38 URLRequestCounter(const GURL& url) : url_(url), count_(0) { | |
| 39 base::RunLoop run_loop; | |
| 40 content::BrowserThread::PostTaskAndReply( | |
| 41 content::BrowserThread::IO, FROM_HERE, | |
| 42 base::Bind(&URLRequestCounter::AddInterceptor, base::Unretained(this)), | |
| 43 run_loop.QuitClosure()); | |
| 44 run_loop.Run(); | |
| 45 } | |
| 46 | |
| 47 ~URLRequestCounter() { | |
| 48 base::RunLoop run_loop; | |
| 49 content::BrowserThread::PostTaskAndReply( | |
| 50 content::BrowserThread::IO, FROM_HERE, | |
| 51 base::Bind(&URLRequestCounter::RemoveInterceptor, | |
| 52 base::Unretained(this)), | |
| 53 run_loop.QuitClosure()); | |
| 54 run_loop.Run(); | |
| 55 } | |
| 56 | |
| 57 int count() { return count_; } | |
| 58 | |
| 59 private: | |
| 60 class SimpleRequestInterceptor : public net::URLRequestInterceptor { | |
| 61 public: | |
| 62 SimpleRequestInterceptor(int* count) : count_(count) {} | |
| 63 | |
| 64 // URLRequestInterceptor implementation: | |
| 65 net::URLRequestJob* MaybeInterceptRequest( | |
| 66 net::URLRequest* request, | |
| 67 net::NetworkDelegate* network_delegate) const override { | |
| 68 (*count_)++; | |
| 69 return nullptr; | |
| 70 } | |
| 71 | |
| 72 int* count_; | |
|
Sam McNally
2016/08/30 03:04:28
private:
raymes
2016/08/30 07:32:54
Done.
| |
| 73 }; | |
| 74 | |
| 75 void AddInterceptor() { | |
| 76 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( | |
| 77 url_, base::WrapUnique(new SimpleRequestInterceptor(&count_))); | |
|
Sam McNally
2016/08/30 03:04:28
base::MakeUnique<SimpleRequestInterceptor>(&count_
raymes
2016/08/30 07:32:54
Done.
| |
| 78 } | |
| 79 | |
| 80 void RemoveInterceptor() { | |
| 81 net::URLRequestFilter::GetInstance()->RemoveUrlHandler(url_); | |
| 82 } | |
| 83 | |
| 84 GURL url_; | |
|
Sam McNally
2016/08/30 03:04:28
const
raymes
2016/08/30 07:32:54
Done.
| |
| 85 int count_; | |
|
Sam McNally
2016/08/30 03:04:28
You're reading and writing |count_| on different t
raymes
2016/08/30 07:32:54
Done - shifted the code around as discussed.
| |
| 86 }; | |
|
Sam McNally
2016/08/30 03:04:28
DISALLOW_COPY_AND_ASSIGN
raymes
2016/08/30 07:32:54
Done.
| |
| 87 | |
| 31 class MimeHandlerViewTest : public ExtensionApiTest { | 88 class MimeHandlerViewTest : public ExtensionApiTest { |
| 32 public: | 89 public: |
| 33 MimeHandlerViewTest() { | 90 MimeHandlerViewTest() { |
| 34 GuestViewManager::set_factory_for_testing(&factory_); | 91 GuestViewManager::set_factory_for_testing(&factory_); |
| 35 } | 92 } |
| 36 | 93 |
| 37 ~MimeHandlerViewTest() override {} | 94 ~MimeHandlerViewTest() override {} |
| 38 | 95 |
| 96 void SetUpOnMainThread() override { | |
| 97 ExtensionApiTest::SetUpOnMainThread(); | |
| 98 | |
| 99 StartEmbeddedTestServer(); | |
|
Sam McNally
2016/08/30 03:04:28
No ASSERT_TRUE()?
raymes
2016/08/30 07:32:54
Done.
| |
| 100 embedded_test_server()->ServeFilesFromDirectory( | |
| 101 test_data_dir_.AppendASCII("mime_handler_view")); | |
| 102 } | |
| 103 | |
| 39 // TODO(paulmeyer): This function is implemented over and over by the | 104 // TODO(paulmeyer): This function is implemented over and over by the |
| 40 // different GuestView test classes. It really needs to be refactored out to | 105 // different GuestView test classes. It really needs to be refactored out to |
| 41 // some kind of GuestViewTest base class. | 106 // some kind of GuestViewTest base class. |
| 42 TestGuestViewManager* GetGuestViewManager() { | 107 TestGuestViewManager* GetGuestViewManager() { |
| 43 TestGuestViewManager* manager = static_cast<TestGuestViewManager*>( | 108 TestGuestViewManager* manager = static_cast<TestGuestViewManager*>( |
| 44 TestGuestViewManager::FromBrowserContext(browser()->profile())); | 109 TestGuestViewManager::FromBrowserContext(browser()->profile())); |
| 45 // TestGuestViewManager::WaitForSingleGuestCreated can and will get called | 110 // TestGuestViewManager::WaitForSingleGuestCreated can and will get called |
| 46 // before a guest is created. Since GuestViewManager is usually not created | 111 // before a guest is created. Since GuestViewManager is usually not created |
| 47 // until the first guest is created, this means that |manager| will be | 112 // 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 | 113 // 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); | 142 ASSERT_TRUE(extension); |
| 78 | 143 |
| 79 extensions::ResultCatcher catcher; | 144 extensions::ResultCatcher catcher; |
| 80 ui_test_utils::NavigateToURL(browser(), url); | 145 ui_test_utils::NavigateToURL(browser(), url); |
| 81 | 146 |
| 82 if (!catcher.GetNextResult()) | 147 if (!catcher.GetNextResult()) |
| 83 FAIL() << catcher.message(); | 148 FAIL() << catcher.message(); |
| 84 } | 149 } |
| 85 | 150 |
| 86 void RunTest(const std::string& path) { | 151 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)); | 152 RunTestWithUrl(embedded_test_server()->GetURL("/" + path)); |
| 92 } | 153 } |
| 93 | 154 |
| 94 private: | 155 private: |
| 95 TestGuestViewManagerFactory factory_; | 156 TestGuestViewManagerFactory factory_; |
| 96 }; | 157 }; |
| 97 | 158 |
| 98 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, PostMessage) { | 159 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, PostMessage) { |
| 99 RunTest("test_postmessage.html"); | 160 RunTest("test_postmessage.html"); |
| 100 } | 161 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 GetGuestViewManager()->WaitForSingleGuestCreated(); | 210 GetGuestViewManager()->WaitForSingleGuestCreated(); |
| 150 TestMimeHandlerViewGuest* guest = static_cast<TestMimeHandlerViewGuest*>( | 211 TestMimeHandlerViewGuest* guest = static_cast<TestMimeHandlerViewGuest*>( |
| 151 MimeHandlerViewGuest::FromWebContents(guest_web_contents)); | 212 MimeHandlerViewGuest::FromWebContents(guest_web_contents)); |
| 152 guest->WaitForGuestAttached(); | 213 guest->WaitForGuestAttached(); |
| 153 | 214 |
| 154 // Ensure that the guest has the correct size after it has attached. | 215 // Ensure that the guest has the correct size after it has attached. |
| 155 auto guest_size = guest->size(); | 216 auto guest_size = guest->size(); |
| 156 CHECK_EQ(guest_size.width(), 500); | 217 CHECK_EQ(guest_size.width(), 500); |
| 157 CHECK_EQ(guest_size.height(), 400); | 218 CHECK_EQ(guest_size.height(), 400); |
| 158 } | 219 } |
| 220 | |
| 221 // Regression test for crbug.com/587709. | |
| 222 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, SingleRequest) { | |
| 223 GURL url(embedded_test_server()->GetURL("/testBasic.csv")); | |
| 224 URLRequestCounter request_counter(url); | |
| 225 RunTest("testBasic.csv"); | |
| 226 EXPECT_EQ(1, request_counter.count()); | |
| 227 } | |
| OLD | NEW |