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"
14 class RenderMediaLogTest
: public testing::Test
{
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
));
30 // AddEvent() could post. Run the task runner to make sure it's executed.
31 task_runner_
->RunUntilIdle();
34 void Advance(base::TimeDelta delta
) {
35 tick_clock_
->Advance(delta
);
36 task_runner_
->FastForwardBy(delta
);
39 int message_count() { return render_thread_
.sink().message_count(); }
41 std::vector
<media::MediaLogEvent
> GetMediaLogEvents() {
42 const IPC::Message
* msg
= render_thread_
.sink().GetFirstMessageMatching(
43 ViewHostMsg_MediaLogEvents::ID
);
45 ADD_FAILURE() << "Did not find ViewHostMsg_MediaLogEvents IPC message";
46 return std::vector
<media::MediaLogEvent
>();
49 base::Tuple
<std::vector
<media::MediaLogEvent
>> events
;
50 ViewHostMsg_MediaLogEvents::Read(msg
, &events
);
51 return base::get
<0>(events
);
55 base::MessageLoop message_loop_
;
56 MockRenderThread render_thread_
;
57 scoped_refptr
<RenderMediaLog
> log_
;
58 base::SimpleTestTickClock
* tick_clock_
; // Owned by |log_|.
59 scoped_refptr
<base::TestMockTimeTaskRunner
> task_runner_
;
61 DISALLOW_COPY_AND_ASSIGN(RenderMediaLogTest
);
64 TEST_F(RenderMediaLogTest
, ThrottleSendingEvents
) {
65 AddEvent(media::MediaLogEvent::LOAD
);
66 EXPECT_EQ(0, message_count());
68 // Still shouldn't send anything.
69 Advance(base::TimeDelta::FromMilliseconds(500));
70 AddEvent(media::MediaLogEvent::SEEK
);
71 EXPECT_EQ(0, message_count());
73 // Now we should expect an IPC.
74 Advance(base::TimeDelta::FromMilliseconds(500));
75 EXPECT_EQ(1, message_count());
78 std::vector
<media::MediaLogEvent
> events
= GetMediaLogEvents();
79 ASSERT_EQ(2u, events
.size());
80 EXPECT_EQ(media::MediaLogEvent::LOAD
, events
[0].type
);
81 EXPECT_EQ(media::MediaLogEvent::SEEK
, events
[1].type
);
83 // Adding another event shouldn't send anything.
84 AddEvent(media::MediaLogEvent::PIPELINE_ERROR
);
85 EXPECT_EQ(1, message_count());
88 TEST_F(RenderMediaLogTest
, EventSentWithoutDelayAfterIpcInterval
) {
89 AddEvent(media::MediaLogEvent::LOAD
);
90 Advance(base::TimeDelta::FromMilliseconds(1000));
91 EXPECT_EQ(1, message_count());
93 // After the ipc send interval passes, the next event should be sent
95 Advance(base::TimeDelta::FromMilliseconds(2000));
96 AddEvent(media::MediaLogEvent::LOAD
);
97 EXPECT_EQ(2, message_count());
100 TEST_F(RenderMediaLogTest
, BufferedExtents
) {
101 AddEvent(media::MediaLogEvent::LOAD
);
102 AddEvent(media::MediaLogEvent::SEEK
);
104 // This event is handled separately and should always appear last regardless
105 // of how many times we see it.
106 AddEvent(media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED
);
107 AddEvent(media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED
);
108 AddEvent(media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED
);
110 EXPECT_EQ(0, message_count());
111 Advance(base::TimeDelta::FromMilliseconds(1000));
112 EXPECT_EQ(1, message_count());
114 // Verify contents. There should only be a single buffered extents changed
116 std::vector
<media::MediaLogEvent
> events
= GetMediaLogEvents();
117 ASSERT_EQ(3u, events
.size());
118 EXPECT_EQ(media::MediaLogEvent::LOAD
, events
[0].type
);
119 EXPECT_EQ(media::MediaLogEvent::SEEK
, events
[1].type
);
120 EXPECT_EQ(media::MediaLogEvent::BUFFERED_EXTENTS_CHANGED
, events
[2].type
);
123 } // namespace content