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.HostTableEntry} */
10 var hostTableEntry_
= null;
11 var onConnect_
= null;
15 QUnit
.module('HostTableEntry', {
16 beforeEach: function() {
17 onConnect_
= /** @type {function(string)} */ (sinon
.spy());
18 onRename_
= /** @type {function(remoting.HostTableEntry)} */ (sinon
.spy());
19 onDelete_
= /** @type {function(remoting.HostTableEntry)} */ (sinon
.spy());
21 new remoting
.HostTableEntry(10,
22 onConnect_
, onRename_
, onDelete_
);
24 // Setup the DOM dependencies on the confirm delete dialog.
25 var fixture
= document
.getElementById('qunit-fixture');
26 fixture
.innerHTML
= '<div id="confirm-host-delete-message"></div>' +
27 '<div id="confirm-host-delete"></div>' +
28 '<div id="cancel-host-delete"></div>';
29 setHost('LocalHost', 'ONLINE');
30 fixture
.appendChild(hostTableEntry_
.element());
31 sinon
.stub(chrome
.i18n
, 'getMessage', function(/** string */ tag
){
35 afterEach: function() {
36 hostTableEntry_
.dispose();
37 hostTableEntry_
= null;
38 $testStub(chrome
.i18n
.getMessage
).restore();
43 * @param {string} hostName
44 * @param {string} status
45 * @param {string=} opt_offlineReason
47 function setHost(hostName
, status
, opt_offlineReason
) {
48 var host
= new remoting
.Host('id');
49 host
.hostName
= hostName
;
51 if (opt_offlineReason
) {
52 host
.hostOfflineReason
= opt_offlineReason
;
54 hostTableEntry_
.setHost(host
);
58 /** @suppress {checkTypes|reportUnknownTypes} */
59 function sendKeydown(/** HTMLElement */ target
, /** number */ keyCode
) {
60 var event
= document
.createEvent('KeyboardEvent');
61 Object
.defineProperty(
62 event
, 'which', {get: function(assert
) { return keyCode
; }});
63 event
.initKeyboardEvent("keydown", true, true, document
.defaultView
,
64 false, false, false, false, keyCode
, keyCode
);
65 target
.dispatchEvent(event
);
68 function verifyVisible(
69 /** QUnit.Assert */ assert
,
70 /** HTMLElement*/ element
,
71 /** boolean */ isVisible
,
72 /** string= */ opt_name
) {
73 var expectedVisibility
= (isVisible
) ? 'visible' : 'hidden';
74 assert
.equal(element
.hidden
, !isVisible
,
75 'Element ' + opt_name
+ ' should be ' + expectedVisibility
);
79 'Clicking on the confirm button in the confirm dialog deletes the host',
82 sinon
.stub(remoting
, 'setMode', function(/** remoting.AppMode */ mode
) {
83 if (mode
=== remoting
.AppMode
.CONFIRM_HOST_DELETE
) {
84 document
.getElementById('confirm-host-delete').click();
89 hostTableEntry_
.element().querySelector('.delete-button').click();
92 sinon
.assert
.calledWith(onDelete_
, hostTableEntry_
);
95 $testStub(remoting
.setMode
).restore();
99 'Clicking on the cancel button in the confirm dialog cancels host deletion',
102 sinon
.stub(remoting
, 'setMode', function(/** remoting.AppMode */ mode
) {
103 if (mode
=== remoting
.AppMode
.CONFIRM_HOST_DELETE
) {
104 document
.getElementById('cancel-host-delete').click();
109 hostTableEntry_
.element().querySelector('.delete-button').click();
112 sinon
.assert
.notCalled(onDelete_
);
115 $testStub(remoting
.setMode
).restore();
118 QUnit
.test('Clicking on the rename button shows the input field.',
121 hostTableEntry_
.element().querySelector('.rename-button').click();
125 hostTableEntry_
.element().querySelector('.host-rename-input');
127 verifyVisible(assert
, inputField
, true, 'inputField');
128 assert
.equal(document
.activeElement
, inputField
);
131 QUnit
.test('Host renaming is canceled on ESCAPE key.', function(assert
) {
134 hostTableEntry_
.element().querySelector('.host-rename-input');
135 hostTableEntry_
.element().querySelector('.rename-button').click();
138 verifyVisible(assert
, inputField
, true, 'inputField');
139 assert
.equal(document
.activeElement
, inputField
);
140 sendKeydown(inputField
, 27 /* ESCAPE */);
141 verifyVisible(assert
, inputField
, false, 'inputField');
144 QUnit
.test('Host renaming commits on ENTER.', function(assert
) {
147 hostTableEntry_
.element().querySelector('.host-rename-input');
148 hostTableEntry_
.element().querySelector('.rename-button').click();
149 inputField
.value
= 'Renamed Host';
150 sendKeydown(inputField
, 13 /* ENTER */);
153 verifyVisible(assert
, inputField
, false, 'inputField');
154 sinon
.assert
.called(onRename_
);
155 assert
.equal(hostTableEntry_
.host
.hostName
, 'Renamed Host');
157 // Renaming shouldn't trigger a connection request.
158 sinon
.assert
.notCalled(onConnect_
);
161 QUnit
.test('HostTableEntry renders the host name correctly.', function(assert
) {
162 var label
= hostTableEntry_
.element().querySelector('.host-name-label');
163 assert
.equal(label
.innerText
, 'LocalHost');
166 QUnit
.test('HostTableEntry renders an offline host correctly.',
168 setHost('LocalHost', 'OFFLINE', 'INITIALIZATION_FAILED');
169 var label
= hostTableEntry_
.element().querySelector('.host-name-label');
170 assert
.equal(label
.innerText
, 'OFFLINE');
171 assert
.equal(label
.title
, 'OFFLINE_REASON_INITIALIZATION_FAILED');
174 QUnit
.test('HostTableEntry renders an out-of-date host correctly',
176 sinon
.stub(remoting
.Host
, 'needsUpdate').returns(true);
177 setHost('LocalHost', 'ONLINE');
179 hostTableEntry_
.element().querySelector('.warning-overlay');
180 var label
= hostTableEntry_
.element().querySelector('.host-name-label');
181 verifyVisible(assert
, warningOverlay
, true, 'warning overlay');
182 assert
.equal(label
.innerText
, 'UPDATE_REQUIRED');
185 QUnit
.test('Clicking on an online host connects it', function(assert
) {
186 hostTableEntry_
.element().querySelector('.host-name-label').click();
187 sinon
.assert
.calledWith(onConnect_
,
188 encodeURIComponent(hostTableEntry_
.host
.hostId
));
191 QUnit
.test('Clicking on an offline host should be a no-op', function(assert
) {
192 setHost('LocalHost', 'OFFLINE');
193 hostTableEntry_
.element().querySelector('.host-name-label').click();
194 sinon
.assert
.notCalled(onConnect_
);
197 QUnit
.test('HostTableEntry handles host that is null', function(assert
) {
198 hostTableEntry_
.setHost(null);
199 hostTableEntry_
.element().querySelector('.host-name-label').click();
200 sinon
.assert
.notCalled(onConnect_
);