helloworlddialog_test.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. goog.provide('goog.demos.editor.HelloWorldDialogTest');
  4. goog.setTestOnly('goog.demos.editor.HelloWorldDialogTest');
  5. goog.require('goog.demos.editor.HelloWorldDialog');
  6. goog.require('goog.demos.editor.HelloWorldDialog.OkEvent');
  7. goog.require('goog.dom.DomHelper');
  8. goog.require('goog.events.EventHandler');
  9. goog.require('goog.testing.LooseMock');
  10. goog.require('goog.testing.events');
  11. goog.require('goog.testing.jsunit');
  12. goog.require('goog.testing.mockmatchers.ArgumentMatcher');
  13. goog.require('goog.ui.editor.AbstractDialog.EventType');
  14. var dialog;
  15. var mockOkHandler;
  16. var CUSTOM_MESSAGE = 'Hello, cruel world...';
  17. function setUp() {
  18. mockOkHandler = new goog.testing.LooseMock(goog.events.EventHandler);
  19. }
  20. function tearDown() {
  21. dialog.dispose();
  22. }
  23. /**
  24. * Creates and shows the dialog to be tested.
  25. */
  26. function createAndShow() {
  27. dialog = new goog.demos.editor.HelloWorldDialog(new goog.dom.DomHelper());
  28. dialog.addEventListener(goog.ui.editor.AbstractDialog.EventType.OK,
  29. mockOkHandler);
  30. dialog.show();
  31. }
  32. /**
  33. * Sets up the mock event handler to expect an OK event with the given
  34. * message.
  35. * @param {string} message Hello world message the OK event is expected to
  36. * carry.
  37. */
  38. function expectOk(message) {
  39. mockOkHandler.handleEvent(new goog.testing.mockmatchers.ArgumentMatcher(
  40. function(arg) {
  41. return arg.type == goog.ui.editor.AbstractDialog.EventType.OK &&
  42. arg.message == message;
  43. }));
  44. }
  45. /**
  46. * Tests that when you show the dialog, the input field has the correct
  47. * sample text in it.
  48. */
  49. function testShow() {
  50. mockOkHandler.$replay();
  51. createAndShow();
  52. assertEquals('Input field has incorrect sample text',
  53. 'Hello, world!',
  54. dialog.input_.value);
  55. mockOkHandler.$verify();
  56. }
  57. /**
  58. * Tests that clicking OK dispatches an event carying the entered message.
  59. */
  60. function testOk() {
  61. expectOk(CUSTOM_MESSAGE);
  62. mockOkHandler.$replay();
  63. createAndShow();
  64. dialog.input_.value = CUSTOM_MESSAGE;
  65. goog.testing.events.fireClickSequence(dialog.getOkButtonElement());
  66. mockOkHandler.$verify(); // Verifies OK is dispatched with correct message.
  67. }