headerformatter_test.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. goog.provide('goog.editor.plugins.HeaderFormatterTest');
  15. goog.setTestOnly('goog.editor.plugins.HeaderFormatterTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.editor.Command');
  18. goog.require('goog.editor.plugins.BasicTextFormatter');
  19. goog.require('goog.editor.plugins.HeaderFormatter');
  20. goog.require('goog.events.BrowserEvent');
  21. goog.require('goog.testing.LooseMock');
  22. goog.require('goog.testing.editor.FieldMock');
  23. goog.require('goog.testing.editor.TestHelper');
  24. goog.require('goog.testing.jsunit');
  25. goog.require('goog.userAgent');
  26. var field;
  27. var editableField;
  28. var headerFormatter;
  29. var btf;
  30. var testHelper;
  31. function setUpPage() {
  32. field = goog.dom.getElement('field');
  33. testHelper = new goog.testing.editor.TestHelper(field);
  34. }
  35. function setUp() {
  36. testHelper.setUpEditableElement();
  37. editableField = new goog.testing.editor.FieldMock();
  38. headerFormatter = new goog.editor.plugins.HeaderFormatter();
  39. headerFormatter.registerFieldObject(editableField);
  40. btf = new goog.editor.plugins.BasicTextFormatter();
  41. btf.registerFieldObject(editableField);
  42. }
  43. function tearDown() {
  44. editableField = null;
  45. headerFormatter.dispose();
  46. testHelper.tearDownEditableElement();
  47. }
  48. function testHeaderShortcuts() {
  49. goog.dom.setTextContent(field, 'myText');
  50. var textNode = field.firstChild;
  51. testHelper.select(textNode, 0, textNode, textNode.length);
  52. editableField.getElement();
  53. editableField.$anyTimes();
  54. editableField.$returns(field);
  55. editableField.getPluginByClassId('Bidi');
  56. editableField.$anyTimes();
  57. editableField.$returns(null);
  58. editableField.execCommand(
  59. goog.editor.Command.FORMAT_BLOCK,
  60. goog.editor.plugins.HeaderFormatter.HEADER_COMMAND.H1);
  61. // Bypass EditableField's execCommand and directly call
  62. // basicTextFormatter's. Future version of headerformatter will include
  63. // that code in its own execCommand.
  64. editableField.$does(function() {
  65. btf.execCommandInternal(
  66. goog.editor.plugins.BasicTextFormatter.COMMAND.FORMAT_BLOCK,
  67. goog.editor.plugins.HeaderFormatter.HEADER_COMMAND.H1);
  68. });
  69. var event = new goog.testing.LooseMock(goog.events.BrowserEvent);
  70. if (goog.userAgent.GECKO) {
  71. event.stopPropagation();
  72. }
  73. editableField.$replay();
  74. event.$replay();
  75. assertTrue(
  76. 'Event handled',
  77. headerFormatter.handleKeyboardShortcut(event, '1', true));
  78. assertEquals('Field contains a header', 'H1', field.firstChild.nodeName);
  79. editableField.$verify();
  80. event.$verify();
  81. }