1 // Copyright 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/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/test/simple_test_tick_clock.h"
8 #include "base/time/tick_clock.h"
9 #include "media/cast/cast_environment.h"
10 #include "media/cast/logging/logging_defines.h"
11 #include "media/cast/rtcp/sender_rtcp_event_subscriber.h"
12 #include "media/cast/test/fake_single_thread_task_runner.h"
13 #include "testing/gtest/include/gtest/gtest.h"
20 const size_t kMaxEventEntries
= 10u;
24 class SenderRtcpEventSubscriberTest
: public ::testing::Test
{
26 SenderRtcpEventSubscriberTest()
27 : testing_clock_(new base::SimpleTestTickClock()),
28 task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_
)),
29 cast_environment_(new CastEnvironment(
30 scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(),
34 event_subscriber_(kMaxEventEntries
) {
35 cast_environment_
->Logging()->AddRawEventSubscriber(&event_subscriber_
);
38 virtual ~SenderRtcpEventSubscriberTest() {
39 cast_environment_
->Logging()->RemoveRawEventSubscriber(&event_subscriber_
);
42 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
43 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
44 scoped_refptr
<CastEnvironment
> cast_environment_
;
45 SenderRtcpEventSubscriber event_subscriber_
;
48 TEST_F(SenderRtcpEventSubscriberTest
, InsertEntry
) {
49 cast_environment_
->Logging()->InsertFrameEvent(
50 testing_clock_
->NowTicks(), kVideoFrameCaptureBegin
, 100u, 1u);
51 cast_environment_
->Logging()->InsertFrameEvent(
52 testing_clock_
->NowTicks(), kVideoFrameCaptureBegin
, 200u, 2u);
53 cast_environment_
->Logging()->InsertFrameEvent(
54 testing_clock_
->NowTicks(), kVideoFrameSentToEncoder
, 100u, 1u);
55 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
56 kVideoFrameEncoded
, 100u, 1u);
57 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
58 kVideoFrameEncoded
, 300u, 3u);
59 cast_environment_
->Logging()->InsertFrameEvent(
60 testing_clock_
->NowTicks(), kVideoFrameSentToEncoder
, 300u, 3u);
63 event_subscriber_
.GetRtcpEventsAndReset(&events
);
65 ASSERT_EQ(3u, events
.size());
67 RtcpEventMap::iterator it
= events
.begin();
68 EXPECT_EQ(100u, it
->first
);
69 EXPECT_EQ(kVideoFrameEncoded
, it
->second
.type
);
72 EXPECT_EQ(200u, it
->first
);
73 EXPECT_EQ(kVideoFrameCaptureBegin
, it
->second
.type
);
76 EXPECT_EQ(300u, it
->first
);
77 EXPECT_EQ(kVideoFrameEncoded
, it
->second
.type
);
80 TEST_F(SenderRtcpEventSubscriberTest
, MapReset
) {
81 cast_environment_
->Logging()->InsertFrameEvent(
82 testing_clock_
->NowTicks(), kVideoFrameCaptureBegin
, 100u, 1u);
85 event_subscriber_
.GetRtcpEventsAndReset(&events
);
86 EXPECT_EQ(1u, events
.size());
88 // Call again without any logging in between, should return empty map.
89 event_subscriber_
.GetRtcpEventsAndReset(&events
);
90 EXPECT_TRUE(events
.empty());
93 TEST_F(SenderRtcpEventSubscriberTest
, DropEventsWhenSizeExceeded
) {
94 for (uint32 i
= 1u; i
<= 10u; ++i
) {
95 cast_environment_
->Logging()->InsertFrameEvent(
96 testing_clock_
->NowTicks(), kVideoFrameCaptureBegin
, i
* 10, i
);
100 event_subscriber_
.GetRtcpEventsAndReset(&events
);
102 ASSERT_EQ(10u, events
.size());
103 EXPECT_EQ(10u, events
.begin()->first
);
104 EXPECT_EQ(100u, events
.rbegin()->first
);
106 for (uint32 i
= 1u; i
<= 11u; ++i
) {
107 cast_environment_
->Logging()->InsertFrameEvent(
108 testing_clock_
->NowTicks(), kVideoFrameCaptureBegin
, i
* 10, i
);
111 event_subscriber_
.GetRtcpEventsAndReset(&events
);
113 // Event with RTP timestamp 10 should have been dropped when 110 is inserted.
114 ASSERT_EQ(10u, events
.size());
115 EXPECT_EQ(20u, events
.begin()->first
);
116 EXPECT_EQ(110u, events
.rbegin()->first
);