OLD | NEW |
| (Empty) |
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. | |
4 | |
5 (function() { | |
6 | |
7 'use strict'; | |
8 | |
9 /** @type {remoting.HostTableEntry} */ | |
10 var hostTableEntry_ = null; | |
11 var onConnect_ = null; | |
12 var onRename_ = null; | |
13 var onDelete_ = null; | |
14 | |
15 module('HostTableEntry', { | |
16 setup: function() { | |
17 onConnect_ = /** @type {function(string)} */ (sinon.spy()); | |
18 onRename_ = /** @type {function(remoting.HostTableEntry)} */ (sinon.spy()); | |
19 onDelete_ = /** @type {function(remoting.HostTableEntry)} */ (sinon.spy()); | |
20 hostTableEntry_ = | |
21 new remoting.HostTableEntry(10, | |
22 onConnect_, onRename_, onDelete_); | |
23 | |
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){ | |
32 return tag; | |
33 }); | |
34 }, | |
35 teardown: function() { | |
36 hostTableEntry_.dispose(); | |
37 hostTableEntry_ = null; | |
38 $testStub(chrome.i18n.getMessage).restore(); | |
39 } | |
40 }); | |
41 | |
42 /** | |
43 * @param {string} hostName | |
44 * @param {string} status | |
45 * @param {string=} opt_offlineReason | |
46 */ | |
47 function setHost(hostName, status, opt_offlineReason) { | |
48 var host = new remoting.Host(); | |
49 host.hostName = hostName; | |
50 host.status = status; | |
51 if (opt_offlineReason) { | |
52 host.hostOfflineReason = opt_offlineReason; | |
53 } | |
54 hostTableEntry_.setHost(host); | |
55 } | |
56 | |
57 | |
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() { return keyCode; }}); | |
63 event.initKeyboardEvent("keydown", true, true, document.defaultView, | |
64 false, false, false, false, keyCode, keyCode); | |
65 target.dispatchEvent(event); | |
66 } | |
67 | |
68 function verifyVisible( | |
69 /** HTMLElement*/ element, | |
70 /** boolean */ isVisible, | |
71 /** string= */ opt_name) { | |
72 var expectedVisibility = (isVisible) ? 'visible' : 'hidden'; | |
73 QUnit.equal(element.hidden, !isVisible, | |
74 'Element ' + opt_name + ' should be ' + expectedVisibility); | |
75 } | |
76 | |
77 test('Clicking on the confirm button in the confirm dialog deletes the host', | |
78 function() { | |
79 // Setup. | |
80 sinon.stub(remoting, 'setMode', function(/** remoting.AppMode */ mode) { | |
81 if (mode === remoting.AppMode.CONFIRM_HOST_DELETE) { | |
82 document.getElementById('confirm-host-delete').click(); | |
83 } | |
84 }); | |
85 | |
86 // Invoke. | |
87 hostTableEntry_.element().querySelector('.delete-button').click(); | |
88 | |
89 // Verify. | |
90 sinon.assert.calledWith(onDelete_, hostTableEntry_); | |
91 | |
92 // Cleanup. | |
93 $testStub(remoting.setMode).restore(); | |
94 }); | |
95 | |
96 test( | |
97 'Clicking on the cancel button in the confirm dialog cancels host deletion', | |
98 function() { | |
99 // Setup. | |
100 sinon.stub(remoting, 'setMode', function(/** remoting.AppMode */ mode) { | |
101 if (mode === remoting.AppMode.CONFIRM_HOST_DELETE) { | |
102 document.getElementById('cancel-host-delete').click(); | |
103 } | |
104 }); | |
105 | |
106 // Invoke. | |
107 hostTableEntry_.element().querySelector('.delete-button').click(); | |
108 | |
109 // Verify. | |
110 sinon.assert.notCalled(onDelete_); | |
111 | |
112 // Cleanup. | |
113 $testStub(remoting.setMode).restore(); | |
114 }); | |
115 | |
116 test('Clicking on the rename button shows the input field.', function() { | |
117 // Invoke. | |
118 hostTableEntry_.element().querySelector('.rename-button').click(); | |
119 | |
120 // Verify. | |
121 var inputField = | |
122 hostTableEntry_.element().querySelector('.host-rename-input'); | |
123 | |
124 verifyVisible(inputField, true, 'inputField'); | |
125 QUnit.equal(document.activeElement, inputField); | |
126 }); | |
127 | |
128 test('Host renaming is canceled on ESCAPE key.', function() { | |
129 // Invoke. | |
130 var inputField = | |
131 hostTableEntry_.element().querySelector('.host-rename-input'); | |
132 hostTableEntry_.element().querySelector('.rename-button').click(); | |
133 | |
134 // Verify. | |
135 verifyVisible(inputField, true, 'inputField'); | |
136 QUnit.equal(document.activeElement, inputField); | |
137 sendKeydown(inputField, 27 /* ESCAPE */); | |
138 verifyVisible(inputField, false, 'inputField'); | |
139 }); | |
140 | |
141 test('Host renaming commits on ENTER.', function() { | |
142 // Invoke. | |
143 var inputField = | |
144 hostTableEntry_.element().querySelector('.host-rename-input'); | |
145 hostTableEntry_.element().querySelector('.rename-button').click(); | |
146 inputField.value = 'Renamed Host'; | |
147 sendKeydown(inputField, 13 /* ENTER */); | |
148 | |
149 // Verify | |
150 verifyVisible(inputField, false, 'inputField'); | |
151 sinon.assert.called(onRename_); | |
152 QUnit.equal(hostTableEntry_.host.hostName, 'Renamed Host'); | |
153 | |
154 // Renaming shouldn't trigger a connection request. | |
155 sinon.assert.notCalled(onConnect_); | |
156 }); | |
157 | |
158 test('HostTableEntry renders the host name correctly.', function() { | |
159 var label = hostTableEntry_.element().querySelector('.host-name-label'); | |
160 QUnit.equal(label.innerText, 'LocalHost'); | |
161 }); | |
162 | |
163 test('HostTableEntry renders an offline host correctly.', function() { | |
164 setHost('LocalHost', 'OFFLINE', 'INITIALIZATION_FAILED'); | |
165 var label = hostTableEntry_.element().querySelector('.host-name-label'); | |
166 QUnit.equal(label.innerText, 'OFFLINE'); | |
167 QUnit.equal(label.title, 'OFFLINE_REASON_INITIALIZATION_FAILED'); | |
168 }); | |
169 | |
170 test('HostTableEntry renders an out-of-date host correctly', function() { | |
171 sinon.stub(remoting.Host, 'needsUpdate').returns(true); | |
172 setHost('LocalHost', 'ONLINE'); | |
173 var warningOverlay = | |
174 hostTableEntry_.element().querySelector('.warning-overlay'); | |
175 var label = hostTableEntry_.element().querySelector('.host-name-label'); | |
176 verifyVisible(warningOverlay, true, 'warning overlay'); | |
177 QUnit.equal(label.innerText, 'UPDATE_REQUIRED'); | |
178 }); | |
179 | |
180 test('Clicking on an online host connects it', function() { | |
181 hostTableEntry_.element().querySelector('.host-name-label').click(); | |
182 sinon.assert.calledWith(onConnect_, | |
183 encodeURIComponent(hostTableEntry_.host.hostId)); | |
184 }); | |
185 | |
186 test('Clicking on an offline host should be a no-op', function() { | |
187 setHost('LocalHost', 'OFFLINE'); | |
188 hostTableEntry_.element().querySelector('.host-name-label').click(); | |
189 sinon.assert.notCalled(onConnect_); | |
190 }); | |
191 | |
192 test('HostTableEntry handles host that is null', function() { | |
193 hostTableEntry_.setHost(null); | |
194 hostTableEntry_.element().querySelector('.host-name-label').click(); | |
195 sinon.assert.notCalled(onConnect_); | |
196 }); | |
197 | |
198 })(); | |
OLD | NEW |