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

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: Add a NavigationThrottle to help track provisional abort metrics Created 4 years, 5 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 25 matching lines...) Expand all
36 #include "content/browser/renderer_host/render_widget_host_impl.h" 36 #include "content/browser/renderer_host/render_widget_host_impl.h"
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/histogram_fetcher.h" 44 #include "content/public/browser/histogram_fetcher.h"
45 #include "content/public/browser/navigation_entry.h" 45 #include "content/public/browser/navigation_entry.h"
46 #include "content/public/browser/navigation_handle.h"
47 #include "content/public/browser/navigation_throttle.h"
46 #include "content/public/browser/notification_service.h" 48 #include "content/public/browser/notification_service.h"
47 #include "content/public/browser/notification_types.h" 49 #include "content/public/browser/notification_types.h"
48 #include "content/public/browser/render_frame_host.h" 50 #include "content/public/browser/render_frame_host.h"
49 #include "content/public/browser/render_process_host.h" 51 #include "content/public/browser/render_process_host.h"
50 #include "content/public/browser/render_view_host.h" 52 #include "content/public/browser/render_view_host.h"
51 #include "content/public/browser/storage_partition.h" 53 #include "content/public/browser/storage_partition.h"
52 #include "content/public/browser/web_contents.h" 54 #include "content/public/browser/web_contents.h"
53 #include "content/public/test/test_navigation_observer.h" 55 #include "content/public/test/test_navigation_observer.h"
54 #include "content/public/test/test_utils.h" 56 #include "content/public/test/test_utils.h"
55 #include "content/test/accessibility_browser_test_utils.h" 57 #include "content/test/accessibility_browser_test_utils.h"
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 GURL redirect_target(redirect_server.Resolve(path)); 341 GURL redirect_target(redirect_server.Resolve(path));
340 DCHECK(redirect_target.is_valid()); 342 DCHECK(redirect_target.is_valid());
341 343
342 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( 344 std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
343 new net::test_server::BasicHttpResponse); 345 new net::test_server::BasicHttpResponse);
344 http_response->set_code(http_status_code); 346 http_response->set_code(http_status_code);
345 http_response->AddCustomHeader("Location", redirect_target.spec()); 347 http_response->AddCustomHeader("Location", redirect_target.spec());
346 return std::move(http_response); 348 return std::move(http_response);
347 } 349 }
348 350
351 // Helper class used by the TestNavigationManager to pause navigations.
352 class TestNavigationManagerThrottle : public NavigationThrottle {
353 public:
354 TestNavigationManagerThrottle(NavigationHandle* handle,
355 base::Closure on_will_start_request_closure)
356 : NavigationThrottle(handle),
357 on_will_start_request_closure_(on_will_start_request_closure) {}
358 ~TestNavigationManagerThrottle() override {}
359
360 private:
361 // NavigationThrottle implementation.
362 NavigationThrottle::ThrottleCheckResult WillStartRequest() override {
363 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
364 on_will_start_request_closure_);
365 return NavigationThrottle::DEFER;
366 }
367
368 base::Closure on_will_start_request_closure_;
369 };
370
349 } // namespace 371 } // namespace
350 372
351 bool NavigateIframeToURL(WebContents* web_contents, 373 bool NavigateIframeToURL(WebContents* web_contents,
352 std::string iframe_id, 374 std::string iframe_id,
353 const GURL& url) { 375 const GURL& url) {
354 std::string script = base::StringPrintf( 376 std::string script = base::StringPrintf(
355 "setTimeout(\"" 377 "setTimeout(\""
356 "var iframes = document.getElementById('%s');iframes.src='%s';" 378 "var iframes = document.getElementById('%s');iframes.src='%s';"
357 "\",0)", 379 "\",0)",
358 iframe_id.c_str(), url.spec().c_str()); 380 iframe_id.c_str(), url.spec().c_str());
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after
1424 uint32_t InputMsgWatcher::WaitForAck() { 1446 uint32_t InputMsgWatcher::WaitForAck() {
1425 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1447 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1426 if (ack_result_ != INPUT_EVENT_ACK_STATE_UNKNOWN) 1448 if (ack_result_ != INPUT_EVENT_ACK_STATE_UNKNOWN)
1427 return ack_result_; 1449 return ack_result_;
1428 base::RunLoop run_loop; 1450 base::RunLoop run_loop;
1429 base::AutoReset<base::Closure> reset_quit(&quit_, run_loop.QuitClosure()); 1451 base::AutoReset<base::Closure> reset_quit(&quit_, run_loop.QuitClosure());
1430 run_loop.Run(); 1452 run_loop.Run();
1431 return ack_result_; 1453 return ack_result_;
1432 } 1454 }
1433 1455
1456 TestNavigationManager::TestNavigationManager(int filtering_frame_tree_node_id,
1457 WebContents* web_contents,
1458 const GURL& url)
1459 : WebContentsObserver(web_contents),
1460 filtering_frame_tree_node_id_(filtering_frame_tree_node_id),
1461 url_(url),
1462 navigation_paused_(false),
1463 handle_(nullptr),
1464 handled_navigation_(false),
1465 weak_factory_(this) {}
1466
1467 TestNavigationManager::TestNavigationManager(WebContents* web_contents,
1468 const GURL& url)
1469 : TestNavigationManager(FrameTreeNode::kFrameTreeNodeInvalidId,
1470 web_contents,
1471 url) {}
1472
1473 TestNavigationManager::~TestNavigationManager() {
1474 ResumeNavigation();
1475 }
1476
1477 void TestNavigationManager::WaitForWillStartRequest() {
1478 DCHECK(!did_finish_loop_runner_);
1479 if (!handle_ && handled_navigation_)
1480 return;
1481 if (navigation_paused_)
1482 return;
1483 will_start_loop_runner_ = new MessageLoopRunner();
1484 will_start_loop_runner_->Run();
1485 will_start_loop_runner_ = nullptr;
1486 }
1487
1488 void TestNavigationManager::WaitForNavigationFinished() {
1489 DCHECK(!will_start_loop_runner_);
1490 if (!handle_ && handled_navigation_)
1491 return;
1492 // Ensure the navigation is resumed if the manager paused it previously.
1493 if (navigation_paused_)
1494 ResumeNavigation();
1495 did_finish_loop_runner_ = new MessageLoopRunner();
1496 did_finish_loop_runner_->Run();
1497 did_finish_loop_runner_ = nullptr;
1498 }
1499
1500 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) {
1501 if (handle_ || handle->GetURL() != url_)
1502 return;
1503 if (handled_navigation_)
1504 return;
1505
1506 if (filtering_frame_tree_node_id_ != FrameTreeNode::kFrameTreeNodeInvalidId &&
1507 handle->GetFrameTreeNodeId() != filtering_frame_tree_node_id_) {
1508 return;
1509 }
1510
1511 handle_ = handle;
1512 std::unique_ptr<NavigationThrottle> throttle(
1513 new TestNavigationManagerThrottle(
1514 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest,
1515 weak_factory_.GetWeakPtr())));
1516 handle_->RegisterThrottleForTesting(std::move(throttle));
1517 }
1518
1519 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) {
1520 if (handle != handle_)
1521 return;
1522 handle_ = nullptr;
1523 handled_navigation_ = true;
1524 navigation_paused_ = false;
1525 if (did_finish_loop_runner_)
1526 did_finish_loop_runner_->Quit();
1527 }
1528
1529 void TestNavigationManager::OnWillStartRequest() {
1530 navigation_paused_ = true;
1531 if (will_start_loop_runner_)
1532 will_start_loop_runner_->Quit();
1533
1534 // If waiting for the navigation to finish, resume the navigation.
1535 if (did_finish_loop_runner_)
1536 ResumeNavigation();
1537 }
1538
1539 void TestNavigationManager::ResumeNavigation() {
1540 if (!navigation_paused_ || !handle_)
1541 return;
1542 navigation_paused_ = false;
1543 handle_->Resume();
1544 }
1545
1434 } // namespace content 1546 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698