Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Apr 24, 2018
1 parent 58c3524 commit 81dacec
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 80 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
videojs-wavesurfer changelog
============================

2.3.1 - unreleased
------------------

- Make sure plugin is only registered once
- Add `isDestroyed` method
- Add more tests


2.3.0 - 2018/04/16
------------------

Expand Down
7 changes: 5 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ var webpackConfig = require('./build-config/webpack.prod.main.js');

var chromeFlags = [
'--no-sandbox',
'--use-fake-ui-for-media-stream'
'--use-fake-device-for-media-stream',
'--use-fake-ui-for-media-stream',
'--use-file-for-fake-audio-capture=test/support/demo.wav',
'--autoplay-policy=no-user-gesture-required'
];

module.exports = function(config) {
Expand All @@ -20,7 +23,7 @@ module.exports = function(config) {
hostname: 'localhost',
port: 9876,
logLevel: config.LOG_INFO,
singleRun: true, // enable for headless testing
singleRun: true,
autoWatch: false,
files: [
// demo files
Expand Down
87 changes: 56 additions & 31 deletions src/js/videojs.wavesurfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ class Wavesurfer extends Plugin {
*/
pause() {
// show play button
this.player.controlBar.playToggle.handlePause();
if (this.player.controlBar.playToggle.contentEl()) {
this.player.controlBar.playToggle.handlePause();
}

if (this.liveMode) {
// pause microphone visualization
Expand Down Expand Up @@ -394,6 +396,15 @@ class Wavesurfer extends Plugin {
this.log('Destroyed plugin');
}

/**
* Indicates whether the plugin is destroyed or not.
*
* @return {boolean} Plugin destroyed or not.
*/
isDestroyed() {
return this.player && (this.player.children() === null);
}

/**
* Remove the player and waveform.
*/
Expand Down Expand Up @@ -486,9 +497,11 @@ class Wavesurfer extends Plugin {
let time = Math.min(currentTime, duration);

// update current time display component
this.player.controlBar.currentTimeDisplay.formattedTime_ =
this.player.controlBar.currentTimeDisplay.contentEl().lastChild.textContent =
formatTime(time, duration, this.msDisplayMax);
if (this.player.controlBar.currentTimeDisplay.contentEl()) {
this.player.controlBar.currentTimeDisplay.formattedTime_ =
this.player.controlBar.currentTimeDisplay.contentEl().lastChild.textContent =
formatTime(time, duration, this.msDisplayMax);
}
}

/**
Expand Down Expand Up @@ -516,9 +529,11 @@ class Wavesurfer extends Plugin {
duration = isNaN(duration) ? 0 : duration;

// update duration display component
this.player.controlBar.durationDisplay.formattedTime_ =
this.player.controlBar.durationDisplay.contentEl().lastChild.textContent =
formatTime(duration, duration, this.msDisplayMax);
if (this.player.controlBar.durationDisplay.contentEl()) {
this.player.controlBar.durationDisplay.formattedTime_ =
this.player.controlBar.durationDisplay.contentEl().lastChild.textContent =
formatTime(duration, duration, this.msDisplayMax);
}
}

/**
Expand All @@ -540,10 +555,14 @@ class Wavesurfer extends Plugin {
this.setDuration();

// enable and show play button
this.player.controlBar.playToggle.show();
if (this.player.controlBar.playToggle.contentEl()) {
this.player.controlBar.playToggle.show();
}

// hide loading spinner
this.player.loadingSpinner.hide();
if (this.player.loadingSpinner.contentEl()) {
this.player.loadingSpinner.hide();
}

// auto-play when ready (if enabled)
if (this.player.options_.autoplay === true) {
Expand Down Expand Up @@ -702,30 +721,34 @@ class Wavesurfer extends Plugin {
* @private
*/
redrawWaveform(newWidth, newHeight) {
let rect = this.player.el_.getBoundingClientRect();
if (newWidth === undefined) {
// get player width
newWidth = rect.width;
}
if (newHeight === undefined) {
// get player height
newHeight = rect.height;
}
if (!this.isDestroyed()) {
if (this.player.el_) {
let rect = this.player.el_.getBoundingClientRect();
if (newWidth === undefined) {
// get player width
newWidth = rect.width;
}
if (newHeight === undefined) {
// get player height
newHeight = rect.height;
}
}

// destroy old drawing
this.surfer.drawer.destroy();
// destroy old drawing
this.surfer.drawer.destroy();

// set new dimensions
this.surfer.params.width = newWidth;
this.surfer.params.height = newHeight - this.player.controlBar.height();
// set new dimensions
this.surfer.params.width = newWidth;
this.surfer.params.height = newHeight - this.player.controlBar.height();

// redraw waveform
this.surfer.createDrawer();
this.surfer.drawer.wrapper.className = wavesurferClassName;
this.surfer.drawBuffer();
// redraw waveform
this.surfer.createDrawer();
this.surfer.drawer.wrapper.className = wavesurferClassName;
this.surfer.drawBuffer();

// make sure playhead is restored at right position
this.surfer.drawer.progress(this.surfer.backend.getPlayedPercents());
// make sure playhead is restored at right position
this.surfer.drawer.progress(this.surfer.backend.getPlayedPercents());
}
}

/**
Expand All @@ -739,9 +762,11 @@ class Wavesurfer extends Plugin {
// version nr is injected during build
Wavesurfer.VERSION = __VERSION__;

// register plugin
// register plugin once
videojs.Wavesurfer = Wavesurfer;
videojs.registerPlugin('wavesurfer', Wavesurfer);
if (videojs.getPlugin('wavesurfer') === undefined) {
videojs.registerPlugin('wavesurfer', Wavesurfer);
}

// register the WavesurferTech as 'Html5' to override the default html5 tech.
// If we register it as anything other then 'Html5', the <audio> element will
Expand Down
13 changes: 8 additions & 5 deletions test/fluid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import window from 'global/window';

import TestHelpers from './test-helpers.js';


/** @test {Wavesurfer} */
describe('Wavesurfer Fluid', function() {
var player;

beforeEach(function() {
// cleanup all players
TestHelpers.cleanup();

// create new player
let options = {
controls: true,
Expand All @@ -32,13 +28,20 @@ describe('Wavesurfer Fluid', function() {
}
}
};
player = TestHelpers.makePlayer(undefined, options);
var tag = TestHelpers.makeTag('audio', 'fluidAudio');
player = TestHelpers.makePlayer(tag, options);
});

afterEach(function() {
// delete player
player.dispose();
});

/** @test {Wavesurfer#redrawWaveform} */
it('should redraw waveform', function(done) {
player.one('waveReady', function() {
player.wavesurfer().redrawWaveform(100, 200);

done();
});
});
Expand Down
43 changes: 10 additions & 33 deletions test/live.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,17 @@

import TestHelpers from './test-helpers.js';


/** @test {Wavesurfer} */
describe('Wavesurfer Live', function() {
var player;

beforeEach(function() {
// cleanup all players
TestHelpers.cleanup();

// create new player
let options = {
controls: true,
fluid: false,
width: 600,
height: 300,
controlBar: {
currentTimeDisplay: false,
timeDivider: false,
durationDisplay: false,
remainingTimeDisplay: false,
volumePanel: false
},
plugins: {
wavesurfer: {
src: 'live',
waveColor: 'black',
debug: false,
cursorWidth: 0,
interact: false,
hideScrollbar: true
}
}
};
player = TestHelpers.makePlayer(undefined, options);
player = TestHelpers.makeLivePlayer();
});

afterEach(function() {
// delete player
player.dispose();
});

/** @test {Wavesurfer} */
Expand All @@ -46,11 +23,11 @@ describe('Wavesurfer Live', function() {
// trigger click event on playToggle button to start mic
TestHelpers.triggerDomEvent(player.controlBar.playToggle.el(), 'click');

player.wavesurfer().pause();
player.wavesurfer().play();
player.wavesurfer().play();
player.wavesurfer().play()
player.wavesurfer().pause()

done();
// mic is active for a while
setTimeout(done, 1000);
});
});

Expand Down
10 changes: 6 additions & 4 deletions test/tech.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ describe('WavesurferTech', function() {
var player;

beforeEach(function() {
// cleanup all players
TestHelpers.cleanup();

// create audio element with text track
const tag = TestHelpers.makeTag('audio', 'myAudioTextTracks');
const track = document.createElement('track');
Expand All @@ -26,6 +23,11 @@ describe('WavesurferTech', function() {
player = TestHelpers.makePlayer(tag);
});

afterEach(function() {
// delete player
player.dispose();
});

/** @test {WavesurferTech} */
it('should display interface elements', function(done) {

Expand Down Expand Up @@ -91,7 +93,7 @@ describe('WavesurferTech', function() {
});
});

/** @test {WavesurferTech#duration} */
/** @test {WavesurferTech#waveready} */
it('should play and pause', function(done) {

player.one('waveReady', function() {
Expand Down
22 changes: 22 additions & 0 deletions test/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ const TestHelpers = {
return videojs(elementTag.id, playerOptions);
},

makeLivePlayer() {
var tag = TestHelpers.makeTag('audio', 'liveAudio');
return this.makePlayer(tag, {
controls: true,
autoplay: false,
fluid: false,
loop: false,
width: 600,
height: 300,
plugins: {
wavesurfer: {
src: 'live',
waveColor: 'black',
debug: true,
cursorWidth: 0,
interact: false,
hideScrollbar: true
}
}
});
},

/**
* Dispose all players.
*/
Expand Down

0 comments on commit 81dacec

Please sign in to comment.