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.TelemetryEventWriter.Service} */
11 /** @type {remoting.XhrEventWriter} */
12 var eventWriter = null;
14 /** @type {remoting.SessionLogger} */
18 * @param {sinon.TestStub} stub
19 * @param {QUnit.Assert} assert
20 * @param {number} index
21 * @param {Object} expected
23 function verifyEvent(stub, assert, index, expected) {
24 var event = /** @type {Object} */ (stub.getCall(index).args[0]);
25 for (var key in expected) {
26 assert.equal(event[key], expected[key], 'Verifying ChromotingEvent.' + key);
30 QUnit.module('TelemetryEventWriter', {
31 beforeEach: function() {
32 remoting.MockXhr.activate();
33 var ipc = base.Ipc.getInstance();
35 new remoting.XhrEventWriter('URL', chrome.storage.local, 'fake-key');
36 service = new remoting.TelemetryEventWriter.Service(ipc, eventWriter);
37 logger = new remoting.SessionLogger(
38 remoting.ChromotingEvent.Role.CLIENT,
39 remoting.TelemetryEventWriter.Client.write);
40 logger.setLogEntryMode(remoting.ChromotingEvent.Mode.ME2ME);
41 logger.setConnectionType('stun');
42 logger.setHostVersion('host_version');
44 afterEach: function() {
45 base.dispose(service);
47 base.Ipc.deleteInstance();
48 remoting.MockXhr.restore();
52 QUnit.test('Client.write() should write request.', function(assert){
53 var mockEventWriter = sinon.mock(eventWriter);
54 return service.init().then(function(){
55 mockEventWriter.expects('write').once().withArgs({id: '1'});
56 return remoting.TelemetryEventWriter.Client.write({id: '1'});
58 mockEventWriter.verify();
62 QUnit.test('should save log requests on suspension.', function(assert){
63 var mockEventWriter = sinon.mock(eventWriter);
64 mockEventWriter.expects('writeToStorage').once();
65 return service.init().then(function(){
66 var mockSuspendEvent =
67 /** @type {chromeMocks.Event} */ (chrome.runtime.onSuspend);
68 mockSuspendEvent.mock$fire();
70 mockEventWriter.verify();
74 QUnit.test('should flush log requests when online.', function(assert) {
75 var mockEventWriter = sinon.mock(eventWriter);
76 mockEventWriter.expects('flush').once();
77 return service.init().then(function() {
78 mockEventWriter.verify();
79 mockEventWriter.expects('flush').once();
80 window.dispatchEvent(new CustomEvent('online'));
82 mockEventWriter.verify();
86 QUnit.test('should send CANCELED event when window is closed while connecting.',
88 var writeStub = sinon.stub(eventWriter, 'write');
89 return service.init().then(function() {
90 chrome.app.window.current().id = 'fake-window-id';
92 logger.logClientSessionStateChange(
93 remoting.ClientSession.State.CONNECTING, remoting.Error.none(), null);
95 return service.unbindSession('fake-window-id');
97 var Event = remoting.ChromotingEvent;
98 verifyEvent(writeStub, assert, 1, {
99 type: Event.Type.SESSION_STATE,
100 session_state: Event.SessionState.CONNECTION_CANCELED,
101 connection_error: Event.ConnectionError.NONE,
102 application_id: 'extensionId',
103 role: Event.Role.CLIENT,
104 mode: Event.Mode.ME2ME,
105 connection_type: Event.ConnectionType.STUN,
106 host_version: 'host_version'
111 QUnit.test('should send CLOSED event when window is closed while connected.',
113 var writeStub = sinon.stub(eventWriter, 'write');
115 return service.init().then(function() {
116 chrome.app.window.current().id = 'fake-window-id';
118 logger.logClientSessionStateChange(
119 remoting.ClientSession.State.CONNECTING, remoting.Error.none(), null);
121 logger.logClientSessionStateChange(
122 remoting.ClientSession.State.CONNECTED, remoting.Error.none(), null);
124 return service.unbindSession('fake-window-id');
126 var Event = remoting.ChromotingEvent;
127 verifyEvent(writeStub, assert, 2, {
128 type: Event.Type.SESSION_STATE,
129 session_state: Event.SessionState.CLOSED,
130 connection_error: Event.ConnectionError.NONE,
131 application_id: 'extensionId',
132 role: Event.Role.CLIENT,
133 mode: Event.Mode.ME2ME,
134 connection_type: Event.ConnectionType.STUN,
135 host_version: 'host_version'
140 QUnit.test('should not send CLOSED event when window is closed unconnected.',
143 var mockEventWriter = sinon.mock(eventWriter);
144 mockEventWriter.expects('write').exactly(2);
146 return service.init().then(function() {
147 chrome.app.window.current().id = 'fake-window-id';
149 logger.logClientSessionStateChange(
150 remoting.ClientSession.State.CONNECTING, remoting.Error.none(), null);
152 logger.logClientSessionStateChange(
153 remoting.ClientSession.State.FAILED, remoting.Error.none(), null);
155 return service.unbindSession('fake-window-id');
157 mockEventWriter.verify();