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

Side by Side Diff: content/public/test/browser_test_utils.cc

Issue 2132603002: [page_load_metrics] Add a NavigationThrottle for richer abort metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix new unit tests Created 4 years, 4 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/public/test/browser_test_utils.h" 5 #include "content/public/test/browser_test_utils.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <tuple> 8 #include <tuple>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 26 matching lines...) Expand all
37 #include "content/browser/web_contents/web_contents_impl.h" 37 #include "content/browser/web_contents/web_contents_impl.h"
38 #include "content/browser/web_contents/web_contents_view.h" 38 #include "content/browser/web_contents/web_contents_view.h"
39 #include "content/common/input/synthetic_web_input_event_builders.h" 39 #include "content/common/input/synthetic_web_input_event_builders.h"
40 #include "content/common/input_messages.h" 40 #include "content/common/input_messages.h"
41 #include "content/common/view_messages.h" 41 #include "content/common/view_messages.h"
42 #include "content/public/browser/browser_context.h" 42 #include "content/public/browser/browser_context.h"
43 #include "content/public/browser/browser_plugin_guest_manager.h" 43 #include "content/public/browser/browser_plugin_guest_manager.h"
44 #include "content/public/browser/browser_thread.h" 44 #include "content/public/browser/browser_thread.h"
45 #include "content/public/browser/histogram_fetcher.h" 45 #include "content/public/browser/histogram_fetcher.h"
46 #include "content/public/browser/navigation_entry.h" 46 #include "content/public/browser/navigation_entry.h"
47 #include "content/public/browser/navigation_handle.h"
48 #include "content/public/browser/navigation_throttle.h"
47 #include "content/public/browser/notification_service.h" 49 #include "content/public/browser/notification_service.h"
48 #include "content/public/browser/notification_types.h" 50 #include "content/public/browser/notification_types.h"
49 #include "content/public/browser/render_frame_host.h" 51 #include "content/public/browser/render_frame_host.h"
50 #include "content/public/browser/render_process_host.h" 52 #include "content/public/browser/render_process_host.h"
51 #include "content/public/browser/render_view_host.h" 53 #include "content/public/browser/render_view_host.h"
52 #include "content/public/browser/storage_partition.h" 54 #include "content/public/browser/storage_partition.h"
53 #include "content/public/browser/web_contents.h" 55 #include "content/public/browser/web_contents.h"
54 #include "content/public/test/test_navigation_observer.h" 56 #include "content/public/test/test_navigation_observer.h"
55 #include "content/public/test/test_utils.h" 57 #include "content/public/test/test_utils.h"
56 #include "content/test/accessibility_browser_test_utils.h" 58 #include "content/test/accessibility_browser_test_utils.h"
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 GURL redirect_target(redirect_server.Resolve(path)); 345 GURL redirect_target(redirect_server.Resolve(path));
344 DCHECK(redirect_target.is_valid()); 346 DCHECK(redirect_target.is_valid());
345 347
346 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( 348 std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
347 new net::test_server::BasicHttpResponse); 349 new net::test_server::BasicHttpResponse);
348 http_response->set_code(http_status_code); 350 http_response->set_code(http_status_code);
349 http_response->AddCustomHeader("Location", redirect_target.spec()); 351 http_response->AddCustomHeader("Location", redirect_target.spec());
350 return std::move(http_response); 352 return std::move(http_response);
351 } 353 }
352 354
355 // Helper class used by the TestNavigationManager to pause navigations.
356 // Note: the throttle should be added to the *end* of the list of throttles,
357 // so all NavigationThrottles that should be attached observe the
358 // WillStartRequest callback. RegisterThrottleForTesting has this behavior.
359 class TestNavigationManagerThrottle : public NavigationThrottle {
360 public:
361 TestNavigationManagerThrottle(NavigationHandle* handle,
362 base::Closure on_will_start_request_closure)
363 : NavigationThrottle(handle),
364 on_will_start_request_closure_(on_will_start_request_closure) {}
365 ~TestNavigationManagerThrottle() override {}
366
367 private:
368 // NavigationThrottle:
369 NavigationThrottle::ThrottleCheckResult WillStartRequest() override {
370 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
371 on_will_start_request_closure_);
372 return NavigationThrottle::DEFER;
373 }
374
375 base::Closure on_will_start_request_closure_;
376 };
377
353 } // namespace 378 } // namespace
354 379
355 bool NavigateIframeToURL(WebContents* web_contents, 380 bool NavigateIframeToURL(WebContents* web_contents,
356 std::string iframe_id, 381 std::string iframe_id,
357 const GURL& url) { 382 const GURL& url) {
358 std::string script = base::StringPrintf( 383 std::string script = base::StringPrintf(
359 "setTimeout(\"" 384 "setTimeout(\""
360 "var iframes = document.getElementById('%s');iframes.src='%s';" 385 "var iframes = document.getElementById('%s');iframes.src='%s';"
361 "\",0)", 386 "\",0)",
362 iframe_id.c_str(), url.spec().c_str()); 387 iframe_id.c_str(), url.spec().c_str());
(...skipping 1168 matching lines...) Expand 10 before | Expand all | Expand 10 after
1531 FrameFocusedObserver::FrameFocusedObserver(RenderFrameHost* owner_host) 1556 FrameFocusedObserver::FrameFocusedObserver(RenderFrameHost* owner_host)
1532 : impl_(new FrameTreeNodeObserverImpl( 1557 : impl_(new FrameTreeNodeObserverImpl(
1533 static_cast<RenderFrameHostImpl*>(owner_host)->frame_tree_node())) {} 1558 static_cast<RenderFrameHostImpl*>(owner_host)->frame_tree_node())) {}
1534 1559
1535 FrameFocusedObserver::~FrameFocusedObserver() {} 1560 FrameFocusedObserver::~FrameFocusedObserver() {}
1536 1561
1537 void FrameFocusedObserver::Wait() { 1562 void FrameFocusedObserver::Wait() {
1538 impl_->Run(); 1563 impl_->Run();
1539 } 1564 }
1540 1565
1566 TestNavigationManager::TestNavigationManager(WebContents* web_contents,
1567 const GURL& url)
1568 : WebContentsObserver(web_contents),
1569 url_(url),
1570 navigation_paused_(false),
1571 handle_(nullptr),
1572 handled_navigation_(false),
1573 weak_factory_(this) {}
1574
1575 TestNavigationManager::~TestNavigationManager() {
1576 ResumeNavigation();
1577 }
1578
1579 bool TestNavigationManager::WaitForWillStartRequest() {
1580 DCHECK(!did_finish_loop_runner_);
1581 if (!handle_ && handled_navigation_)
1582 return true;
1583 if (navigation_paused_)
1584 return true;
1585 will_start_loop_runner_ = new MessageLoopRunner();
1586 will_start_loop_runner_->Run();
1587 will_start_loop_runner_ = nullptr;
1588
1589 // This will only be false if DidFinishNavigation is called before
1590 // OnWillStartRequest, which could occur if a throttle cancels the navigation
1591 // before the TestNavigationManagerThrottle's method is called.
1592 return !handled_navigation_;
1593 }
1594
1595 void TestNavigationManager::WaitForNavigationFinished() {
1596 DCHECK(!will_start_loop_runner_);
1597 if (!handle_ && handled_navigation_)
1598 return;
1599 // Ensure the navigation is resumed if the manager paused it previously.
1600 if (navigation_paused_)
1601 ResumeNavigation();
1602 did_finish_loop_runner_ = new MessageLoopRunner();
1603 did_finish_loop_runner_->Run();
1604 did_finish_loop_runner_ = nullptr;
1605 }
1606
1607 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) {
1608 if (!ShouldMonitorNavigation(handle))
1609 return;
1610
1611 handle_ = handle;
1612 std::unique_ptr<NavigationThrottle> throttle(
1613 new TestNavigationManagerThrottle(
1614 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest,
1615 weak_factory_.GetWeakPtr())));
1616 handle_->RegisterThrottleForTesting(std::move(throttle));
1617 }
1618
1619 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) {
1620 if (handle != handle_)
1621 return;
1622 handle_ = nullptr;
1623 handled_navigation_ = true;
1624 navigation_paused_ = false;
1625 if (did_finish_loop_runner_)
1626 did_finish_loop_runner_->Quit();
1627 if (will_start_loop_runner_)
1628 will_start_loop_runner_->Quit();
nasko 2016/08/02 16:22:16 Why would we have either of the two runners runnin
Charlie Harrison 2016/08/02 16:47:13 For |did_finish_loop_runner_|, this is the normal
1629 }
1630
1631 void TestNavigationManager::OnWillStartRequest() {
1632 navigation_paused_ = true;
1633 if (will_start_loop_runner_)
1634 will_start_loop_runner_->Quit();
1635
1636 // If waiting for the navigation to finish, resume the navigation.
1637 if (did_finish_loop_runner_)
1638 ResumeNavigation();
1639 }
1640
1641 void TestNavigationManager::ResumeNavigation() {
1642 if (!navigation_paused_ || !handle_)
1643 return;
1644 navigation_paused_ = false;
1645 handle_->Resume();
1646 }
1647
1648 bool TestNavigationManager::ShouldMonitorNavigation(NavigationHandle* handle) {
1649 if (handle_ || handle->GetURL() != url_)
1650 return false;
1651 if (handled_navigation_)
1652 return false;
1653 return true;
1654 }
1655
1541 } // namespace content 1656 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698