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. 4 5#include "base/strings/string_util.h" 6#include "media/base/data_buffer.h" 7#include "testing/gtest/include/gtest/gtest.h" 8 9namespace media { 10 11TEST(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()); 19} 20 21TEST(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()); 28} 29 30TEST(DataBufferTest, Constructor_ScopedArray) { 31 // Data should be passed and both data and buffer size should be set. 32 const int kSize = 8; 33 scoped_ptr<uint8[]> data(new uint8[kSize]); 34 const uint8* kData = data.get(); 35 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()); 42} 43 44TEST(DataBufferTest, CopyFrom) { 45 const uint8 kTestData[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }; 46 const int kTestDataSize = arraysize(kTestData); 47 48 scoped_refptr<DataBuffer> buffer = 49 DataBuffer::CopyFrom(kTestData, kTestDataSize); 50 EXPECT_EQ(kTestDataSize, buffer->data_size()); 51 EXPECT_FALSE(buffer->end_of_stream()); 52 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)); 57} 58 59TEST(DataBufferTest, CreateEOSBuffer) { 60 scoped_refptr<DataBuffer> buffer = DataBuffer::CreateEOSBuffer(); 61 EXPECT_TRUE(buffer->end_of_stream()); 62} 63 64TEST(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); 68 69 scoped_refptr<DataBuffer> buffer = new DataBuffer(0); 70 EXPECT_TRUE(buffer->timestamp() == kZero); 71 72 buffer->set_timestamp(kTimestampA); 73 EXPECT_TRUE(buffer->timestamp() == kTimestampA); 74 75 buffer->set_timestamp(kTimestampB); 76 EXPECT_TRUE(buffer->timestamp() == kTimestampB); 77} 78 79TEST(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); 83 84 scoped_refptr<DataBuffer> buffer = new DataBuffer(0); 85 EXPECT_TRUE(buffer->duration() == kZero); 86 87 buffer->set_duration(kDurationA); 88 EXPECT_TRUE(buffer->duration() == kDurationA); 89 90 buffer->set_duration(kDurationB); 91 EXPECT_TRUE(buffer->duration() == kDurationB); 92} 93 94TEST(DataBufferTest, ReadingWriting) { 95 const char kData[] = "hello"; 96 const int kDataSize = arraysize(kData); 97 const char kNewData[] = "chromium"; 98 const int kNewDataSize = arraysize(kNewData); 99 100 // Create a DataBuffer. 101 scoped_refptr<DataBuffer> buffer(new DataBuffer(kDataSize)); 102 ASSERT_TRUE(buffer.get()); 103 104 uint8* data = buffer->writable_data(); 105 ASSERT_TRUE(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()); 112 113 scoped_refptr<DataBuffer> buffer2(new DataBuffer(kNewDataSize + 10)); 114 data = buffer2->writable_data(); 115 ASSERT_TRUE(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)); 122} 123 124} // namespace media 125