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

Side by Side Diff: content/test/content_browser_test_utils_internal.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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/test/content_browser_test_utils_internal.h" 5 #include "content/test/content_browser_test_utils_internal.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <map> 10 #include <map>
11 #include <set> 11 #include <set>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/test/test_timeouts.h" 15 #include "base/test/test_timeouts.h"
16 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 #include "cc/surfaces/surface.h" 17 #include "cc/surfaces/surface.h"
18 #include "cc/surfaces/surface_manager.h" 18 #include "cc/surfaces/surface_manager.h"
19 #include "content/browser/compositor/surface_utils.h" 19 #include "content/browser/compositor/surface_utils.h"
20 #include "content/browser/frame_host/cross_process_frame_connector.h" 20 #include "content/browser/frame_host/cross_process_frame_connector.h"
21 #include "content/browser/frame_host/frame_tree_node.h" 21 #include "content/browser/frame_host/frame_tree_node.h"
22 #include "content/browser/frame_host/navigator.h" 22 #include "content/browser/frame_host/navigator.h"
23 #include "content/browser/frame_host/render_frame_proxy_host.h" 23 #include "content/browser/frame_host/render_frame_proxy_host.h"
24 #include "content/browser/frame_host/render_widget_host_view_child_frame.h" 24 #include "content/browser/frame_host/render_widget_host_view_child_frame.h"
25 #include "content/browser/renderer_host/delegated_frame_host.h" 25 #include "content/browser/renderer_host/delegated_frame_host.h"
26 #include "content/browser/renderer_host/render_widget_host_view_base.h" 26 #include "content/browser/renderer_host/render_widget_host_view_base.h"
27 #include "content/public/browser/navigation_handle.h"
27 #include "content/public/browser/render_frame_host.h" 28 #include "content/public/browser/render_frame_host.h"
28 #include "content/public/browser/resource_dispatcher_host.h" 29 #include "content/public/browser/resource_dispatcher_host.h"
29 #include "content/public/browser/resource_throttle.h" 30 #include "content/public/browser/resource_throttle.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/common/file_chooser_file_info.h" 31 #include "content/public/common/file_chooser_file_info.h"
32 #include "content/public/common/file_chooser_params.h" 32 #include "content/public/common/file_chooser_params.h"
33 #include "content/public/test/browser_test_utils.h" 33 #include "content/public/test/browser_test_utils.h"
34 #include "content/public/test/content_browser_test_utils.h" 34 #include "content/public/test/content_browser_test_utils.h"
35 #include "content/shell/browser/shell.h" 35 #include "content/shell/browser/shell.h"
36 #include "content/shell/browser/shell_javascript_dialog_manager.h" 36 #include "content/shell/browser/shell_javascript_dialog_manager.h"
37 #include "content/test/test_frame_navigation_observer.h" 37 #include "content/test/test_frame_navigation_observer.h"
38 #include "net/url_request/url_request.h" 38 #include "net/url_request/url_request.h"
39 39
40 namespace content { 40 namespace content {
41 41
42 namespace {
43
44 // Helper class used by the TestNavigationManager to pause navigations.
45 class TestNavigationManagerThrottle : public NavigationThrottle {
46 public:
47 TestNavigationManagerThrottle(NavigationHandle* handle,
48 base::Closure on_will_start_request_closure)
49 : NavigationThrottle(handle),
50 on_will_start_request_closure_(on_will_start_request_closure) {}
51 ~TestNavigationManagerThrottle() override {}
52
53 private:
54 // NavigationThrottle implementation.
55 NavigationThrottle::ThrottleCheckResult WillStartRequest() override {
56 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
57 on_will_start_request_closure_);
58 return NavigationThrottle::DEFER;
59 }
60
61 base::Closure on_will_start_request_closure_;
62 };
63
64 } // namespace
65
66 void NavigateFrameToURL(FrameTreeNode* node, const GURL& url) { 42 void NavigateFrameToURL(FrameTreeNode* node, const GURL& url) {
67 TestFrameNavigationObserver observer(node); 43 TestFrameNavigationObserver observer(node);
68 NavigationController::LoadURLParams params(url); 44 NavigationController::LoadURLParams params(url);
69 params.transition_type = ui::PAGE_TRANSITION_LINK; 45 params.transition_type = ui::PAGE_TRANSITION_LINK;
70 params.frame_tree_node_id = node->frame_tree_node_id(); 46 params.frame_tree_node_id = node->frame_tree_node_id();
71 node->navigator()->GetController()->LoadURLWithParams(params); 47 node->navigator()->GetController()->LoadURLWithParams(params);
72 observer.Wait(); 48 observer.Wait();
73 } 49 }
74 50
75 void SetShouldProceedOnBeforeUnload(Shell* shell, bool proceed) { 51 void SetShouldProceedOnBeforeUnload(Shell* shell, bool proceed) {
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 net::URLRequest* request, 336 net::URLRequest* request,
361 content::ResourceContext* resource_context, 337 content::ResourceContext* resource_context,
362 content::AppCacheService* appcache_service, 338 content::AppCacheService* appcache_service,
363 ResourceType resource_type, 339 ResourceType resource_type,
364 ScopedVector<content::ResourceThrottle>* throttles) { 340 ScopedVector<content::ResourceThrottle>* throttles) {
365 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 341 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
366 if (request->url() == url_) 342 if (request->url() == url_)
367 throttles->push_back(new HttpRequestStallThrottle); 343 throttles->push_back(new HttpRequestStallThrottle);
368 } 344 }
369 345
370 TestNavigationManager::TestNavigationManager(
371 int filtering_frame_tree_node_id,
372 WebContents* web_contents,
373 const GURL& url)
374 : WebContentsObserver(web_contents),
375 filtering_frame_tree_node_id_(filtering_frame_tree_node_id),
376 url_(url),
377 navigation_paused_(false),
378 handle_(nullptr),
379 weak_factory_(this) {}
380
381 TestNavigationManager::TestNavigationManager(WebContents* web_contents,
382 const GURL& url)
383 : TestNavigationManager(FrameTreeNode::kFrameTreeNodeInvalidId,
384 web_contents,
385 url) {}
386
387 TestNavigationManager::~TestNavigationManager() {
388 ResumeNavigation();
389 }
390
391 void TestNavigationManager::WaitForWillStartRequest() {
392 DCHECK(!did_finish_loop_runner_);
393 if (navigation_paused_)
394 return;
395 will_start_loop_runner_ = new MessageLoopRunner();
396 will_start_loop_runner_->Run();
397 will_start_loop_runner_ = nullptr;
398 }
399
400 void TestNavigationManager::WaitForNavigationFinished() {
401 DCHECK(!will_start_loop_runner_);
402 // Ensure the navigation is resumed if the manager paused it previously.
403 if (navigation_paused_)
404 ResumeNavigation();
405 did_finish_loop_runner_ = new MessageLoopRunner();
406 did_finish_loop_runner_->Run();
407 did_finish_loop_runner_ = nullptr;
408 }
409
410 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) {
411 if (handle_ || handle->GetURL() != url_)
412 return;
413
414 if (filtering_frame_tree_node_id_ != FrameTreeNode::kFrameTreeNodeInvalidId &&
415 handle->GetFrameTreeNodeId() != filtering_frame_tree_node_id_) {
416 return;
417 }
418
419 handle_ = handle;
420 std::unique_ptr<NavigationThrottle> throttle(
421 new TestNavigationManagerThrottle(
422 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest,
423 weak_factory_.GetWeakPtr())));
424 handle_->RegisterThrottleForTesting(std::move(throttle));
425 }
426
427 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) {
428 if (handle != handle_)
429 return;
430 handle_ = nullptr;
431 navigation_paused_ = false;
432 if (did_finish_loop_runner_)
433 did_finish_loop_runner_->Quit();
434 }
435
436 void TestNavigationManager::OnWillStartRequest() {
437 navigation_paused_ = true;
438 if (will_start_loop_runner_)
439 will_start_loop_runner_->Quit();
440
441 // If waiting for the navigation to finish, resume the navigation.
442 if (did_finish_loop_runner_)
443 ResumeNavigation();
444 }
445
446 void TestNavigationManager::ResumeNavigation() {
447 if (!navigation_paused_ || !handle_)
448 return;
449 navigation_paused_ = false;
450 handle_->Resume();
451 }
452
453 FileChooserDelegate::FileChooserDelegate(const base::FilePath& file) 346 FileChooserDelegate::FileChooserDelegate(const base::FilePath& file)
454 : file_(file), file_chosen_(false) {} 347 : file_(file), file_chosen_(false) {}
455 348
456 void FileChooserDelegate::RunFileChooser(RenderFrameHost* render_frame_host, 349 void FileChooserDelegate::RunFileChooser(RenderFrameHost* render_frame_host,
457 const FileChooserParams& params) { 350 const FileChooserParams& params) {
458 // Send the selected file to the renderer process. 351 // Send the selected file to the renderer process.
459 FileChooserFileInfo file_info; 352 FileChooserFileInfo file_info;
460 file_info.file_path = file_; 353 file_info.file_path = file_;
461 std::vector<FileChooserFileInfo> files; 354 std::vector<FileChooserFileInfo> files;
462 files.push_back(file_info); 355 files.push_back(file_info);
463 render_frame_host->FilesSelectedInChooser(files, FileChooserParams::Open); 356 render_frame_host->FilesSelectedInChooser(files, FileChooserParams::Open);
464 357
465 file_chosen_ = true; 358 file_chosen_ = true;
466 } 359 }
467 360
361 PerFrameTestNavigationManager::PerFrameTestNavigationManager(
362 int filtering_frame_tree_node_id,
363 WebContents* web_contents,
364 const GURL& url)
365 : TestNavigationManager(web_contents, url),
366 filtering_frame_tree_node_id_(filtering_frame_tree_node_id) {}
367
368 bool PerFrameTestNavigationManager::ShouldMonitorNavigation(
369 NavigationHandle* handle) {
370 return TestNavigationManager::ShouldMonitorNavigation(handle) &&
371 handle->GetFrameTreeNodeId() == filtering_frame_tree_node_id_;
372 }
373
468 } // namespace content 374 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698