Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 semantics. | |
|
Charlie Harrison
2016/07/28 01:29:59
s/has this semantics/has this behavior/g
| |
| 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 implementation. | |
| 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 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1500 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 1525 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 1501 RunTaskOnIOThreadAndWait(base::Bind(&BrowserTestClipboardScope::SetText, | 1526 RunTaskOnIOThreadAndWait(base::Bind(&BrowserTestClipboardScope::SetText, |
| 1502 base::Unretained(this), text)); | 1527 base::Unretained(this), text)); |
| 1503 return; | 1528 return; |
| 1504 } | 1529 } |
| 1505 #endif | 1530 #endif |
| 1506 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); | 1531 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 1507 clipboard_writer.WriteText(base::ASCIIToUTF16(text)); | 1532 clipboard_writer.WriteText(base::ASCIIToUTF16(text)); |
| 1508 } | 1533 } |
| 1509 | 1534 |
| 1535 TestNavigationManager::TestNavigationManager(int filtering_frame_tree_node_id, | |
| 1536 WebContents* web_contents, | |
| 1537 const GURL& url) | |
| 1538 : WebContentsObserver(web_contents), | |
| 1539 filtering_frame_tree_node_id_(filtering_frame_tree_node_id), | |
| 1540 url_(url), | |
| 1541 navigation_paused_(false), | |
| 1542 handle_(nullptr), | |
| 1543 handled_navigation_(false), | |
| 1544 weak_factory_(this) {} | |
| 1545 | |
| 1546 TestNavigationManager::TestNavigationManager(WebContents* web_contents, | |
| 1547 const GURL& url) | |
| 1548 : TestNavigationManager(FrameTreeNode::kFrameTreeNodeInvalidId, | |
| 1549 web_contents, | |
| 1550 url) {} | |
| 1551 | |
| 1552 TestNavigationManager::~TestNavigationManager() { | |
| 1553 ResumeNavigation(); | |
| 1554 } | |
| 1555 | |
| 1556 bool TestNavigationManager::WaitForWillStartRequest() { | |
| 1557 DCHECK(!did_finish_loop_runner_); | |
| 1558 if (!handle_ && handled_navigation_) | |
| 1559 return true; | |
| 1560 if (navigation_paused_) | |
| 1561 return true; | |
| 1562 will_start_loop_runner_ = new MessageLoopRunner(); | |
| 1563 will_start_loop_runner_->Run(); | |
| 1564 will_start_loop_runner_ = nullptr; | |
| 1565 | |
| 1566 // This will only be false if DidFinishNavigation is called before | |
| 1567 // OnWillStartRequest, which could occur if a throttle cancels the navigation | |
| 1568 // before the TestNavigationManagerThrottle's method is called. | |
| 1569 return !handled_navigation_; | |
| 1570 } | |
| 1571 | |
| 1572 void TestNavigationManager::WaitForNavigationFinished() { | |
| 1573 DCHECK(!will_start_loop_runner_); | |
| 1574 if (!handle_ && handled_navigation_) | |
| 1575 return; | |
| 1576 // Ensure the navigation is resumed if the manager paused it previously. | |
| 1577 if (navigation_paused_) | |
| 1578 ResumeNavigation(); | |
| 1579 did_finish_loop_runner_ = new MessageLoopRunner(); | |
| 1580 did_finish_loop_runner_->Run(); | |
| 1581 did_finish_loop_runner_ = nullptr; | |
| 1582 } | |
| 1583 | |
| 1584 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) { | |
| 1585 if (handle_ || handle->GetURL() != url_) | |
| 1586 return; | |
| 1587 if (handled_navigation_) | |
| 1588 return; | |
| 1589 | |
| 1590 if (filtering_frame_tree_node_id_ != FrameTreeNode::kFrameTreeNodeInvalidId && | |
| 1591 handle->GetFrameTreeNodeId() != filtering_frame_tree_node_id_) { | |
| 1592 return; | |
| 1593 } | |
| 1594 | |
| 1595 handle_ = handle; | |
| 1596 std::unique_ptr<NavigationThrottle> throttle( | |
| 1597 new TestNavigationManagerThrottle( | |
| 1598 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest, | |
| 1599 weak_factory_.GetWeakPtr()))); | |
| 1600 handle_->RegisterThrottleForTesting(std::move(throttle)); | |
| 1601 } | |
| 1602 | |
| 1603 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) { | |
| 1604 if (handle != handle_) | |
| 1605 return; | |
| 1606 handle_ = nullptr; | |
| 1607 handled_navigation_ = true; | |
| 1608 navigation_paused_ = false; | |
| 1609 if (did_finish_loop_runner_) | |
| 1610 did_finish_loop_runner_->Quit(); | |
| 1611 if (will_start_loop_runner_) | |
| 1612 will_start_loop_runner_->Quit(); | |
| 1613 } | |
| 1614 | |
| 1615 void TestNavigationManager::OnWillStartRequest() { | |
| 1616 navigation_paused_ = true; | |
| 1617 if (will_start_loop_runner_) | |
| 1618 will_start_loop_runner_->Quit(); | |
| 1619 | |
| 1620 // If waiting for the navigation to finish, resume the navigation. | |
| 1621 if (did_finish_loop_runner_) | |
| 1622 ResumeNavigation(); | |
| 1623 } | |
| 1624 | |
| 1625 void TestNavigationManager::ResumeNavigation() { | |
| 1626 if (!navigation_paused_ || !handle_) | |
| 1627 return; | |
| 1628 navigation_paused_ = false; | |
| 1629 handle_->Resume(); | |
| 1630 } | |
| 1631 | |
| 1510 } // namespace content | 1632 } // namespace content |
| OLD | NEW |