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.
9 /** @type {remoting.SessionLogger} */
12 /** @type {function(Object)} */
15 /** @type {sinon.Spy} */
16 var logWriterSpy = null;
18 /** @type {sinon.TestStub} */
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);
32 afterEach: function() {
33 userAgentStub.restore();
39 * @param {QUnit.Assert} assert
40 * @param {number} index
41 * @param {Object} expected
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);
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
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
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,
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
109 QUnit.test('logClientSessionStateChange() should handle XMPP error',
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,
132 os_version: '10.9.5',
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,
142 raw_stanza: '<fake-stanza/>'
147 QUnit.test('logClientSessionStateChange() should handle sessionId change.',
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);
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,
171 os_version: '10.9.5',
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'
181 verifyEvent(assert, 1, {
182 type: Event.Type.SESSION_ID_NEW,
183 session_id: newSessionId,
185 os_version: '10.9.5',
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'
195 verifyEvent(assert, 2, {
196 type: Event.Type.SESSION_STATE,
197 session_state: Event.SessionState.AUTHENTICATED,
198 connection_error: Event.ConnectionError.NONE,
200 os_version: '10.9.5',
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
212 QUnit.test('logClientSessionStateChange() should log session_duration.',
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);
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,
234 os_version: '10.9.5',
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
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({
264 roundtripLatency: 1.0
267 logger.logStatistics({
273 roundtripLatency: 2.0
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({
286 roundtripLatency: 0.0
289 verifyEvent(assert, 0, {
290 type: Event.Type.CONNECTION_STATISTICS,
292 os_version: '10.9.5',
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,
306 roundtrip_latency: 1.0
310 QUnit.test('logStatistics() should not log if all stats are zeros ',
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({
324 roundtripLatency: 0.0
327 clock.tick(remoting.Logger.CONNECTION_STATS_ACCUMULATE_TIME + 10);
329 logger.logStatistics({
335 roundtripLatency: 0.0
338 sinon.assert.notCalled(logWriterSpy);