OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 "base/scoped_ptr.h" | |
6 #include "base/string_util.h" | |
7 #include "base/time.h" | |
8 #include "media/base/buffer_queue.h" | |
9 #include "media/base/data_buffer.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace media { | |
13 | |
14 class BufferQueueTest : public testing::Test { | |
15 protected: | |
16 BufferQueueTest() { | |
17 buffer1 = new DataBuffer(kDataSize); | |
18 buffer1->SetDataSize(kDataSize); | |
19 data1 = buffer1->GetWritableData(); | |
20 | |
21 buffer2 = new DataBuffer(kNewDataSize); | |
22 buffer2->SetDataSize(kNewDataSize); | |
23 data2 = buffer2->GetWritableData(); | |
24 | |
25 bufferBig = new DataBuffer(2 * (kDataSize + kNewDataSize)); | |
26 bufferBig->SetDataSize(2 * (kDataSize + kNewDataSize)); | |
27 dataBig = bufferBig->GetWritableData(); | |
28 | |
29 memcpy(data1, kData, kDataSize); | |
30 memcpy(data2, kNewData, kNewDataSize); | |
31 } | |
32 | |
33 BufferQueue queue_; | |
34 | |
35 static const char kData[]; | |
36 static const size_t kDataSize; | |
37 static const char kNewData[]; | |
38 static const size_t kNewDataSize; | |
39 scoped_refptr<DataBuffer> buffer1; | |
40 scoped_refptr<DataBuffer> buffer2; | |
41 scoped_refptr<DataBuffer> bufferBig; | |
42 uint8* data1; | |
43 uint8* data2; | |
44 uint8* dataBig; | |
45 | |
46 private: | |
47 DISALLOW_COPY_AND_ASSIGN(BufferQueueTest); | |
48 }; | |
49 | |
50 const char BufferQueueTest::kData[] = "hello"; | |
51 const size_t BufferQueueTest::kDataSize = arraysize(BufferQueueTest::kData); | |
52 const char BufferQueueTest::kNewData[] = "chromium"; | |
53 const size_t BufferQueueTest::kNewDataSize = | |
54 arraysize(BufferQueueTest::kNewData); | |
55 | |
56 TEST_F(BufferQueueTest, ValidTestData) { | |
57 ASSERT_TRUE(kNewDataSize > kDataSize); | |
58 ASSERT_TRUE(buffer1.get()); | |
59 ASSERT_TRUE(data1); | |
60 ASSERT_TRUE(buffer2.get()); | |
61 ASSERT_TRUE(data2); | |
62 ASSERT_TRUE(bufferBig.get()); | |
63 ASSERT_TRUE(dataBig); | |
64 } | |
65 | |
66 TEST_F(BufferQueueTest, Ctor) { | |
67 EXPECT_TRUE(queue_.IsEmpty()); | |
68 EXPECT_EQ(0u, queue_.SizeInBytes()); | |
69 } | |
70 | |
71 TEST_F(BufferQueueTest, Enqueue) { | |
72 queue_.Enqueue(buffer1.get()); | |
73 EXPECT_FALSE(queue_.IsEmpty()); | |
74 EXPECT_EQ(kDataSize, queue_.SizeInBytes()); | |
75 } | |
76 | |
77 TEST_F(BufferQueueTest, CopyWithOneBuffer) { | |
78 queue_.Enqueue(buffer1.get()); | |
79 | |
80 EXPECT_EQ(kDataSize, queue_.Copy(data2, kDataSize)); | |
81 EXPECT_EQ(0, memcmp(data2, kData, kDataSize)); | |
82 } | |
83 | |
84 TEST_F(BufferQueueTest, Clear) { | |
85 queue_.Enqueue(buffer1.get()); | |
86 | |
87 queue_.Clear(); | |
88 EXPECT_TRUE(queue_.IsEmpty()); | |
89 EXPECT_EQ(0u, queue_.SizeInBytes()); | |
90 } | |
91 | |
92 TEST_F(BufferQueueTest, MultipleEnqueues) { | |
93 queue_.Enqueue(buffer2.get()); | |
94 queue_.Enqueue(buffer1.get()); | |
95 EXPECT_EQ(kDataSize + kNewDataSize, queue_.SizeInBytes()); | |
96 } | |
97 | |
98 TEST_F(BufferQueueTest, CopyWithMultipleBuffers) { | |
99 queue_.Enqueue(buffer2.get()); | |
100 queue_.Enqueue(buffer1.get()); | |
101 | |
102 EXPECT_EQ(kDataSize + kNewDataSize, | |
103 queue_.Copy(dataBig, kDataSize + kNewDataSize)); | |
104 EXPECT_EQ(0, memcmp(dataBig, kNewData, kNewDataSize)); | |
105 dataBig += kNewDataSize; | |
106 EXPECT_EQ(0, memcmp(dataBig, kData, kDataSize)); | |
107 } | |
108 | |
109 TEST_F(BufferQueueTest, Consume) { | |
110 queue_.Enqueue(buffer2.get()); | |
111 queue_.Enqueue(buffer1.get()); | |
112 | |
113 queue_.Consume(kDataSize); | |
114 EXPECT_EQ(kNewDataSize, queue_.SizeInBytes()); | |
115 } | |
116 | |
117 TEST_F(BufferQueueTest, CopyFromMiddleOfBuffer) { | |
118 queue_.Enqueue(buffer2.get()); | |
119 queue_.Enqueue(buffer1.get()); | |
120 queue_.Consume(kDataSize); | |
121 | |
122 EXPECT_EQ(kNewDataSize, queue_.Copy(dataBig, kNewDataSize)); | |
123 EXPECT_EQ(0, memcmp(dataBig, | |
124 kNewData + kDataSize, | |
125 kNewDataSize - kDataSize)); | |
126 dataBig += (kNewDataSize - kDataSize); | |
127 EXPECT_EQ(0, memcmp(dataBig, kData, kDataSize)); | |
128 } | |
129 | |
130 | |
131 TEST_F(BufferQueueTest, GetTime) { | |
132 const struct { | |
133 int64 first_time_useconds; | |
134 int64 duration_useconds; | |
135 size_t consume_bytes; | |
136 } tests[] = { | |
137 // Timestamps of 0 are treated as garbage. | |
138 { 0, 1000000, 0 }, | |
139 { 0, 4000000, 0 }, | |
140 { 0, 8000000, 0 }, | |
141 // { 0, 1000000, 4 }, | |
142 // { 0, 4000000, 4 }, | |
143 // { 0, 8000000, 4 }, | |
144 // { 0, 1000000, kNewDataSize }, | |
145 // { 0, 4000000, kNewDataSize }, | |
146 // { 0, 8000000, kNewDataSize }, | |
147 { 5, 1000000, 0 }, | |
148 { 5, 4000000, 0 }, | |
149 { 5, 8000000, 0 }, | |
150 { 5, 1000000, 4 }, | |
151 { 5, 4000000, 4 }, | |
152 { 5, 8000000, 4 }, | |
153 { 5, 1000000, kNewDataSize }, | |
154 { 5, 4000000, kNewDataSize }, | |
155 { 5, 8000000, kNewDataSize }, | |
156 }; | |
157 | |
158 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
159 buffer2->SetTimestamp(base::TimeDelta::FromMicroseconds( | |
160 tests[i].first_time_useconds)); | |
161 buffer2->SetDuration(base::TimeDelta::FromMicroseconds( | |
162 tests[i].duration_useconds)); | |
163 queue_.Enqueue(buffer2.get()); | |
164 queue_.Consume(tests[i].consume_bytes); | |
165 | |
166 int64 expected = base::TimeDelta::FromMicroseconds(static_cast<int64>( | |
167 tests[i].first_time_useconds + (tests[i].consume_bytes * | |
168 tests[i].duration_useconds) / kNewDataSize)).ToInternalValue(); | |
169 | |
170 int64 actual = queue_.GetTime().ToInternalValue(); | |
171 | |
172 EXPECT_EQ(expected, actual) << "With test = { start:" | |
173 << tests[i].first_time_useconds << ", duration:" | |
174 << tests[i].duration_useconds << ", consumed:" | |
175 << tests[i].consume_bytes << "}\n"; | |
176 | |
177 queue_.Clear(); | |
178 } | |
179 } | |
180 | |
181 } // namespace media | |
OLD | NEW |