spacestabhandler_test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.SpacesTabHandlerTest');
  15. goog.setTestOnly('goog.editor.plugins.SpacesTabHandlerTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.Range');
  18. goog.require('goog.editor.plugins.SpacesTabHandler');
  19. goog.require('goog.events.BrowserEvent');
  20. goog.require('goog.events.KeyCodes');
  21. goog.require('goog.functions');
  22. goog.require('goog.testing.StrictMock');
  23. goog.require('goog.testing.editor.FieldMock');
  24. goog.require('goog.testing.editor.TestHelper');
  25. goog.require('goog.testing.jsunit');
  26. var field;
  27. var editableField;
  28. var tabHandler;
  29. var testHelper;
  30. function setUp() {
  31. field = goog.dom.getElement('field');
  32. editableField = new goog.testing.editor.FieldMock();
  33. // Modal mode behavior tested in AbstractTabHandler.
  34. editableField.inModalMode = goog.functions.FALSE;
  35. testHelper = new goog.testing.editor.TestHelper(field);
  36. testHelper.setUpEditableElement();
  37. tabHandler = new goog.editor.plugins.SpacesTabHandler();
  38. tabHandler.registerFieldObject(editableField);
  39. }
  40. function tearDown() {
  41. editableField = null;
  42. testHelper.tearDownEditableElement();
  43. tabHandler.dispose();
  44. }
  45. function testSelectedTextIndent() {
  46. goog.dom.setTextContent(field, 'Test');
  47. var testText = field.firstChild;
  48. testHelper.select(testText, 0, testText, 4);
  49. var event = new goog.testing.StrictMock(goog.events.BrowserEvent);
  50. event.keyCode = goog.events.KeyCodes.TAB;
  51. event.shiftKey = false;
  52. editableField.stopChangeEvents(true, true);
  53. editableField.dispatchChange();
  54. editableField.dispatchSelectionChangeEvent();
  55. event.preventDefault();
  56. editableField.$replay();
  57. event.$replay();
  58. assertTrue(
  59. 'Event marked as handled',
  60. tabHandler.handleKeyboardShortcut(event, '', false));
  61. var contents = field.textContent || field.innerText;
  62. // Chrome doesn't treat \u00a0 as a space.
  63. assertTrue(
  64. 'Text should be replaced with 4 spaces but was: "' + contents + '"',
  65. /^(\s|\u00a0){4}$/.test(contents));
  66. editableField.$verify();
  67. event.$verify();
  68. }
  69. function testCursorIndent() {
  70. goog.dom.setTextContent(field, 'Test');
  71. var testText = field.firstChild;
  72. testHelper.select(testText, 2, testText, 2);
  73. var event = new goog.testing.StrictMock(goog.events.BrowserEvent);
  74. event.keyCode = goog.events.KeyCodes.TAB;
  75. event.shiftKey = false;
  76. editableField.stopChangeEvents(true, true);
  77. editableField.dispatchChange();
  78. editableField.dispatchSelectionChangeEvent();
  79. event.preventDefault();
  80. editableField.$replay();
  81. event.$replay();
  82. assertTrue(
  83. 'Event marked as handled',
  84. tabHandler.handleKeyboardShortcut(event, '', false));
  85. var contents = field.textContent || field.innerText;
  86. assertTrue(
  87. 'Expected contents "Te st" but was: "' + contents + '"',
  88. /Te[\s|\u00a0]{4}st/.test(contents));
  89. editableField.$verify();
  90. event.$verify();
  91. }
  92. function testShiftTabNoOp() {
  93. goog.dom.setTextContent(field, 'Test');
  94. range = goog.dom.Range.createFromNodeContents(field);
  95. range.collapse();
  96. range.select();
  97. var event = new goog.testing.StrictMock(goog.events.BrowserEvent);
  98. event.keyCode = goog.events.KeyCodes.TAB;
  99. event.shiftKey = true;
  100. event.preventDefault();
  101. editableField.$replay();
  102. event.$replay();
  103. assertTrue(
  104. 'Event marked as handled',
  105. tabHandler.handleKeyboardShortcut(event, '', false));
  106. var contents = field.textContent || field.innerText;
  107. assertEquals('Shift+tab should not change contents', 'Test', contents);
  108. editableField.$verify();
  109. event.$verify();
  110. }
  111. function testInListNoOp() {
  112. field.innerHTML = '<ul><li>Test</li></ul>';
  113. var testText = field.firstChild.firstChild.firstChild; // div ul li Test
  114. testHelper.select(testText, 2, testText, 2);
  115. var event = new goog.testing.StrictMock(goog.events.BrowserEvent);
  116. event.keyCode = goog.events.KeyCodes.TAB;
  117. event.shiftKey = false;
  118. editableField.$replay();
  119. event.$replay();
  120. assertFalse(
  121. 'Event must not be handled when selection inside list.',
  122. tabHandler.handleKeyboardShortcut(event, '', false));
  123. testHelper.assertHtmlMatches('<ul><li>Test</li></ul>');
  124. editableField.$verify();
  125. event.$verify();
  126. }
  127. function testContainsListNoOp() {
  128. field.innerHTML = '<ul><li>Test</li></ul>';
  129. var testText = field.firstChild.firstChild.firstChild; // div ul li Test
  130. testHelper.select(field.firstChild, 0, testText, 2);
  131. var event = new goog.testing.StrictMock(goog.events.BrowserEvent);
  132. event.keyCode = goog.events.KeyCodes.TAB;
  133. event.shiftKey = false;
  134. editableField.$replay();
  135. event.$replay();
  136. assertFalse(
  137. 'Event must not be handled when selection inside list.',
  138. tabHandler.handleKeyboardShortcut(event, '', false));
  139. testHelper.assertHtmlMatches('<ul><li>Test</li></ul>');
  140. editableField.$verify();
  141. event.$verify();
  142. }