Revert of Revert of Roll android_tools f6e2370:2a860d8 (patchset #1 id:1 of https...
[chromium-blink-merge.git] / media / base / decoder_buffer_queue_unittest.cc
blob5eb06d2152dcc1d4247ccf7ccb202fd3e8ca2676
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 "media/base/buffers.h"
6 #include "media/base/decoder_buffer.h"
7 #include "media/base/decoder_buffer_queue.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace media {
12 static base::TimeDelta ToTimeDelta(int seconds) {
13 if (seconds < 0)
14 return kNoTimestamp();
15 return base::TimeDelta::FromSeconds(seconds);
18 // Helper to create buffers with specified timestamp in seconds.
20 // Negative numbers will be converted to kNoTimestamp();
21 static scoped_refptr<DecoderBuffer> CreateBuffer(int timestamp) {
22 scoped_refptr<DecoderBuffer> buffer = new DecoderBuffer(0);
23 buffer->set_timestamp(ToTimeDelta(timestamp));
24 buffer->set_duration(ToTimeDelta(0));
25 return buffer;
28 static scoped_refptr<DecoderBuffer> CreateBuffer(int timestamp, int size) {
29 scoped_refptr<DecoderBuffer> buffer = new DecoderBuffer(size);
30 buffer->set_timestamp(ToTimeDelta(timestamp));
31 buffer->set_duration(ToTimeDelta(0));
32 return buffer;
35 TEST(DecoderBufferQueueTest, IsEmpty) {
36 DecoderBufferQueue queue;
37 EXPECT_TRUE(queue.IsEmpty());
39 queue.Push(CreateBuffer(0));
40 EXPECT_FALSE(queue.IsEmpty());
43 TEST(DecoderBufferQueueTest, Clear) {
44 DecoderBufferQueue queue;
45 queue.Push(CreateBuffer(0));
46 queue.Push(CreateBuffer(1));
47 EXPECT_FALSE(queue.IsEmpty());
48 EXPECT_EQ(1, queue.Duration().InSeconds());
50 queue.Clear();
51 EXPECT_TRUE(queue.IsEmpty());
52 EXPECT_EQ(0, queue.Duration().InSeconds());
55 TEST(DecoderBufferQueueTest, Duration) {
56 DecoderBufferQueue queue;
57 EXPECT_EQ(0, queue.Duration().InSeconds());
59 queue.Push(CreateBuffer(0));
60 EXPECT_EQ(0, queue.Duration().InSeconds());
62 queue.Push(CreateBuffer(1));
63 EXPECT_EQ(1, queue.Duration().InSeconds());
65 queue.Push(CreateBuffer(2));
66 EXPECT_EQ(2, queue.Duration().InSeconds());
68 queue.Push(CreateBuffer(4));
69 EXPECT_EQ(4, queue.Duration().InSeconds());
71 queue.Pop();
72 EXPECT_EQ(3, queue.Duration().InSeconds());
74 queue.Pop();
75 EXPECT_EQ(2, queue.Duration().InSeconds());
77 queue.Pop();
78 EXPECT_EQ(0, queue.Duration().InSeconds());
80 queue.Pop();
81 EXPECT_EQ(0, queue.Duration().InSeconds());
84 TEST(DecoderBufferQueueTest, Duration_OutOfOrder) {
85 DecoderBufferQueue queue;
86 queue.Push(CreateBuffer(10));
87 queue.Push(CreateBuffer(12));
88 EXPECT_EQ(2, queue.Duration().InSeconds());
90 // Out of order: duration shouldn't change.
91 queue.Push(CreateBuffer(8));
92 EXPECT_EQ(2, queue.Duration().InSeconds());
94 // Removing first buffer should leave the second buffer as the only buffer
95 // included in the duration calculation.
96 queue.Pop();
97 EXPECT_EQ(0, queue.Duration().InSeconds());
99 // Removing second buffer leaves the out-of-order buffer. It shouldn't be
100 // included in duration calculations.
101 queue.Pop();
102 EXPECT_EQ(0, queue.Duration().InSeconds());
104 // Push a still-too-early buffer. It shouldn't be included in duration
105 // calculations.
106 queue.Push(CreateBuffer(11));
107 EXPECT_EQ(0, queue.Duration().InSeconds());
109 // Push a buffer that's after the earliest valid time. It's a singular valid
110 // buffer so duration is still zero.
111 queue.Push(CreateBuffer(14));
112 EXPECT_EQ(0, queue.Duration().InSeconds());
114 // Push a second valid buffer. We should now have a duration.
115 queue.Push(CreateBuffer(17));
116 EXPECT_EQ(3, queue.Duration().InSeconds());
119 TEST(DecoderBufferQueueTest, Duration_NoTimestamp) {
120 // Buffers with no timestamp don't affect duration.
121 DecoderBufferQueue queue;
122 queue.Push(CreateBuffer(0));
123 queue.Push(CreateBuffer(4));
124 EXPECT_EQ(4, queue.Duration().InSeconds());
126 queue.Push(CreateBuffer(-1));
127 EXPECT_EQ(4, queue.Duration().InSeconds());
129 queue.Push(CreateBuffer(6));
130 EXPECT_EQ(6, queue.Duration().InSeconds());
132 queue.Pop();
133 EXPECT_EQ(2, queue.Duration().InSeconds());
135 queue.Pop();
136 EXPECT_EQ(0, queue.Duration().InSeconds());
138 queue.Pop();
139 EXPECT_EQ(0, queue.Duration().InSeconds());
141 queue.Pop();
142 EXPECT_EQ(0, queue.Duration().InSeconds());
145 TEST(DecoderBufferQueueTest, DataSize) {
146 DecoderBufferQueue queue;
147 EXPECT_EQ(queue.data_size(), 0u);
149 queue.Push(CreateBuffer(0, 1200u));
150 EXPECT_EQ(queue.data_size(), 1200u);
152 queue.Push(CreateBuffer(1, 1000u));
153 EXPECT_EQ(queue.data_size(), 2200u);
155 queue.Pop();
156 EXPECT_EQ(queue.data_size(), 1000u);
158 queue.Push(CreateBuffer(2, 999u));
159 queue.Push(CreateBuffer(3, 999u));
160 EXPECT_EQ(queue.data_size(), 2998u);
162 queue.Clear();
163 EXPECT_EQ(queue.data_size(), 0u);
165 queue.Push(CreateBuffer(4, 1400u));
166 EXPECT_EQ(queue.data_size(), 1400u);
169 } // namespace media