OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/page_load_metrics/renderer/page_timing_metrics_sender.h" |
| 6 |
| 7 #include "base/time/time.h" |
| 8 #include "base/timer/mock_timer.h" |
| 9 #include "components/page_load_metrics/common/page_load_metrics_messages.h" |
| 10 #include "components/page_load_metrics/common/page_load_timing.h" |
| 11 #include "ipc/ipc_message.h" |
| 12 #include "ipc/ipc_message_macros.h" |
| 13 #include "ipc/ipc_sender.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace page_load_metrics { |
| 18 |
| 19 class MockIPCSender : public IPC::Sender { |
| 20 public: |
| 21 bool Send(IPC::Message* message) { |
| 22 IPC_BEGIN_MESSAGE_MAP(MockIPCSender, *message) |
| 23 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) |
| 24 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
| 25 IPC_END_MESSAGE_MAP() |
| 26 |
| 27 delete message; |
| 28 return true; |
| 29 } |
| 30 |
| 31 MOCK_METHOD1(OnTimingUpdated, void(PageLoadTiming)); |
| 32 }; |
| 33 |
| 34 // Thin wrapper around PageTimingMetricsSender that provides access to the |
| 35 // MockTimer instance. |
| 36 class TestPageTimingMetricsSender : public PageTimingMetricsSender { |
| 37 public: |
| 38 explicit TestPageTimingMetricsSender(IPC::Sender* ipc_sender) |
| 39 : PageTimingMetricsSender( |
| 40 ipc_sender, |
| 41 MSG_ROUTING_NONE, |
| 42 scoped_ptr<base::Timer>(new base::MockTimer(false, false))) {} |
| 43 |
| 44 base::MockTimer* mock_timer() const { |
| 45 return reinterpret_cast<base::MockTimer*>(timer()); |
| 46 } |
| 47 }; |
| 48 |
| 49 class PageTimingMetricsSenderTest : public testing::Test { |
| 50 public: |
| 51 PageTimingMetricsSenderTest() : metrics_sender_(&mock_ipc_sender_) {} |
| 52 |
| 53 protected: |
| 54 testing::StrictMock<MockIPCSender> mock_ipc_sender_; |
| 55 TestPageTimingMetricsSender metrics_sender_; |
| 56 }; |
| 57 |
| 58 TEST_F(PageTimingMetricsSenderTest, Basic) { |
| 59 base::Time nav_start = base::Time::FromDoubleT(10); |
| 60 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); |
| 61 |
| 62 PageLoadTiming timing; |
| 63 timing.navigation_start = nav_start; |
| 64 timing.first_layout = first_layout; |
| 65 |
| 66 metrics_sender_.Send(timing); |
| 67 |
| 68 // Firing the timer should trigger sending of an OnTimingUpdated IPC. |
| 69 EXPECT_CALL(mock_ipc_sender_, OnTimingUpdated(timing)); |
| 70 ASSERT_TRUE(metrics_sender_.mock_timer()->IsRunning()); |
| 71 metrics_sender_.mock_timer()->Fire(); |
| 72 EXPECT_FALSE(metrics_sender_.mock_timer()->IsRunning()); |
| 73 |
| 74 // At this point, we should have triggered the send of the PageLoadTiming IPC. |
| 75 testing::Mock::VerifyAndClearExpectations(&mock_ipc_sender_); |
| 76 |
| 77 // Attempt to send the same timing instance again. The send should be |
| 78 // suppressed, since the timing instance hasn't changed since the last send. |
| 79 metrics_sender_.Send(timing); |
| 80 EXPECT_FALSE(metrics_sender_.mock_timer()->IsRunning()); |
| 81 } |
| 82 |
| 83 TEST_F(PageTimingMetricsSenderTest, CoalesceMultipleIPCs) { |
| 84 base::Time nav_start = base::Time::FromDoubleT(10); |
| 85 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); |
| 86 base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(4); |
| 87 |
| 88 PageLoadTiming timing; |
| 89 timing.navigation_start = nav_start; |
| 90 timing.first_layout = first_layout; |
| 91 |
| 92 metrics_sender_.Send(timing); |
| 93 ASSERT_TRUE(metrics_sender_.mock_timer()->IsRunning()); |
| 94 |
| 95 // Send an updated PageLoadTiming before the timer has fired. When the timer |
| 96 // fires, the updated PageLoadTiming should be sent. |
| 97 timing.load_event_start = load_event; |
| 98 metrics_sender_.Send(timing); |
| 99 |
| 100 // Firing the timer should trigger sending of the OnTimingUpdated IPC with |
| 101 // the most recently provided PageLoadTiming instance. |
| 102 EXPECT_CALL(mock_ipc_sender_, OnTimingUpdated(timing)); |
| 103 metrics_sender_.mock_timer()->Fire(); |
| 104 EXPECT_FALSE(metrics_sender_.mock_timer()->IsRunning()); |
| 105 } |
| 106 |
| 107 TEST_F(PageTimingMetricsSenderTest, MultipleIPCs) { |
| 108 base::Time nav_start = base::Time::FromDoubleT(10); |
| 109 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); |
| 110 base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(4); |
| 111 |
| 112 PageLoadTiming timing; |
| 113 timing.navigation_start = nav_start; |
| 114 timing.first_layout = first_layout; |
| 115 |
| 116 metrics_sender_.Send(timing); |
| 117 ASSERT_TRUE(metrics_sender_.mock_timer()->IsRunning()); |
| 118 EXPECT_CALL(mock_ipc_sender_, OnTimingUpdated(timing)); |
| 119 metrics_sender_.mock_timer()->Fire(); |
| 120 EXPECT_FALSE(metrics_sender_.mock_timer()->IsRunning()); |
| 121 testing::Mock::VerifyAndClearExpectations(&mock_ipc_sender_); |
| 122 |
| 123 // Send an updated PageLoadTiming after the timer for the first send request |
| 124 // has fired, and verify that a second IPC is sent. |
| 125 timing.load_event_start = load_event; |
| 126 metrics_sender_.Send(timing); |
| 127 ASSERT_TRUE(metrics_sender_.mock_timer()->IsRunning()); |
| 128 EXPECT_CALL(mock_ipc_sender_, OnTimingUpdated(timing)); |
| 129 metrics_sender_.mock_timer()->Fire(); |
| 130 EXPECT_FALSE(metrics_sender_.mock_timer()->IsRunning()); |
| 131 } |
| 132 |
| 133 TEST_F(PageTimingMetricsSenderTest, SendIPCOnDestructor) { |
| 134 PageLoadTiming timing; |
| 135 timing.navigation_start = base::Time::FromDoubleT(10); |
| 136 { |
| 137 // This test wants to verify behavior in the PageTimingMetricsSender |
| 138 // destructor, so we create our own instance to make it go out of scope |
| 139 // before the end of the test body. |
| 140 TestPageTimingMetricsSender sender(&mock_ipc_sender_); |
| 141 |
| 142 sender.Send(timing); |
| 143 EXPECT_CALL(mock_ipc_sender_, OnTimingUpdated(timing)); |
| 144 ASSERT_TRUE(sender.mock_timer()->IsRunning()); |
| 145 } |
| 146 } |
| 147 |
| 148 } // namespace page_load_metrics |
OLD | NEW |