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/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
->GetData());
16 EXPECT_TRUE(buffer
->GetWritableData());
17 EXPECT_EQ(0, buffer
->GetDataSize());
18 EXPECT_FALSE(buffer
->IsEndOfStream());
21 TEST(DataBufferTest
, Constructor_NonZeroSize
) {
22 // Buffer size should be set.
23 scoped_refptr
<DataBuffer
> buffer
= new DataBuffer(10);
24 EXPECT_TRUE(buffer
->GetData());
25 EXPECT_TRUE(buffer
->GetWritableData());
26 EXPECT_EQ(0, buffer
->GetDataSize());
27 EXPECT_FALSE(buffer
->IsEndOfStream());
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
->GetData());
38 EXPECT_TRUE(buffer
->GetWritableData());
39 EXPECT_EQ(kData
, buffer
->GetData());
40 EXPECT_EQ(kSize
, buffer
->GetDataSize());
41 EXPECT_FALSE(buffer
->IsEndOfStream());
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
->GetDataSize());
51 EXPECT_FALSE(buffer
->IsEndOfStream());
53 // Ensure we are copying the data, not just pointing to the original data.
54 EXPECT_EQ(0, memcmp(buffer
->GetData(), kTestData
, kTestDataSize
));
55 buffer
->GetWritableData()[0] = 0xFF;
56 EXPECT_NE(0, memcmp(buffer
->GetData(), kTestData
, kTestDataSize
));
59 TEST(DataBufferTest
, CreateEOSBuffer
) {
60 scoped_refptr
<DataBuffer
> buffer
= DataBuffer::CreateEOSBuffer();
61 EXPECT_TRUE(buffer
->IsEndOfStream());
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
->GetTimestamp() == kZero
);
72 buffer
->SetTimestamp(kTimestampA
);
73 EXPECT_TRUE(buffer
->GetTimestamp() == kTimestampA
);
75 buffer
->SetTimestamp(kTimestampB
);
76 EXPECT_TRUE(buffer
->GetTimestamp() == 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
->GetDuration() == kZero
);
87 buffer
->SetDuration(kDurationA
);
88 EXPECT_TRUE(buffer
->GetDuration() == kDurationA
);
90 buffer
->SetDuration(kDurationB
);
91 EXPECT_TRUE(buffer
->GetDuration() == 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
));
104 uint8
* data
= buffer
->GetWritableData();
106 memcpy(data
, kData
, kDataSize
);
107 buffer
->SetDataSize(kDataSize
);
108 const uint8
* read_only_data
= buffer
->GetData();
109 ASSERT_EQ(data
, read_only_data
);
110 ASSERT_EQ(0, memcmp(read_only_data
, kData
, kDataSize
));
111 EXPECT_FALSE(buffer
->IsEndOfStream());
113 scoped_refptr
<DataBuffer
> buffer2(new DataBuffer(kNewDataSize
+ 10));
114 data
= buffer2
->GetWritableData();
116 memcpy(data
, kNewData
, kNewDataSize
);
117 buffer2
->SetDataSize(kNewDataSize
);
118 read_only_data
= buffer2
->GetData();
119 EXPECT_EQ(kNewDataSize
, buffer2
->GetDataSize());
120 ASSERT_EQ(data
, read_only_data
);
121 EXPECT_EQ(0, memcmp(read_only_data
, kNewData
, kNewDataSize
));