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