| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/page_load_metrics_observer_
test_harness.h" | 5 #include "chrome/common/page_load_metrics/test/page_load_metrics_test_util.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include "chrome/common/page_load_metrics/page_load_metrics.mojom.h" |
| 8 #include <string> | 8 #include "chrome/common/page_load_metrics/page_load_metrics_util.h" |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 using page_load_metrics::OptionalMin; |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "chrome/browser/page_load_metrics/page_load_metrics_embedder_interface.
h" | |
| 13 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" | |
| 14 #include "chrome/common/page_load_metrics/page_load_metrics_messages.h" | |
| 15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" | |
| 16 #include "content/public/browser/navigation_entry.h" | |
| 17 #include "content/public/browser/web_contents.h" | |
| 18 #include "content/public/browser/web_contents_observer.h" | |
| 19 #include "content/public/test/web_contents_tester.h" | |
| 20 #include "third_party/WebKit/public/platform/WebInputEvent.h" | |
| 21 | 11 |
| 22 namespace page_load_metrics { | 12 void PopulateRequiredTimingFields( |
| 23 | 13 page_load_metrics::mojom::PageLoadTiming* inout_timing) { |
| 24 namespace { | |
| 25 | |
| 26 class TestPageLoadMetricsEmbedderInterface | |
| 27 : public PageLoadMetricsEmbedderInterface { | |
| 28 public: | |
| 29 explicit TestPageLoadMetricsEmbedderInterface( | |
| 30 PageLoadMetricsObserverTestHarness* test) | |
| 31 : test_(test) {} | |
| 32 | |
| 33 bool IsNewTabPageUrl(const GURL& url) override { return false; } | |
| 34 | |
| 35 // Forward the registration logic to the test class so that derived classes | |
| 36 // can override the logic there without depending on the embedder interface. | |
| 37 void RegisterObservers(PageLoadTracker* tracker) override { | |
| 38 test_->RegisterObservers(tracker); | |
| 39 } | |
| 40 | |
| 41 private: | |
| 42 PageLoadMetricsObserverTestHarness* test_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(TestPageLoadMetricsEmbedderInterface); | |
| 45 }; | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 PageLoadMetricsObserverTestHarness::PageLoadMetricsObserverTestHarness() | |
| 50 : ChromeRenderViewHostTestHarness() {} | |
| 51 | |
| 52 PageLoadMetricsObserverTestHarness::~PageLoadMetricsObserverTestHarness() {} | |
| 53 | |
| 54 // static | |
| 55 void PageLoadMetricsObserverTestHarness::PopulateRequiredTimingFields( | |
| 56 mojom::PageLoadTiming* inout_timing) { | |
| 57 if (inout_timing->paint_timing->first_meaningful_paint && | 14 if (inout_timing->paint_timing->first_meaningful_paint && |
| 58 !inout_timing->paint_timing->first_contentful_paint) { | 15 !inout_timing->paint_timing->first_contentful_paint) { |
| 59 inout_timing->paint_timing->first_contentful_paint = | 16 inout_timing->paint_timing->first_contentful_paint = |
| 60 inout_timing->paint_timing->first_meaningful_paint; | 17 inout_timing->paint_timing->first_meaningful_paint; |
| 61 } | 18 } |
| 62 if ((inout_timing->paint_timing->first_text_paint || | 19 if ((inout_timing->paint_timing->first_text_paint || |
| 63 inout_timing->paint_timing->first_image_paint || | 20 inout_timing->paint_timing->first_image_paint || |
| 64 inout_timing->paint_timing->first_contentful_paint) && | 21 inout_timing->paint_timing->first_contentful_paint) && |
| 65 !inout_timing->paint_timing->first_paint) { | 22 !inout_timing->paint_timing->first_paint) { |
| 66 inout_timing->paint_timing->first_paint = | 23 inout_timing->paint_timing->first_paint = |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 base::TimeDelta(); | 70 base::TimeDelta(); |
| 114 } | 71 } |
| 115 if (!inout_timing->parse_timing | 72 if (!inout_timing->parse_timing |
| 116 ->parse_blocked_on_script_execution_from_document_write_duration) { | 73 ->parse_blocked_on_script_execution_from_document_write_duration) { |
| 117 inout_timing->parse_timing | 74 inout_timing->parse_timing |
| 118 ->parse_blocked_on_script_execution_from_document_write_duration = | 75 ->parse_blocked_on_script_execution_from_document_write_duration = |
| 119 base::TimeDelta(); | 76 base::TimeDelta(); |
| 120 } | 77 } |
| 121 } | 78 } |
| 122 } | 79 } |
| 123 | |
| 124 void PageLoadMetricsObserverTestHarness::SetUp() { | |
| 125 ChromeRenderViewHostTestHarness::SetUp(); | |
| 126 SetContents(CreateTestWebContents()); | |
| 127 NavigateAndCommit(GURL("http://www.google.com")); | |
| 128 observer_ = MetricsWebContentsObserver::CreateForWebContents( | |
| 129 web_contents(), base::nullopt, | |
| 130 base::MakeUnique<TestPageLoadMetricsEmbedderInterface>(this)); | |
| 131 web_contents()->WasShown(); | |
| 132 } | |
| 133 | |
| 134 void PageLoadMetricsObserverTestHarness::StartNavigation(const GURL& gurl) { | |
| 135 content::WebContentsTester* web_contents_tester = | |
| 136 content::WebContentsTester::For(web_contents()); | |
| 137 web_contents_tester->StartNavigation(gurl); | |
| 138 } | |
| 139 | |
| 140 void PageLoadMetricsObserverTestHarness::SimulateTimingUpdate( | |
| 141 const mojom::PageLoadTiming& timing) { | |
| 142 SimulateTimingAndMetadataUpdate(timing, mojom::PageLoadMetadata()); | |
| 143 } | |
| 144 | |
| 145 void PageLoadMetricsObserverTestHarness::SimulateTimingAndMetadataUpdate( | |
| 146 const mojom::PageLoadTiming& timing, | |
| 147 const mojom::PageLoadMetadata& metadata) { | |
| 148 observer_->OnTimingUpdated(web_contents()->GetMainFrame(), timing, metadata); | |
| 149 } | |
| 150 | |
| 151 void PageLoadMetricsObserverTestHarness::SimulateStartedResource( | |
| 152 const ExtraRequestStartInfo& info) { | |
| 153 observer_->OnRequestStarted(content::GlobalRequestID(), info.resource_type, | |
| 154 base::TimeTicks::Now()); | |
| 155 } | |
| 156 | |
| 157 void PageLoadMetricsObserverTestHarness::SimulateLoadedResource( | |
| 158 const ExtraRequestCompleteInfo& info) { | |
| 159 observer_->OnRequestComplete( | |
| 160 info.url, info.frame_tree_node_id, content::GlobalRequestID(), | |
| 161 info.resource_type, info.was_cached, | |
| 162 info.data_reduction_proxy_data | |
| 163 ? info.data_reduction_proxy_data->DeepCopy() | |
| 164 : nullptr, | |
| 165 info.raw_body_bytes, info.original_network_content_length, | |
| 166 base::TimeTicks::Now()); | |
| 167 } | |
| 168 | |
| 169 void PageLoadMetricsObserverTestHarness::SimulateInputEvent( | |
| 170 const blink::WebInputEvent& event) { | |
| 171 observer_->OnInputEvent(event); | |
| 172 } | |
| 173 | |
| 174 void PageLoadMetricsObserverTestHarness::SimulateAppEnterBackground() { | |
| 175 observer_->FlushMetricsOnAppEnterBackground(); | |
| 176 } | |
| 177 | |
| 178 void PageLoadMetricsObserverTestHarness::SimulateMediaPlayed() { | |
| 179 content::WebContentsObserver::MediaPlayerInfo video_type( | |
| 180 true /* in_has_video*/); | |
| 181 content::RenderFrameHost* render_frame_host = web_contents()->GetMainFrame(); | |
| 182 observer_->MediaStartedPlaying(video_type, | |
| 183 std::make_pair(render_frame_host, 0)); | |
| 184 } | |
| 185 | |
| 186 const base::HistogramTester& | |
| 187 PageLoadMetricsObserverTestHarness::histogram_tester() const { | |
| 188 return histogram_tester_; | |
| 189 } | |
| 190 | |
| 191 MetricsWebContentsObserver* PageLoadMetricsObserverTestHarness::observer() | |
| 192 const { | |
| 193 return observer_; | |
| 194 } | |
| 195 | |
| 196 const PageLoadExtraInfo | |
| 197 PageLoadMetricsObserverTestHarness::GetPageLoadExtraInfoForCommittedLoad() { | |
| 198 return observer_->GetPageLoadExtraInfoForCommittedLoad(); | |
| 199 } | |
| 200 | |
| 201 void PageLoadMetricsObserverTestHarness::NavigateWithPageTransitionAndCommit( | |
| 202 const GURL& url, | |
| 203 ui::PageTransition transition) { | |
| 204 controller().LoadURL(url, content::Referrer(), transition, std::string()); | |
| 205 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); | |
| 206 } | |
| 207 | |
| 208 } // namespace page_load_metrics | |
| OLD | NEW |