OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
17 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
19 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
20 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
21 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/test/browser_test_utils.h" | 23 #include "content/public/test/browser_test_utils.h" |
24 #include "net/base/load_timing_info.h" | 24 #include "net/base/load_timing_info.h" |
25 #include "net/test/spawned_test_server/spawned_test_server.h" | 25 #include "net/test/spawned_test_server/spawned_test_server.h" |
26 #include "net/url_request/url_request_file_job.h" | 26 #include "net/url_request/url_request_file_job.h" |
27 #include "net/url_request/url_request_filter.h" | 27 #include "net/url_request/url_request_filter.h" |
28 #include "net/url_request/url_request_job_factory.h" | 28 #include "net/url_request/url_request_interceptor.h" |
29 #include "url/gurl.h" | 29 #include "url/gurl.h" |
30 | 30 |
31 // This file tests that net::LoadTimingInfo is correctly hooked up to the | 31 // This file tests that net::LoadTimingInfo is correctly hooked up to the |
32 // NavigationTiming API. It depends on behavior in a large number of files | 32 // NavigationTiming API. It depends on behavior in a large number of files |
33 // spread across multiple projects, so is somewhat arbitrarily put in | 33 // spread across multiple projects, so is somewhat arbitrarily put in |
34 // chrome/browser/net. | 34 // chrome/browser/net. |
35 | 35 |
36 using content::BrowserThread; | 36 using content::BrowserThread; |
37 | 37 |
38 namespace { | 38 namespace { |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 | 207 |
208 // Load times to use, relative to |start_time_|. | 208 // Load times to use, relative to |start_time_|. |
209 const TimingDeltas load_timing_deltas_; | 209 const TimingDeltas load_timing_deltas_; |
210 base::TimeTicks start_time_; | 210 base::TimeTicks start_time_; |
211 | 211 |
212 base::WeakPtrFactory<MockUrlRequestJobWithTiming> weak_factory_; | 212 base::WeakPtrFactory<MockUrlRequestJobWithTiming> weak_factory_; |
213 | 213 |
214 DISALLOW_COPY_AND_ASSIGN(MockUrlRequestJobWithTiming); | 214 DISALLOW_COPY_AND_ASSIGN(MockUrlRequestJobWithTiming); |
215 }; | 215 }; |
216 | 216 |
217 // A protocol handler that returns mock URLRequestJobs that return the specified | 217 // AURLRequestInterceptor that returns mock URLRequestJobs that return the |
218 // file with the given timings. Constructed on the UI thread, but after that, | 218 // specified file with the given timings. Constructed on the UI thread, but |
219 // lives and is destroyed on the IO thread. | 219 // after that, lives and is destroyed on the IO thread. |
220 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { | 220 class TestInterceptor : public net::URLRequestInterceptor { |
221 public: | 221 public: |
222 TestProtocolHandler(const base::FilePath& path, | 222 TestInterceptor(const base::FilePath& path, |
223 const TimingDeltas& load_timing_deltas) | 223 const TimingDeltas& load_timing_deltas) |
224 : path_(path), load_timing_deltas_(load_timing_deltas) { | 224 : path_(path), load_timing_deltas_(load_timing_deltas) { |
225 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 225 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
226 } | 226 } |
227 | 227 |
228 virtual ~TestProtocolHandler() { | 228 virtual ~TestInterceptor() { |
229 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 229 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
230 } | 230 } |
231 | 231 |
232 // Registers |this| with the URLRequestFilter, which takes ownership of it. | 232 // Registers |this| with the URLRequestFilter, which takes ownership of it. |
233 void Register() { | 233 void Register() { |
234 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 234 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
235 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( | 235 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
236 "http", kTestDomain, | 236 "http", kTestDomain, scoped_ptr<net::URLRequestInterceptor>(this)); |
237 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this)); | |
238 } | 237 } |
239 | 238 |
240 // Unregisters |this| with the URLRequestFilter, which should then delete | 239 // Unregisters |this| with the URLRequestFilter, which should then delete |
241 // |this|. | 240 // |this|. |
242 void Unregister() { | 241 void Unregister() { |
243 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 242 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
244 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler( | 243 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler( |
245 "http", kTestDomain); | 244 "http", kTestDomain); |
246 } | 245 } |
247 | 246 |
248 // net::URLRequestJobFactory::ProtocolHandler implementation: | 247 // net::URLRequestJobFactory::ProtocolHandler implementation: |
249 virtual net::URLRequestJob* MaybeCreateJob( | 248 virtual net::URLRequestJob* MaybeInterceptRequest( |
250 net::URLRequest* request, | 249 net::URLRequest* request, |
251 net::NetworkDelegate* network_delegate) const OVERRIDE { | 250 net::NetworkDelegate* network_delegate) const OVERRIDE { |
252 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 251 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
253 | 252 |
254 return new MockUrlRequestJobWithTiming(request, network_delegate, path_, | 253 return new MockUrlRequestJobWithTiming(request, network_delegate, path_, |
255 load_timing_deltas_); | 254 load_timing_deltas_); |
256 } | 255 } |
257 | 256 |
258 private: | 257 private: |
259 // Path of the file to use as the response body. | 258 // Path of the file to use as the response body. |
260 const base::FilePath path_; | 259 const base::FilePath path_; |
261 | 260 |
262 // Load times for each request to use, relative to when the Job starts. | 261 // Load times for each request to use, relative to when the Job starts. |
263 const TimingDeltas load_timing_deltas_; | 262 const TimingDeltas load_timing_deltas_; |
264 | 263 |
265 DISALLOW_COPY_AND_ASSIGN(TestProtocolHandler); | 264 DISALLOW_COPY_AND_ASSIGN(TestInterceptor); |
266 }; | 265 }; |
267 | 266 |
268 class LoadTimingBrowserTest : public InProcessBrowserTest { | 267 class LoadTimingBrowserTest : public InProcessBrowserTest { |
269 public: | 268 public: |
270 LoadTimingBrowserTest() { | 269 LoadTimingBrowserTest() { |
271 } | 270 } |
272 | 271 |
273 virtual ~LoadTimingBrowserTest() { | 272 virtual ~LoadTimingBrowserTest() { |
274 } | 273 } |
275 | 274 |
276 // Navigates to |url| and writes the resulting navigation timings to | 275 // Navigates to |url| and writes the resulting navigation timings to |
277 // |navigation_deltas|. | 276 // |navigation_deltas|. |
278 void RunTestWithUrl(const GURL& url, TimingDeltas* navigation_deltas) { | 277 void RunTestWithUrl(const GURL& url, TimingDeltas* navigation_deltas) { |
279 ui_test_utils::NavigateToURL(browser(), url); | 278 ui_test_utils::NavigateToURL(browser(), url); |
280 GetResultDeltas(navigation_deltas); | 279 GetResultDeltas(navigation_deltas); |
281 } | 280 } |
282 | 281 |
283 // Navigates to a url that returns the timings indicated by | 282 // Navigates to a url that returns the timings indicated by |
284 // |load_timing_deltas| and writes the resulting navigation timings to | 283 // |load_timing_deltas| and writes the resulting navigation timings to |
285 // |navigation_deltas|. Uses a generic test page. | 284 // |navigation_deltas|. Uses a generic test page. |
286 void RunTest(const TimingDeltas& load_timing_deltas, | 285 void RunTest(const TimingDeltas& load_timing_deltas, |
287 TimingDeltas* navigation_deltas) { | 286 TimingDeltas* navigation_deltas) { |
288 // None of the tests care about the contents of the test page. Just do | 287 // None of the tests care about the contents of the test page. Just do |
289 // this here because PathService has thread restrictions on some platforms. | 288 // this here because PathService has thread restrictions on some platforms. |
290 base::FilePath path; | 289 base::FilePath path; |
291 PathService::Get(chrome::DIR_TEST_DATA, &path); | 290 PathService::Get(chrome::DIR_TEST_DATA, &path); |
292 path = path.AppendASCII("title1.html"); | 291 path = path.AppendASCII("title1.html"); |
293 | 292 |
294 // Create and register protocol handler. | 293 // Create and register request interceptor. |
295 TestProtocolHandler* protocol_handler = | 294 TestInterceptor* test_interceptor = |
296 new TestProtocolHandler(path, load_timing_deltas); | 295 new TestInterceptor(path, load_timing_deltas); |
297 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 296 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
298 base::Bind(&TestProtocolHandler::Register, | 297 base::Bind(&TestInterceptor::Register, |
299 base::Unretained(protocol_handler))); | 298 base::Unretained(test_interceptor))); |
300 | 299 |
301 // Navigate to the page. | 300 // Navigate to the page. |
302 RunTestWithUrl(GURL(kTestUrl), navigation_deltas); | 301 RunTestWithUrl(GURL(kTestUrl), navigation_deltas); |
303 | 302 |
304 // Once navigation is complete, unregister the protocol handler. | 303 // Once navigation is complete, unregister the protocol handler. |
305 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 304 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
306 base::Bind(&TestProtocolHandler::Unregister, | 305 base::Bind(&TestInterceptor::Unregister, |
307 base::Unretained(protocol_handler))); | 306 base::Unretained(test_interceptor))); |
308 } | 307 } |
309 | 308 |
310 private: | 309 private: |
311 // Reads applicable times from performance.timing and writes them to | 310 // Reads applicable times from performance.timing and writes them to |
312 // |navigation_deltas|. Proxy times and send end cannot be read from the | 311 // |navigation_deltas|. Proxy times and send end cannot be read from the |
313 // Navigation Timing API, so those are all left as null. | 312 // Navigation Timing API, so those are all left as null. |
314 void GetResultDeltas(TimingDeltas* navigation_deltas) { | 313 void GetResultDeltas(TimingDeltas* navigation_deltas) { |
315 *navigation_deltas = TimingDeltas(); | 314 *navigation_deltas = TimingDeltas(); |
316 | 315 |
317 navigation_deltas->dns_start = GetResultDelta("domainLookupStart"); | 316 navigation_deltas->dns_start = GetResultDelta("domainLookupStart"); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 navigation_deltas.send_start.GetDelta()); | 589 navigation_deltas.send_start.GetDelta()); |
591 // The only times that are guaranteed to be distinct are send_start and | 590 // The only times that are guaranteed to be distinct are send_start and |
592 // received_headers_end. | 591 // received_headers_end. |
593 EXPECT_LT(navigation_deltas.send_start.GetDelta(), | 592 EXPECT_LT(navigation_deltas.send_start.GetDelta(), |
594 navigation_deltas.receive_headers_end.GetDelta()); | 593 navigation_deltas.receive_headers_end.GetDelta()); |
595 | 594 |
596 EXPECT_TRUE(navigation_deltas.ssl_start.is_null()); | 595 EXPECT_TRUE(navigation_deltas.ssl_start.is_null()); |
597 } | 596 } |
598 | 597 |
599 } // namespace | 598 } // namespace |
OLD | NEW |