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

Unified 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: Reviewer's feedbacks with unit tests Created 5 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: remoting/webapp/unittests/host_table_entry_unittest.js
diff --git a/remoting/webapp/unittests/host_table_entry_unittest.js b/remoting/webapp/unittests/host_table_entry_unittest.js
new file mode 100644
index 0000000000000000000000000000000000000000..1c1bad3ff9562f2c6fb0326df616fb1d1ae598dd
--- /dev/null
+++ b/remoting/webapp/unittests/host_table_entry_unittest.js
@@ -0,0 +1,161 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+(function() {
+
+'use strict';
+
+var hostTableEntry_ = null;
+var onConnect_ = null;
+var onRename_ = null;
+var onDelete_ = null;
+var host_ = null;
+
+module('HostTableEntry', {
+ setup: function() {
+ onConnect_ = sinon.spy();
+ onRename_ = sinon.spy();
+ onDelete_ = sinon.spy();
+ hostTableEntry_ =
+ new remoting.HostTableEntry(10, onConnect_, onRename_, onDelete_);
+
+ // Setup the DOM dependencies on the confirm delete dialog.
+ var fixture = document.getElementById('qunit-fixture');
+ fixture.innerHTML = '<div id="confirm-host-delete-message"></div>' +
+ '<div id="confirm-host-delete"></div>' +
+ '<div id="cancel-host-delete"></div>';
+ hostTableEntry_.setHost({
+ hostName: 'LocalHost',
+ status: 'ONLINE'
+ });
+ fixture.appendChild(hostTableEntry_.element());
+ sinon.stub(chrome.i18n, 'getMessage', function(tag) {
+ return tag;
+ });
+ },
+ teardown: function() {
+ hostTableEntry_.dispose();
+ hostTableEntry_ = null;
+ chrome.i18n.getMessage.restore();
+ }
+});
+
+
+function sendKeydown(/** HTMLElement */ target, /** number */ keyCode) {
+ var event = document.createEvent('KeyboardEvent');
+ Object.defineProperty(
+ event, 'which', {get: function() { return keyCode; }});
+ event.initKeyboardEvent("keydown", true, true, document.defaultView,
+ false, false, false, false, keyCode, keyCode);
+ target.dispatchEvent(event);
+}
+
+function verifyVisible(
+ /** HTMLElement*/ element,
+ /** boolean */ isVisible,
+ /** string= */ opt_name) {
+ var expectedVisibility = (isVisible) ? 'visible' : 'hidden';
+ QUnit.equal(element.hidden, !isVisible,
+ 'Element ' + opt_name + ' should be ' + expectedVisibility);
+}
+
+test('Clicking confirm on confirm dialog deletes the host', function() {
Jamie 2015/02/27 18:19:41 Should there also be a test for clicking Cancel?
kelvinp 2015/03/03 21:57:54 Done.
+ // Setup.
+ sinon.stub(remoting, 'setMode', function(mode) {
+ if (mode === remoting.AppMode.CONFIRM_HOST_DELETE) {
+ document.getElementById('confirm-host-delete').click();
+ }
+ });
+
+ // Invoke.
+ hostTableEntry_.element().querySelector('.delete-button').click();
+
+ // Verify.
+ sinon.assert.calledWith(onDelete_, hostTableEntry_);
+
+ // Cleanup.
+ remoting.setMode.restore();
+});
+
+test('Clicking rename button shows the input field.', function() {
+ // Invoke.
+ hostTableEntry_.element().querySelector('.rename-button').click();
+
+ // Verify.
+ var inputField =
+ hostTableEntry_.element().querySelector('.box-spacer input');
+
+ verifyVisible(inputField, true, 'inputField');
+ QUnit.equal(document.activeElement, inputField);
+});
+
+test('Host renaming is canceled when input field lost focus.', function() {
Jamie 2015/02/27 18:19:41 s/lost/loses/
kelvinp 2015/03/03 21:57:54 Done.
+ // Invoke.
+ hostTableEntry_.element().querySelector('.rename-button').click();
+
+ // Verify.
+ var inputField =
+ hostTableEntry_.element().querySelector('.box-spacer input');
+
+ verifyVisible(inputField, true, 'inputField');
+ QUnit.equal(document.activeElement, inputField);
+ inputField.blur();
+ verifyVisible(inputField, false, 'inputField');
+});
+
+test('Host renaming commits on ENTER.', function() {
Jamie 2015/02/27 18:19:41 Add a test for Escape as well?
kelvinp 2015/03/03 21:57:54 Done.
+ // Invoke.
+ var inputField =
+ hostTableEntry_.element().querySelector('.box-spacer input');
+ hostTableEntry_.element().querySelector('.rename-button').click();
+ inputField.value = 'Renamed Host';
+ sendKeydown(inputField, 13 /* ENTER */);
+
+ // Verify
+ verifyVisible(inputField, false, 'inputField');
+ sinon.assert.called(onRename_);
+ QUnit.equal(hostTableEntry_.host.hostName, 'Renamed Host');
+
+ // Renaming shouldn't trigger a connection request.
+ sinon.assert.notCalled(onConnect_);
+});
+
+test('HostTableEntry renders the host name correctly.', function() {
+ var label = hostTableEntry_.element().querySelector('.host-list-label');
+ QUnit.equal(label.innerText, 'LocalHost');
+});
+
+test('HostTableEntry renders an offline host correctly.', function() {
+ hostTableEntry_.setHost({
+ hostName: 'LocalHost',
+ status: 'OFFLINE',
+ hostOfflineReason: 'INITIALIZATION_FAILED'
+ });
+ var label = hostTableEntry_.element().querySelector('.host-list-label');
+ QUnit.equal(label.innerText, 'OFFLINE');
+ QUnit.equal(label.title, 'OFFLINE_REASON_INITIALIZATION_FAILED');
+});
+
+test('Clicking on an online host connects it', function() {
+ hostTableEntry_.element().querySelector('.host-list-label').click();
+ sinon.assert.calledWith(onConnect_,
+ encodeURIComponent(hostTableEntry_.host.hostId));
+});
+
+test('Clicking on an offline host should be a no-op', function() {
+ hostTableEntry_.setHost({
+ hostName: 'LocalHost',
+ status: 'OFFLINE'
+ });
+ hostTableEntry_.element().querySelector('.host-list-label').click();
+ sinon.assert.notCalled(onConnect_);
+});
+
+test('HostTableEntry handles host that is null', function() {
+ hostTableEntry_.setHost(null);
+ hostTableEntry_.element().querySelector('.host-list-label').click();
+ sinon.assert.notCalled(onConnect_);
+});
+
Jamie 2015/02/27 18:19:41 Can you add a test for host-out-of-date as well? I
kelvinp 2015/03/03 21:57:54 Done. Unfortunately, the host out of date warning
+})();
« remoting/webapp/crd/js/local_host_section.js ('K') | « remoting/webapp/crd/js/local_host_section.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698