Debian Codegraph @

closure-library/closure/goog/demos/editor/helloworlddialog_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 // Use of this source code is governed by the Apache License, Version 2.0.
3
4 goog.provide('goog.demos.editor.HelloWorldDialogTest');
5 goog.setTestOnly('goog.demos.editor.HelloWorldDialogTest');
6
7 goog.require('goog.demos.editor.HelloWorldDialog');
8 goog.require('goog.demos.editor.HelloWorldDialog.OkEvent');
9 goog.require('goog.dom.DomHelper');
10 goog.require('goog.events.EventHandler');
11 goog.require('goog.testing.LooseMock');
12 goog.require('goog.testing.events');
13 goog.require('goog.testing.jsunit');
14 goog.require('goog.testing.mockmatchers.ArgumentMatcher');
15 goog.require('goog.ui.editor.AbstractDialog.EventType');
16
17 var dialog;
18 var mockOkHandler;
19
20 var CUSTOM_MESSAGE = 'Hello, cruel world...';
21
22 function setUp() {
23 mockOkHandler = new goog.testing.LooseMock(goog.events.EventHandler);
24 }
25
26 function tearDown() {
27 dialog.dispose();
28 }
29
30 /**
31 * Creates and shows the dialog to be tested.
32 */
33 function createAndShow() {
34 dialog = new goog.demos.editor.HelloWorldDialog(new goog.dom.DomHelper());
35 dialog.addEventListener(goog.ui.editor.AbstractDialog.EventType.OK,
36 mockOkHandler);
37 dialog.show();
38 }
39
40 /**
41 * Sets up the mock event handler to expect an OK event with the given
42 * message.
43 * @param {string} message Hello world message the OK event is expected to
44 * carry.
45 */
46 function expectOk(message) {
47 mockOkHandler.handleEvent(new goog.testing.mockmatchers.ArgumentMatcher(
48 function(arg) {
49 return arg.type == goog.ui.editor.AbstractDialog.EventType.OK &&
50 arg.message == message;
51 }));
52 }
53
54 /**
55 * Tests that when you show the dialog, the input field has the correct
56 * sample text in it.
57 */
58 function testShow() {
59 mockOkHandler.$replay();
60 createAndShow();
61
62 assertEquals('Input field has incorrect sample text',
63 'Hello, world!',
64 dialog.input_.value);
65 mockOkHandler.$verify();
66 }
67
68 /**
69 * Tests that clicking OK dispatches an event carying the entered message.
70 */
71 function testOk() {
72 expectOk(CUSTOM_MESSAGE);
73 mockOkHandler.$replay();
74 createAndShow();
75
76 dialog.input_.value = CUSTOM_MESSAGE;
77 goog.testing.events.fireClickSequence(dialog.getOkButtonElement());
78
79 mockOkHandler.$verify(); // Verifies OK is dispatched with correct message.
80 }