1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PAGE_LOAD_METRICS_RENDERER_FAKE_PAGE_TIMING_SENDER_H_
#define COMPONENTS_PAGE_LOAD_METRICS_RENDERER_FAKE_PAGE_TIMING_SENDER_H_
#include <set>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "components/page_load_metrics/common/page_load_metrics.mojom.h"
#include "components/page_load_metrics/common/page_load_timing.h"
#include "components/page_load_metrics/renderer/page_timing_sender.h"
#include "third_party/blink/public/common/use_counter/use_counter_feature.h"
namespace page_load_metrics {
// PageTimingSender implementation for use in tests. Allows for setting and
// verifying basic expectations when sending PageLoadTiming. By default,
// FakePageTimingSender will verify that expected and actual
// PageLoadTimings match on each invocation to ExpectPageLoadTiming() and
// SendTiming(), as well as in the destructor. Tests can force additional
// validations by calling VerifyExpectedTimings.
//
// Expected PageLoadTimings are specified via ExpectPageLoadTiming, and actual
// PageLoadTimings are dispatched through SendTiming(). When SendTiming() is
// called, we verify that the actual PageLoadTimings dipatched through
// SendTiming() match the expected PageLoadTimings provided via
// ExpectPageLoadTiming.
//
// Normally, gmock would be used in place of this class, but gmock is not
// compatible with structures that use aligned memory, and PageLoadTiming uses
// std::optional which uses aligned memory, so we're forced to roll
// our own implementation here. See
// https://groups.google.com/forum/#!topic/googletestframework/W-Hud3j_c6I for
// more details.
class FakePageTimingSender : public PageTimingSender {
public:
class PageTimingValidator {
public:
PageTimingValidator();
PageTimingValidator(const PageTimingValidator&) = delete;
PageTimingValidator& operator=(const PageTimingValidator&) = delete;
~PageTimingValidator();
// PageLoadTimings that are expected to be sent through SendTiming() should
// be passed to ExpectPageLoadTiming.
void ExpectPageLoadTiming(const mojom::PageLoadTiming& timing);
void ExpectSoftNavigationMetrics(
const mojom::SoftNavigationMetrics& soft_navigation_metrics);
// CpuTimings that are expected to be sent through SendTiming() should be
// passed to ExpectCpuTiming.
void ExpectCpuTiming(const base::TimeDelta& timing);
// Forces verification that actual timings sent through SendTiming() match
// expected timings provided via ExpectPageLoadTiming.
void VerifyExpectedTimings() const;
void VerifyExpectedSoftNavigationMetrics() const;
// Forces verification that actual timings sent through SendTiming() match
// expected timings provided via ExpectCpuTiming.
void VerifyExpectedCpuTimings() const;
void VerifyExpectedInteractionTiming() const;
void VerifyExpectedSubresourceLoadMetrics() const;
// PageLoad features that are expected to be sent through SendTiming()
// should be passed via UpdateExpectedPageLoadFeatures.
void UpdateExpectPageLoadFeatures(const blink::UseCounterFeature& feature);
void UpdateExpectFrameRenderDataUpdate(
const mojom::FrameRenderDataUpdate& render_data) {
expected_render_data_ = render_data.Clone();
}
void UpdateExpectedInteractionTiming(
const base::TimeDelta interaction_duration,
uint64_t interaction_offset,
const base::TimeTicks interaction_time);
void UpdateExpectedSubresourceLoadMetrics(
const blink::SubresourceLoadMetrics& subresource_load_metrics);
void UpdateExpectedMainFrameIntersectionRect(
const gfx::Rect& main_frame_intersection_rect) {
expected_main_frame_intersection_rect_ = main_frame_intersection_rect;
}
void UpdateExpectedMainFrameViewportRect(
const gfx::Rect& main_frame_viewport_rect) {
expected_main_frame_viewport_rect_ = main_frame_viewport_rect;
}
// Forces verification that actual features sent through SendTiming match
// expected features provided via ExpectPageLoadFeatures.
void VerifyExpectedFeatures() const;
void VerifyExpectedRenderData() const;
void VerifyExpectedMainFrameIntersectionRect() const;
void VerifyExpectedMainFrameViewportRect() const;
const std::vector<mojom::PageLoadTimingPtr>& expected_timings() const {
return expected_timings_;
}
const std::vector<mojom::PageLoadTimingPtr>& actual_timings() const {
return actual_timings_;
}
void UpdateTiming(
const mojom::PageLoadTimingPtr& timing,
const mojom::FrameMetadataPtr& metadata,
const std::vector<blink::UseCounterFeature>& new_features,
const std::vector<mojom::ResourceDataUpdatePtr>& resources,
const mojom::FrameRenderDataUpdate& render_data,
const mojom::CpuTimingPtr& cpu_timing,
const mojom::InputTimingPtr& input_timing,
const std::optional<blink::SubresourceLoadMetrics>&
subresource_load_metrics,
const mojom::SoftNavigationMetricsPtr& soft_navigation_metrics);
private:
std::vector<mojom::PageLoadTimingPtr> expected_timings_;
std::vector<mojom::PageLoadTimingPtr> actual_timings_;
std::vector<mojom::SoftNavigationMetricsPtr>
expected_soft_navigation_metrics_;
std::vector<mojom::SoftNavigationMetricsPtr>
actual_soft_navigation_metrics_;
std::vector<mojom::CpuTimingPtr> expected_cpu_timings_;
std::vector<mojom::CpuTimingPtr> actual_cpu_timings_;
std::set<blink::UseCounterFeature> expected_features_;
std::set<blink::UseCounterFeature> actual_features_;
mojom::FrameRenderDataUpdatePtr expected_render_data_;
mojom::FrameRenderDataUpdate actual_render_data_;
std::optional<gfx::Rect> expected_main_frame_intersection_rect_;
std::optional<gfx::Rect> actual_main_frame_intersection_rect_;
std::optional<gfx::Rect> expected_main_frame_viewport_rect_;
std::optional<gfx::Rect> actual_main_frame_viewport_rect_;
mojom::InputTiming expected_input_timing;
mojom::InputTiming actual_input_timing;
std::optional<blink::SubresourceLoadMetrics>
expected_subresource_load_metrics_;
std::optional<blink::SubresourceLoadMetrics>
actual_subresource_load_metrics_;
};
explicit FakePageTimingSender(PageTimingValidator* validator);
FakePageTimingSender(const FakePageTimingSender&) = delete;
FakePageTimingSender& operator=(const FakePageTimingSender&) = delete;
~FakePageTimingSender() override;
void SendTiming(
const mojom::PageLoadTimingPtr& timing,
const mojom::FrameMetadataPtr& metadata,
const std::vector<blink::UseCounterFeature>& new_features,
std::vector<mojom::ResourceDataUpdatePtr> resources,
const mojom::FrameRenderDataUpdate& render_data,
const mojom::CpuTimingPtr& cpu_timing,
mojom::InputTimingPtr new_input_timing,
const std::optional<blink::SubresourceLoadMetrics>&
subresource_load_metrics,
const mojom::SoftNavigationMetricsPtr& soft_navigation_metrics) override;
void SetUpUkmReporting(
base::ReadOnlySharedMemoryRegion smoothness_memory,
base::ReadOnlySharedMemoryRegion dropped_frames_memory) override;
void SendCustomUserTiming(mojom::CustomUserTimingMarkPtr timing) override;
private:
const raw_ptr<PageTimingValidator> validator_;
};
} // namespace page_load_metrics
#endif // COMPONENTS_PAGE_LOAD_METRICS_RENDERER_FAKE_PAGE_TIMING_SENDER_H_
|