Debian Codegraph @

closure-library/closure/goog/ui/editor/abstractdialog_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.ui.editor.AbstractDialogTest');
16 goog.setTestOnly('goog.ui.editor.AbstractDialogTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.DomHelper');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.dom.classlist');
22 goog.require('goog.events.Event');
23 goog.require('goog.events.EventHandler');
24 goog.require('goog.events.KeyCodes');
25 goog.require('goog.testing.MockControl');
26 goog.require('goog.testing.events');
27 goog.require('goog.testing.jsunit');
28 goog.require('goog.testing.mockmatchers.ArgumentMatcher');
29 goog.require('goog.ui.editor.AbstractDialog');
30 goog.require('goog.userAgent');
31
32 function shouldRunTests() {
33 // Test disabled in IE7 due to flakiness. See b/4269021.
34 return !(goog.userAgent.IE && goog.userAgent.isVersionOrHigher('7'));
35 }
36
37 var dialog;
38 var builder;
39
40 var mockCtrl;
41 var mockAfterHideHandler;
42 var mockOkHandler;
43 var mockCancelHandler;
44 var mockCustomButtonHandler;
45
46 var CUSTOM_EVENT = 'customEvent';
47 var CUSTOM_BUTTON_ID = 'customButton';
48
49
50 function setUp() {
51 mockCtrl = new goog.testing.MockControl();
52 mockAfterHideHandler = mockCtrl.createLooseMock(goog.events.EventHandler);
53 mockOkHandler = mockCtrl.createLooseMock(goog.events.EventHandler);
54 mockCancelHandler = mockCtrl.createLooseMock(goog.events.EventHandler);
55 mockCustomButtonHandler = mockCtrl.createLooseMock(goog.events.EventHandler);
56 }
57
58 function tearDown() {
59 if (dialog) {
60 mockAfterHideHandler.$setIgnoreUnexpectedCalls(true);
61 dialog.dispose();
62 }
63 }
64
65
66 /**
67 * Sets up the mock event handler to expect an AFTER_HIDE event.
68 */
69 function expectAfterHide() {
70 mockAfterHideHandler.handleEvent(
71 new goog.testing.mockmatchers.ArgumentMatcher(function(arg) {
72 return arg.type == goog.ui.editor.AbstractDialog.EventType.AFTER_HIDE;
73 }));
74 }
75
76
77 /**
78 * Sets up the mock event handler to expect an OK event.
79 */
80 function expectOk() {
81 mockOkHandler.handleEvent(
82 new goog.testing.mockmatchers.ArgumentMatcher(function(arg) {
83 return arg.type == goog.ui.editor.AbstractDialog.EventType.OK;
84 }));
85 }
86
87
88 /**
89 * Sets up the mock event handler to expect an OK event and to call
90 * preventDefault when handling it.
91 */
92 function expectOkPreventDefault() {
93 expectOk();
94 mockOkHandler.$does(function(e) { e.preventDefault(); });
95 }
96
97
98 /**
99 * Sets up the mock event handler to expect an OK event and to return false
100 * when handling it.
101 */
102 function expectOkReturnFalse() {
103 expectOk();
104 mockOkHandler.$returns(false);
105 }
106
107
108 /**
109 * Sets up the mock event handler to expect a CANCEL event.
110 */
111 function expectCancel() {
112 mockCancelHandler.handleEvent(
113 new goog.testing.mockmatchers.ArgumentMatcher(function(arg) {
114 return arg.type == goog.ui.editor.AbstractDialog.EventType.CANCEL;
115 }));
116 }
117
118
119 /**
120 * Sets up the mock event handler to expect a custom button event.
121 */
122 function expectCustomButton() {
123 mockCustomButtonHandler.handleEvent(
124 new goog.testing.mockmatchers.ArgumentMatcher(function(arg) {
125 return arg.type == CUSTOM_EVENT;
126 }));
127 }
128
129
130 /**
131 * Helper to create the dialog being tested in each test. Since NewDialog is
132 * abstract, needs to add a concrete version of any abstract methods. Also
133 * creates up the global builder variable which should be set up after the call
134 * to this method.
135 * @return {goog.ui.editor.AbstractDialog} The dialog.
136 */
137 function createTestDialog() {
138 var dialog = new goog.ui.editor.AbstractDialog(new goog.dom.DomHelper());
139 builder = new goog.ui.editor.AbstractDialog.Builder(dialog);
140 dialog.createDialogControl = function() { return builder.build(); };
141 dialog.createOkEvent = function(e) {
142 return new goog.events.Event(goog.ui.editor.AbstractDialog.EventType.OK);
143 };
144 dialog.addEventListener(
145 goog.ui.editor.AbstractDialog.EventType.AFTER_HIDE, mockAfterHideHandler);
146 dialog.addEventListener(
147 goog.ui.editor.AbstractDialog.EventType.OK, mockOkHandler);
148 dialog.addEventListener(
149 goog.ui.editor.AbstractDialog.EventType.CANCEL, mockCancelHandler);
150 dialog.addEventListener(CUSTOM_EVENT, mockCustomButtonHandler);
151 return dialog;
152 }
153
154
155 /**
156 * Asserts that the given dialog is open.
157 * @param {string} msg Message to be printed in case of failure.
158 * @param {goog.ui.editor.AbstractDialog} dialog Dialog to be tested.
159 */
160 function assertOpen(msg, dialog) {
161 assertTrue(msg + ' [AbstractDialog.isOpen()]', dialog && dialog.isOpen());
162 }
163
164
165 /**
166 * Asserts that the given dialog is closed.
167 * @param {string} msg Message to be printed in case of failure.
168 * @param {goog.ui.editor.AbstractDialog} dialog Dialog to be tested.
169 */
170 function assertNotOpen(msg, dialog) {
171 assertFalse(msg + ' [AbstractDialog.isOpen()]', dialog && dialog.isOpen());
172 }
173
174
175 /**
176 * Tests that if you create a dialog and hide it without having shown it, no
177 * errors occur.
178 */
179 function testCreateAndHide() {
180 dialog = createTestDialog();
181 mockCtrl.$replayAll();
182
183 assertNotOpen('Dialog should not be open after creation', dialog);
184 dialog.hide();
185 assertNotOpen('Dialog should not be open after hide()', dialog);
186
187 mockCtrl.$verifyAll(); // Verifies AFTER_HIDE was not dispatched.
188 }
189
190
191 /**
192 * Tests that when you show and hide a dialog the flags indicating open are
193 * correct and the AFTER_HIDE event is dispatched (and no errors happen).
194 */
195 function testShowAndHide() {
196 dialog = createTestDialog();
197 expectAfterHide(dialog);
198 mockCtrl.$replayAll();
199
200 assertNotOpen('Dialog should not be open before show()', dialog);
201 dialog.show();
202 assertOpen('Dialog should be open after show()', dialog);
203 dialog.hide();
204 assertNotOpen('Dialog should not be open after hide()', dialog);
205
206 mockCtrl.$verifyAll(); // Verifies AFTER_HIDE was dispatched.
207 }
208
209
210 /**
211 * Tests that when you show and dispose a dialog (without hiding it first) the
212 * flags indicating open are correct and the AFTER_HIDE event is dispatched (and
213 * no errors happen).
214 */
215 function testShowAndDispose() {
216 dialog = createTestDialog();
217 expectAfterHide(dialog);
218 mockCtrl.$replayAll();
219
220 assertNotOpen('Dialog should not be open before show()', dialog);
221 dialog.show();
222 assertOpen('Dialog should be open after show()', dialog);
223 dialog.dispose();
224 assertNotOpen('Dialog should not be open after dispose()', dialog);
225
226 mockCtrl.$verifyAll(); // Verifies AFTER_HIDE was dispatched.
227 }
228
229
230 /**
231 * Tests that when you dispose a dialog (without ever showing it first) the
232 * flags indicating open are correct and the AFTER_HIDE event is never
233 * dispatched (and no errors happen).
234 */
235 function testDisposeWithoutShow() {
236 dialog = createTestDialog();
237 mockCtrl.$replayAll();
238
239 assertNotOpen('Dialog should not be open before dispose()', dialog);
240 dialog.dispose();
241 assertNotOpen('Dialog should not be open after dispose()', dialog);
242
243 mockCtrl.$verifyAll(); // Verifies AFTER_HIDE was NOT dispatched.
244 }
245
246
247 /**
248 * Tests that labels set in the builder can be found in the resulting dialog's
249 * HTML.
250 */
251 function testBasicLayout() {
252 dialog = createTestDialog();
253 mockCtrl.$replayAll();
254
255 // create some dialog content
256 var content = goog.dom.createDom(goog.dom.TagName.DIV, null, 'The Content');
257 builder.setTitle('The Title')
258 .setContent(content)
259 .addOkButton('The OK Button')
260 .addCancelButton()
261 .addButton('The Apply Button', goog.nullFunction)
262 .addClassName('myClassName');
263 dialog.show();
264
265 var dialogElem = dialog.dialogInternal_.getElement();
266 var html = dialogElem.innerHTML;
267 // TODO(user): This is really insufficient. If the title and content
268 // were swapped this test would still pass!
269 assertContains('Dialog html should contain title', '>The Title<', html);
270 assertContains('Dialog html should contain content', '>The Content<', html);
271 assertContains(
272 'Dialog html should contain custom OK button label', '>The OK Button<',
273 html);
274 assertContains(
275 'Dialog html should contain default Cancel button label', '>Cancel<',
276 html);
277 assertContains(
278 'Dialog html should contain custom button label', '>The Apply Button<',
279 html);
280 assertTrue(
281 'Dialog should have default Closure class',
282 goog.dom.classlist.contains(dialogElem, 'modal-dialog'));
283 assertTrue(
284 'Dialog should have our custom class',
285 goog.dom.classlist.contains(dialogElem, 'myClassName'));
286
287 mockCtrl.$verifyAll();
288 }
289
290
291 /**
292 * Tests that clicking the OK button dispatches the OK event and closes the
293 * dialog (dispatching the AFTER_HIDE event too).
294 */
295 function testOk() {
296 dialog = createTestDialog();
297 expectOk(dialog);
298 expectAfterHide(dialog);
299 mockCtrl.$replayAll();
300
301 dialog.show();
302 goog.testing.events.fireClickSequence(dialog.getOkButtonElement());
303 assertNotOpen('Dialog should not be open after clicking OK', dialog);
304
305 mockCtrl.$verifyAll();
306 }
307
308
309 /**
310 * Tests that hitting the enter key dispatches the OK event and closes the
311 * dialog (dispatching the AFTER_HIDE event too).
312 */
313 function testEnter() {
314 dialog = createTestDialog();
315 expectOk(dialog);
316 expectAfterHide(dialog);
317 mockCtrl.$replayAll();
318
319 dialog.show();
320 goog.testing.events.fireKeySequence(
321 dialog.dialogInternal_.getElement(), goog.events.KeyCodes.ENTER);
322 assertNotOpen('Dialog should not be open after hitting enter', dialog);
323
324 mockCtrl.$verifyAll();
325 }
326
327
328 /**
329 * Tests that clicking the Cancel button dispatches the CANCEL event and closes
330 * the dialog (dispatching the AFTER_HIDE event too).
331 */
332 function testCancel() {
333 dialog = createTestDialog();
334 expectCancel(dialog);
335 expectAfterHide(dialog);
336 mockCtrl.$replayAll();
337
338 builder.addCancelButton('My Cancel Button');
339
340 dialog.show();
341 goog.testing.events.fireClickSequence(dialog.getCancelButtonElement());
342 assertNotOpen('Dialog should not be open after clicking Cancel', dialog);
343
344 mockCtrl.$verifyAll();
345 }
346
347
348 /**
349 * Tests that hitting the escape key dispatches the CANCEL event and closes
350 * the dialog (dispatching the AFTER_HIDE event too).
351 */
352 function testEscape() {
353 dialog = createTestDialog();
354 expectCancel(dialog);
355 expectAfterHide(dialog);
356 mockCtrl.$replayAll();
357
358 dialog.show();
359 goog.testing.events.fireKeySequence(
360 dialog.dialogInternal_.getElement(), goog.events.KeyCodes.ESC);
361 assertNotOpen('Dialog should not be open after hitting escape', dialog);
362
363 mockCtrl.$verifyAll();
364 }
365
366
367 /**
368 * Tests that clicking the custom button dispatches the custom event and closes
369 * the dialog (dispatching the AFTER_HIDE event too).
370 */
371 function testCustomButton() {
372 dialog = createTestDialog();
373 expectCustomButton(dialog);
374 expectAfterHide(dialog);
375 mockCtrl.$replayAll();
376
377 builder.addButton('My Custom Button', function() {
378 dialog.dispatchEvent(CUSTOM_EVENT);
379 }, CUSTOM_BUTTON_ID);
380
381 dialog.show();
382 goog.testing.events.fireClickSequence(
383 dialog.getButtonElement(CUSTOM_BUTTON_ID));
384 assertNotOpen(
385 'Dialog should not be open after clicking custom button', dialog);
386
387 mockCtrl.$verifyAll();
388 }
389
390
391 /**
392 * Tests that if the OK handler calls preventDefault, the dialog doesn't close.
393 */
394 function testOkPreventDefault() {
395 dialog = createTestDialog();
396 expectOkPreventDefault(dialog);
397 mockCtrl.$replayAll();
398
399 dialog.show();
400 goog.testing.events.fireClickSequence(dialog.getOkButtonElement());
401 assertOpen(
402 'Dialog should not be closed because preventDefault was called', dialog);
403
404 mockCtrl.$verifyAll();
405 }
406
407
408 /**
409 * Tests that if the OK handler returns false, the dialog doesn't close.
410 */
411 function testOkReturnFalse() {
412 dialog = createTestDialog();
413 expectOkReturnFalse(dialog);
414 mockCtrl.$replayAll();
415
416 dialog.show();
417 goog.testing.events.fireClickSequence(dialog.getOkButtonElement());
418 assertOpen(
419 'Dialog should not be closed because handler returned false', dialog);
420
421 mockCtrl.$verifyAll();
422 }
423
424
425 /**
426 * Tests that if creating the OK event fails, no event is dispatched and the
427 * dialog doesn't close.
428 */
429 function testCreateOkEventFail() {
430 dialog = createTestDialog();
431 dialog.createOkEvent = function() { // Override our mock createOkEvent.
432 return null;
433 };
434 mockCtrl.$replayAll();
435
436 dialog.show();
437 goog.testing.events.fireClickSequence(dialog.getOkButtonElement());
438 assertOpen(
439 'Dialog should not be closed because OK event creation failed', dialog);
440
441 mockCtrl.$verifyAll(); // Verifies that no event was dispatched.
442 }
443
444
445 /**
446 * Tests that processOkAndClose() dispatches the OK event and closes the
447 * dialog (dispatching the AFTER_HIDE event too).
448 */
449 function testProcessOkAndClose() {
450 dialog = createTestDialog();
451 expectOk(dialog);
452 expectAfterHide(dialog);
453 mockCtrl.$replayAll();
454
455 dialog.show();
456 dialog.processOkAndClose();
457 assertNotOpen('Dialog should not be open after processOkAndClose()', dialog);
458
459 mockCtrl.$verifyAll();
460 }
461
462
463 /**
464 * Tests that if the OK handler triggered by processOkAndClose calls
465 * preventDefault, the dialog doesn't close (in the old implementation this
466 * failed due to not great design, so this is sort of a regression test).
467 */
468 function testProcessOkAndClosePreventDefault() {
469 dialog = createTestDialog();
470 expectOkPreventDefault(dialog);
471 mockCtrl.$replayAll();
472
473 dialog.show();
474 dialog.processOkAndClose();
475 assertOpen(
476 'Dialog should not be closed because preventDefault was called', dialog);
477
478 mockCtrl.$verifyAll();
479 }