Skip to content

Commit

Permalink
fix: player.duration() should return NaN if duration is not known (#4456
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alex-barstow authored and gkatsev committed Jul 6, 2017
1 parent 8599c8e commit 2576eda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1694,10 +1694,11 @@ class Player extends Component {
*/
duration(seconds) {
if (seconds === undefined) {
return this.cache_.duration || 0;
// return NaN if the duration is not known
return this.cache_.duration !== undefined ? this.cache_.duration : NaN;
}

seconds = parseFloat(seconds) || 0;
seconds = parseFloat(seconds);

// Standardize on Inifity for signaling video is live
if (seconds < 0) {
Expand Down
28 changes: 28 additions & 0 deletions test/unit/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1425,3 +1425,31 @@ QUnit.test('should add a class with major version', function(assert) {

player.dispose();
});

QUnit.test('player.duration() returns NaN if player.cache_.duration is undefined', function(assert) {
const player = TestHelpers.makePlayer();

player.cache_.duration = undefined;
assert.ok(Number.isNaN(player.duration()), 'returned NaN for unkown duration');
});

QUnit.test('player.duration() returns player.cache_.duration if it is defined', function(assert) {
const player = TestHelpers.makePlayer();

player.cache_.duration = 200;
assert.equal(player.duration(), 200, 'returned correct integer duration');
player.cache_.duration = 942;
assert.equal(player.duration(), 942, 'returned correct integer duration');
});

QUnit.test('player.duration() sets the value of player.cache_.duration', function(assert) {
const player = TestHelpers.makePlayer();

// set an arbitrary initial cached duration value for testing the setter functionality
player.cache_.duration = 1;

player.duration(NaN);
assert.ok(Number.isNaN(player.duration()), 'duration() set and get NaN duration value');
player.duration(200);
assert.equal(player.duration(), 200, 'duration() set and get integer duration value');
});

0 comments on commit 2576eda

Please sign in to comment.