Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / base / js / session_logger_unittest.js
blob035c655afc28dca28b99c4b51585d9db4b0b3666
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 (function() {
7 'use strict';
9 /** @type {remoting.SessionLogger} */
10 var logger = null;
12 /** @type {function(Object)} */
13 var logWriter;
15 /** @type {sinon.Spy} */
16 var logWriterSpy = null;
18 /** @type {sinon.TestStub} */
19 var userAgentStub;
21 QUnit.module('SessionLogger', {
22   beforeEach: function() {
23     userAgentStub = sinon.stub(remoting, 'getUserAgent');
24     userAgentStub.returns(
25       'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36' +
26       ' (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36,gzip(gfe)');
28     var spy = sinon.spy();
29     logWriterSpy = /** @type {sinon.Spy} */ (spy);
30     logWriter = /** @type {function(Object)} */ (spy);
31   },
32   afterEach: function() {
33     userAgentStub.restore();
34     logger = null;
35   }
36 });
38 /**
39  *  @param {QUnit.Assert} assert
40  *  @param {number} index
41  *  @param {Object} expected
42  */
43 function verifyEvent(assert, index, expected) {
44   var event = /** @type {Object} */ (logWriterSpy.getCall(index).args[0]);
46   for (var key in expected) {
47     assert.deepEqual(event[key], expected[key],
48                      'Verifying ChromotingEvent.' + key);
49   }
52 QUnit.test('logSignalStrategyProgress()', function(assert) {
53   var Event = remoting.ChromotingEvent;
54   logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
56   logger.logSignalStrategyProgress(
57       remoting.SignalStrategy.Type.WCS,
58       remoting.FallbackSignalStrategy.Progress.TIMED_OUT);
60   logger.logSignalStrategyProgress(
61       remoting.SignalStrategy.Type.XMPP,
62       remoting.FallbackSignalStrategy.Progress.SUCCEEDED);
64   verifyEvent(assert, 0, {
65     type: Event.Type.SIGNAL_STRATEGY_PROGRESS,
66     signal_strategy_type: Event.SignalStrategyType.WCS,
67     signal_strategy_progress: Event.SignalStrategyProgress.TIMED_OUT
68   });
70   verifyEvent(assert, 1, {
71     type: Event.Type.SIGNAL_STRATEGY_PROGRESS,
72     signal_strategy_type: Event.SignalStrategyType.XMPP,
73     signal_strategy_progress: Event.SignalStrategyProgress.SUCCEEDED
74   });
75 });
77 QUnit.test('logClientSessionStateChange()', function(assert){
78   var Event = remoting.ChromotingEvent;
80   logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
81   logger.setLogEntryMode(Event.Mode.ME2ME);
82   logger.setConnectionType('stun');
83   logger.setHostVersion('host_version');
85   logger.logClientSessionStateChange(
86       remoting.ClientSession.State.FAILED,
87       new remoting.Error(remoting.Error.Tag.HOST_IS_OFFLINE), null);
88   var sessionId = logger.getSessionId();
90   assert.ok(sessionId !== null);
92   verifyEvent(assert, 0, {
93     type: Event.Type.SESSION_STATE,
94     session_state: Event.SessionState.CONNECTION_FAILED,
95     connection_error: Event.ConnectionError.HOST_OFFLINE,
96     os: Event.Os.MAC,
97     os_version: '10.9.5',
98     cpu: 'Intel',
99     browser_version: '43.0.2357.81',
100     application_id: 'extensionId',
101     role: Event.Role.CLIENT,
102     mode: Event.Mode.ME2ME,
103     connection_type: Event.ConnectionType.STUN,
104     host_version: 'host_version',
105     session_id: sessionId
106   });
109 QUnit.test('logClientSessionStateChange() should handle XMPP error',
110     function(assert){
111   var Event = remoting.ChromotingEvent;
113   logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
114   logger.setLogEntryMode(Event.Mode.ME2ME);
115   logger.setConnectionType('stun');
116   logger.setHostVersion('host_version');
118   var xmppError = new remoting.ChromotingEvent.XmppError('<fake-stanza/>');
120   logger.logClientSessionStateChange(
121       remoting.ClientSession.State.FAILED,
122       new remoting.Error(remoting.Error.Tag.HOST_IS_OFFLINE), xmppError);
123   var sessionId = logger.getSessionId();
125   assert.ok(sessionId !== null);
127   verifyEvent(assert, 0, {
128     type: Event.Type.SESSION_STATE,
129     session_state: Event.SessionState.CONNECTION_FAILED,
130     connection_error: Event.ConnectionError.HOST_OFFLINE,
131     os: Event.Os.MAC,
132     os_version: '10.9.5',
133     cpu: 'Intel',
134     browser_version: '43.0.2357.81',
135     application_id: 'extensionId',
136     role: Event.Role.CLIENT,
137     mode: Event.Mode.ME2ME,
138     connection_type: Event.ConnectionType.STUN,
139     host_version: 'host_version',
140     session_id: sessionId,
141     xmpp_error: {
142       raw_stanza: '<fake-stanza/>'
143     }
144   });
147 QUnit.test('logClientSessionStateChange() should handle sessionId change.',
148   function(assert){
149   var clock = sinon.useFakeTimers();
150   var Event = remoting.ChromotingEvent;
152   // Creates the logger.
153   logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
154   logger.setLogEntryMode(Event.Mode.ME2ME);
155   logger.setConnectionType('relay');
156   logger.setHostVersion('host_version');
157   var oldSessionId = logger.getSessionId();
159   // Expires the session id.
160   clock.tick(remoting.Logger.MAX_SESSION_ID_AGE + 100);
162   // Logs the event.
163   logger.logClientSessionStateChange(
164       remoting.ClientSession.State.AUTHENTICATED, remoting.Error.none(), null);
166   var newSessionId = logger.getSessionId();
167   verifyEvent(assert, 0, {
168     type: Event.Type.SESSION_ID_OLD,
169     session_id: oldSessionId,
170     os: Event.Os.MAC,
171     os_version: '10.9.5',
172     cpu: 'Intel',
173     browser_version: '43.0.2357.81',
174     application_id: 'extensionId',
175     role: Event.Role.CLIENT,
176     mode: Event.Mode.ME2ME,
177     connection_type: Event.ConnectionType.RELAY,
178     host_version: 'host_version'
179   });
181   verifyEvent(assert, 1, {
182     type: Event.Type.SESSION_ID_NEW,
183     session_id: newSessionId,
184     os: Event.Os.MAC,
185     os_version: '10.9.5',
186     cpu: 'Intel',
187     browser_version: '43.0.2357.81',
188     application_id: 'extensionId',
189     role: Event.Role.CLIENT,
190     mode: Event.Mode.ME2ME,
191     connection_type: Event.ConnectionType.RELAY,
192     host_version: 'host_version'
193   });
195   verifyEvent(assert, 2, {
196     type: Event.Type.SESSION_STATE,
197     session_state: Event.SessionState.AUTHENTICATED,
198     connection_error: Event.ConnectionError.NONE,
199     os: Event.Os.MAC,
200     os_version: '10.9.5',
201     cpu: 'Intel',
202     browser_version: '43.0.2357.81',
203     application_id: 'extensionId',
204     role: Event.Role.CLIENT,
205     mode: Event.Mode.ME2ME,
206     connection_type: Event.ConnectionType.RELAY,
207     host_version: 'host_version',
208     session_id: newSessionId
209   });
212 QUnit.test('logClientSessionStateChange() should log session_duration.',
213   function(assert){
214   var clock = sinon.useFakeTimers();
215   var Event = remoting.ChromotingEvent;
217   // Creates the logger.
218   logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
219   logger.setLogEntryMode(Event.Mode.ME2ME);
220   logger.setConnectionType('direct');
221   logger.setHostVersion('host_version');
222   logger.setAuthTotalTime(1000);
223   clock.tick(2500);
225   // Logs the event.
226   logger.logClientSessionStateChange(
227       remoting.ClientSession.State.CONNECTED, remoting.Error.none(), null);
229   verifyEvent(assert, 0, {
230     type: Event.Type.SESSION_STATE,
231     session_state: Event.SessionState.CONNECTED,
232     connection_error: Event.ConnectionError.NONE,
233     os: Event.Os.MAC,
234     os_version: '10.9.5',
235     cpu: 'Intel',
236     browser_version: '43.0.2357.81',
237     application_id: 'extensionId',
238     role: Event.Role.CLIENT,
239     mode: Event.Mode.ME2ME,
240     connection_type: Event.ConnectionType.DIRECT,
241     host_version: 'host_version',
242     session_id: logger.getSessionId(),
243     session_duration: 1.5
244   });
247 QUnit.test('logStatistics()', function(assert) {
248   var clock = sinon.useFakeTimers();
249   var Event = remoting.ChromotingEvent;
251   // Creates the logger.
252   logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
253   logger.setLogEntryMode(Event.Mode.LGAPP);
254   logger.setConnectionType('direct');
255   logger.setHostVersion('host_version');
257   // Log the statistics.
258   logger.logStatistics({
259     videoBandwidth: 1.0,
260     captureLatency: 1.0,
261     encodeLatency: 1.0,
262     decodeLatency: 0.0,
263     renderLatency: 1.0,
264     roundtripLatency: 1.0
265   });
267   logger.logStatistics({
268     videoBandwidth: 2.0,
269     captureLatency: 2.0,
270     encodeLatency: 1.0,
271     decodeLatency: 0.0,
272     renderLatency: 2.0,
273     roundtripLatency: 2.0
274   });
276   sinon.assert.notCalled(logWriterSpy);
277   // Stats should only be accumulated at |CONNECTION_STATS_ACCUMULATE_TIME|.
278   clock.tick(remoting.Logger.CONNECTION_STATS_ACCUMULATE_TIME + 10);
280   logger.logStatistics({
281     videoBandwidth: 3.0,
282     captureLatency: 3.0,
283     encodeLatency: 1.0,
284     decodeLatency: 0.0,
285     renderLatency: 0.0,
286     roundtripLatency: 0.0
287   });
289   verifyEvent(assert, 0, {
290     type: Event.Type.CONNECTION_STATISTICS,
291     os: Event.Os.MAC,
292     os_version: '10.9.5',
293     cpu: 'Intel',
294     browser_version: '43.0.2357.81',
295     application_id: 'extensionId',
296     role: Event.Role.CLIENT,
297     mode: Event.Mode.LGAPP,
298     connection_type: Event.ConnectionType.DIRECT,
299     host_version: 'host_version',
300     session_id: logger.getSessionId(),
301     video_bandwidth: 2.0,
302     capture_latency: 2.0,
303     encode_latency: 1.0,
304     decode_latency: 0.0,
305     render_latency: 1.0,
306     roundtrip_latency: 1.0
307   });
310 QUnit.test('logStatistics() should not log if all stats are zeros ',
311     function(assert) {
312   var clock = sinon.useFakeTimers();
313   var Event = remoting.ChromotingEvent;
315   // Creates the logger.
316   logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
318   logger.logStatistics({
319     videoBandwidth: 0.0,
320     captureLatency: 0.0,
321     encodeLatency: 0.0,
322     decodeLatency: 0.0,
323     renderLatency: 0.0,
324     roundtripLatency: 0.0
325   });
327   clock.tick(remoting.Logger.CONNECTION_STATS_ACCUMULATE_TIME + 10);
329   logger.logStatistics({
330     videoBandwidth: 0.0,
331     captureLatency: 0.0,
332     encodeLatency: 0.0,
333     decodeLatency: 0.0,
334     renderLatency: 0.0,
335     roundtripLatency: 0.0
336   });
338   sinon.assert.notCalled(logWriterSpy);
342 })();