summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/tests/qunit/mmv/logging/mmv.logging.DurationLogger.test.js
blob: 474fc08cdbd78dac874c3e7da80799b6d99f0311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
( function ( mw, $ ) {
	QUnit.module( 'mmv.logging.DurationLogger', QUnit.newMwEnvironment( {
		setup: function () {
			this.clock = this.sandbox.useFakeTimers();

			// since jQuery 2/3, $.now will capture a reference to Date.now
			// before above fake timer gets a chance to override it, so I'll
			// override that new behavior in order to run these tests...
			// @see https://github.com/sinonjs/lolex/issues/76
			this.oldNow = $.now;
			$.now = function () { return +( new Date() ); };
		},

		teardown: function () {
			$.now = this.oldNow;
			this.clock.restore();
		}
	} ) );

	QUnit.test( 'start()', function ( assert ) {
		var durationLogger = new mw.mmv.durationLogger.constructor();
		durationLogger.samplingFactor = 1;

		try {
			durationLogger.start();
		} catch ( e ) {
			assert.ok( true, 'Exception raised when calling start() without parameters' );
		}
		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' );
	} );

	QUnit.test( 'stop()', function ( assert ) {
		var durationLogger = new mw.mmv.durationLogger.constructor();

		try {
			durationLogger.stop();
		} catch ( e ) {
			assert.ok( true, 'Exception raised when calling stop() without parameters' );
		}

		durationLogger.stop( 'foo' );

		assert.strictEqual( durationLogger.stops.foo, 0, 'Event stop saved' );

		this.clock.tick( 1000 );
		durationLogger.stop( 'foo' );

		assert.strictEqual( durationLogger.stops.foo, 0, 'Event stop not overwritten' );

		durationLogger.stop( 'foo', 1 );

		assert.strictEqual( durationLogger.starts.foo, 1, 'Event start saved' );

		durationLogger.stop( 'foo', 2 );

		assert.strictEqual( durationLogger.starts.foo, 1, 'Event start not overwritten' );
	} );

	QUnit.test( 'record()', function ( assert ) {
		var dependenciesDeferred = $.Deferred(),
			fakeEventLog = { logEvent: this.sandbox.stub() },
			durationLogger = new mw.mmv.durationLogger.constructor();

		durationLogger.samplingFactor = 1;
		durationLogger.schemaSupportsCountry = this.sandbox.stub().returns( true );

		this.sandbox.stub( mw.user, 'isAnon' ).returns( false );
		this.sandbox.stub( durationLogger, 'loadDependencies' ).returns( dependenciesDeferred.promise() );

		try {
			durationLogger.record();
		} catch ( e ) {
			assert.ok( true, 'Exception raised when calling record() without parameters' );
		}

		durationLogger.setEventLog( fakeEventLog );

		durationLogger.start( 'bar' );
		this.clock.tick( 1000 );
		durationLogger.stop( 'bar' );
		durationLogger.record( 'bar' );

		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' );
		durationLogger.record( 'bob' );

		assert.ok( !fakeEventLog.logEvent.called, 'Event queued if dependencies not loaded' );

		dependenciesDeferred.resolve();
		this.clock.tick( 10 );

		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' );

		assert.strictEqual( fakeEventLog.logEvent.callCount, 2, 'logEvent called when processing the queue' );

		durationLogger.start( 'foo' );
		this.clock.tick( 3000 );
		durationLogger.stop( 'foo' );
		durationLogger.record( 'foo' );
		this.clock.tick( 10 );

		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' );

		assert.strictEqual( durationLogger.starts.bar, undefined, 'Start value deleted after record' );
		assert.strictEqual( durationLogger.stops.bar, undefined, 'Stop value deleted after record' );

		durationLogger.setGeo( { country: 'FR' } );
		mw.user.isAnon.returns( true );

		durationLogger.start( 'baz' );
		this.clock.tick( 2000 );
		durationLogger.stop( 'baz' );
		durationLogger.record( 'baz' );
		this.clock.tick( 10 );

		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' );

		assert.strictEqual( durationLogger.starts.bar, undefined, 'Start value deleted after record' );
		assert.strictEqual( durationLogger.stops.bar, undefined, 'Stop value deleted after record' );

		durationLogger.stop( 'fooz', $.now() - 9000 );
		durationLogger.record( 'fooz' );
		this.clock.tick( 10 );

		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' );

		durationLogger.stop( 'foo' );
		durationLogger.record( 'foo' );
		this.clock.tick( 10 );

		assert.strictEqual( fakeEventLog.logEvent.callCount, 5, 'Record without a start doesn\'t get logged' );

		durationLogger.start( 'foofoo' );
		durationLogger.record( 'foofoo' );
		this.clock.tick( 10 );

		assert.strictEqual( fakeEventLog.logEvent.callCount, 5, 'Record without a stop doesn\'t get logged' );

		durationLogger.start( 'extra' );
		this.clock.tick( 5000 );
		durationLogger.stop( 'extra' );
		durationLogger.record( 'extra', { bim: 'bam' } );
		this.clock.tick( 10 );

		assert.deepEqual( fakeEventLog.logEvent.getCall( 5 ).args[ 1 ], { type: 'extra', duration: 5000, loggedIn: false, country: 'FR', samplingFactor: 1, bim: 'bam' },
			'EventLogging data is correct' );
	} );

	QUnit.test( 'loadDependencies()', function ( assert ) {
		var promise,
			durationLogger = new mw.mmv.durationLogger.constructor();

		this.sandbox.stub( mw.loader, 'using' );

		mw.loader.using.withArgs( [ 'ext.eventLogging', 'schema.MultimediaViewerDuration' ] ).throwsException( 'EventLogging is missing' );

		promise = durationLogger.loadDependencies();
		this.clock.tick( 10 );

		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' );

		mw.loader.using.withArgs( [ 'ext.eventLogging', 'schema.MultimediaViewerDuration' ] ).throwsException( 'EventLogging is missing' );

		promise = durationLogger.loadDependencies();
		this.clock.tick( 10 );

		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' );

		mw.loader.using.withArgs( [ 'ext.eventLogging', 'schema.MultimediaViewerDuration' ] ).callsArg( 1 );

		promise = durationLogger.loadDependencies();
		this.clock.tick( 10 );

		assert.strictEqual( promise.state(), 'resolved', 'Promise is resolved' );
	} );
}( mediaWiki, jQuery ) );