Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / base / js / telemetry_event_writer_unittest.js
blob37a82cad72496242895a13783b7fbfeebf98f47d
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.TelemetryEventWriter.Service} */
10 var service;
11 /** @type {remoting.XhrEventWriter} */
12 var eventWriter = null;
14 /** @type {remoting.SessionLogger} */
15 var logger = null;
17 /**
18  *  @param {sinon.TestStub} stub
19  *  @param {QUnit.Assert} assert
20  *  @param {number} index
21  *  @param {Object} expected
22  */
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);
27   }
30 QUnit.module('TelemetryEventWriter', {
31   beforeEach: function() {
32     remoting.MockXhr.activate();
33     var ipc = base.Ipc.getInstance();
34     eventWriter =
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');
43   },
44   afterEach: function() {
45     base.dispose(service);
46     service = null;
47     base.Ipc.deleteInstance();
48     remoting.MockXhr.restore();
49   }
50 });
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'});
57   }).then(function(){
58     mockEventWriter.verify();
59   });
60 });
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();
69   }).then(function() {
70     mockEventWriter.verify();
71   });
72 });
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'));
81   }).then(function() {
82     mockEventWriter.verify();
83   });
84 });
86 QUnit.test('should send CANCELED event when window is closed while connecting.',
87   function(assert) {
88   var writeStub = sinon.stub(eventWriter, 'write');
89   return service.init().then(function() {
90     chrome.app.window.current().id = 'fake-window-id';
91   }).then(function() {
92     logger.logClientSessionStateChange(
93         remoting.ClientSession.State.CONNECTING, remoting.Error.none(), null);
94   }).then(function() {
95     return service.unbindSession('fake-window-id');
96   }).then(function() {
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'
107     });
108   });
111 QUnit.test('should send CLOSED event when window is closed while connected.',
112   function(assert) {
113   var writeStub = sinon.stub(eventWriter, 'write');
115   return service.init().then(function() {
116     chrome.app.window.current().id = 'fake-window-id';
117   }).then(function() {
118     logger.logClientSessionStateChange(
119         remoting.ClientSession.State.CONNECTING, remoting.Error.none(), null);
120   }).then(function() {
121     logger.logClientSessionStateChange(
122         remoting.ClientSession.State.CONNECTED, remoting.Error.none(), null);
123   }).then(function() {
124     return service.unbindSession('fake-window-id');
125   }).then(function() {
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'
136     });
137   });
140 QUnit.test('should not send CLOSED event when window is closed unconnected.',
141   function(assert) {
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';
148   }).then(function() {
149     logger.logClientSessionStateChange(
150         remoting.ClientSession.State.CONNECTING, remoting.Error.none(), null);
151   }).then(function() {
152     logger.logClientSessionStateChange(
153         remoting.ClientSession.State.FAILED, remoting.Error.none(), null);
154   }).then(function() {
155     return service.unbindSession('fake-window-id');
156   }).then(function() {
157     mockEventWriter.verify();
158   });
161 })();