Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / crd / js / me2me_telemetry_integration_test.js
blob1ec6a86d2263e9cbee13c020cff0332148745668
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.
6 /** @suppress {duplicate} */
7 var remoting = remoting || {};
9 (function() {
11 'use strict';
13 /** @type {remoting.Me2MeTestDriver} */
14 var testDriver;
16 QUnit.module('Me2Me Telemetry Integration', {
17   // We rely on our telemetry data to gain insight into the reliability and
18   // the durability on our connections.  This test verifies the integrity of our
19   // telemetry by ensuring that certain well known connection sequences will
20   // generate the correct sequence of telemetry data.
21   beforeEach: function() {
22     testDriver = new remoting.Me2MeTestDriver();
23   },
24   afterEach: function() {
25     base.dispose(testDriver);
26     testDriver = null;
27   }
28 });
30 /**
31  * @param {remoting.Me2MeTestDriver} testDriver
32  * @param {Object} baseEvent
33  */
34 var expectSucceeded = function(testDriver, baseEvent) {
35   var ChromotingEvent = remoting.ChromotingEvent;
36   var sequence = [{
37     session_state: ChromotingEvent.SessionState.STARTED,
38   },{
39     session_state: ChromotingEvent.SessionState.SIGNALING,
40     previous_session_state: ChromotingEvent.SessionState.STARTED
41   },{
42     session_state: ChromotingEvent.SessionState.CREATING_PLUGIN,
43     previous_session_state: ChromotingEvent.SessionState.SIGNALING
44   },{
45     session_state: ChromotingEvent.SessionState.CONNECTING,
46     previous_session_state: ChromotingEvent.SessionState.CREATING_PLUGIN
47   },{
48     session_state: ChromotingEvent.SessionState.AUTHENTICATED,
49     previous_session_state: ChromotingEvent.SessionState.CONNECTING
50   },{
51     session_state: ChromotingEvent.SessionState.CONNECTED,
52     previous_session_state: ChromotingEvent.SessionState.AUTHENTICATED
53   },{
54     session_state: ChromotingEvent.SessionState.CLOSED,
55     previous_session_state: ChromotingEvent.SessionState.CONNECTED
56   }];
58   var expectedEvents = sequence.map(function(/** Object */ sequenceValue) {
59     var event = /** @type {Object} */ (base.deepCopy(baseEvent));
60     base.mix(event, sequenceValue);
61     return event;
62   });
63   testDriver.expectEvents(expectedEvents);
66 /**
67  * @param {remoting.Me2MeTestDriver} testDriver
68  * @param {Object} baseEvent
69  */
70 var expectCanceled = function(testDriver, baseEvent) {
71   var ChromotingEvent = remoting.ChromotingEvent;
72   var sequence = [{
73     session_state: ChromotingEvent.SessionState.STARTED,
74   },{
75     session_state: ChromotingEvent.SessionState.SIGNALING,
76     previous_session_state: ChromotingEvent.SessionState.STARTED
77   },{
78     session_state: ChromotingEvent.SessionState.CREATING_PLUGIN,
79     previous_session_state: ChromotingEvent.SessionState.SIGNALING
80   },{
81     session_state: ChromotingEvent.SessionState.CONNECTING,
82     previous_session_state: ChromotingEvent.SessionState.CREATING_PLUGIN
83   },{
84     session_state: ChromotingEvent.SessionState.CONNECTION_CANCELED,
85     previous_session_state: ChromotingEvent.SessionState.CONNECTING
86   }];
88   var expectedEvents = sequence.map(function(/** Object */ sequenceValue) {
89     var event = /** @type {Object} */ (base.deepCopy(baseEvent));
90     base.mix(event, sequenceValue);
91     return event;
92   });
93   testDriver.expectEvents(expectedEvents);
96 /**
97  * @param {remoting.Me2MeTestDriver} testDriver
98  * @param {Object} baseEvent
99  * @param {remoting.ChromotingEvent.ConnectionError} error
100  */
101 var expectFailed = function(testDriver, baseEvent, error) {
102   var ChromotingEvent = remoting.ChromotingEvent;
103   var sequence = [{
104     session_state: ChromotingEvent.SessionState.STARTED,
105   },{
106     session_state: ChromotingEvent.SessionState.SIGNALING,
107     previous_session_state: ChromotingEvent.SessionState.STARTED
108   },{
109     session_state: ChromotingEvent.SessionState.CREATING_PLUGIN,
110     previous_session_state: ChromotingEvent.SessionState.SIGNALING
111   },{
112     session_state: ChromotingEvent.SessionState.CONNECTING,
113     previous_session_state: ChromotingEvent.SessionState.CREATING_PLUGIN
114   },{
115     session_state: ChromotingEvent.SessionState.CONNECTION_FAILED,
116     previous_session_state: ChromotingEvent.SessionState.CONNECTING,
117     connection_error: error
118   }];
120   var expectedEvents = sequence.map(function(/** Object */ sequenceValue) {
121     var event = /** @type {Object} */ (base.deepCopy(baseEvent));
122     base.mix(event, sequenceValue);
123     return event;
124   });
125   testDriver.expectEvents(expectedEvents);
128 QUnit.test('Connection succeeded', function() {
129   expectSucceeded(testDriver, {
130     session_entry_point:
131         remoting.ChromotingEvent.SessionEntryPoint.CONNECT_BUTTON,
132     role: remoting.ChromotingEvent.Role.CLIENT,
133     mode: remoting.ChromotingEvent.Mode.ME2ME,
134   });
136   /**
137    * @param {remoting.MockClientPlugin} plugin
138    * @param {remoting.ClientSession.State} state
139    */
140   function onStatusChanged(plugin, state) {
141     if (state == remoting.ClientSession.State.CONNECTED) {
142       testDriver.me2meActivity().stop();
143       testDriver.endTest();
144     }
145   }
147   testDriver.mockConnection().pluginFactory().mock$setPluginStatusChanged(
148       onStatusChanged);
150   return testDriver.startTest();
153 QUnit.test('Connection canceled - Pin prompt', function() {
154    expectCanceled(testDriver, {
155      session_entry_point:
156          remoting.ChromotingEvent.SessionEntryPoint.CONNECT_BUTTON,
157      role: remoting.ChromotingEvent.Role.CLIENT,
158      mode: remoting.ChromotingEvent.Mode.ME2ME,
159    });
161   /**
162    * @param {remoting.MockClientPlugin} plugin
163    * @param {remoting.ClientSession.State} state
164    */
165   function onStatusChanged(plugin, state) {
166     if (state == remoting.ClientSession.State.CONNECTING) {
167       testDriver.cancelWhenPinPrompted();
168       plugin.mock$onDisposed().then(function(){
169         testDriver.endTest();
170       });
171     }
172   }
174   testDriver.mockConnection().pluginFactory().mock$setPluginStatusChanged(
175       onStatusChanged);
177   return testDriver.startTest();
180 QUnit.test('Connection failed - Signal strategy', function() {
181   var EntryPoint = remoting.ChromotingEvent.SessionEntryPoint;
182   testDriver.expectEvents([{
183     session_entry_point: EntryPoint.CONNECT_BUTTON,
184     session_state: remoting.ChromotingEvent.SessionState.STARTED
185   },{
186     session_entry_point: EntryPoint.CONNECT_BUTTON,
187     session_state: remoting.ChromotingEvent.SessionState.SIGNALING,
188     previous_session_state: remoting.ChromotingEvent.SessionState.STARTED
189   },{
190     session_entry_point: EntryPoint.CONNECT_BUTTON,
191     previous_session_state: remoting.ChromotingEvent.SessionState.SIGNALING,
192     session_state: remoting.ChromotingEvent.SessionState.CONNECTION_FAILED,
193     connection_error: remoting.ChromotingEvent.ConnectionError.UNEXPECTED
194   }]);
196   var promise = testDriver.startTest();
198   // The message dialog is shown when the connection fails.
199   testDriver.mockDialogFactory().messageDialog.show = function() {
200     testDriver.endTest();
201     return Promise.resolve(remoting.MessageDialog.Result.PRIMARY);
202   };
204   var signalStrategy = testDriver.mockConnection().signalStrategy();
205   signalStrategy.connect = function() {
206     Promise.resolve().then(function(){
207       signalStrategy.setStateForTesting(remoting.SignalStrategy.State.FAILED);
208     });
209   };
211   return promise;
214 QUnit.test('Reconnect', function() {
215   var EntryPoint = remoting.ChromotingEvent.SessionEntryPoint;
216   expectSucceeded(testDriver, {
217     session_entry_point: EntryPoint.CONNECT_BUTTON,
218     role: remoting.ChromotingEvent.Role.CLIENT,
219     mode: remoting.ChromotingEvent.Mode.ME2ME,
220   });
221   expectSucceeded(testDriver, {
222     session_entry_point: EntryPoint.RECONNECT_BUTTON,
223     role: remoting.ChromotingEvent.Role.CLIENT,
224     mode: remoting.ChromotingEvent.Mode.ME2ME,
225   });
227   var count = 0;
228   /**
229    * @param {remoting.MockClientPlugin} plugin
230    * @param {remoting.ClientSession.State} state
231    */
232   function onStatusChanged(plugin, state) {
233     if (state == remoting.ClientSession.State.CONNECTED) {
234       count++;
235       if (count == 1) {
236         testDriver.clickReconnectWhenFinished();
237         testDriver.me2meActivity().stop();
238       } else if (count == 2) {
239         testDriver.clickOkWhenFinished();
240         testDriver.me2meActivity().stop();
241         testDriver.endTest();
242       }
243     }
244   }
246   testDriver.mockConnection().pluginFactory().mock$setPluginStatusChanged(
247       onStatusChanged);
249   return testDriver.startTest();
252 QUnit.test('HOST_OFFLINE - JID refresh failed', function() {
253   var EntryPoint = remoting.ChromotingEvent.SessionEntryPoint;
254   // Expects the first connection to fail with HOST_OFFLINE
255   expectFailed(testDriver, {
256     session_entry_point:EntryPoint.CONNECT_BUTTON,
257     role: remoting.ChromotingEvent.Role.CLIENT,
258     mode: remoting.ChromotingEvent.Mode.ME2ME,
259   }, remoting.ChromotingEvent.ConnectionError.HOST_OFFLINE);
261   function onPluginCreated(/** remoting.MockClientPlugin */ plugin) {
262     plugin.mock$returnErrorOnConnect(
263         remoting.ClientSession.ConnectionError.HOST_IS_OFFLINE);
264   }
266   /**
267    * @param {remoting.MockClientPlugin} plugin
268    * @param {remoting.ClientSession.State} state
269    */
270   function onStatusChanged(plugin, state) {
271     if (state == remoting.ClientSession.State.FAILED) {
272       testDriver.endTest();
273     }
274   }
276   testDriver.mockConnection().pluginFactory().mock$setPluginCreated(
277       onPluginCreated);
278   testDriver.mockConnection().pluginFactory().mock$setPluginStatusChanged(
279       onStatusChanged);
280   sinon.stub(testDriver.mockHostList(), 'refresh',
281     function(/** function(boolean)*/ callback) {
282       // Fail the refresh.
283       Promise.resolve().then(function(){
284         callback(false);
285       });
286   });
288   return testDriver.startTest();
291 QUnit.test('HOST_OFFLINE - JID refresh succeeded', function() {
292   var EntryPoint = remoting.ChromotingEvent.SessionEntryPoint;
293   // Expects the first connection to fail with HOST_OFFLINE
294   expectFailed(testDriver, {
295     session_entry_point:EntryPoint.CONNECT_BUTTON,
296     role: remoting.ChromotingEvent.Role.CLIENT,
297     mode: remoting.ChromotingEvent.Mode.ME2ME,
298   }, remoting.ChromotingEvent.ConnectionError.HOST_OFFLINE);
299   // Expects the second connection to succeed with RECONNECT
300   expectSucceeded(testDriver, {
301     session_entry_point: EntryPoint.AUTO_RECONNECT_ON_HOST_OFFLINE,
302     role: remoting.ChromotingEvent.Role.CLIENT,
303     mode: remoting.ChromotingEvent.Mode.ME2ME,
304   });
306   var count = 0;
307   function onPluginCreated(/** remoting.MockClientPlugin */ plugin) {
308     count++;
309     if (count == 1) {
310       plugin.mock$returnErrorOnConnect(
311           remoting.ClientSession.ConnectionError.HOST_IS_OFFLINE);
312     } else if (count == 2) {
313       plugin.mock$useDefaultBehavior(
314           remoting.MockClientPlugin.AuthMethod.PIN);
315     }
316   }
318   /**
319    * @param {remoting.MockClientPlugin} plugin
320    * @param {remoting.ClientSession.State} state
321    */
322   function onStatusChanged(plugin, state) {
323     if (state == remoting.ClientSession.State.CONNECTED) {
324       testDriver.me2meActivity().stop();
325       testDriver.endTest();
326     }
327   }
329   testDriver.mockConnection().pluginFactory().mock$setPluginCreated(
330       onPluginCreated);
331   testDriver.mockConnection().pluginFactory().mock$setPluginStatusChanged(
332       onStatusChanged);
334   return testDriver.startTest();
337 QUnit.test('HOST_OFFLINE - Reconnect failed', function() {
338   var EntryPoint = remoting.ChromotingEvent.SessionEntryPoint;
339   // Expects the first connection to fail with HOST_OFFLINE
340   expectFailed(testDriver, {
341     session_entry_point:EntryPoint.CONNECT_BUTTON,
342     role: remoting.ChromotingEvent.Role.CLIENT,
343     mode: remoting.ChromotingEvent.Mode.ME2ME,
344   }, remoting.ChromotingEvent.ConnectionError.HOST_OFFLINE);
345   // Expects the second connection to fail with HOST_OVERLOAD
346   expectFailed(testDriver, {
347     session_entry_point:EntryPoint.AUTO_RECONNECT_ON_HOST_OFFLINE,
348     role: remoting.ChromotingEvent.Role.CLIENT,
349     mode: remoting.ChromotingEvent.Mode.ME2ME,
350   }, remoting.ChromotingEvent.ConnectionError.HOST_OVERLOAD);
352   var count = 0;
353   function onPluginCreated(/** remoting.MockClientPlugin */ plugin) {
354     count++;
355     if (count == 1) {
356       plugin.mock$returnErrorOnConnect(
357           remoting.ClientSession.ConnectionError.HOST_IS_OFFLINE);
358     } else if (count == 2) {
359       plugin.mock$returnErrorOnConnect(
360           remoting.ClientSession.ConnectionError.HOST_OVERLOAD);
361     }
362   }
364   var failureCount = 0;
365   /**
366    * @param {remoting.MockClientPlugin} plugin
367    * @param {remoting.ClientSession.State} state
368    */
369   function onStatusChanged(plugin, state) {
370     if (state == remoting.ClientSession.State.FAILED) {
371       failureCount++;
373       if (failureCount == 2) {
374         testDriver.endTest();
375       }
376     }
377   }
378   testDriver.mockConnection().pluginFactory().mock$setPluginCreated(
379       onPluginCreated);
380   testDriver.mockConnection().pluginFactory().mock$setPluginStatusChanged(
381       onStatusChanged);
382   return testDriver.startTest();
385 })();