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 || {};
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().
21 * @extends {remoting.HostList}
23 var MockHostList = function() {};
26 MockHostList.prototype.refresh = function(callback) {
27 Promise.resolve().then(function() {
33 MockHostList.prototype.getHostForId = function(hostId) {
34 var host = new remoting.Host(hostId);
35 host.jabberId = 'fake_jabber_id';
36 host.loggingChannel = 'APIARY';
42 * @extends {remoting.DesktopConnectedView}
44 var MockDesktopConnectedView = function() {};
46 MockDesktopConnectedView.prototype.setVideoFrameRecorder = function() {};
48 MockDesktopConnectedView.prototype.dispose = function() {};
51 * A test driver that mocks out the UI components that are required by the
52 * DesktopRemotingActivity.
54 * @param {string} mockHTML
57 * @implements {base.Disposable}
59 var BaseTestDriver = function(mockHTML) {
61 this.deferred_ = new base.Deferred();
63 this.mockConnection_ = new remoting.MockConnection();
65 this.originalDialogFactory_ = remoting.modalDialogFactory;
67 this.mockDialogFactory_ = new remoting.MockModalDialogFactory();
69 this.desktopConnectedViewCreateStub_ =
70 sinon.stub(remoting.DesktopConnectedView, 'create');
72 this.eventWriterMock_ = sinon.mock(remoting.TelemetryEventWriter.Client);
74 this.setModeStub_ = sinon.stub(remoting, 'setMode');
76 * Use fake timers to prevent the generation of session ID expiration events.
79 this.clock_ = sinon.useFakeTimers();
85 * @param {string} mockHTML
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;
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) {
121 events.forEach(function(/** Object */ event){
122 that.eventWriterMock_.expects('write').withArgs(sinon.match(event));
127 * @return {Promise} A promise that will be resolved when endTest() is called.
129 BaseTestDriver.prototype.startTest = function() {
130 return this.deferred_.promise();
134 * Resolves the promise that is returned by startTest().
136 BaseTestDriver.prototype.endTest = function() {
138 this.eventWriterMock_.verify();
139 this.deferred_.resolve();
140 } catch (/** @type {*} */ reason) {
141 this.deferred_.reject(reason);
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
151 * @extends {BaseTestDriver}
153 remoting.Me2MeTestDriver = function() {
154 base.inherits(this, BaseTestDriver, remoting.Me2MeTestDriver.FIXTURE);
156 this.mockHostList_ = new MockHostList();
157 /** @private {?remoting.Me2MeActivity} */
158 this.me2meActivity_ = null;
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');
175 remoting.Me2MeTestDriver.prototype.cancelWhenPinPrompted = function() {
176 this.mockDialogFactory().inputDialog.show = function() {
177 return Promise.reject(new remoting.Error(remoting.Error.Tag.CANCELLED));
181 remoting.Me2MeTestDriver.prototype.clickOkWhenFinished = function() {
182 this.mockDialogFactory().messageDialog.show = function() {
183 return Promise.resolve(remoting.MessageDialog.Result.PRIMARY);
187 remoting.Me2MeTestDriver.prototype.clickReconnectWhenFinished = function() {
188 this.mockDialogFactory().messageDialog.show = function() {
189 return Promise.resolve(remoting.MessageDialog.Result.SECONDARY);
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';
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">' +
222 '<div id="pin-dialog">' +
224 '<input type="password" class="pin-inputField" />' +
225 '<button class="cancel-button"></button>' +
227 '<div class="pairing-section">' +
228 '<input type="checkbox" class="pairing-checkbox" />' +
229 '<div class="pin-message"></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>' +