summaryrefslogtreecommitdiffstats
path: root/media/base/buffer_queue_unittest.cc
blob: 8ba60be2d061399d79f65dd469cd39234afdeb51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "base/time.h"
#include "media/base/buffer_queue.h"
#include "media/base/data_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class BufferQueueTest : public testing::Test {
 protected:
  BufferQueueTest() {
    buffer1 = new DataBuffer(kDataSize);
    buffer1->SetDataSize(kDataSize);
    data1 = buffer1->GetWritableData();

    buffer2 = new DataBuffer(kNewDataSize);
    buffer2->SetDataSize(kNewDataSize);
    data2 = buffer2->GetWritableData();

    bufferBig = new DataBuffer(2 * (kDataSize + kNewDataSize));
    bufferBig->SetDataSize(2 * (kDataSize + kNewDataSize));
    dataBig = bufferBig->GetWritableData();

    memcpy(data1, kData, kDataSize);
    memcpy(data2, kNewData, kNewDataSize);
  }

  BufferQueue queue_;

  static const char kData[];
  static const size_t kDataSize;
  static const char kNewData[];
  static const size_t kNewDataSize;
  scoped_refptr<DataBuffer> buffer1;
  scoped_refptr<DataBuffer> buffer2;
  scoped_refptr<DataBuffer> bufferBig;
  uint8* data1;
  uint8* data2;
  uint8* dataBig;

 private:
  DISALLOW_COPY_AND_ASSIGN(BufferQueueTest);
};

const char BufferQueueTest::kData[] = "hello";
const size_t BufferQueueTest::kDataSize = arraysize(BufferQueueTest::kData);
const char BufferQueueTest::kNewData[] = "chromium";
const size_t BufferQueueTest::kNewDataSize =
    arraysize(BufferQueueTest::kNewData);

TEST_F(BufferQueueTest, ValidTestData) {
  ASSERT_TRUE(kNewDataSize > kDataSize);
  ASSERT_TRUE(buffer1.get());
  ASSERT_TRUE(data1);
  ASSERT_TRUE(buffer2.get());
  ASSERT_TRUE(data2);
  ASSERT_TRUE(bufferBig.get());
  ASSERT_TRUE(dataBig);
}

TEST_F(BufferQueueTest, Ctor) {
  EXPECT_TRUE(queue_.IsEmpty());
  EXPECT_EQ(0u, queue_.SizeInBytes());
}

TEST_F(BufferQueueTest, Enqueue) {
  queue_.Enqueue(buffer1.get());
  EXPECT_FALSE(queue_.IsEmpty());
  EXPECT_EQ(kDataSize, queue_.SizeInBytes());
}

TEST_F(BufferQueueTest, CopyWithOneBuffer) {
  queue_.Enqueue(buffer1.get());

  EXPECT_EQ(kDataSize, queue_.Copy(data2, kDataSize));
  EXPECT_EQ(0, memcmp(data2, kData, kDataSize));
}

TEST_F(BufferQueueTest, Clear) {
  queue_.Enqueue(buffer1.get());

  queue_.Clear();
  EXPECT_TRUE(queue_.IsEmpty());
  EXPECT_EQ(0u, queue_.SizeInBytes());
}

TEST_F(BufferQueueTest, MultipleEnqueues) {
  queue_.Enqueue(buffer2.get());
  queue_.Enqueue(buffer1.get());
  EXPECT_EQ(kDataSize + kNewDataSize, queue_.SizeInBytes());
}

TEST_F(BufferQueueTest, CopyWithMultipleBuffers) {
  queue_.Enqueue(buffer2.get());
  queue_.Enqueue(buffer1.get());

  EXPECT_EQ(kDataSize + kNewDataSize,
            queue_.Copy(dataBig, kDataSize + kNewDataSize));
  EXPECT_EQ(0, memcmp(dataBig, kNewData, kNewDataSize));
  dataBig += kNewDataSize;
  EXPECT_EQ(0, memcmp(dataBig, kData, kDataSize));
}

TEST_F(BufferQueueTest, Consume) {
  queue_.Enqueue(buffer2.get());
  queue_.Enqueue(buffer1.get());

  queue_.Consume(kDataSize);
  EXPECT_EQ(kNewDataSize, queue_.SizeInBytes());
}

TEST_F(BufferQueueTest, CopyFromMiddleOfBuffer) {
  queue_.Enqueue(buffer2.get());
  queue_.Enqueue(buffer1.get());
  queue_.Consume(kDataSize);

  EXPECT_EQ(kNewDataSize, queue_.Copy(dataBig, kNewDataSize));
  EXPECT_EQ(0, memcmp(dataBig,
                      kNewData + kDataSize,
                      kNewDataSize - kDataSize));
  dataBig += (kNewDataSize - kDataSize);
  EXPECT_EQ(0, memcmp(dataBig, kData, kDataSize));
}

TEST_F(BufferQueueTest, GetTime) {
  const struct {
    int64 first_time_seconds;
    size_t consume_bytes;
    double bytes_to_seconds;
  } tests[] = {
    { 0, 0, 0.5  },
    { 0, 0, 1    },
    { 0, 0, 1.75 },
    { 0, 4, 0.5  },
    { 0, 4, 1    },
    { 0, 4, 1.75 },
    { 5, 0, 0.5  },
    { 5, 0, 1    },
    { 5, 0, 1.75 },
    { 5, 4, 0.5  },
    { 5, 4, 1    },
    { 5, 4, 1.75 },
  };

  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
    buffer2->SetTimestamp(base::TimeDelta::FromSeconds(
        tests[i].first_time_seconds));
    queue_.Enqueue(buffer2.get());
    queue_.Consume(tests[i].consume_bytes);

    int64 expected = base::TimeDelta::FromSeconds(static_cast<int64>(
        tests[i].first_time_seconds + (tests[i].consume_bytes *
        tests[i].bytes_to_seconds))).ToInternalValue();

    int64 actual = queue_.GetTime(tests[i].bytes_to_seconds).ToInternalValue();

    EXPECT_EQ(expected, actual) << "With test = {"
        << tests[i].first_time_seconds << ", " << tests[i].consume_bytes
        << ", " << tests[i].bytes_to_seconds << "}\n";

    queue_.Clear();
  }
}

}  // namespace media