2014-04-28 16:11:55 +00:00
|
|
|
( function ( mw, $ ) {
|
2014-09-04 23:00:55 +00:00
|
|
|
QUnit.module( 'mmv.logging.DurationLogger', QUnit.newMwEnvironment({
|
2014-04-28 16:11:55 +00:00
|
|
|
setup: function () {
|
|
|
|
this.clock = this.sandbox.useFakeTimers();
|
|
|
|
}
|
|
|
|
} ) );
|
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
QUnit.test( 'start()', 8, function ( assert ) {
|
|
|
|
var durationLogger = new mw.mmv.durationLogger.constructor();
|
2014-05-19 09:24:54 +00:00
|
|
|
durationLogger.samplingFactor = 1;
|
2014-04-28 16:11:55 +00:00
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
try {
|
|
|
|
durationLogger.start();
|
|
|
|
} catch ( e ) {
|
|
|
|
assert.ok( true, 'Exception raised when calling start() without parameters' );
|
|
|
|
}
|
2014-04-28 16:11:55 +00:00
|
|
|
assert.ok( $.isEmptyObject( durationLogger.starts ), 'No events saved by DurationLogger' );
|
|
|
|
|
|
|
|
durationLogger.start( 'foo' );
|
|
|
|
assert.strictEqual( durationLogger.starts.foo, 0, 'Event start saved' );
|
|
|
|
|
|
|
|
this.clock.tick( 1000 );
|
|
|
|
durationLogger.start( 'bar' );
|
|
|
|
assert.strictEqual( durationLogger.starts.bar, 1000, 'Later event start saved' );
|
|
|
|
|
|
|
|
durationLogger.start( 'foo' );
|
|
|
|
assert.strictEqual( durationLogger.starts.foo, 0, 'Event start not overritten' );
|
|
|
|
|
|
|
|
this.clock.tick( 666 );
|
|
|
|
durationLogger.start( [ 'baz', 'bob', 'bar' ] );
|
|
|
|
assert.strictEqual( durationLogger.starts.baz, 1666, 'First simultaneous event start saved' );
|
|
|
|
assert.strictEqual( durationLogger.starts.bob, 1666, 'Second simultaneous event start saved' );
|
|
|
|
assert.strictEqual( durationLogger.starts.bar, 1000, 'Third simultaneous event start not overwritten' );
|
|
|
|
} );
|
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
QUnit.test( 'stop()', 17, function ( assert ) {
|
2014-05-05 07:52:34 +00:00
|
|
|
var dependenciesDeferred = $.Deferred(),
|
2014-05-19 09:24:54 +00:00
|
|
|
fakeEventLog = { logEvent : this.sandbox.stub() },
|
2014-05-05 07:52:34 +00:00
|
|
|
durationLogger = new mw.mmv.durationLogger.constructor();
|
2014-04-28 16:11:55 +00:00
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
durationLogger.samplingFactor = 1;
|
2014-09-26 21:42:30 +00:00
|
|
|
durationLogger.schemaSupportsCountry = this.sandbox.stub().returns( true );
|
2014-05-19 09:24:54 +00:00
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
this.sandbox.stub( mw.user, 'isAnon' ).returns( false );
|
2014-05-19 09:24:54 +00:00
|
|
|
this.sandbox.stub( durationLogger, 'loadDependencies' ).returns( dependenciesDeferred.promise() );
|
2014-04-28 16:11:55 +00:00
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
try {
|
|
|
|
durationLogger.stop();
|
|
|
|
} catch ( e ) {
|
|
|
|
assert.ok( true, 'Exception raised when calling stop() without parameters' );
|
2014-04-28 16:11:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
durationLogger.setEventLog( fakeEventLog );
|
2014-04-28 16:11:55 +00:00
|
|
|
|
|
|
|
durationLogger.start( 'bar' );
|
|
|
|
this.clock.tick( 1000 );
|
|
|
|
durationLogger.stop( 'bar' );
|
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
assert.ok( !fakeEventLog.logEvent.called, 'Event queued if dependencies not loaded' );
|
|
|
|
|
|
|
|
// Queue a second item
|
|
|
|
|
|
|
|
durationLogger.start( 'bob' );
|
|
|
|
this.clock.tick( 4000 );
|
|
|
|
durationLogger.stop( 'bob' );
|
|
|
|
|
|
|
|
assert.ok( !fakeEventLog.logEvent.called, 'Event queued if dependencies not loaded' );
|
|
|
|
|
|
|
|
dependenciesDeferred.resolve();
|
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
assert.strictEqual( fakeEventLog.logEvent.getCall( 0 ).args[ 0 ], 'MultimediaViewerDuration', 'EventLogging schema is correct' );
|
|
|
|
assert.deepEqual( fakeEventLog.logEvent.getCall( 0 ).args[ 1 ], { type : 'bar', duration : 1000, loggedIn : true, samplingFactor : 1 },
|
|
|
|
'EventLogging data is correct' );
|
|
|
|
|
|
|
|
assert.strictEqual( fakeEventLog.logEvent.getCall( 1 ).args[ 0 ], 'MultimediaViewerDuration', 'EventLogging schema is correct' );
|
|
|
|
assert.deepEqual( fakeEventLog.logEvent.getCall( 1 ).args[ 1 ], { type : 'bob', duration : 4000, loggedIn : true, samplingFactor : 1 },
|
|
|
|
'EventLogging data is correct' );
|
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
assert.strictEqual( fakeEventLog.logEvent.callCount, 2, 'logEvent called when processing the queue' );
|
|
|
|
|
|
|
|
durationLogger.start( 'foo' );
|
|
|
|
this.clock.tick( 3000 );
|
|
|
|
durationLogger.stop( 'foo' );
|
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
assert.strictEqual( fakeEventLog.logEvent.getCall( 2 ).args[ 0 ], 'MultimediaViewerDuration', 'EventLogging schema is correct' );
|
|
|
|
assert.deepEqual( fakeEventLog.logEvent.getCall( 2 ).args[ 1 ], { type : 'foo', duration : 3000, loggedIn : true, samplingFactor : 1 },
|
|
|
|
'EventLogging data is correct' );
|
2014-05-05 07:52:34 +00:00
|
|
|
|
2014-04-28 16:11:55 +00:00
|
|
|
assert.strictEqual( durationLogger.starts.bar, undefined, 'Start value deleted after stop' );
|
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
durationLogger.setGeo( { country : 'FR' } );
|
|
|
|
mw.user.isAnon.returns( true );
|
2014-04-28 16:11:55 +00:00
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
durationLogger.start( 'baz' );
|
|
|
|
this.clock.tick( 2000 );
|
|
|
|
durationLogger.stop( 'baz' );
|
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
assert.strictEqual( fakeEventLog.logEvent.getCall( 3 ).args[ 0 ], 'MultimediaViewerDuration', 'EventLogging schema is correct' );
|
|
|
|
assert.deepEqual( fakeEventLog.logEvent.getCall( 3 ).args[ 1 ], { type : 'baz', duration : 2000, loggedIn : false, country : 'FR', samplingFactor : 1 },
|
|
|
|
'EventLogging data is correct' );
|
2014-05-05 07:52:34 +00:00
|
|
|
|
|
|
|
assert.strictEqual( durationLogger.starts.bar, undefined, 'Start value deleted after stop' );
|
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
durationLogger.stop( 'fooz', $.now() - 9000 );
|
|
|
|
|
|
|
|
assert.deepEqual( fakeEventLog.logEvent.getCall( 4 ).args[ 1 ], { type : 'fooz', duration : 9000, loggedIn : false, country : 'FR', samplingFactor : 1 },
|
|
|
|
'EventLogging data is correct' );
|
|
|
|
|
|
|
|
assert.strictEqual( fakeEventLog.logEvent.callCount, 5, 'logEvent has been called fives times at this point in the test' );
|
2014-05-05 07:52:34 +00:00
|
|
|
|
|
|
|
durationLogger.stop( 'foo' );
|
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
assert.strictEqual( fakeEventLog.logEvent.callCount, 5, 'Stop without a start doesn\'t get logged' );
|
2014-05-05 07:52:34 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'loadDependencies()', 3, function ( assert ) {
|
|
|
|
var promise,
|
|
|
|
durationLogger = new mw.mmv.durationLogger.constructor();
|
|
|
|
|
|
|
|
this.sandbox.stub( mw.loader, 'using' );
|
|
|
|
|
2014-05-28 22:43:35 +00:00
|
|
|
mw.loader.using.withArgs( [ 'ext.eventLogging', 'schema.MultimediaViewerDuration' ] )['throws']( 'EventLogging is missing' );
|
2014-05-05 07:52:34 +00:00
|
|
|
|
|
|
|
promise = durationLogger.loadDependencies();
|
|
|
|
|
|
|
|
assert.strictEqual( promise.state(), 'rejected', 'Promise is rejected' );
|
|
|
|
|
|
|
|
// It's necessary to reset the stub, otherwise the original withArgs keeps running alongside the new one
|
|
|
|
mw.loader.using.restore();
|
|
|
|
this.sandbox.stub( mw.loader, 'using' );
|
|
|
|
|
2014-05-28 22:43:35 +00:00
|
|
|
mw.loader.using.withArgs( [ 'ext.eventLogging', 'schema.MultimediaViewerDuration' ] )['throws']( 'EventLogging is missing' );
|
2014-05-05 07:52:34 +00:00
|
|
|
|
|
|
|
promise = durationLogger.loadDependencies();
|
|
|
|
|
|
|
|
assert.strictEqual( promise.state(), 'rejected', 'Promise is rejected' );
|
|
|
|
|
|
|
|
// It's necessary to reset the stub, otherwise the original withArgs keeps running alongside the new one
|
|
|
|
mw.loader.using.restore();
|
|
|
|
this.sandbox.stub( mw.loader, 'using' );
|
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
mw.loader.using.withArgs( [ 'ext.eventLogging', 'schema.MultimediaViewerDuration' ] ).callsArg( 1 );
|
2014-05-05 07:52:34 +00:00
|
|
|
|
|
|
|
promise = durationLogger.loadDependencies();
|
|
|
|
|
|
|
|
assert.strictEqual( promise.state(), 'resolved', 'Promise is resolved' );
|
2014-04-28 16:11:55 +00:00
|
|
|
} );
|
|
|
|
}( mediaWiki, jQuery ) );
|