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/previews_page_load_metrics_
observer.h" | 5 #include "chrome/browser/page_load_metrics/observers/previews_page_load_metrics_
observer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 }; | 37 }; |
38 | 38 |
39 } // namespace | 39 } // namespace |
40 | 40 |
41 class PreviewsPageLoadMetricsObserverTest | 41 class PreviewsPageLoadMetricsObserverTest |
42 : public page_load_metrics::PageLoadMetricsObserverTestHarness { | 42 : public page_load_metrics::PageLoadMetricsObserverTestHarness { |
43 public: | 43 public: |
44 PreviewsPageLoadMetricsObserverTest() : is_offline_preview_(false) {} | 44 PreviewsPageLoadMetricsObserverTest() : is_offline_preview_(false) {} |
45 | 45 |
46 void ResetTest() { | 46 void ResetTest() { |
| 47 page_load_metrics::InitPageLoadTimingForTest(&timing_); |
47 // Reset to the default testing state. Does not reset histogram state. | 48 // Reset to the default testing state. Does not reset histogram state. |
48 timing_.navigation_start = base::Time::FromDoubleT(1); | 49 timing_.navigation_start = base::Time::FromDoubleT(1); |
49 timing_.response_start = base::TimeDelta::FromSeconds(2); | 50 timing_.response_start = base::TimeDelta::FromSeconds(2); |
50 timing_.parse_timing.parse_start = base::TimeDelta::FromSeconds(3); | 51 timing_.parse_timing->parse_start = base::TimeDelta::FromSeconds(3); |
51 timing_.paint_timing.first_contentful_paint = | 52 timing_.paint_timing->first_contentful_paint = |
52 base::TimeDelta::FromSeconds(4); | 53 base::TimeDelta::FromSeconds(4); |
53 timing_.paint_timing.first_image_paint = base::TimeDelta::FromSeconds(5); | 54 timing_.paint_timing->first_image_paint = base::TimeDelta::FromSeconds(5); |
54 timing_.paint_timing.first_text_paint = base::TimeDelta::FromSeconds(6); | 55 timing_.paint_timing->first_text_paint = base::TimeDelta::FromSeconds(6); |
55 timing_.document_timing.load_event_start = base::TimeDelta::FromSeconds(7); | 56 timing_.document_timing->load_event_start = base::TimeDelta::FromSeconds(7); |
56 PopulateRequiredTimingFields(&timing_); | 57 PopulateRequiredTimingFields(&timing_); |
57 } | 58 } |
58 | 59 |
59 void RunTest(bool is_offline_preview) { | 60 void RunTest(bool is_offline_preview) { |
60 is_offline_preview_ = is_offline_preview; | 61 is_offline_preview_ = is_offline_preview; |
61 NavigateAndCommit(GURL(kDefaultTestUrl1)); | 62 NavigateAndCommit(GURL(kDefaultTestUrl1)); |
62 SimulateTimingUpdate(timing_); | 63 SimulateTimingUpdate(timing_); |
63 | 64 |
64 // Navigate again to force OnComplete, which happens when a new navigation | 65 // Navigate again to force OnComplete, which happens when a new navigation |
65 // occurs. | 66 // occurs. |
66 NavigateAndCommit(GURL(kDefaultTestUrl2)); | 67 NavigateAndCommit(GURL(kDefaultTestUrl2)); |
67 } | 68 } |
68 | 69 |
69 void ValidateHistograms() { | 70 void ValidateHistograms() { |
70 ValidateHistogramsFor( | 71 ValidateHistogramsFor( |
71 internal::kHistogramOfflinePreviewsDOMContentLoadedEventFired, | 72 internal::kHistogramOfflinePreviewsDOMContentLoadedEventFired, |
72 timing_.document_timing.dom_content_loaded_event_start); | 73 timing_.document_timing->dom_content_loaded_event_start); |
73 ValidateHistogramsFor(internal::kHistogramOfflinePreviewsFirstLayout, | 74 ValidateHistogramsFor(internal::kHistogramOfflinePreviewsFirstLayout, |
74 timing_.document_timing.first_layout); | 75 timing_.document_timing->first_layout); |
75 ValidateHistogramsFor(internal::kHistogramOfflinePreviewsLoadEventFired, | 76 ValidateHistogramsFor(internal::kHistogramOfflinePreviewsLoadEventFired, |
76 timing_.document_timing.load_event_start); | 77 timing_.document_timing->load_event_start); |
77 ValidateHistogramsFor( | 78 ValidateHistogramsFor( |
78 internal::kHistogramOfflinePreviewsFirstContentfulPaint, | 79 internal::kHistogramOfflinePreviewsFirstContentfulPaint, |
79 timing_.paint_timing.first_contentful_paint); | 80 timing_.paint_timing->first_contentful_paint); |
80 ValidateHistogramsFor(internal::kHistogramOfflinePreviewsParseStart, | 81 ValidateHistogramsFor(internal::kHistogramOfflinePreviewsParseStart, |
81 timing_.parse_timing.parse_start); | 82 timing_.parse_timing->parse_start); |
82 } | 83 } |
83 | 84 |
84 void ValidateHistogramsFor(const std::string& histogram_, | 85 void ValidateHistogramsFor(const std::string& histogram_, |
85 const base::Optional<base::TimeDelta>& event) { | 86 const base::Optional<base::TimeDelta>& event) { |
86 histogram_tester().ExpectTotalCount(histogram_, | 87 histogram_tester().ExpectTotalCount(histogram_, |
87 is_offline_preview_ ? 1 : 0); | 88 is_offline_preview_ ? 1 : 0); |
88 if (!is_offline_preview_) | 89 if (!is_offline_preview_) |
89 return; | 90 return; |
90 histogram_tester().ExpectUniqueSample( | 91 histogram_tester().ExpectUniqueSample( |
91 histogram_, static_cast<base::HistogramBase::Sample>( | 92 histogram_, static_cast<base::HistogramBase::Sample>( |
92 event.value().InMilliseconds()), | 93 event.value().InMilliseconds()), |
93 1); | 94 1); |
94 } | 95 } |
95 | 96 |
96 protected: | 97 protected: |
97 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { | 98 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { |
98 tracker->AddObserver(base::WrapUnique( | 99 tracker->AddObserver(base::WrapUnique( |
99 new TestPreviewsPageLoadMetricsObserver(is_offline_preview_))); | 100 new TestPreviewsPageLoadMetricsObserver(is_offline_preview_))); |
100 } | 101 } |
101 | 102 |
102 private: | 103 private: |
103 page_load_metrics::PageLoadTiming timing_; | 104 page_load_metrics::mojom::PageLoadTiming timing_; |
104 bool is_offline_preview_; | 105 bool is_offline_preview_; |
105 | 106 |
106 DISALLOW_COPY_AND_ASSIGN(PreviewsPageLoadMetricsObserverTest); | 107 DISALLOW_COPY_AND_ASSIGN(PreviewsPageLoadMetricsObserverTest); |
107 }; | 108 }; |
108 | 109 |
109 TEST_F(PreviewsPageLoadMetricsObserverTest, NoPreview) { | 110 TEST_F(PreviewsPageLoadMetricsObserverTest, NoPreview) { |
110 ResetTest(); | 111 ResetTest(); |
111 RunTest(false); | 112 RunTest(false); |
112 ValidateHistograms(); | 113 ValidateHistograms(); |
113 } | 114 } |
114 | 115 |
115 TEST_F(PreviewsPageLoadMetricsObserverTest, OfflinePreviews) { | 116 TEST_F(PreviewsPageLoadMetricsObserverTest, OfflinePreviews) { |
116 ResetTest(); | 117 ResetTest(); |
117 RunTest(true); | 118 RunTest(true); |
118 ValidateHistograms(); | 119 ValidateHistograms(); |
119 } | 120 } |
120 | 121 |
121 } // namespace previews | 122 } // namespace previews |
OLD | NEW |