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. This throttle is added, and will re-add itself | |
359 // to the end of the list on WillStartRequest. | |
clamy
2016/07/27 17:02:24
Comment needs to be updated since we now always at
Charlie Harrison
2016/07/28 01:29:59
Done.
| |
360 class TestNavigationManagerThrottle : public NavigationThrottle { | |
361 public: | |
362 TestNavigationManagerThrottle(NavigationHandle* handle, | |
363 base::Closure on_will_start_request_closure) | |
364 : NavigationThrottle(handle), | |
365 on_will_start_request_closure_(on_will_start_request_closure) {} | |
366 ~TestNavigationManagerThrottle() override {} | |
367 | |
368 private: | |
369 // NavigationThrottle implementation. | |
370 NavigationThrottle::ThrottleCheckResult WillStartRequest() override { | |
371 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
372 on_will_start_request_closure_); | |
373 return NavigationThrottle::DEFER; | |
374 } | |
375 | |
376 base::Closure on_will_start_request_closure_; | |
377 }; | |
378 | |
353 } // namespace | 379 } // namespace |
354 | 380 |
355 bool NavigateIframeToURL(WebContents* web_contents, | 381 bool NavigateIframeToURL(WebContents* web_contents, |
356 std::string iframe_id, | 382 std::string iframe_id, |
357 const GURL& url) { | 383 const GURL& url) { |
358 std::string script = base::StringPrintf( | 384 std::string script = base::StringPrintf( |
359 "setTimeout(\"" | 385 "setTimeout(\"" |
360 "var iframes = document.getElementById('%s');iframes.src='%s';" | 386 "var iframes = document.getElementById('%s');iframes.src='%s';" |
361 "\",0)", | 387 "\",0)", |
362 iframe_id.c_str(), url.spec().c_str()); | 388 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)) { | 1526 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
1501 RunTaskOnIOThreadAndWait(base::Bind(&BrowserTestClipboardScope::SetText, | 1527 RunTaskOnIOThreadAndWait(base::Bind(&BrowserTestClipboardScope::SetText, |
1502 base::Unretained(this), text)); | 1528 base::Unretained(this), text)); |
1503 return; | 1529 return; |
1504 } | 1530 } |
1505 #endif | 1531 #endif |
1506 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); | 1532 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE); |
1507 clipboard_writer.WriteText(base::ASCIIToUTF16(text)); | 1533 clipboard_writer.WriteText(base::ASCIIToUTF16(text)); |
1508 } | 1534 } |
1509 | 1535 |
1536 TestNavigationManager::TestNavigationManager(int filtering_frame_tree_node_id, | |
1537 WebContents* web_contents, | |
1538 const GURL& url) | |
1539 : WebContentsObserver(web_contents), | |
1540 filtering_frame_tree_node_id_(filtering_frame_tree_node_id), | |
1541 url_(url), | |
1542 navigation_paused_(false), | |
1543 handle_(nullptr), | |
1544 handled_navigation_(false), | |
1545 weak_factory_(this) {} | |
1546 | |
1547 TestNavigationManager::TestNavigationManager(WebContents* web_contents, | |
1548 const GURL& url) | |
1549 : TestNavigationManager(FrameTreeNode::kFrameTreeNodeInvalidId, | |
1550 web_contents, | |
1551 url) {} | |
1552 | |
1553 TestNavigationManager::~TestNavigationManager() { | |
1554 ResumeNavigation(); | |
1555 } | |
1556 | |
1557 bool TestNavigationManager::WaitForWillStartRequest() { | |
1558 DCHECK(!did_finish_loop_runner_); | |
1559 if (!handle_ && handled_navigation_) | |
1560 return false; | |
1561 if (navigation_paused_) | |
1562 return false; | |
1563 will_start_loop_runner_ = new MessageLoopRunner(); | |
1564 will_start_loop_runner_->Run(); | |
1565 will_start_loop_runner_ = nullptr; | |
1566 | |
1567 // This will only be if DidFinishNavigation is called before | |
Charlie Harrison
2016/07/26 19:56:07
added "true" here in next PS.
| |
1568 // OnWillStartRequest, which could occur if a throttle cancels the navigation | |
1569 // before the TestNavigationManagerThrottle's method is called. | |
1570 return handled_navigation_; | |
1571 } | |
1572 | |
1573 void TestNavigationManager::WaitForNavigationFinished() { | |
1574 DCHECK(!will_start_loop_runner_); | |
1575 if (!handle_ && handled_navigation_) | |
1576 return; | |
1577 // Ensure the navigation is resumed if the manager paused it previously. | |
1578 if (navigation_paused_) | |
1579 ResumeNavigation(); | |
1580 did_finish_loop_runner_ = new MessageLoopRunner(); | |
1581 did_finish_loop_runner_->Run(); | |
1582 did_finish_loop_runner_ = nullptr; | |
1583 } | |
1584 | |
1585 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) { | |
1586 if (handle_ || handle->GetURL() != url_) | |
1587 return; | |
1588 if (handled_navigation_) | |
1589 return; | |
1590 | |
1591 if (filtering_frame_tree_node_id_ != FrameTreeNode::kFrameTreeNodeInvalidId && | |
1592 handle->GetFrameTreeNodeId() != filtering_frame_tree_node_id_) { | |
1593 return; | |
1594 } | |
1595 | |
1596 handle_ = handle; | |
1597 std::unique_ptr<NavigationThrottle> throttle( | |
1598 new TestNavigationManagerThrottle( | |
1599 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest, | |
1600 weak_factory_.GetWeakPtr()))); | |
1601 handle_->RegisterThrottleForTesting(std::move(throttle)); | |
1602 } | |
1603 | |
1604 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) { | |
1605 if (handle != handle_) | |
1606 return; | |
1607 handle_ = nullptr; | |
1608 handled_navigation_ = true; | |
1609 navigation_paused_ = false; | |
1610 if (did_finish_loop_runner_) | |
1611 did_finish_loop_runner_->Quit(); | |
1612 if (will_start_loop_runner_) | |
1613 will_start_loop_runner_->Quit(); | |
1614 } | |
1615 | |
1616 void TestNavigationManager::OnWillStartRequest() { | |
1617 navigation_paused_ = true; | |
1618 if (will_start_loop_runner_) | |
1619 will_start_loop_runner_->Quit(); | |
1620 | |
1621 // If waiting for the navigation to finish, resume the navigation. | |
1622 if (did_finish_loop_runner_) | |
1623 ResumeNavigation(); | |
1624 } | |
1625 | |
1626 void TestNavigationManager::ResumeNavigation() { | |
1627 if (!navigation_paused_ || !handle_) | |
1628 return; | |
1629 navigation_paused_ = false; | |
1630 handle_->Resume(); | |
1631 } | |
1632 | |
1510 } // namespace content | 1633 } // namespace content |
OLD | NEW |