1 // Copyright (c) 2012 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/strings/string_util.h"
6 #include "media/base/data_buffer.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(DataBufferTest
, Constructor_ZeroSize
) {
12 // Zero-sized buffers are valid. In practice they aren't used very much but it
13 // eliminates clients from worrying about null data pointers.
14 scoped_refptr
<DataBuffer
> buffer
= new DataBuffer(0);
15 EXPECT_TRUE(buffer
->data());
16 EXPECT_TRUE(buffer
->writable_data());
17 EXPECT_EQ(0, buffer
->data_size());
18 EXPECT_FALSE(buffer
->end_of_stream());
21 TEST(DataBufferTest
, Constructor_NonZeroSize
) {
22 // Buffer size should be set.
23 scoped_refptr
<DataBuffer
> buffer
= new DataBuffer(10);
24 EXPECT_TRUE(buffer
->data());
25 EXPECT_TRUE(buffer
->writable_data());
26 EXPECT_EQ(0, buffer
->data_size());
27 EXPECT_FALSE(buffer
->end_of_stream());
30 TEST(DataBufferTest
, Constructor_ScopedArray
) {
31 // Data should be passed and both data and buffer size should be set.
33 scoped_ptr
<uint8
[]> data(new uint8
[kSize
]);
34 const uint8
* kData
= data
.get();
36 scoped_refptr
<DataBuffer
> buffer
= new DataBuffer(data
.Pass(), kSize
);
37 EXPECT_TRUE(buffer
->data());
38 EXPECT_TRUE(buffer
->writable_data());
39 EXPECT_EQ(kData
, buffer
->data());
40 EXPECT_EQ(kSize
, buffer
->data_size());
41 EXPECT_FALSE(buffer
->end_of_stream());
44 TEST(DataBufferTest
, CopyFrom
) {
45 const uint8 kTestData
[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
46 const int kTestDataSize
= arraysize(kTestData
);
48 scoped_refptr
<DataBuffer
> buffer
=
49 DataBuffer::CopyFrom(kTestData
, kTestDataSize
);
50 EXPECT_EQ(kTestDataSize
, buffer
->data_size());
51 EXPECT_FALSE(buffer
->end_of_stream());
53 // Ensure we are copying the data, not just pointing to the original data.
54 EXPECT_EQ(0, memcmp(buffer
->data(), kTestData
, kTestDataSize
));
55 buffer
->writable_data()[0] = 0xFF;
56 EXPECT_NE(0, memcmp(buffer
->data(), kTestData
, kTestDataSize
));
59 TEST(DataBufferTest
, CreateEOSBuffer
) {
60 scoped_refptr
<DataBuffer
> buffer
= DataBuffer::CreateEOSBuffer();
61 EXPECT_TRUE(buffer
->end_of_stream());
64 TEST(DataBufferTest
, Timestamp
) {
65 const base::TimeDelta kZero
;
66 const base::TimeDelta kTimestampA
= base::TimeDelta::FromMicroseconds(1337);
67 const base::TimeDelta kTimestampB
= base::TimeDelta::FromMicroseconds(1234);
69 scoped_refptr
<DataBuffer
> buffer
= new DataBuffer(0);
70 EXPECT_TRUE(buffer
->timestamp() == kZero
);
72 buffer
->set_timestamp(kTimestampA
);
73 EXPECT_TRUE(buffer
->timestamp() == kTimestampA
);
75 buffer
->set_timestamp(kTimestampB
);
76 EXPECT_TRUE(buffer
->timestamp() == kTimestampB
);
79 TEST(DataBufferTest
, Duration
) {
80 const base::TimeDelta kZero
;
81 const base::TimeDelta kDurationA
= base::TimeDelta::FromMicroseconds(1337);
82 const base::TimeDelta kDurationB
= base::TimeDelta::FromMicroseconds(1234);
84 scoped_refptr
<DataBuffer
> buffer
= new DataBuffer(0);
85 EXPECT_TRUE(buffer
->duration() == kZero
);
87 buffer
->set_duration(kDurationA
);
88 EXPECT_TRUE(buffer
->duration() == kDurationA
);
90 buffer
->set_duration(kDurationB
);
91 EXPECT_TRUE(buffer
->duration() == kDurationB
);
94 TEST(DataBufferTest
, ReadingWriting
) {
95 const char kData
[] = "hello";
96 const int kDataSize
= arraysize(kData
);
97 const char kNewData
[] = "chromium";
98 const int kNewDataSize
= arraysize(kNewData
);
100 // Create a DataBuffer.
101 scoped_refptr
<DataBuffer
> buffer(new DataBuffer(kDataSize
));
102 ASSERT_TRUE(buffer
.get());
104 uint8
* data
= buffer
->writable_data();
106 memcpy(data
, kData
, kDataSize
);
107 buffer
->set_data_size(kDataSize
);
108 const uint8
* read_only_data
= buffer
->data();
109 ASSERT_EQ(data
, read_only_data
);
110 ASSERT_EQ(0, memcmp(read_only_data
, kData
, kDataSize
));
111 EXPECT_FALSE(buffer
->end_of_stream());
113 scoped_refptr
<DataBuffer
> buffer2(new DataBuffer(kNewDataSize
+ 10));
114 data
= buffer2
->writable_data();
116 memcpy(data
, kNewData
, kNewDataSize
);
117 buffer2
->set_data_size(kNewDataSize
);
118 read_only_data
= buffer2
->data();
119 EXPECT_EQ(kNewDataSize
, buffer2
->data_size());
120 ASSERT_EQ(data
, read_only_data
);
121 EXPECT_EQ(0, memcmp(read_only_data
, kNewData
, kNewDataSize
));