Skip to content

Commit

Permalink
Increase BufferHelper test coverage to 100% (video-dev#2052)
Browse files Browse the repository at this point in the history
  • Loading branch information
springuper authored and johnBartos committed Jan 22, 2019
1 parent dc6c404 commit 3fd4e2a
Showing 1 changed file with 179 additions and 41 deletions.
220 changes: 179 additions & 41 deletions tests/unit/utils/buffer-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function createMockBuffer (buffered) {

describe('BufferHelper', function () {
describe('isBuffered', function () {
// |////////|__________|////////////////|
// 0 0.5 1 2.0
// |////////|________|////////////////|
// 0 0.5 1 2
const media = {
get buffered () {
return createMockBuffer([
Expand All @@ -23,20 +23,12 @@ describe('BufferHelper', function () {
},
{
startPTS: 1,
endPTS: 2.0
endPTS: 2
}
]);
}
};

it('should return true if media.buffered throw error', function () {
const invalidMedia = {
get buffered () {
throw new Error('InvalidStateError');
}
};
assert.equal(BufferHelper.isBuffered(invalidMedia, 0), false);
});
it('should return true if some media.buffered includes the position', function () {
assert.equal(BufferHelper.isBuffered(media, 0), true);
assert.equal(BufferHelper.isBuffered(media, 0.1), true);
Expand All @@ -50,11 +42,22 @@ describe('BufferHelper', function () {
assert.equal(BufferHelper.isBuffered(media, 0.9), false);
assert.equal(BufferHelper.isBuffered(media, 2.1), false);
});
it('should return false if media.buffered throws error', function () {
const invalidMedia = {
get buffered () {
throw new Error('InvalidStateError');
}
};
assert.equal(BufferHelper.isBuffered(invalidMedia, 0), false);
});
it('should return false if media does not exist', function () {
assert.equal(BufferHelper.isBuffered(null, 0), false);
});
});
describe('bufferInfo', () => {
it('should return found buffer info when maxHoleDuration is 0', function () {
// |////////|__________|////////////////|
// 0 0.5 1 2.0
it('should return found buffer info if some media.buffered includes pos with allowed error', function () {
// |////////|________|////////////////|
// 0 0.5 1 2
const media = {
get buffered () {
return createMockBuffer([
Expand All @@ -64,7 +67,7 @@ describe('BufferHelper', function () {
},
{
startPTS: 1,
endPTS: 2.0
endPTS: 2
}
]);
}
Expand All @@ -76,70 +79,205 @@ describe('BufferHelper', function () {
end: 0.5,
nextStart: 1
});
assert.deepEqual(BufferHelper.bufferInfo(media, 0.5, maxHoleDuration), {
});
it('should return empty buffer info if media does not exist', function () {
const invalidMedia = {
get buffered () {
throw new Error('InvalidStateError');
}
};
const maxHoleDuration = 0;
assert.deepEqual(BufferHelper.bufferInfo(invalidMedia, 0, maxHoleDuration), {
len: 0,
start: 0,
end: 0,
nextStart: undefined
});
});
it('should return empty buffer info if media does not exist', function () {
const maxHoleDuration = 0;
assert.deepEqual(BufferHelper.bufferInfo(null, 0, maxHoleDuration), {
len: 0,
start: 0,
end: 0,
nextStart: undefined
});
});
});
describe('bufferedInfo', () => {
it('should return found buffer info when maxHoleDuration is 0', function () {
// |////////|________|////////////////|
// 0 0.5 1 2
const buffered = [
{
start: 0,
end: 0.5
},
{
start: 1,
end: 2
}
];
const maxHoleDuration = 0;
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 0, maxHoleDuration), {
len: 0.5,
start: 0,
end: 0.5,
nextStart: 1
});
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 0.5, maxHoleDuration), {
len: 0,
start: 0.5,
end: 0.5,
nextStart: 1
});
assert.deepEqual(BufferHelper.bufferInfo(media, 1, maxHoleDuration), {
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 1, maxHoleDuration), {
len: 1,
start: 1,
end: 2,
nextStart: undefined
});
assert.deepEqual(BufferHelper.bufferInfo(media, 2, maxHoleDuration), {
len: 0,
start: 2,
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 1.5, maxHoleDuration), {
len: 0.5,
start: 1,
end: 2,
nextStart: undefined
});
});
it('should return found buffer info when maxHoleDuration is 0.5', function () {
// |////////|__________|////////////////|
// 0 0.5 1 2.0
const media = {
get buffered () {
return createMockBuffer([
{
startPTS: 0,
endPTS: 0.5
},
{
startPTS: 1,
endPTS: 2.0
}
]);
// |////////|________|////////////////|
// 0 0.5 1 2
const buffered = [
{
start: 0,
end: 0.5
},
{
start: 1,
end: 2
}
};
];
const maxHoleDuration = 0.5;
assert.deepEqual(BufferHelper.bufferInfo(media, 0, maxHoleDuration), {
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 0, maxHoleDuration), {
len: 0.5,
start: 0,
end: 0.5,
nextStart: 1
});
// M: maxHoleDuration: 0.5
// |////////|__________|////////////////|
// 0 0.5 --- M --- 1 2.0
assert.deepEqual(BufferHelper.bufferInfo(media, 0.5, maxHoleDuration), {
// |////////|________|////////////////|
// 0 0.5 - M - 1 2
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 0.5, maxHoleDuration), {
len: 1.5,
start: 1,
end: 2,
nextStart: undefined
});
assert.deepEqual(BufferHelper.bufferInfo(media, 1, maxHoleDuration), {
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 1, maxHoleDuration), {
len: 1,
start: 1,
end: 2,
nextStart: undefined
});
assert.deepEqual(BufferHelper.bufferInfo(media, 2, maxHoleDuration), {
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 2, maxHoleDuration), {
len: 0,
start: 2,
end: 2,
nextStart: undefined
});
});
it('should be able to handle unordered buffered', function () {
// |////////|________|////////////////|
// 0 0.5 1.0 2.0
const buffered = [
{
start: 1,
end: 2
},
{
start: 0,
end: 0.5
}
];
const maxHoleDuration = 0.5;
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 0, maxHoleDuration), {
len: 0.5,
start: 0,
end: 0.5,
nextStart: 1
});
});
it('should be able to merge adjacent time ranges with a small hole', function () {
// |////////|________|////////////////|
// 0 0.5 1 2
const buffered = [
{
start: 0,
end: 0.5
},
{
start: 1,
end: 2
}
];
const maxHoleDuration = 1;
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 0.8, maxHoleDuration), {
len: 1.2,
start: 0,
end: 2,
nextStart: undefined
});
});
it('should be able to merge overlapping time ranges', function () {
// |////////|________|
// |////////|////////|
// 0 0.5 1
const buffered = [
{
start: 0,
end: 0.5
},
{
start: 0,
end: 1
}
];
const maxHoleDuration = 0.5;
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 0.5, maxHoleDuration), {
len: 0.5,
start: 0,
end: 1,
nextStart: undefined
});
});
it('should return empty buffered if pos is out of range', function () {
const buffered = [
{
start: 0,
end: 0.5
},
{
start: 0,
end: 1
}
];
const maxHoleDuration = 0;
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 5, maxHoleDuration), {
len: 0,
start: 5,
end: 5,
nextStart: undefined
});
});
it('should return empty buffered if buffered is empty', function () {
const buffered = [];
const maxHoleDuration = 0;
assert.deepEqual(BufferHelper.bufferedInfo(buffered, 5, maxHoleDuration), {
len: 0,
start: 5,
end: 5,
nextStart: undefined
});
});
});
});

0 comments on commit 3fd4e2a

Please sign in to comment.