Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / render_media_log_unittest.cc
blobe9774ac5187bcd263226dca682fab45cad0c9e09
1 // Copyright (c) 2014 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.
5 #include "base/test/simple_test_tick_clock.h"
6 #include "base/test/test_mock_time_task_runner.h"
7 #include "content/common/view_messages.h"
8 #include "content/public/test/mock_render_thread.h"
9 #include "content/renderer/media/render_media_log.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace content {
14 class RenderMediaLogTest : public testing::Test {
15 public:
16 RenderMediaLogTest()
17 : log_(new RenderMediaLog()),
18 tick_clock_(new base::SimpleTestTickClock()),
19 task_runner_(new base::TestMockTimeTaskRunner()) {
20 log_->SetTickClockForTesting(scoped_ptr<base::TickClock>(tick_clock_));
21 log_->SetTaskRunnerForTesting(task_runner_);
24 ~RenderMediaLogTest() override {
25 task_runner_->ClearPendingTasks();
28 void AddEvent(media::MediaLogEvent::Type type) {
29 log_->AddEvent(log_->CreateEvent(type));
32 void Advance(base::TimeDelta delta) {
33 tick_clock_->Advance(delta);
34 task_runner_->FastForwardBy(delta);
37 int message_count() { return render_thread_.sink().message_count(); }
39 std::vector<media::MediaLogEvent> GetMediaLogEvents() {
40 const IPC::Message* msg = render_thread_.sink().GetFirstMessageMatching(
41 ViewHostMsg_MediaLogEvents::ID);
42 if (!msg) {
43 ADD_FAILURE() << "Did not find ViewHostMsg_MediaLogEvents IPC message";
44 return std::vector<media::MediaLogEvent>();
47 Tuple<std::vector<media::MediaLogEvent>> events;
48 ViewHostMsg_MediaLogEvents::Read(msg, &events);
49 return get<0>(events);
52 private:
53 MockRenderThread render_thread_;
54 scoped_refptr<RenderMediaLog> log_;
55 base::SimpleTestTickClock* tick_clock_; // Owned by |log_|.
56 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
58 DISALLOW_COPY_AND_ASSIGN(RenderMediaLogTest);
61 TEST_F(RenderMediaLogTest, ThrottleSendingEvents) {
62 AddEvent(media::MediaLogEvent::LOAD);
63 EXPECT_EQ(0, message_count());
65 // Still shouldn't send anything.
66 Advance(base::TimeDelta::FromMilliseconds(500));
67 AddEvent(media::MediaLogEvent::SEEK);
68 EXPECT_EQ(0, message_count());
70 // Now we should expect an IPC.
71 Advance(base::TimeDelta::FromMilliseconds(500));
72 EXPECT_EQ(1, message_count());
74 // Verify contents.
75 std::vector<media::MediaLogEvent> events = GetMediaLogEvents();
76 ASSERT_EQ(2u, events.size());
77 EXPECT_EQ(media::MediaLogEvent::LOAD, events[0].type);
78 EXPECT_EQ(media::MediaLogEvent::SEEK, events[1].type);
80 // Adding another event shouldn't send anything.
81 AddEvent(media::MediaLogEvent::PIPELINE_ERROR);
82 EXPECT_EQ(1, message_count());
85 TEST_F(RenderMediaLogTest, EventSentWithoutDelayAfterIpcInterval) {
86 AddEvent(media::MediaLogEvent::LOAD);
87 Advance(base::TimeDelta::FromMilliseconds(1000));
88 EXPECT_EQ(1, message_count());
90 // After the ipc send interval passes, the next event should be sent
91 // right away.
92 Advance(base::TimeDelta::FromMilliseconds(2000));
93 AddEvent(media::MediaLogEvent::LOAD);
94 EXPECT_EQ(2, message_count());
97 TEST_F(RenderMediaLogTest, BufferedExtents) {
98 AddEvent(media::MediaLogEvent::LOAD);
99 AddEvent(media::MediaLogEvent::SEEK);
101 // This event is handled separately and should always appear last regardless
102 // of how many times we see it.
103 AddEvent(media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED);
104 AddEvent(media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED);
105 AddEvent(media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED);
107 EXPECT_EQ(0, message_count());
108 Advance(base::TimeDelta::FromMilliseconds(1000));
109 EXPECT_EQ(1, message_count());
111 // Verify contents. There should only be a single buffered extents changed
112 // event.
113 std::vector<media::MediaLogEvent> events = GetMediaLogEvents();
114 ASSERT_EQ(3u, events.size());
115 EXPECT_EQ(media::MediaLogEvent::LOAD, events[0].type);
116 EXPECT_EQ(media::MediaLogEvent::SEEK, events[1].type);
117 EXPECT_EQ(media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED, events[2].type);
120 } // namespace content