Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: remoting/webapp/unittests/host_table_entry_unittest.js

Issue 944183002: HostTableEntry refactor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Jamie's feedback Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.$setupStub(chrome.i18n, 'getMessage', function(/** string */ tag){
32 return tag;
33 });
34 },
35 teardown: function() {
36 hostTableEntry_.dispose();
37 hostTableEntry_ = null;
38 chrome.i18n.getMessage.$testStub.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.$setupStub(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 remoting.setMode.$testStub.restore();
94 });
95
96 test(
97 'Clicking on the cancel button in the confirm dialog cancels host deletion',
98 function() {
99 // Setup.
100 sinon.$setupStub(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 remoting.setMode.$testStub.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 when input field losses focus.', function() {
129 // Invoke.
130 hostTableEntry_.element().querySelector('.rename-button').click();
131
132 // Verify.
133 var inputField =
134 hostTableEntry_.element().querySelector('.host-rename-input');
135
136 verifyVisible(inputField, true, 'inputField');
137 QUnit.equal(document.activeElement, inputField);
138 inputField.blur();
139 verifyVisible(inputField, false, 'inputField');
140 });
141
142 test('Host renaming is canceled on ESCAPE key.', function() {
143 // Invoke.
144 var inputField =
145 hostTableEntry_.element().querySelector('.host-rename-input');
146 hostTableEntry_.element().querySelector('.rename-button').click();
147
148 // Verify.
149 verifyVisible(inputField, true, 'inputField');
150 QUnit.equal(document.activeElement, inputField);
151 sendKeydown(inputField, 27 /* ESCAPE */);
152 verifyVisible(inputField, false, 'inputField');
153 });
154
155 test('Host renaming commits on ENTER.', function() {
156 // Invoke.
157 var inputField =
158 hostTableEntry_.element().querySelector('.host-rename-input');
159 hostTableEntry_.element().querySelector('.rename-button').click();
160 inputField.value = 'Renamed Host';
161 sendKeydown(inputField, 13 /* ENTER */);
162
163 // Verify
164 verifyVisible(inputField, false, 'inputField');
165 sinon.assert.called(onRename_);
166 QUnit.equal(hostTableEntry_.host.hostName, 'Renamed Host');
167
168 // Renaming shouldn't trigger a connection request.
169 sinon.assert.notCalled(onConnect_);
170 });
171
172 test('HostTableEntry renders the host name correctly.', function() {
173 var label = hostTableEntry_.element().querySelector('.host-name-label');
174 QUnit.equal(label.innerText, 'LocalHost');
175 });
176
177 test('HostTableEntry renders an offline host correctly.', function() {
178 setHost('LocalHost', 'OFFLINE', 'INITIALIZATION_FAILED');
179 var label = hostTableEntry_.element().querySelector('.host-name-label');
180 QUnit.equal(label.innerText, 'OFFLINE');
181 QUnit.equal(label.title, 'OFFLINE_REASON_INITIALIZATION_FAILED');
182 });
183
184 test('HostTableEntry renders an out-of-date host correctly', function() {
185 sinon.$setupStub(remoting.Host, 'needsUpdate').returns(true);
186 setHost('LocalHost', 'ONLINE');
187 var warningOverlay =
188 hostTableEntry_.element().querySelector('.warning-overlay');
189 var label = hostTableEntry_.element().querySelector('.host-name-label');
190 verifyVisible(warningOverlay, true, 'warning overlay');
191 QUnit.equal(label.innerText, 'UPDATE_REQUIRED');
192 });
193
194 test('Clicking on an online host connects it', function() {
195 hostTableEntry_.element().querySelector('.host-name-label').click();
196 sinon.assert.calledWith(onConnect_,
197 encodeURIComponent(hostTableEntry_.host.hostId));
198 });
199
200 test('Clicking on an offline host should be a no-op', function() {
201 setHost('LocalHost', 'OFFLINE');
202 hostTableEntry_.element().querySelector('.host-name-label').click();
203 sinon.assert.notCalled(onConnect_);
204 });
205
206 test('HostTableEntry handles host that is null', function() {
207 hostTableEntry_.setHost(null);
208 hostTableEntry_.element().querySelector('.host-name-label').click();
209 sinon.assert.notCalled(onConnect_);
210 });
211
212 })();
OLDNEW
« no previous file with comments | « remoting/webapp/js_proto/sinon_stub_proto.js ('k') | remoting/webapp/unittests/sinon_helpers.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698