Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: webrtc/call/rtp_demuxer_unittest.cc

Issue 2904903002: Create unit tests for RtpDemuxer (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« webrtc/call/rtp_demuxer.cc ('K') | « webrtc/call/rtp_demuxer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <memory>
12
13 #include "webrtc/call/rtp_demuxer.h"
danilchap 2017/05/24 13:45:43 order includes alphabetically, except for the rtp_
14 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
15
16 #include "webrtc/base/checks.h"
17 #include "webrtc/test/gmock.h"
18 #include "webrtc/test/gtest.h"
19
20 // TODO(elad.alon): This is not video-specific, and so should not be
elad.alon_webrtc.org 2017/05/24 12:19:35 Please advise. :-)
holmer 2017/05/24 13:10:23 How is this currently a video engine test? Because
danilchap 2017/05/24 13:45:43 you put it into call_tests set, which is reasonabl
21 // in the video-engine tests.
22
23 namespace webrtc {
24
25 namespace {
26
27 constexpr uint32_t kSsrcs[] = {101, 202, 303};
danilchap 2017/05/24 13:45:43 may be prefer same order as recommended for classe
28
29 class MockRtpPacketSink : public RtpPacketSinkInterface {
30 public:
31 MOCK_METHOD1(OnRtpPacket, void(const RtpPacketReceived&));
32 };
33
34 MATCHER_P(RtpPacketReceivedSsrcMatcher, other, "") {
35 return arg.Ssrc() == other.Ssrc();
36 }
37
38 std::unique_ptr<RtpPacketReceived> GetRtpPacketReceived(uint32_t ssrc) {
danilchap 2017/05/24 13:45:43 why return unique_ptr instead of copy of the packe
39 constexpr int8_t kPayloadType = 100;
40 constexpr uint16_t kSeqNum = 0x1234;
41 constexpr uint8_t kSeqNumFirstHalf = kSeqNum >> 8;
42 constexpr uint8_t kSeqNumSecondHalf = kSeqNum & 0xff;
43 // clang-format off
44 constexpr uint8_t kMinimumPacket[] = {
45 0x80, kPayloadType, kSeqNumFirstHalf, kSeqNumSecondHalf,
46 0x65, 0x43, 0x12, 0x78,
47 0x12, 0x34, 0x56, 0x78};
48 // clang-format on
49
50 std::unique_ptr<RtpPacketReceived> packet(new RtpPacketReceived());
51 EXPECT_TRUE(packet->Parse(kMinimumPacket, sizeof(kMinimumPacket)));
danilchap 2017/05/24 13:45:43 you do not have to parse packet to create it: pack
52 packet->SetSsrc(ssrc);
53 return packet;
54 }
55
56 } // namespace
57
58 namespace test {
danilchap 2017/05/24 13:45:43 might be better to keep using unnamed namespace.
59
60 class RtpDemuxerTest : public ::testing::Test {
61 protected:
62 RtpDemuxerTest() = default;
63 virtual ~RtpDemuxerTest() = default;
64
65 void SetUp() {
danilchap 2017/05/24 13:45:43 prefer constructor over SetUp when you can
66 for (size_t i = 0; i < kNumOfSinks; i++) {
67 demuxer.AddSink(kSsrcs[i], &sinks[i]);
68 }
69 }
70
71 void TearDown() {
72 for (size_t i = 0; i < kNumOfSinks; i++) {
73 EXPECT_EQ(demuxer.RemoveSink(&sinks[i]), 1u);
74 }
75 }
76
77 static constexpr size_t kNumOfSinks = 3;
danilchap 2017/05/24 13:45:43 put constants before constructors. Since you assum
78
79 RtpDemuxer demuxer;
80 MockRtpPacketSink sinks[kNumOfSinks];
81 };
82
83 TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSink) {
84 for (size_t i = 0; i < kNumOfSinks; i++) {
85 auto packet = GetRtpPacketReceived(kSsrcs[i]);
86 EXPECT_CALL(sinks[i], OnRtpPacket(RtpPacketReceivedSsrcMatcher(*packet)));
danilchap 2017/05/24 13:45:43 OnRtpPacket(Property(&RtpPacketReceived::Ssrc, kSs
87 demuxer.OnRtpPacket(*packet);
88 testing::Mock::VerifyAndClearExpectations(&sinks[i]);
89 }
90 }
91
92 TEST_F(RtpDemuxerTest, MultipleSinksMappedToSameSsrc) {
93 // |sinks| associated with different SSRCs each. Add a few additional sinks
94 // that are all associated with one new, distinct SSRC.
95 constexpr size_t kNumOfSameSsrcSinks = 3;
96 MockRtpPacketSink same_ssrc_sinks[kNumOfSameSsrcSinks];
97 constexpr size_t kSharedSsrc = 404;
danilchap 2017/05/24 13:45:43 use uint32_t for ssrc
98 for (size_t i = 0; i < kNumOfSameSsrcSinks; i++) {
99 demuxer.AddSink(kSharedSsrc, &same_ssrc_sinks[i]);
100 }
101
102 // Reception of an RTP packet associated with the shared SSRC triggers the
103 // callback on all of the interfaces associated with it.
104 auto packet = GetRtpPacketReceived(kSharedSsrc);
105 for (size_t i = 0; i < kNumOfSameSsrcSinks; i++) {
106 EXPECT_CALL(same_ssrc_sinks[i],
107 OnRtpPacket(RtpPacketReceivedSsrcMatcher(*packet)));
108 }
109 demuxer.OnRtpPacket(*packet);
110
111 // Test-specific tear-down
112 for (size_t i = 0; i < kNumOfSameSsrcSinks; i++) {
113 EXPECT_EQ(demuxer.RemoveSink(&same_ssrc_sinks[i]), 1u);
114 }
115 }
116
117 TEST_F(RtpDemuxerTest, SinkMappedToMultipleSsrcs) {
118 // |sinks| associated with different SSRCs each. We set one of them to also
119 // be mapped to additional SSRCs.
120 constexpr uint32_t kSsrcsOfMultiSsrcSink[] = {404, 505, 606};
121 MockRtpPacketSink multi_ssrc_sink;
122 for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) {
123 demuxer.AddSink(ssrc, &multi_ssrc_sink);
124 }
125
126 // The sink which is associated with multiple SSRCs gets the callback
127 // triggered for each of those SSRCs.
128 for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) {
129 auto packet = GetRtpPacketReceived(ssrc);
130 EXPECT_CALL(multi_ssrc_sink,
131 OnRtpPacket(RtpPacketReceivedSsrcMatcher(*packet)));
132 demuxer.OnRtpPacket(*packet);
133 testing::Mock::VerifyAndClearExpectations(&multi_ssrc_sink);
134 }
135
136 // Test-specific tear-down
137 EXPECT_EQ(demuxer.RemoveSink(&multi_ssrc_sink),
138 sizeof(kSsrcsOfMultiSsrcSink) / sizeof(kSsrcsOfMultiSsrcSink[0]));
danilchap 2017/05/24 13:45:43 there is arraysize macro in base/arraysize.h exact
139 }
140
141 TEST_F(RtpDemuxerTest, SinkRemovalSanity) {
142 // |sinks| associated with different SSRCs each. We set one of them to also
143 // be mapped to additional SSRCs.
144 constexpr uint32_t kSsrcsOfMultiSsrcSink[] = {404, 505, 606};
145 MockRtpPacketSink multi_ssrc_sink;
146 for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) {
147 demuxer.AddSink(ssrc, &multi_ssrc_sink);
148 }
149
150 // Remove the sink.
151 EXPECT_EQ(demuxer.RemoveSink(&multi_ssrc_sink),
152 sizeof(kSsrcsOfMultiSsrcSink) / sizeof(kSsrcsOfMultiSsrcSink[0]));
153
154 // The removed sink does not get callbacks triggered for any of the SSRCs
155 // with which it was previously associated.
156 for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) {
157 auto packet = GetRtpPacketReceived(ssrc);
158 demuxer.OnRtpPacket(*packet);
159 }
160 }
161
162 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
danilchap 2017/05/24 13:45:43 why exclude android?
163 TEST_F(RtpDemuxerTest, RepeatedAssociationsForbidden) {
164 // Set-up already associated sinks[0] with kSsrcs[0]. Repeating the
165 // association is an error.
166 EXPECT_DEATH(demuxer.AddSink(kSsrcs[0], &sinks[0]), "");
167 }
168
169 TEST_F(RtpDemuxerTest, SinksMustBeRemovedBeforeDestruction) {
170 std::unique_ptr<RtpDemuxer> bad_demuxer(new RtpDemuxer());
171 MockRtpPacketSink sink;
172 constexpr uint32_t ssrc = 111;
173 bad_demuxer->AddSink(ssrc, &sink);
174 EXPECT_DEATH(bad_demuxer.reset(), "");
175 EXPECT_EQ(bad_demuxer->RemoveSink(&sink), 1u);
176 }
177 #endif
178
179 } // namespace test
180 } // namespace webrtc
OLDNEW
« webrtc/call/rtp_demuxer.cc ('K') | « webrtc/call/rtp_demuxer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698