OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/metrics_render_frame_observer.h" | 5 #include "chrome/renderer/page_load_metrics/metrics_render_frame_observer.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
11 #include "base/time/time.h" | 13 #include "base/time/time.h" |
12 #include "base/timer/mock_timer.h" | 14 #include "base/timer/mock_timer.h" |
13 #include "chrome/common/page_load_metrics/page_load_timing.h" | 15 #include "chrome/common/page_load_metrics/page_load_timing.h" |
| 16 #include "chrome/renderer/page_load_metrics/fake_page_load_metrics.h" |
14 #include "chrome/renderer/page_load_metrics/fake_page_timing_metrics_ipc_sender.
h" | 17 #include "chrome/renderer/page_load_metrics/fake_page_timing_metrics_ipc_sender.
h" |
| 18 #include "mojo/public/cpp/bindings/interface_request.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
16 | 20 |
17 namespace page_load_metrics { | 21 namespace page_load_metrics { |
18 | 22 |
19 // Implementation of the MetricsRenderFrameObserver class we're testing, | 23 // Implementation of the MetricsRenderFrameObserver class we're testing, |
20 // with the GetTiming() and ShouldSendMetrics() methods stubbed out to make | 24 // with the GetTiming() and ShouldSendMetrics() methods stubbed out to make |
21 // the rest of the class more testable. | 25 // the rest of the class more testable. |
22 class TestMetricsRenderFrameObserver : public MetricsRenderFrameObserver { | 26 class TestMetricsRenderFrameObserver : public MetricsRenderFrameObserver { |
23 public: | 27 public: |
24 TestMetricsRenderFrameObserver() : MetricsRenderFrameObserver(nullptr) {} | 28 TestMetricsRenderFrameObserver() : MetricsRenderFrameObserver(nullptr) {} |
25 | 29 |
26 std::unique_ptr<base::Timer> CreateTimer() const override { | 30 std::unique_ptr<base::Timer> CreateTimer() const override { |
27 if (!mock_timer_) | 31 if (!mock_timer_) |
28 ADD_FAILURE() << "CreateTimer() called, but no MockTimer available."; | 32 ADD_FAILURE() << "CreateTimer() called, but no MockTimer available."; |
29 return std::move(mock_timer_); | 33 return std::move(mock_timer_); |
30 } | 34 } |
31 | 35 |
32 // We intercept sent messages and dispatch them to our | 36 // We intercept sent messages and dispatch them to our |
| 37 // FakePageLoadMetricsImpl, which we use to verify that the expected timing |
| 38 // get sent. |
| 39 void SendTiming(const PageLoadTiming& timing, |
| 40 const PageLoadMetadata& metadata) override { |
| 41 if (!page_load_metrics_ptr_) { |
| 42 mojom::PageLoadMetricsRequest request = |
| 43 MakeRequest(&page_load_metrics_ptr_); |
| 44 fake_page_load_metrics_.Bind(std::move(request)); |
| 45 } |
| 46 page_load_metrics_ptr_->UpdateTiming(timing, metadata); |
| 47 base::RunLoop().RunUntilIdle(); |
| 48 } |
| 49 |
| 50 // We intercept sent messages and dispatch them to our |
33 // FakePageTimingMetricsIPCSender, which we use to verify that the expected | 51 // FakePageTimingMetricsIPCSender, which we use to verify that the expected |
34 // IPC messages get sent. | 52 // IPC messages get sent. |
35 bool Send(IPC::Message* message) override { | 53 bool Send(IPC::Message* message) override { |
36 return fake_timing_ipc_sender_.Send(message); | 54 return fake_timing_ipc_sender_.Send(message); |
37 } | 55 } |
38 | 56 |
39 void set_mock_timer(std::unique_ptr<base::Timer> timer) { | 57 void set_mock_timer(std::unique_ptr<base::Timer> timer) { |
40 ASSERT_EQ(nullptr, mock_timer_); | 58 ASSERT_EQ(nullptr, mock_timer_); |
41 mock_timer_ = std::move(timer); | 59 mock_timer_ = std::move(timer); |
42 } | 60 } |
43 | 61 |
44 void set_is_main_frame(bool is_main_frame) { is_main_frame_ = is_main_frame; } | 62 void set_is_main_frame(bool is_main_frame) { is_main_frame_ = is_main_frame; } |
45 | 63 |
46 bool WasFakeTimingConsumed() const { return fake_timing_.IsEmpty(); } | 64 bool WasFakeTimingConsumed() const { return fake_timing_.IsEmpty(); } |
47 | 65 |
48 void ExpectPageLoadTiming(const PageLoadTiming& timing) { | 66 void ExpectPageLoadTiming(const PageLoadTiming& timing) { |
49 SetFakePageLoadTiming(timing); | 67 SetFakePageLoadTiming(timing); |
50 fake_timing_ipc_sender_.ExpectPageLoadTiming(timing); | 68 fake_timing_ipc_sender_.ExpectPageLoadTiming(timing); |
| 69 fake_page_load_metrics_.ExpectPageLoadTiming(timing); |
51 } | 70 } |
52 | 71 |
53 void SetFakePageLoadTiming(const PageLoadTiming& timing) { | 72 void SetFakePageLoadTiming(const PageLoadTiming& timing) { |
54 EXPECT_TRUE(fake_timing_.IsEmpty()); | 73 EXPECT_TRUE(fake_timing_.IsEmpty()); |
55 fake_timing_ = timing; | 74 fake_timing_ = timing; |
56 } | 75 } |
57 | 76 |
58 PageLoadTiming GetTiming() const override { | 77 PageLoadTiming GetTiming() const override { |
59 PageLoadTiming tmp = fake_timing_; | 78 PageLoadTiming tmp = fake_timing_; |
60 fake_timing_ = PageLoadTiming(); | 79 fake_timing_ = PageLoadTiming(); |
61 return tmp; | 80 return tmp; |
62 } | 81 } |
63 | 82 |
64 void VerifyExpectedTimings() const { | 83 void VerifyExpectedTimings() const { |
65 EXPECT_TRUE(fake_timing_.IsEmpty()); | 84 EXPECT_TRUE(fake_timing_.IsEmpty()); |
66 fake_timing_ipc_sender_.VerifyExpectedTimings(); | 85 fake_timing_ipc_sender_.VerifyExpectedTimings(); |
| 86 fake_page_load_metrics_.VerifyExpectedTimings(); |
67 } | 87 } |
68 | 88 |
69 bool ShouldSendMetrics() const override { return true; } | 89 bool ShouldSendMetrics() const override { return true; } |
70 bool HasNoRenderFrame() const override { return false; } | 90 bool HasNoRenderFrame() const override { return false; } |
71 bool IsMainFrame() const override { return is_main_frame_; } | 91 bool IsMainFrame() const override { return is_main_frame_; } |
72 | 92 |
73 private: | 93 private: |
| 94 base::MessageLoop loop_; |
74 FakePageTimingMetricsIPCSender fake_timing_ipc_sender_; | 95 FakePageTimingMetricsIPCSender fake_timing_ipc_sender_; |
| 96 mojom::PageLoadMetricsPtr page_load_metrics_ptr_; |
| 97 FakePageLoadMetricsImpl fake_page_load_metrics_; |
75 mutable PageLoadTiming fake_timing_; | 98 mutable PageLoadTiming fake_timing_; |
76 mutable std::unique_ptr<base::Timer> mock_timer_; | 99 mutable std::unique_ptr<base::Timer> mock_timer_; |
77 bool is_main_frame_ = true; | 100 bool is_main_frame_ = true; |
78 }; | 101 }; |
79 | 102 |
80 typedef testing::Test MetricsRenderFrameObserverTest; | 103 typedef testing::Test MetricsRenderFrameObserverTest; |
81 | 104 |
82 TEST_F(MetricsRenderFrameObserverTest, NoMetrics) { | 105 TEST_F(MetricsRenderFrameObserverTest, NoMetrics) { |
83 TestMetricsRenderFrameObserver observer; | 106 TestMetricsRenderFrameObserver observer; |
84 base::MockTimer* mock_timer = new base::MockTimer(false, false); | 107 base::MockTimer* mock_timer = new base::MockTimer(false, false); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 observer.DidCommitProvisionalLoad(true, false); | 243 observer.DidCommitProvisionalLoad(true, false); |
221 ASSERT_FALSE(observer.WasFakeTimingConsumed()); | 244 ASSERT_FALSE(observer.WasFakeTimingConsumed()); |
222 ASSERT_FALSE(mock_timer->IsRunning()); | 245 ASSERT_FALSE(mock_timer->IsRunning()); |
223 | 246 |
224 observer.DidChangePerformanceTiming(); | 247 observer.DidChangePerformanceTiming(); |
225 ASSERT_FALSE(observer.WasFakeTimingConsumed()); | 248 ASSERT_FALSE(observer.WasFakeTimingConsumed()); |
226 ASSERT_FALSE(mock_timer->IsRunning()); | 249 ASSERT_FALSE(mock_timer->IsRunning()); |
227 } | 250 } |
228 | 251 |
229 } // namespace page_load_metrics | 252 } // namespace page_load_metrics |
OLD | NEW |