Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / crd / js / remoting_activity_test_driver.js
blob2197db433225a7b3b2613812468fff90f3bae761
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 /**
14  * @constructor
15  *
16  * The |extends| annotation is used to make JSCompile happy.  The mock object
17  * should never extends from the actual HostList as all its implementation
18  * should be mocked out.  The caller of this class is responsible for ensuring
19  * the methods that they need are implemented either here or via sinon.stub().
20  *
21  * @extends {remoting.HostList}
22  */
23 var MockHostList = function() {};
25 /** @override */
26 MockHostList.prototype.refresh = function(callback) {
27   Promise.resolve().then(function() {
28     callback(true);
29   });
32 /** @override */
33 MockHostList.prototype.getHostForId = function(hostId) {
34   var host = new remoting.Host(hostId);
35   host.jabberId = 'fake_jabber_id';
36   host.loggingChannel = 'APIARY';
37   return host;
40 /**
41  * @constructor
42  * @extends {remoting.DesktopConnectedView}
43  */
44 var MockDesktopConnectedView = function() {};
45 /** @override */
46 MockDesktopConnectedView.prototype.setVideoFrameRecorder = function() {};
47 /** @override */
48 MockDesktopConnectedView.prototype.dispose = function() {};
50 /**
51  * A test driver that mocks out the UI components that are required by the
52  * DesktopRemotingActivity.
53  *
54  * @param {string} mockHTML
55  *
56  * @constructor
57  * @implements {base.Disposable}
58  */
59 var BaseTestDriver = function(mockHTML) {
60   /** @private */
61   this.deferred_ = new base.Deferred();
62   /** @protected */
63   this.mockConnection_ = new remoting.MockConnection();
64   /** @private */
65   this.originalDialogFactory_ = remoting.modalDialogFactory;
66   /** @protected */
67   this.mockDialogFactory_ = new remoting.MockModalDialogFactory();
68   /** @private */
69   this.desktopConnectedViewCreateStub_ =
70       sinon.stub(remoting.DesktopConnectedView, 'create');
71   /** @private */
72   this.eventWriterMock_ = sinon.mock(remoting.TelemetryEventWriter.Client);
73   /** @private */
74   this.setModeStub_ = sinon.stub(remoting, 'setMode');
75   /**
76    * Use fake timers to prevent the generation of session ID expiration events.
77    * @private
78    */
79   this.clock_ = sinon.useFakeTimers();
81   this.init_(mockHTML);
84 /**
85  * @param {string} mockHTML
86  */
87 BaseTestDriver.prototype.init_ = function(mockHTML) {
88   document.getElementById('qunit-fixture').innerHTML = mockHTML;
89   // Return a token to pretend that we are signed-in.
90   chromeMocks.identity.mock$setToken('fake_token');
91   this.desktopConnectedViewCreateStub_.returns(new MockDesktopConnectedView());
92   remoting.modalDialogFactory = this.mockDialogFactory_;
95 BaseTestDriver.prototype.dispose = function() {
96   this.clock_.restore();
97   remoting.modalDialogFactory = this.originalDialogFactory_;
98   this.setModeStub_.restore();
99   this.eventWriterMock_.restore();
100   this.desktopConnectedViewCreateStub_.restore();
102   if (Boolean(this.mockConnection_)) {
103     this.mockConnection_.restore();
104     this.mockConnection_ = null;
105   }
108 /** @return {remoting.MockConnection} */
109 BaseTestDriver.prototype.mockConnection = function() {
110   return this.mockConnection_;
113 /** @return {remoting.MockModalDialogFactory} */
114 BaseTestDriver.prototype.mockDialogFactory = function() {
115   return this.mockDialogFactory_;
118 /** @param {Array<Object>} events */
119 BaseTestDriver.prototype.expectEvents = function(events) {
120   var that = this;
121   events.forEach(function(/** Object */ event){
122     that.eventWriterMock_.expects('write').withArgs(sinon.match(event));
123   });
127  * @return {Promise} A promise that will be resolved when endTest() is called.
128  */
129 BaseTestDriver.prototype.startTest = function() {
130   return this.deferred_.promise();
134  * Resolves the promise that is returned by startTest().
135  */
136 BaseTestDriver.prototype.endTest = function() {
137   try {
138     this.eventWriterMock_.verify();
139     this.deferred_.resolve();
140   } catch (/** @type {*} */ reason) {
141     this.deferred_.reject(reason);
142   }
146  * The Me2MeTest Driver mocks out the UI components that are required by the
147  * Me2MeActivity.  It provides test hooks for the caller to fake behavior of
148  * those components.
150  * @constructor
151  * @extends {BaseTestDriver}
152  */
153 remoting.Me2MeTestDriver = function() {
154   base.inherits(this, BaseTestDriver, remoting.Me2MeTestDriver.FIXTURE);
155   /** @private */
156   this.mockHostList_ = new MockHostList();
157   /** @private {?remoting.Me2MeActivity} */
158   this.me2meActivity_ = null;
161 /** @override */
162 remoting.Me2MeTestDriver.prototype.dispose = function() {
163   base.dispose(this.me2meActivity_);
164   this.me2meActivity_ = null;
166   BaseTestDriver.prototype.dispose.call(this);
169 remoting.Me2MeTestDriver.prototype.enterPinWhenPrompted = function() {
170   this.mockDialogFactory().inputDialog.show = function() {
171     return Promise.resolve('fake_pin');
172   };
175 remoting.Me2MeTestDriver.prototype.cancelWhenPinPrompted = function() {
176   this.mockDialogFactory().inputDialog.show = function() {
177     return Promise.reject(new remoting.Error(remoting.Error.Tag.CANCELLED));
178   };
181 remoting.Me2MeTestDriver.prototype.clickOkWhenFinished = function() {
182   this.mockDialogFactory().messageDialog.show = function() {
183     return Promise.resolve(remoting.MessageDialog.Result.PRIMARY);
184   };
187 remoting.Me2MeTestDriver.prototype.clickReconnectWhenFinished = function() {
188   this.mockDialogFactory().messageDialog.show = function() {
189     return Promise.resolve(remoting.MessageDialog.Result.SECONDARY);
190   };
193 /** @return {MockHostList} */
194 remoting.Me2MeTestDriver.prototype.mockHostList = function() {
195   return this.mockHostList_;
198 /** @return {remoting.Me2MeActivity} */
199 remoting.Me2MeTestDriver.prototype.me2meActivity = function() {
200   return this.me2meActivity_;
203 /** @return {Promise} */
204 remoting.Me2MeTestDriver.prototype.startTest = function() {
205   var host = new remoting.Host('fake_host_id');
206   host.loggingChannel = 'APIARY';
208   // Default behavior.
209   this.enterPinWhenPrompted();
210   this.clickOkWhenFinished();
212   this.me2meActivity_ = new remoting.Me2MeActivity(host, this.mockHostList_);
213   this.me2meActivity_.start();
214   return BaseTestDriver.prototype.startTest.call(this);
217 remoting.Me2MeTestDriver.FIXTURE =
218   '<div id="connect-error-message"></div>' +
219   '<div id="client-container">' +
220     '<div class="client-plugin-container">' +
221   '</div>' +
222   '<div id="pin-dialog">' +
223     '<form>' +
224       '<input type="password" class="pin-inputField" />' +
225       '<button class="cancel-button"></button>' +
226     '</form>' +
227     '<div class="pairing-section">' +
228       '<input type="checkbox" class="pairing-checkbox" />' +
229       '<div class="pin-message"></div>' +
230     '</div>' +
231   '</div>' +
232   '<div id="host-needs-update-dialog">' +
233     '<input class="connect-button" />' +
234     '<input class="cancel-button" />' +
235     '<div class="host-needs-update-message"></div>' +
236   '</div>';
238 })();