OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/renderer/page_load_metrics/page_timing_metrics_sender.h" | 5 #include "chrome/renderer/page_load_metrics/page_timing_metrics_sender.h" |
6 | 6 |
| 7 #include "base/message_loop/message_loop.h" |
7 #include "base/time/time.h" | 8 #include "base/time/time.h" |
8 #include "base/timer/mock_timer.h" | 9 #include "base/timer/mock_timer.h" |
9 #include "chrome/common/page_load_metrics/page_load_timing.h" | 10 #include "chrome/common/page_load_metrics/page_load_timing.h" |
| 11 #include "chrome/renderer/page_load_metrics/fake_page_load_metrics.h" |
10 #include "chrome/renderer/page_load_metrics/fake_page_timing_metrics_ipc_sender.
h" | 12 #include "chrome/renderer/page_load_metrics/fake_page_timing_metrics_ipc_sender.
h" |
| 13 #include "chrome/renderer/page_load_metrics/fake_page_timing_sender.h" |
11 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
| 15 #include "mojo/public/cpp/bindings/binding.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
13 | 17 |
14 namespace page_load_metrics { | 18 namespace page_load_metrics { |
15 | 19 |
16 // Thin wrapper around PageTimingMetricsSender that provides access to the | 20 // Thin wrapper around PageTimingMetricsSender that provides access to the |
17 // MockTimer instance. | 21 // MockTimer instance. |
18 class TestPageTimingMetricsSender : public PageTimingMetricsSender { | 22 class TestPageTimingMetricsSender : public PageTimingMetricsSender { |
19 public: | 23 public: |
20 explicit TestPageTimingMetricsSender(IPC::Sender* ipc_sender, | 24 explicit TestPageTimingMetricsSender(IPC::Sender* ipc_sender, |
| 25 PageTimingSender* page_timing_sender, |
21 const PageLoadTiming& initial_timing) | 26 const PageLoadTiming& initial_timing) |
22 : PageTimingMetricsSender( | 27 : PageTimingMetricsSender( |
23 ipc_sender, | 28 ipc_sender, |
24 MSG_ROUTING_NONE, | 29 MSG_ROUTING_NONE, |
| 30 page_timing_sender, |
25 std::unique_ptr<base::Timer>(new base::MockTimer(false, false)), | 31 std::unique_ptr<base::Timer>(new base::MockTimer(false, false)), |
26 initial_timing) {} | 32 initial_timing) {} |
27 | 33 |
28 base::MockTimer* mock_timer() const { | 34 base::MockTimer* mock_timer() const { |
29 return reinterpret_cast<base::MockTimer*>(timer()); | 35 return reinterpret_cast<base::MockTimer*>(timer()); |
30 } | 36 } |
31 }; | 37 }; |
32 | 38 |
33 class PageTimingMetricsSenderTest : public testing::Test { | 39 class PageTimingMetricsSenderTest : public testing::Test { |
34 public: | 40 public: |
35 PageTimingMetricsSenderTest() | 41 PageTimingMetricsSenderTest() |
36 : metrics_sender_(new TestPageTimingMetricsSender(&fake_ipc_sender_, | 42 : binding_(&fake_page_load_metrics_), |
| 43 metrics_sender_(new TestPageTimingMetricsSender(&fake_ipc_sender_, |
| 44 &fake_sender_, |
37 PageLoadTiming())) {} | 45 PageLoadTiming())) {} |
38 | 46 |
39 protected: | 47 protected: |
| 48 void SetUp() override { |
| 49 mojom::PageLoadMetricsRequest request = |
| 50 MakeRequest(&fake_sender_.page_load_metrics_ptr()); |
| 51 fake_page_load_metrics_.Bind(std::move(request)); |
| 52 } |
| 53 |
| 54 base::MessageLoop loop_; |
| 55 FakePageLoadMetricsImpl fake_page_load_metrics_; |
40 FakePageTimingMetricsIPCSender fake_ipc_sender_; | 56 FakePageTimingMetricsIPCSender fake_ipc_sender_; |
| 57 FakePageTimingSender fake_sender_; |
| 58 mojo::Binding<mojom::PageLoadMetrics> binding_; |
41 std::unique_ptr<TestPageTimingMetricsSender> metrics_sender_; | 59 std::unique_ptr<TestPageTimingMetricsSender> metrics_sender_; |
42 }; | 60 }; |
43 | 61 |
44 TEST_F(PageTimingMetricsSenderTest, Basic) { | 62 TEST_F(PageTimingMetricsSenderTest, Basic) { |
45 base::Time nav_start = base::Time::FromDoubleT(10); | 63 base::Time nav_start = base::Time::FromDoubleT(10); |
46 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); | 64 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); |
47 | 65 |
48 PageLoadTiming timing; | 66 PageLoadTiming timing; |
49 timing.navigation_start = nav_start; | 67 timing.navigation_start = nav_start; |
50 timing.document_timing.first_layout = first_layout; | 68 timing.document_timing.first_layout = first_layout; |
51 | 69 |
52 metrics_sender_->Send(timing); | 70 metrics_sender_->Send(timing); |
53 | 71 |
54 // Firing the timer should trigger sending of an OnTimingUpdated IPC. | 72 // Firing the timer should trigger sending of an OnTimingUpdated IPC. |
55 fake_ipc_sender_.ExpectPageLoadTiming(timing); | 73 fake_ipc_sender_.ExpectPageLoadTiming(timing); |
| 74 fake_page_load_metrics_.ExpectPageLoadTiming(timing); |
56 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); | 75 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); |
57 metrics_sender_->mock_timer()->Fire(); | 76 metrics_sender_->mock_timer()->Fire(); |
58 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); | 77 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); |
59 | 78 |
60 // At this point, we should have triggered the send of the PageLoadTiming IPC. | 79 // At this point, we should have triggered the send of the PageLoadTiming IPC. |
61 fake_ipc_sender_.VerifyExpectedTimings(); | 80 fake_ipc_sender_.VerifyExpectedTimings(); |
| 81 fake_page_load_metrics_.VerifyExpectedTimings(); |
62 | 82 |
63 // Attempt to send the same timing instance again. The send should be | 83 // Attempt to send the same timing instance again. The send should be |
64 // suppressed, since the timing instance hasn't changed since the last send. | 84 // suppressed, since the timing instance hasn't changed since the last send. |
65 metrics_sender_->Send(timing); | 85 metrics_sender_->Send(timing); |
66 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); | 86 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); |
67 } | 87 } |
68 | 88 |
69 TEST_F(PageTimingMetricsSenderTest, CoalesceMultipleIPCs) { | 89 TEST_F(PageTimingMetricsSenderTest, CoalesceMultipleIPCs) { |
70 base::Time nav_start = base::Time::FromDoubleT(10); | 90 base::Time nav_start = base::Time::FromDoubleT(10); |
71 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); | 91 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); |
72 base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(4); | 92 base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(4); |
73 | 93 |
74 PageLoadTiming timing; | 94 PageLoadTiming timing; |
75 timing.navigation_start = nav_start; | 95 timing.navigation_start = nav_start; |
76 timing.document_timing.first_layout = first_layout; | 96 timing.document_timing.first_layout = first_layout; |
77 | 97 |
78 metrics_sender_->Send(timing); | 98 metrics_sender_->Send(timing); |
79 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); | 99 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); |
80 | 100 |
81 // Send an updated PageLoadTiming before the timer has fired. When the timer | 101 // Send an updated PageLoadTiming before the timer has fired. When the timer |
82 // fires, the updated PageLoadTiming should be sent. | 102 // fires, the updated PageLoadTiming should be sent. |
83 timing.document_timing.load_event_start = load_event; | 103 timing.document_timing.load_event_start = load_event; |
84 metrics_sender_->Send(timing); | 104 metrics_sender_->Send(timing); |
85 | 105 |
86 // Firing the timer should trigger sending of the OnTimingUpdated IPC with | 106 // Firing the timer should trigger sending of the OnTimingUpdated IPC with |
87 // the most recently provided PageLoadTiming instance. | 107 // the most recently provided PageLoadTiming instance. |
88 fake_ipc_sender_.ExpectPageLoadTiming(timing); | 108 fake_ipc_sender_.ExpectPageLoadTiming(timing); |
| 109 fake_page_load_metrics_.ExpectPageLoadTiming(timing); |
89 metrics_sender_->mock_timer()->Fire(); | 110 metrics_sender_->mock_timer()->Fire(); |
90 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); | 111 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); |
91 } | 112 } |
92 | 113 |
93 TEST_F(PageTimingMetricsSenderTest, MultipleIPCs) { | 114 TEST_F(PageTimingMetricsSenderTest, MultipleIPCs) { |
94 base::Time nav_start = base::Time::FromDoubleT(10); | 115 base::Time nav_start = base::Time::FromDoubleT(10); |
95 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); | 116 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); |
96 base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(4); | 117 base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(4); |
97 | 118 |
98 PageLoadTiming timing; | 119 PageLoadTiming timing; |
99 timing.navigation_start = nav_start; | 120 timing.navigation_start = nav_start; |
100 timing.document_timing.first_layout = first_layout; | 121 timing.document_timing.first_layout = first_layout; |
101 | 122 |
102 metrics_sender_->Send(timing); | 123 metrics_sender_->Send(timing); |
103 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); | 124 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); |
104 fake_ipc_sender_.ExpectPageLoadTiming(timing); | 125 fake_ipc_sender_.ExpectPageLoadTiming(timing); |
| 126 fake_page_load_metrics_.ExpectPageLoadTiming(timing); |
105 metrics_sender_->mock_timer()->Fire(); | 127 metrics_sender_->mock_timer()->Fire(); |
106 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); | 128 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); |
107 fake_ipc_sender_.VerifyExpectedTimings(); | 129 fake_ipc_sender_.VerifyExpectedTimings(); |
| 130 fake_page_load_metrics_.VerifyExpectedTimings(); |
108 | 131 |
109 // Send an updated PageLoadTiming after the timer for the first send request | 132 // Send an updated PageLoadTiming after the timer for the first send request |
110 // has fired, and verify that a second IPC is sent. | 133 // has fired, and verify that a second IPC is sent. |
111 timing.document_timing.load_event_start = load_event; | 134 timing.document_timing.load_event_start = load_event; |
112 metrics_sender_->Send(timing); | 135 metrics_sender_->Send(timing); |
113 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); | 136 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); |
114 fake_ipc_sender_.ExpectPageLoadTiming(timing); | 137 fake_ipc_sender_.ExpectPageLoadTiming(timing); |
| 138 fake_page_load_metrics_.ExpectPageLoadTiming(timing); |
115 metrics_sender_->mock_timer()->Fire(); | 139 metrics_sender_->mock_timer()->Fire(); |
116 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); | 140 EXPECT_FALSE(metrics_sender_->mock_timer()->IsRunning()); |
117 } | 141 } |
118 | 142 |
119 TEST_F(PageTimingMetricsSenderTest, SendIPCOnDestructor) { | 143 TEST_F(PageTimingMetricsSenderTest, SendIPCOnDestructor) { |
120 PageLoadTiming timing; | 144 PageLoadTiming timing; |
121 timing.navigation_start = base::Time::FromDoubleT(10); | 145 timing.navigation_start = base::Time::FromDoubleT(10); |
122 timing.document_timing.first_layout = base::TimeDelta::FromMilliseconds(10); | 146 timing.document_timing.first_layout = base::TimeDelta::FromMilliseconds(10); |
123 | 147 |
124 // This test wants to verify behavior in the PageTimingMetricsSender | 148 // This test wants to verify behavior in the PageTimingMetricsSender |
125 // destructor. The EXPECT_CALL will be satisfied when the |metrics_sender_| | 149 // destructor. The EXPECT_CALL will be satisfied when the |metrics_sender_| |
126 // is destroyed below. | 150 // is destroyed below. |
127 metrics_sender_->Send(timing); | 151 metrics_sender_->Send(timing); |
128 fake_ipc_sender_.ExpectPageLoadTiming(timing); | 152 fake_ipc_sender_.ExpectPageLoadTiming(timing); |
| 153 fake_page_load_metrics_.ExpectPageLoadTiming(timing); |
129 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); | 154 ASSERT_TRUE(metrics_sender_->mock_timer()->IsRunning()); |
130 | 155 |
131 // Destroy |metrics_sender_|, in order to force its destructor to run. | 156 // Destroy |metrics_sender_|, in order to force its destructor to run. |
132 metrics_sender_.reset(); | 157 metrics_sender_.reset(); |
133 } | 158 } |
134 | 159 |
135 } // namespace page_load_metrics | 160 } // namespace page_load_metrics |
OLD | NEW |