Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / cast / transport / rtp_sender / packet_storage / packet_storage_unittest.cc
1 // Copyright 2013 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.
4
5 #include "media/cast/transport/rtp_sender/packet_storage/packet_storage.h"
6
7 #include <vector>
8
9 #include "base/test/simple_test_tick_clock.h"
10 #include "base/time/time.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12
13 namespace media {
14 namespace cast {
15 namespace transport {
16
17 static const int kMaxDeltaStoredMs = 500;
18 static const base::TimeDelta kDeltaBetweenFrames =
19     base::TimeDelta::FromMilliseconds(33);
20
21 static const int64 kStartMillisecond = GG_INT64_C(12345678900000);
22
23 class PacketStorageTest : public ::testing::Test {
24  protected:
25   PacketStorageTest() : packet_storage_(&testing_clock_, kMaxDeltaStoredMs) {
26     testing_clock_.Advance(
27         base::TimeDelta::FromMilliseconds(kStartMillisecond));
28   }
29
30   base::SimpleTestTickClock testing_clock_;
31   PacketStorage packet_storage_;
32
33   DISALLOW_COPY_AND_ASSIGN(PacketStorageTest);
34 };
35
36 TEST_F(PacketStorageTest, TimeOut) {
37   Packet test_123(100, 123);  // 100 insertions of the value 123.
38   PacketList packets;
39   for (uint32 frame_id = 0; frame_id < 30; ++frame_id) {
40     for (uint16 packet_id = 0; packet_id < 10; ++packet_id) {
41       packet_storage_.StorePacket(frame_id, packet_id, &test_123);
42     }
43     testing_clock_.Advance(kDeltaBetweenFrames);
44   }
45
46   // All packets belonging to the first 14 frames is expected to be expired.
47   for (uint32 frame_id = 0; frame_id < 14; ++frame_id) {
48     for (uint16 packet_id = 0; packet_id < 10; ++packet_id) {
49       Packet packet;
50       EXPECT_FALSE(packet_storage_.GetPacket(frame_id, packet_id, &packets));
51     }
52   }
53   // All packets belonging to the next 15 frames is expected to be valid.
54   for (uint32 frame_id = 14; frame_id < 30; ++frame_id) {
55     for (uint16 packet_id = 0; packet_id < 10; ++packet_id) {
56       EXPECT_TRUE(packet_storage_.GetPacket(frame_id, packet_id, &packets));
57       EXPECT_TRUE(packets.front() == test_123);
58     }
59   }
60 }
61
62 TEST_F(PacketStorageTest, MaxNumberOfPackets) {
63   Packet test_123(100, 123);  // 100 insertions of the value 123.
64   PacketList packets;
65
66   uint32 frame_id = 0;
67   for (uint16 packet_id = 0; packet_id <= PacketStorage::kMaxStoredPackets;
68        ++packet_id) {
69     packet_storage_.StorePacket(frame_id, packet_id, &test_123);
70   }
71   Packet packet;
72   uint16 packet_id = 0;
73   EXPECT_FALSE(packet_storage_.GetPacket(frame_id, packet_id, &packets));
74
75   ++packet_id;
76   for (; packet_id <= PacketStorage::kMaxStoredPackets; ++packet_id) {
77     EXPECT_TRUE(packet_storage_.GetPacket(frame_id, packet_id, &packets));
78     EXPECT_TRUE(packets.back() == test_123);
79   }
80 }
81
82 TEST_F(PacketStorageTest, PacketContent) {
83   Packet test_123(100, 123);  // 100 insertions of the value 123.
84   Packet test_234(200, 234);  // 200 insertions of the value 234.
85   PacketList packets;
86
87   for (uint32 frame_id = 0; frame_id < 10; ++frame_id) {
88     for (uint16 packet_id = 0; packet_id < 10; ++packet_id) {
89       // Every other packet.
90       if (packet_id % 2 == 0) {
91         packet_storage_.StorePacket(frame_id, packet_id, &test_123);
92       } else {
93         packet_storage_.StorePacket(frame_id, packet_id, &test_234);
94       }
95     }
96     testing_clock_.Advance(kDeltaBetweenFrames);
97   }
98   for (uint32 frame_id = 0; frame_id < 10; ++frame_id) {
99     for (uint16 packet_id = 0; packet_id < 10; ++packet_id) {
100       EXPECT_TRUE(packet_storage_.GetPacket(frame_id, packet_id, &packets));
101       // Every other packet.
102       if (packet_id % 2 == 0) {
103         EXPECT_TRUE(packets.back() == test_123);
104       } else {
105         EXPECT_TRUE(packets.back() == test_234);
106       }
107     }
108   }
109 }
110
111 }  // namespace transport
112 }  // namespace cast
113 }  // namespace media