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 var hostTableEntry_ = null; | |
10 var onConnect_ = null; | |
11 var onRename_ = null; | |
12 var onDelete_ = null; | |
13 var host_ = null; | |
14 | |
15 module('HostTableEntry', { | |
16 setup: function() { | |
17 onConnect_ = sinon.spy(); | |
18 onRename_ = sinon.spy(); | |
19 onDelete_ = sinon.spy(); | |
20 hostTableEntry_ = | |
21 new remoting.HostTableEntry(10, onConnect_, onRename_, onDelete_); | |
22 | |
23 // Setup the DOM dependencies on the confirm delete dialog. | |
24 var fixture = document.getElementById('qunit-fixture'); | |
25 fixture.innerHTML = '<div id="confirm-host-delete-message"></div>' + | |
26 '<div id="confirm-host-delete"></div>' + | |
27 '<div id="cancel-host-delete"></div>'; | |
28 hostTableEntry_.setHost({ | |
29 hostName: 'LocalHost', | |
30 status: 'ONLINE' | |
31 }); | |
32 fixture.appendChild(hostTableEntry_.element()); | |
33 sinon.stub(chrome.i18n, 'getMessage', function(tag) { | |
34 return tag; | |
35 }); | |
36 }, | |
37 teardown: function() { | |
38 hostTableEntry_.dispose(); | |
39 hostTableEntry_ = null; | |
40 chrome.i18n.getMessage.restore(); | |
41 } | |
42 }); | |
43 | |
44 | |
45 function sendKeydown(/** HTMLElement */ target, /** number */ keyCode) { | |
46 var event = document.createEvent('KeyboardEvent'); | |
47 Object.defineProperty( | |
48 event, 'which', {get: function() { return keyCode; }}); | |
49 event.initKeyboardEvent("keydown", true, true, document.defaultView, | |
50 false, false, false, false, keyCode, keyCode); | |
51 target.dispatchEvent(event); | |
52 } | |
53 | |
54 function verifyVisible( | |
55 /** HTMLElement*/ element, | |
56 /** boolean */ isVisible, | |
57 /** string= */ opt_name) { | |
58 var expectedVisibility = (isVisible) ? 'visible' : 'hidden'; | |
59 QUnit.equal(element.hidden, !isVisible, | |
60 'Element ' + opt_name + ' should be ' + expectedVisibility); | |
61 } | |
62 | |
63 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.
| |
64 // Setup. | |
65 sinon.stub(remoting, 'setMode', function(mode) { | |
66 if (mode === remoting.AppMode.CONFIRM_HOST_DELETE) { | |
67 document.getElementById('confirm-host-delete').click(); | |
68 } | |
69 }); | |
70 | |
71 // Invoke. | |
72 hostTableEntry_.element().querySelector('.delete-button').click(); | |
73 | |
74 // Verify. | |
75 sinon.assert.calledWith(onDelete_, hostTableEntry_); | |
76 | |
77 // Cleanup. | |
78 remoting.setMode.restore(); | |
79 }); | |
80 | |
81 test('Clicking rename button shows the input field.', function() { | |
82 // Invoke. | |
83 hostTableEntry_.element().querySelector('.rename-button').click(); | |
84 | |
85 // Verify. | |
86 var inputField = | |
87 hostTableEntry_.element().querySelector('.box-spacer input'); | |
88 | |
89 verifyVisible(inputField, true, 'inputField'); | |
90 QUnit.equal(document.activeElement, inputField); | |
91 }); | |
92 | |
93 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.
| |
94 // Invoke. | |
95 hostTableEntry_.element().querySelector('.rename-button').click(); | |
96 | |
97 // Verify. | |
98 var inputField = | |
99 hostTableEntry_.element().querySelector('.box-spacer input'); | |
100 | |
101 verifyVisible(inputField, true, 'inputField'); | |
102 QUnit.equal(document.activeElement, inputField); | |
103 inputField.blur(); | |
104 verifyVisible(inputField, false, 'inputField'); | |
105 }); | |
106 | |
107 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.
| |
108 // Invoke. | |
109 var inputField = | |
110 hostTableEntry_.element().querySelector('.box-spacer input'); | |
111 hostTableEntry_.element().querySelector('.rename-button').click(); | |
112 inputField.value = 'Renamed Host'; | |
113 sendKeydown(inputField, 13 /* ENTER */); | |
114 | |
115 // Verify | |
116 verifyVisible(inputField, false, 'inputField'); | |
117 sinon.assert.called(onRename_); | |
118 QUnit.equal(hostTableEntry_.host.hostName, 'Renamed Host'); | |
119 | |
120 // Renaming shouldn't trigger a connection request. | |
121 sinon.assert.notCalled(onConnect_); | |
122 }); | |
123 | |
124 test('HostTableEntry renders the host name correctly.', function() { | |
125 var label = hostTableEntry_.element().querySelector('.host-list-label'); | |
126 QUnit.equal(label.innerText, 'LocalHost'); | |
127 }); | |
128 | |
129 test('HostTableEntry renders an offline host correctly.', function() { | |
130 hostTableEntry_.setHost({ | |
131 hostName: 'LocalHost', | |
132 status: 'OFFLINE', | |
133 hostOfflineReason: 'INITIALIZATION_FAILED' | |
134 }); | |
135 var label = hostTableEntry_.element().querySelector('.host-list-label'); | |
136 QUnit.equal(label.innerText, 'OFFLINE'); | |
137 QUnit.equal(label.title, 'OFFLINE_REASON_INITIALIZATION_FAILED'); | |
138 }); | |
139 | |
140 test('Clicking on an online host connects it', function() { | |
141 hostTableEntry_.element().querySelector('.host-list-label').click(); | |
142 sinon.assert.calledWith(onConnect_, | |
143 encodeURIComponent(hostTableEntry_.host.hostId)); | |
144 }); | |
145 | |
146 test('Clicking on an offline host should be a no-op', function() { | |
147 hostTableEntry_.setHost({ | |
148 hostName: 'LocalHost', | |
149 status: 'OFFLINE' | |
150 }); | |
151 hostTableEntry_.element().querySelector('.host-list-label').click(); | |
152 sinon.assert.notCalled(onConnect_); | |
153 }); | |
154 | |
155 test('HostTableEntry handles host that is null', function() { | |
156 hostTableEntry_.setHost(null); | |
157 hostTableEntry_.element().querySelector('.host-list-label').click(); | |
158 sinon.assert.notCalled(onConnect_); | |
159 }); | |
160 | |
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
| |
161 })(); | |
OLD | NEW |