123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- // Copyright 2017 The Chromium Authors. All rights reserved.
- // 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 "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"
- #include "third_party/blink/public/mojom/mobile_metrics/mobile_friendliness.mojom.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
- // absl::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);
- // 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;
- // Forces verification that actual timings sent through SendTiming() match
- // expected timings provided via ExpectCpuTiming.
- void VerifyExpectedCpuTimings() const;
- void VerifyExpectedInputTiming() const;
- void VerifyExpectedMobileFriendliness() 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 UpdateExpectedInputTiming(const base::TimeDelta input_delay);
- void UpdateExpectedMobileFriendliness(
- const blink::MobileFriendliness& mobile_friendliness);
- 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 absl::optional<blink::MobileFriendliness>& mobile_friendliness,
- uint32_t soft_navigation_count);
- private:
- std::vector<mojom::PageLoadTimingPtr> expected_timings_;
- std::vector<mojom::PageLoadTimingPtr> actual_timings_;
- 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_;
- absl::optional<gfx::Rect> expected_main_frame_intersection_rect_;
- absl::optional<gfx::Rect> actual_main_frame_intersection_rect_;
- absl::optional<gfx::Rect> expected_main_frame_viewport_rect_;
- absl::optional<gfx::Rect> actual_main_frame_viewport_rect_;
- mojom::InputTimingPtr expected_input_timing;
- mojom::InputTimingPtr actual_input_timing;
- absl::optional<blink::MobileFriendliness> expected_mobile_friendliness;
- absl::optional<blink::MobileFriendliness> actual_mobile_friendliness;
- };
- 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 absl::optional<blink::MobileFriendliness>& mobile_friendliness,
- uint32_t soft_navigation_count) override;
- void SetUpSmoothnessReporting(
- base::ReadOnlySharedMemoryRegion shared_memory) override;
- private:
- PageTimingValidator* const validator_;
- };
- } // namespace page_load_metrics
- #endif // COMPONENTS_PAGE_LOAD_METRICS_RENDERER_FAKE_PAGE_TIMING_SENDER_H_
|