OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" | 5 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
25 | 25 |
26 namespace page_load_metrics { | 26 namespace page_load_metrics { |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 const char kDefaultTestUrl[] = "https://google.com/"; | 30 const char kDefaultTestUrl[] = "https://google.com/"; |
31 const char kDefaultTestUrlAnchor[] = "https://google.com/#samepage"; | 31 const char kDefaultTestUrlAnchor[] = "https://google.com/#samepage"; |
32 const char kDefaultTestUrl2[] = "https://whatever.com/"; | 32 const char kDefaultTestUrl2[] = "https://whatever.com/"; |
| 33 const char kFilteredStartUrl[] = "https://whatever.com/ignore-on-start"; |
33 const char kFilteredCommitUrl[] = "https://whatever.com/ignore-on-commit"; | 34 const char kFilteredCommitUrl[] = "https://whatever.com/ignore-on-commit"; |
34 | 35 |
35 // Simple PageLoadMetricsObserver that copies observed PageLoadTimings into the | 36 // Simple PageLoadMetricsObserver that copies observed PageLoadTimings into the |
36 // provided std::vector, so they can be analyzed by unit tests. | 37 // provided std::vector, so they can be analyzed by unit tests. |
37 class TestPageLoadMetricsObserver : public PageLoadMetricsObserver { | 38 class TestPageLoadMetricsObserver : public PageLoadMetricsObserver { |
38 public: | 39 public: |
39 TestPageLoadMetricsObserver(std::vector<PageLoadTiming>* updated_timings, | 40 TestPageLoadMetricsObserver(std::vector<PageLoadTiming>* updated_timings, |
40 std::vector<PageLoadTiming>* complete_timings, | 41 std::vector<PageLoadTiming>* complete_timings, |
41 std::vector<GURL>* observed_committed_urls) | 42 std::vector<GURL>* observed_committed_urls) |
42 : updated_timings_(updated_timings), | 43 : updated_timings_(updated_timings), |
43 complete_timings_(complete_timings), | 44 complete_timings_(complete_timings), |
44 observed_committed_urls_(observed_committed_urls) {} | 45 observed_committed_urls_(observed_committed_urls) {} |
45 | 46 |
46 void OnStart(content::NavigationHandle* navigation_handle, | 47 ObservePolicy OnStart(content::NavigationHandle* navigation_handle, |
47 const GURL& currently_committed_url, | 48 const GURL& currently_committed_url, |
48 bool started_in_foreground) override { | 49 bool started_in_foreground) override { |
49 observed_committed_urls_->push_back(currently_committed_url); | 50 observed_committed_urls_->push_back(currently_committed_url); |
| 51 return CONTINUE_OBSERVING; |
50 } | 52 } |
51 | 53 |
52 void OnTimingUpdate(const PageLoadTiming& timing, | 54 void OnTimingUpdate(const PageLoadTiming& timing, |
53 const PageLoadExtraInfo& extra_info) override { | 55 const PageLoadExtraInfo& extra_info) override { |
54 updated_timings_->push_back(timing); | 56 updated_timings_->push_back(timing); |
55 } | 57 } |
56 | 58 |
57 void OnComplete(const PageLoadTiming& timing, | 59 void OnComplete(const PageLoadTiming& timing, |
58 const PageLoadExtraInfo& extra_info) override { | 60 const PageLoadExtraInfo& extra_info) override { |
59 complete_timings_->push_back(timing); | 61 complete_timings_->push_back(timing); |
(...skipping 12 matching lines...) Expand all Loading... |
72 }; | 74 }; |
73 | 75 |
74 // Test PageLoadMetricsObserver that stops observing page loads with certain | 76 // Test PageLoadMetricsObserver that stops observing page loads with certain |
75 // substrings in the URL. | 77 // substrings in the URL. |
76 class FilteringPageLoadMetricsObserver : public PageLoadMetricsObserver { | 78 class FilteringPageLoadMetricsObserver : public PageLoadMetricsObserver { |
77 public: | 79 public: |
78 explicit FilteringPageLoadMetricsObserver( | 80 explicit FilteringPageLoadMetricsObserver( |
79 std::vector<GURL>* completed_filtered_urls) | 81 std::vector<GURL>* completed_filtered_urls) |
80 : completed_filtered_urls_(completed_filtered_urls) {} | 82 : completed_filtered_urls_(completed_filtered_urls) {} |
81 | 83 |
| 84 ObservePolicy OnStart(content::NavigationHandle* handle, |
| 85 const GURL& currently_committed_url, |
| 86 bool started_in_foreground) override { |
| 87 const bool should_ignore = |
| 88 handle->GetURL().spec().find("ignore-on-start") != std::string::npos; |
| 89 return should_ignore ? STOP_OBSERVING : CONTINUE_OBSERVING; |
| 90 } |
| 91 |
82 ObservePolicy OnCommit(content::NavigationHandle* handle) override { | 92 ObservePolicy OnCommit(content::NavigationHandle* handle) override { |
83 const bool should_ignore = | 93 const bool should_ignore = |
84 handle->GetURL().spec().find("ignore-on-commit") != std::string::npos; | 94 handle->GetURL().spec().find("ignore-on-commit") != std::string::npos; |
85 return should_ignore ? STOP_OBSERVING : CONTINUE_OBSERVING; | 95 return should_ignore ? STOP_OBSERVING : CONTINUE_OBSERVING; |
86 } | 96 } |
87 | 97 |
88 void OnComplete(const PageLoadTiming& timing, | 98 void OnComplete(const PageLoadTiming& timing, |
89 const PageLoadExtraInfo& extra_info) override { | 99 const PageLoadExtraInfo& extra_info) override { |
90 completed_filtered_urls_->push_back(extra_info.committed_url); | 100 completed_filtered_urls_->push_back(extra_info.committed_url); |
91 } | 101 } |
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 | 584 |
575 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 585 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
576 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl)}), | 586 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl)}), |
577 completed_filtered_urls()); | 587 completed_filtered_urls()); |
578 | 588 |
579 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 589 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
580 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl), GURL(kDefaultTestUrl2)}), | 590 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl), GURL(kDefaultTestUrl2)}), |
581 completed_filtered_urls()); | 591 completed_filtered_urls()); |
582 } | 592 } |
583 | 593 |
| 594 TEST_F(MetricsWebContentsObserverTest, StopObservingOnStart) { |
| 595 content::WebContentsTester* web_contents_tester = |
| 596 content::WebContentsTester::For(web_contents()); |
| 597 ASSERT_TRUE(completed_filtered_urls().empty()); |
| 598 |
| 599 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 600 ASSERT_TRUE(completed_filtered_urls().empty()); |
| 601 |
| 602 // kFilteredCommitUrl should stop observing in OnStart, and thus should not |
| 603 // reach OnComplete(). |
| 604 web_contents_tester->NavigateAndCommit(GURL(kFilteredStartUrl)); |
| 605 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl)}), |
| 606 completed_filtered_urls()); |
| 607 |
| 608 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 609 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl)}), |
| 610 completed_filtered_urls()); |
| 611 |
| 612 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 613 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl), GURL(kDefaultTestUrl2)}), |
| 614 completed_filtered_urls()); |
| 615 } |
| 616 |
584 } // namespace page_load_metrics | 617 } // namespace page_load_metrics |
OLD | NEW |