| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/observers/amp_page_load_metrics_obser
ver.h" | 5 #include "chrome/browser/page_load_metrics/observers/amp_page_load_metrics_obser
ver.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/optional.h" | 10 #include "base/optional.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_
test_harness.h" | 12 #include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_
test_harness.h" |
| 13 #include "chrome/common/page_load_metrics/page_load_timing.h" | 13 #include "chrome/common/page_load_metrics/page_load_timing.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 class AMPPageLoadMetricsObserverTest | 16 class AMPPageLoadMetricsObserverTest |
| 17 : public page_load_metrics::PageLoadMetricsObserverTestHarness { | 17 : public page_load_metrics::PageLoadMetricsObserverTestHarness { |
| 18 public: | 18 public: |
| 19 AMPPageLoadMetricsObserverTest() {} | 19 AMPPageLoadMetricsObserverTest() {} |
| 20 | 20 |
| 21 void ResetTest() { | 21 void ResetTest() { |
| 22 page_load_metrics::InitPageLoadTimingForTest(&timing_); |
| 22 // Reset to the default testing state. Does not reset histogram state. | 23 // Reset to the default testing state. Does not reset histogram state. |
| 23 timing_.navigation_start = base::Time::FromDoubleT(1); | 24 timing_.navigation_start = base::Time::FromDoubleT(1); |
| 24 timing_.response_start = base::TimeDelta::FromSeconds(2); | 25 timing_.response_start = base::TimeDelta::FromSeconds(2); |
| 25 timing_.parse_timing.parse_start = base::TimeDelta::FromSeconds(3); | 26 timing_.parse_timing->parse_start = base::TimeDelta::FromSeconds(3); |
| 26 timing_.paint_timing.first_contentful_paint = | 27 timing_.paint_timing->first_contentful_paint = |
| 27 base::TimeDelta::FromSeconds(4); | 28 base::TimeDelta::FromSeconds(4); |
| 28 timing_.paint_timing.first_image_paint = base::TimeDelta::FromSeconds(5); | 29 timing_.paint_timing->first_image_paint = base::TimeDelta::FromSeconds(5); |
| 29 timing_.paint_timing.first_text_paint = base::TimeDelta::FromSeconds(6); | 30 timing_.paint_timing->first_text_paint = base::TimeDelta::FromSeconds(6); |
| 30 timing_.document_timing.load_event_start = base::TimeDelta::FromSeconds(7); | 31 timing_.document_timing->load_event_start = base::TimeDelta::FromSeconds(7); |
| 31 PopulateRequiredTimingFields(&timing_); | 32 PopulateRequiredTimingFields(&timing_); |
| 32 } | 33 } |
| 33 | 34 |
| 34 void RunTest(const GURL& url) { | 35 void RunTest(const GURL& url) { |
| 35 NavigateAndCommit(url); | 36 NavigateAndCommit(url); |
| 36 SimulateTimingUpdate(timing_); | 37 SimulateTimingUpdate(timing_); |
| 37 | 38 |
| 38 // Navigate again to force OnComplete, which happens when a new navigation | 39 // Navigate again to force OnComplete, which happens when a new navigation |
| 39 // occurs. | 40 // occurs. |
| 40 NavigateAndCommit(GURL("http://otherurl.com")); | 41 NavigateAndCommit(GURL("http://otherurl.com")); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void ValidateHistograms(bool expect_histograms, const char* view_type) { | 44 void ValidateHistograms(bool expect_histograms, const char* view_type) { |
| 44 ValidateHistogramsFor( | 45 ValidateHistogramsFor( |
| 45 "PageLoad.Clients.AMP.DocumentTiming." | 46 "PageLoad.Clients.AMP.DocumentTiming." |
| 46 "NavigationToDOMContentLoadedEventFired", | 47 "NavigationToDOMContentLoadedEventFired", |
| 47 view_type, timing_.document_timing.dom_content_loaded_event_start, | 48 view_type, timing_.document_timing->dom_content_loaded_event_start, |
| 48 expect_histograms); | 49 expect_histograms); |
| 49 ValidateHistogramsFor( | 50 ValidateHistogramsFor( |
| 50 "PageLoad.Clients.AMP.DocumentTiming.NavigationToFirstLayout", | 51 "PageLoad.Clients.AMP.DocumentTiming.NavigationToFirstLayout", |
| 51 view_type, timing_.document_timing.first_layout, expect_histograms); | 52 view_type, timing_.document_timing->first_layout, expect_histograms); |
| 52 ValidateHistogramsFor( | 53 ValidateHistogramsFor( |
| 53 "PageLoad.Clients.AMP.DocumentTiming." | 54 "PageLoad.Clients.AMP.DocumentTiming." |
| 54 "NavigationToLoadEventFired", | 55 "NavigationToLoadEventFired", |
| 55 view_type, timing_.document_timing.load_event_start, expect_histograms); | 56 view_type, timing_.document_timing->load_event_start, |
| 57 expect_histograms); |
| 56 ValidateHistogramsFor( | 58 ValidateHistogramsFor( |
| 57 "PageLoad.Clients.AMP.PaintTiming." | 59 "PageLoad.Clients.AMP.PaintTiming." |
| 58 "NavigationToFirstContentfulPaint", | 60 "NavigationToFirstContentfulPaint", |
| 59 view_type, timing_.paint_timing.first_contentful_paint, | 61 view_type, timing_.paint_timing->first_contentful_paint, |
| 60 expect_histograms); | 62 expect_histograms); |
| 61 ValidateHistogramsFor( | 63 ValidateHistogramsFor( |
| 62 "PageLoad.Clients.AMP.ParseTiming.NavigationToParseStart", view_type, | 64 "PageLoad.Clients.AMP.ParseTiming.NavigationToParseStart", view_type, |
| 63 timing_.parse_timing.parse_start, expect_histograms); | 65 timing_.parse_timing->parse_start, expect_histograms); |
| 64 } | 66 } |
| 65 | 67 |
| 66 void ValidateHistogramsFor(const std::string& histogram, | 68 void ValidateHistogramsFor(const std::string& histogram, |
| 67 const char* view_type, | 69 const char* view_type, |
| 68 const base::Optional<base::TimeDelta>& event, | 70 const base::Optional<base::TimeDelta>& event, |
| 69 bool expect_histograms) { | 71 bool expect_histograms) { |
| 70 const size_t kTypeOffset = strlen("PageLoad.Clients.AMP."); | 72 const size_t kTypeOffset = strlen("PageLoad.Clients.AMP."); |
| 71 std::string view_type_histogram = histogram; | 73 std::string view_type_histogram = histogram; |
| 72 view_type_histogram.insert(kTypeOffset, view_type); | 74 view_type_histogram.insert(kTypeOffset, view_type); |
| 73 histogram_tester().ExpectTotalCount(histogram, expect_histograms ? 1 : 0); | 75 histogram_tester().ExpectTotalCount(histogram, expect_histograms ? 1 : 0); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 86 event.value().InMilliseconds()), | 88 event.value().InMilliseconds()), |
| 87 1); | 89 1); |
| 88 } | 90 } |
| 89 | 91 |
| 90 protected: | 92 protected: |
| 91 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { | 93 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { |
| 92 tracker->AddObserver(base::WrapUnique(new AMPPageLoadMetricsObserver())); | 94 tracker->AddObserver(base::WrapUnique(new AMPPageLoadMetricsObserver())); |
| 93 } | 95 } |
| 94 | 96 |
| 95 private: | 97 private: |
| 96 page_load_metrics::PageLoadTiming timing_; | 98 page_load_metrics::mojom::PageLoadTiming timing_; |
| 97 | 99 |
| 98 DISALLOW_COPY_AND_ASSIGN(AMPPageLoadMetricsObserverTest); | 100 DISALLOW_COPY_AND_ASSIGN(AMPPageLoadMetricsObserverTest); |
| 99 }; | 101 }; |
| 100 | 102 |
| 101 TEST_F(AMPPageLoadMetricsObserverTest, AMPViewType) { | 103 TEST_F(AMPPageLoadMetricsObserverTest, AMPViewType) { |
| 102 using AMPViewType = AMPPageLoadMetricsObserver::AMPViewType; | 104 using AMPViewType = AMPPageLoadMetricsObserver::AMPViewType; |
| 103 | 105 |
| 104 struct { | 106 struct { |
| 105 AMPViewType expected_type; | 107 AMPViewType expected_type; |
| 106 const char* url; | 108 const char* url; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 ResetTest(); | 144 ResetTest(); |
| 143 RunTest(GURL("https://news.google.com/news/amp?page")); | 145 RunTest(GURL("https://news.google.com/news/amp?page")); |
| 144 ValidateHistograms(true, "GoogleNews."); | 146 ValidateHistograms(true, "GoogleNews."); |
| 145 } | 147 } |
| 146 | 148 |
| 147 TEST_F(AMPPageLoadMetricsObserverTest, NonAMPPage) { | 149 TEST_F(AMPPageLoadMetricsObserverTest, NonAMPPage) { |
| 148 ResetTest(); | 150 ResetTest(); |
| 149 RunTest(GURL("https://www.google.com/not-amp/page")); | 151 RunTest(GURL("https://www.google.com/not-amp/page")); |
| 150 ValidateHistograms(false, ""); | 152 ValidateHistograms(false, ""); |
| 151 } | 153 } |
| OLD | NEW |