fake_page_timing_sender.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_PAGE_LOAD_METRICS_RENDERER_FAKE_PAGE_TIMING_SENDER_H_
  5. #define COMPONENTS_PAGE_LOAD_METRICS_RENDERER_FAKE_PAGE_TIMING_SENDER_H_
  6. #include <set>
  7. #include <vector>
  8. #include "components/page_load_metrics/common/page_load_metrics.mojom.h"
  9. #include "components/page_load_metrics/common/page_load_timing.h"
  10. #include "components/page_load_metrics/renderer/page_timing_sender.h"
  11. #include "third_party/blink/public/common/use_counter/use_counter_feature.h"
  12. #include "third_party/blink/public/mojom/mobile_metrics/mobile_friendliness.mojom.h"
  13. namespace page_load_metrics {
  14. // PageTimingSender implementation for use in tests. Allows for setting and
  15. // verifying basic expectations when sending PageLoadTiming. By default,
  16. // FakePageTimingSender will verify that expected and actual
  17. // PageLoadTimings match on each invocation to ExpectPageLoadTiming() and
  18. // SendTiming(), as well as in the destructor. Tests can force additional
  19. // validations by calling VerifyExpectedTimings.
  20. //
  21. // Expected PageLoadTimings are specified via ExpectPageLoadTiming, and actual
  22. // PageLoadTimings are dispatched through SendTiming(). When SendTiming() is
  23. // called, we verify that the actual PageLoadTimings dipatched through
  24. // SendTiming() match the expected PageLoadTimings provided via
  25. // ExpectPageLoadTiming.
  26. //
  27. // Normally, gmock would be used in place of this class, but gmock is not
  28. // compatible with structures that use aligned memory, and PageLoadTiming uses
  29. // absl::optional which uses aligned memory, so we're forced to roll
  30. // our own implementation here. See
  31. // https://groups.google.com/forum/#!topic/googletestframework/W-Hud3j_c6I for
  32. // more details.
  33. class FakePageTimingSender : public PageTimingSender {
  34. public:
  35. class PageTimingValidator {
  36. public:
  37. PageTimingValidator();
  38. PageTimingValidator(const PageTimingValidator&) = delete;
  39. PageTimingValidator& operator=(const PageTimingValidator&) = delete;
  40. ~PageTimingValidator();
  41. // PageLoadTimings that are expected to be sent through SendTiming() should
  42. // be passed to ExpectPageLoadTiming.
  43. void ExpectPageLoadTiming(const mojom::PageLoadTiming& timing);
  44. // CpuTimings that are expected to be sent through SendTiming() should be
  45. // passed to ExpectCpuTiming.
  46. void ExpectCpuTiming(const base::TimeDelta& timing);
  47. // Forces verification that actual timings sent through SendTiming() match
  48. // expected timings provided via ExpectPageLoadTiming.
  49. void VerifyExpectedTimings() const;
  50. // Forces verification that actual timings sent through SendTiming() match
  51. // expected timings provided via ExpectCpuTiming.
  52. void VerifyExpectedCpuTimings() const;
  53. void VerifyExpectedInputTiming() const;
  54. void VerifyExpectedMobileFriendliness() const;
  55. // PageLoad features that are expected to be sent through SendTiming()
  56. // should be passed via UpdateExpectedPageLoadFeatures.
  57. void UpdateExpectPageLoadFeatures(const blink::UseCounterFeature& feature);
  58. void UpdateExpectFrameRenderDataUpdate(
  59. const mojom::FrameRenderDataUpdate& render_data) {
  60. expected_render_data_ = render_data.Clone();
  61. }
  62. void UpdateExpectedInputTiming(const base::TimeDelta input_delay);
  63. void UpdateExpectedMobileFriendliness(
  64. const blink::MobileFriendliness& mobile_friendliness);
  65. void UpdateExpectedMainFrameIntersectionRect(
  66. const gfx::Rect& main_frame_intersection_rect) {
  67. expected_main_frame_intersection_rect_ = main_frame_intersection_rect;
  68. }
  69. void UpdateExpectedMainFrameViewportRect(
  70. const gfx::Rect& main_frame_viewport_rect) {
  71. expected_main_frame_viewport_rect_ = main_frame_viewport_rect;
  72. }
  73. // Forces verification that actual features sent through SendTiming match
  74. // expected features provided via ExpectPageLoadFeatures.
  75. void VerifyExpectedFeatures() const;
  76. void VerifyExpectedRenderData() const;
  77. void VerifyExpectedMainFrameIntersectionRect() const;
  78. void VerifyExpectedMainFrameViewportRect() const;
  79. const std::vector<mojom::PageLoadTimingPtr>& expected_timings() const {
  80. return expected_timings_;
  81. }
  82. const std::vector<mojom::PageLoadTimingPtr>& actual_timings() const {
  83. return actual_timings_;
  84. }
  85. void UpdateTiming(
  86. const mojom::PageLoadTimingPtr& timing,
  87. const mojom::FrameMetadataPtr& metadata,
  88. const std::vector<blink::UseCounterFeature>& new_features,
  89. const std::vector<mojom::ResourceDataUpdatePtr>& resources,
  90. const mojom::FrameRenderDataUpdate& render_data,
  91. const mojom::CpuTimingPtr& cpu_timing,
  92. const mojom::InputTimingPtr& input_timing,
  93. const absl::optional<blink::MobileFriendliness>& mobile_friendliness,
  94. uint32_t soft_navigation_count);
  95. private:
  96. std::vector<mojom::PageLoadTimingPtr> expected_timings_;
  97. std::vector<mojom::PageLoadTimingPtr> actual_timings_;
  98. std::vector<mojom::CpuTimingPtr> expected_cpu_timings_;
  99. std::vector<mojom::CpuTimingPtr> actual_cpu_timings_;
  100. std::set<blink::UseCounterFeature> expected_features_;
  101. std::set<blink::UseCounterFeature> actual_features_;
  102. mojom::FrameRenderDataUpdatePtr expected_render_data_;
  103. mojom::FrameRenderDataUpdate actual_render_data_;
  104. absl::optional<gfx::Rect> expected_main_frame_intersection_rect_;
  105. absl::optional<gfx::Rect> actual_main_frame_intersection_rect_;
  106. absl::optional<gfx::Rect> expected_main_frame_viewport_rect_;
  107. absl::optional<gfx::Rect> actual_main_frame_viewport_rect_;
  108. mojom::InputTimingPtr expected_input_timing;
  109. mojom::InputTimingPtr actual_input_timing;
  110. absl::optional<blink::MobileFriendliness> expected_mobile_friendliness;
  111. absl::optional<blink::MobileFriendliness> actual_mobile_friendliness;
  112. };
  113. explicit FakePageTimingSender(PageTimingValidator* validator);
  114. FakePageTimingSender(const FakePageTimingSender&) = delete;
  115. FakePageTimingSender& operator=(const FakePageTimingSender&) = delete;
  116. ~FakePageTimingSender() override;
  117. void SendTiming(
  118. const mojom::PageLoadTimingPtr& timing,
  119. const mojom::FrameMetadataPtr& metadata,
  120. const std::vector<blink::UseCounterFeature>& new_features,
  121. std::vector<mojom::ResourceDataUpdatePtr> resources,
  122. const mojom::FrameRenderDataUpdate& render_data,
  123. const mojom::CpuTimingPtr& cpu_timing,
  124. mojom::InputTimingPtr new_input_timing,
  125. const absl::optional<blink::MobileFriendliness>& mobile_friendliness,
  126. uint32_t soft_navigation_count) override;
  127. void SetUpSmoothnessReporting(
  128. base::ReadOnlySharedMemoryRegion shared_memory) override;
  129. private:
  130. PageTimingValidator* const validator_;
  131. };
  132. } // namespace page_load_metrics
  133. #endif // COMPONENTS_PAGE_LOAD_METRICS_RENDERER_FAKE_PAGE_TIMING_SENDER_H_