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.equal(event[key], expected[key], 'Verifying ChromotingEvent.' + key);
51 QUnit.test('logSignalStrategyProgress()', function(assert) {
52 var Event = remoting.ChromotingEvent;
53 logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
55 logger.logSignalStrategyProgress(
56 remoting.SignalStrategy.Type.WCS,
57 remoting.FallbackSignalStrategy.Progress.TIMED_OUT);
59 logger.logSignalStrategyProgress(
60 remoting.SignalStrategy.Type.XMPP,
61 remoting.FallbackSignalStrategy.Progress.SUCCEEDED);
63 verifyEvent(assert, 0, {
64 type: Event.Type.SIGNAL_STRATEGY_PROGRESS,
65 signal_strategy_type: Event.SignalStrategyType.WCS,
66 signal_strategy_progress: Event.SignalStrategyProgress.TIMED_OUT
69 verifyEvent(assert, 1, {
70 type: Event.Type.SIGNAL_STRATEGY_PROGRESS,
71 signal_strategy_type: Event.SignalStrategyType.XMPP,
72 signal_strategy_progress: Event.SignalStrategyProgress.SUCCEEDED
76 QUnit.test('logClientSessionStateChange()', function(assert){
77 var Event = remoting.ChromotingEvent;
79 logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
80 logger.setLogEntryMode(Event.Mode.ME2ME);
81 logger.setConnectionType('stun');
82 logger.setHostVersion('host_version');
84 logger.logClientSessionStateChange(
85 remoting.ClientSession.State.FAILED,
86 new remoting.Error(remoting.Error.Tag.HOST_IS_OFFLINE));
87 var sessionId = logger.getSessionId();
89 assert.ok(sessionId !== null);
91 verifyEvent(assert, 0, {
92 type: Event.Type.SESSION_STATE,
93 session_state: Event.SessionState.CONNECTION_FAILED,
94 connection_error: Event.ConnectionError.HOST_OFFLINE,
98 browser_version: '43.0.2357.81',
99 application_id: 'extensionId',
100 role: Event.Role.CLIENT,
101 mode: Event.Mode.ME2ME,
102 connection_type: Event.ConnectionType.STUN,
103 host_version: 'host_version',
104 session_id: sessionId
108 QUnit.test('logClientSessionStateChange() should handle sessionId change.',
110 var clock = sinon.useFakeTimers();
111 var Event = remoting.ChromotingEvent;
113 // Creates the logger.
114 logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
115 logger.setLogEntryMode(Event.Mode.ME2ME);
116 logger.setConnectionType('relay');
117 logger.setHostVersion('host_version');
118 var oldSessionId = logger.getSessionId();
120 // Expires the session id.
121 clock.tick(remoting.Logger.MAX_SESSION_ID_AGE + 100);
124 logger.logClientSessionStateChange(
125 remoting.ClientSession.State.AUTHENTICATED, remoting.Error.none());
127 var newSessionId = logger.getSessionId();
128 verifyEvent(assert, 0, {
129 type: Event.Type.SESSION_ID_OLD,
130 session_id: oldSessionId,
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.RELAY,
139 host_version: 'host_version'
142 verifyEvent(assert, 1, {
143 type: Event.Type.SESSION_ID_NEW,
144 session_id: newSessionId,
146 os_version: '10.9.5',
148 browser_version: '43.0.2357.81',
149 application_id: 'extensionId',
150 role: Event.Role.CLIENT,
151 mode: Event.Mode.ME2ME,
152 connection_type: Event.ConnectionType.RELAY,
153 host_version: 'host_version'
156 verifyEvent(assert, 2, {
157 type: Event.Type.SESSION_STATE,
158 session_state: Event.SessionState.AUTHENTICATED,
159 connection_error: Event.ConnectionError.NONE,
161 os_version: '10.9.5',
163 browser_version: '43.0.2357.81',
164 application_id: 'extensionId',
165 role: Event.Role.CLIENT,
166 mode: Event.Mode.ME2ME,
167 connection_type: Event.ConnectionType.RELAY,
168 host_version: 'host_version',
169 session_id: newSessionId
173 QUnit.test('logClientSessionStateChange() should log session_duration.',
175 var clock = sinon.useFakeTimers();
176 var Event = remoting.ChromotingEvent;
178 // Creates the logger.
179 logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
180 logger.setLogEntryMode(Event.Mode.ME2ME);
181 logger.setConnectionType('direct');
182 logger.setHostVersion('host_version');
183 logger.setAuthTotalTime(1000);
187 logger.logClientSessionStateChange(
188 remoting.ClientSession.State.CONNECTED, remoting.Error.none());
190 verifyEvent(assert, 0, {
191 type: Event.Type.SESSION_STATE,
192 session_state: Event.SessionState.CONNECTED,
193 connection_error: Event.ConnectionError.NONE,
195 os_version: '10.9.5',
197 browser_version: '43.0.2357.81',
198 application_id: 'extensionId',
199 role: Event.Role.CLIENT,
200 mode: Event.Mode.ME2ME,
201 connection_type: Event.ConnectionType.DIRECT,
202 host_version: 'host_version',
203 session_id: logger.getSessionId(),
204 session_duration: 1.5
208 QUnit.test('logStatistics()', function(assert) {
209 var clock = sinon.useFakeTimers();
210 var Event = remoting.ChromotingEvent;
212 // Creates the logger.
213 logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
214 logger.setLogEntryMode(Event.Mode.LGAPP);
215 logger.setConnectionType('direct');
216 logger.setHostVersion('host_version');
218 // Log the statistics.
219 logger.logStatistics({
225 roundtripLatency: 1.0
228 logger.logStatistics({
234 roundtripLatency: 2.0
237 sinon.assert.notCalled(logWriterSpy);
238 // Stats should only be accumulated at |CONNECTION_STATS_ACCUMULATE_TIME|.
239 clock.tick(remoting.Logger.CONNECTION_STATS_ACCUMULATE_TIME + 10);
241 logger.logStatistics({
247 roundtripLatency: 0.0
250 verifyEvent(assert, 0, {
251 type: Event.Type.CONNECTION_STATISTICS,
253 os_version: '10.9.5',
255 browser_version: '43.0.2357.81',
256 application_id: 'extensionId',
257 role: Event.Role.CLIENT,
258 mode: Event.Mode.LGAPP,
259 connection_type: Event.ConnectionType.DIRECT,
260 host_version: 'host_version',
261 session_id: logger.getSessionId(),
262 video_bandwidth: 2.0,
263 capture_latency: 2.0,
267 roundtrip_latency: 1.0
271 QUnit.test('logStatistics() should not log if all stats are zeros ',
273 var clock = sinon.useFakeTimers();
274 var Event = remoting.ChromotingEvent;
276 // Creates the logger.
277 logger = new remoting.SessionLogger(Event.Role.CLIENT, logWriter);
279 logger.logStatistics({
285 roundtripLatency: 0.0
288 clock.tick(remoting.Logger.CONNECTION_STATS_ACCUMULATE_TIME + 10);
290 logger.logStatistics({
296 roundtripLatency: 0.0
299 sinon.assert.notCalled(logWriterSpy);