Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / webapp / base / js / session_logger_unittest.js
blob475c279f55f5693a22c5c99d6363610ecb64bc01
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.equal(event[key], expected[key], 'Verifying ChromotingEvent.' + key);
48   }
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
67   });
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
73   });
74 });
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,
95     os: Event.Os.MAC,
96     os_version: '10.9.5',
97     cpu: 'Intel',
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
105   });
108 QUnit.test('logClientSessionStateChange() should handle sessionId change.',
109   function(assert){
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);
123   // Logs the event.
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,
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.RELAY,
139     host_version: 'host_version'
140   });
142   verifyEvent(assert, 1, {
143     type: Event.Type.SESSION_ID_NEW,
144     session_id: newSessionId,
145     os: Event.Os.MAC,
146     os_version: '10.9.5',
147     cpu: 'Intel',
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'
154   });
156   verifyEvent(assert, 2, {
157     type: Event.Type.SESSION_STATE,
158     session_state: Event.SessionState.AUTHENTICATED,
159     connection_error: Event.ConnectionError.NONE,
160     os: Event.Os.MAC,
161     os_version: '10.9.5',
162     cpu: 'Intel',
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
170   });
173 QUnit.test('logClientSessionStateChange() should log session_duration.',
174   function(assert){
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);
184   clock.tick(2500);
186   // Logs the event.
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,
194     os: Event.Os.MAC,
195     os_version: '10.9.5',
196     cpu: 'Intel',
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
205   });
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({
220     videoBandwidth: 1.0,
221     captureLatency: 1.0,
222     encodeLatency: 1.0,
223     decodeLatency: 0.0,
224     renderLatency: 1.0,
225     roundtripLatency: 1.0
226   });
228   logger.logStatistics({
229     videoBandwidth: 2.0,
230     captureLatency: 2.0,
231     encodeLatency: 1.0,
232     decodeLatency: 0.0,
233     renderLatency: 2.0,
234     roundtripLatency: 2.0
235   });
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({
242     videoBandwidth: 3.0,
243     captureLatency: 3.0,
244     encodeLatency: 1.0,
245     decodeLatency: 0.0,
246     renderLatency: 0.0,
247     roundtripLatency: 0.0
248   });
250   verifyEvent(assert, 0, {
251     type: Event.Type.CONNECTION_STATISTICS,
252     os: Event.Os.MAC,
253     os_version: '10.9.5',
254     cpu: 'Intel',
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,
264     encode_latency: 1.0,
265     decode_latency: 0.0,
266     render_latency: 1.0,
267     roundtrip_latency: 1.0
268   });
271 QUnit.test('logStatistics() should not log if all stats are zeros ',
272     function(assert) {
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({
280     videoBandwidth: 0.0,
281     captureLatency: 0.0,
282     encodeLatency: 0.0,
283     decodeLatency: 0.0,
284     renderLatency: 0.0,
285     roundtripLatency: 0.0
286   });
288   clock.tick(remoting.Logger.CONNECTION_STATS_ACCUMULATE_TIME + 10);
290   logger.logStatistics({
291     videoBandwidth: 0.0,
292     captureLatency: 0.0,
293     encodeLatency: 0.0,
294     decodeLatency: 0.0,
295     renderLatency: 0.0,
296     roundtripLatency: 0.0
297   });
299   sinon.assert.notCalled(logWriterSpy);
303 })();