Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(401)

Side by Side Diff: extensions/browser/guest_view/mime_handler_view/mime_handler_view_browsertest.cc

Issue 2287213002: Ensure loading PDFs doesn't generate 2 http requests (Closed)
Patch Set: Ensure loading PDFs doesn't generate 2 http requests Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | extensions/renderer/guest_view/mime_handler_view/mime_handler_view_container.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/macros.h"
7 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "base/run_loop.h"
8 #include "chrome/browser/extensions/extension_apitest.h" 10 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/test/base/ui_test_utils.h" 11 #include "chrome/test/base/ui_test_utils.h"
10 #include "components/guest_view/browser/test_guest_view_manager.h" 12 #include "components/guest_view/browser/test_guest_view_manager.h"
13 #include "content/public/browser/browser_thread.h"
11 #include "content/public/test/browser_test_utils.h" 14 #include "content/public/test/browser_test_utils.h"
12 #include "extensions/browser/api/extensions_api_client.h" 15 #include "extensions/browser/api/extensions_api_client.h"
13 #include "extensions/browser/extension_registry.h" 16 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h " 17 #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" 18 #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" 19 #include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view _guest.h"
17 #include "extensions/test/result_catcher.h" 20 #include "extensions/test/result_catcher.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h" 21 #include "net/test/embedded_test_server/embedded_test_server.h"
22 #include "net/url_request/url_request_filter.h"
23 #include "net/url_request/url_request_interceptor.h"
19 24
20 using extensions::ExtensionsAPIClient; 25 using extensions::ExtensionsAPIClient;
21 using extensions::MimeHandlerViewGuest; 26 using extensions::MimeHandlerViewGuest;
22 using extensions::TestMimeHandlerViewGuest; 27 using extensions::TestMimeHandlerViewGuest;
23 using guest_view::GuestViewManager; 28 using guest_view::GuestViewManager;
24 using guest_view::GuestViewManagerDelegate; 29 using guest_view::GuestViewManagerDelegate;
25 using guest_view::TestGuestViewManager; 30 using guest_view::TestGuestViewManager;
26 using guest_view::TestGuestViewManagerFactory; 31 using guest_view::TestGuestViewManagerFactory;
27 32
28 // The test extension id is set by the key value in the manifest. 33 // The test extension id is set by the key value in the manifest.
29 const char* kExtensionId = "oickdpebdnfbgkcaoklfcdhjniefkcji"; 34 const char* kExtensionId = "oickdpebdnfbgkcaoklfcdhjniefkcji";
30 35
36 // Counts the number of URL requests made for a given URL.
37 class URLRequestCounter {
38 public:
39 URLRequestCounter(const GURL& url) : url_(url), count_(0) {
mmenke 2016/08/30 14:51:12 explicit
raymes 2016/08/31 00:25:48 Done.
40 base::RunLoop run_loop;
41 content::BrowserThread::PostTaskAndReply(
42 content::BrowserThread::IO, FROM_HERE,
43 base::Bind(&URLRequestCounter::AddInterceptor, base::Unretained(this)),
44 run_loop.QuitClosure());
45 run_loop.Run();
46 }
47
48 ~URLRequestCounter() {
49 base::RunLoop run_loop;
50 content::BrowserThread::PostTaskAndReply(
51 content::BrowserThread::IO, FROM_HERE,
52 base::Bind(&URLRequestCounter::RemoveInterceptor,
53 base::Unretained(this)),
54 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.
55 run_loop.Run();
56 }
57
58 int GetCount() {
59 // Do a round-trip to the IO thread to guarantee that the UI thread has
60 // been notified of all the requests triggered by the IO thread.
61 base::RunLoop run_loop;
62 content::BrowserThread::PostTaskAndReply(
63 content::BrowserThread::IO, FROM_HERE, base::Bind(&base::DoNothing),
64 run_loop.QuitClosure());
65 run_loop.Run();
66 return count_;
67 }
68
69 private:
70 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.
71 public:
72 SimpleRequestInterceptor(const base::Closure& callback)
mmenke 2016/08/30 14:51:13 explicit
raymes 2016/08/31 00:25:48 Done.
73 : callback_(callback) {}
74
75 // URLRequestInterceptor implementation:
76 net::URLRequestJob* MaybeInterceptRequest(
77 net::URLRequest* request,
78 net::NetworkDelegate* network_delegate) const override {
79 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
80 callback_);
81 return nullptr;
82 }
83
84 private:
85 const base::Closure callback_;
86 };
87
88 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.
89
90 void AddInterceptor() {
91 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
92 url_, base::MakeUnique<SimpleRequestInterceptor>(base::Bind(
93 &URLRequestCounter::RequestFired, base::Unretained(this))));
94 }
95
96 void RemoveInterceptor() {
97 net::URLRequestFilter::GetInstance()->RemoveUrlHandler(url_);
98 }
99
100 const GURL url_;
101 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.
102
103 DISALLOW_COPY_AND_ASSIGN(URLRequestCounter);
104 };
105
31 class MimeHandlerViewTest : public ExtensionApiTest { 106 class MimeHandlerViewTest : public ExtensionApiTest {
32 public: 107 public:
33 MimeHandlerViewTest() { 108 MimeHandlerViewTest() {
34 GuestViewManager::set_factory_for_testing(&factory_); 109 GuestViewManager::set_factory_for_testing(&factory_);
35 } 110 }
36 111
37 ~MimeHandlerViewTest() override {} 112 ~MimeHandlerViewTest() override {}
38 113
114 void SetUpOnMainThread() override {
115 ExtensionApiTest::SetUpOnMainThread();
116
117 ASSERT_TRUE(StartEmbeddedTestServer());
118 embedded_test_server()->ServeFilesFromDirectory(
119 test_data_dir_.AppendASCII("mime_handler_view"));
120 }
121
39 // TODO(paulmeyer): This function is implemented over and over by the 122 // TODO(paulmeyer): This function is implemented over and over by the
40 // different GuestView test classes. It really needs to be refactored out to 123 // different GuestView test classes. It really needs to be refactored out to
41 // some kind of GuestViewTest base class. 124 // some kind of GuestViewTest base class.
42 TestGuestViewManager* GetGuestViewManager() { 125 TestGuestViewManager* GetGuestViewManager() {
43 TestGuestViewManager* manager = static_cast<TestGuestViewManager*>( 126 TestGuestViewManager* manager = static_cast<TestGuestViewManager*>(
44 TestGuestViewManager::FromBrowserContext(browser()->profile())); 127 TestGuestViewManager::FromBrowserContext(browser()->profile()));
45 // TestGuestViewManager::WaitForSingleGuestCreated can and will get called 128 // TestGuestViewManager::WaitForSingleGuestCreated can and will get called
46 // before a guest is created. Since GuestViewManager is usually not created 129 // before a guest is created. Since GuestViewManager is usually not created
47 // until the first guest is created, this means that |manager| will be 130 // 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 131 // nullptr if trying to use the manager to wait for the first guest. Because
(...skipping 28 matching lines...) Expand all
77 ASSERT_TRUE(extension); 160 ASSERT_TRUE(extension);
78 161
79 extensions::ResultCatcher catcher; 162 extensions::ResultCatcher catcher;
80 ui_test_utils::NavigateToURL(browser(), url); 163 ui_test_utils::NavigateToURL(browser(), url);
81 164
82 if (!catcher.GetNextResult()) 165 if (!catcher.GetNextResult())
83 FAIL() << catcher.message(); 166 FAIL() << catcher.message();
84 } 167 }
85 168
86 void RunTest(const std::string& path) { 169 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)); 170 RunTestWithUrl(embedded_test_server()->GetURL("/" + path));
92 } 171 }
93 172
94 private: 173 private:
95 TestGuestViewManagerFactory factory_; 174 TestGuestViewManagerFactory factory_;
96 }; 175 };
97 176
98 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, PostMessage) { 177 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, PostMessage) {
99 RunTest("test_postmessage.html"); 178 RunTest("test_postmessage.html");
100 } 179 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 GetGuestViewManager()->WaitForSingleGuestCreated(); 228 GetGuestViewManager()->WaitForSingleGuestCreated();
150 TestMimeHandlerViewGuest* guest = static_cast<TestMimeHandlerViewGuest*>( 229 TestMimeHandlerViewGuest* guest = static_cast<TestMimeHandlerViewGuest*>(
151 MimeHandlerViewGuest::FromWebContents(guest_web_contents)); 230 MimeHandlerViewGuest::FromWebContents(guest_web_contents));
152 guest->WaitForGuestAttached(); 231 guest->WaitForGuestAttached();
153 232
154 // Ensure that the guest has the correct size after it has attached. 233 // Ensure that the guest has the correct size after it has attached.
155 auto guest_size = guest->size(); 234 auto guest_size = guest->size();
156 CHECK_EQ(guest_size.width(), 500); 235 CHECK_EQ(guest_size.width(), 500);
157 CHECK_EQ(guest_size.height(), 400); 236 CHECK_EQ(guest_size.height(), 400);
158 } 237 }
238
239 // Regression test for crbug.com/587709.
240 IN_PROC_BROWSER_TEST_F(MimeHandlerViewTest, SingleRequest) {
241 GURL url(embedded_test_server()->GetURL("/testBasic.csv"));
242 URLRequestCounter request_counter(url);
243 RunTest("testBasic.csv");
244 EXPECT_EQ(1, request_counter.GetCount());
245 }
OLDNEW
« no previous file with comments | « no previous file | extensions/renderer/guest_view/mime_handler_view/mime_handler_view_container.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698