clicktoeditwrapper_test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.ClickToEditWrapperTest');
  15. goog.setTestOnly('goog.editor.ClickToEditWrapperTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.Range');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.editor.ClickToEditWrapper');
  20. goog.require('goog.editor.SeamlessField');
  21. goog.require('goog.testing.MockClock');
  22. goog.require('goog.testing.events');
  23. goog.require('goog.testing.jsunit');
  24. var FIELD;
  25. var CLOCK;
  26. var HTML;
  27. function setUp() {
  28. HTML = goog.dom.getElement('root').innerHTML;
  29. CLOCK = new goog.testing.MockClock(true);
  30. // The following 3 lines are to get around an IE bug where it says
  31. // 'Incompatible markup pointers for this operation'.
  32. // Must be done in the setup, not teardown, or else it won't take effect for
  33. // the first test that is run, or any test that runs immediately after a
  34. // "breaking async" message from the jsunit framework.
  35. goog.dom.Range.clearSelection();
  36. window.blur();
  37. window.focus();
  38. }
  39. /** @param {boolean=} opt_isBlended */
  40. function setUpField(opt_isBlended) {
  41. FIELD = opt_isBlended ? new goog.editor.SeamlessField('testField') :
  42. new goog.editor.SeamlessField('testField');
  43. (new goog.editor.ClickToEditWrapper(FIELD));
  44. goog.dom.Range.clearSelection();
  45. }
  46. function tearDown() {
  47. if (FIELD) {
  48. FIELD.dispose();
  49. }
  50. CLOCK.dispose();
  51. goog.dom.getElement('root').innerHTML = HTML;
  52. }
  53. function testClickToEdit(opt_isBlended) {
  54. setUpField(opt_isBlended);
  55. var text = goog.dom.getElement('testField').firstChild;
  56. goog.dom.Range.createFromNodes(text, 4, text, 8).select();
  57. goog.testing.events.fireClickSequence(text.parentNode);
  58. assertFalse(
  59. 'Field should not be made editable immediately after clicking',
  60. FIELD.isLoaded());
  61. CLOCK.tick(1);
  62. assertTrue('Field should be editable', FIELD.isLoaded());
  63. var dom = FIELD.getEditableDomHelper();
  64. var selection = goog.dom.Range.createFromWindow(dom.getWindow());
  65. var body = FIELD.getElement();
  66. text = body.firstChild;
  67. assertEquals('Wrong start node', text, selection.getStartNode());
  68. assertEquals('Wrong end node', text, selection.getEndNode());
  69. assertEquals('Wrong start offset', 4, selection.getStartOffset());
  70. assertEquals('Wrong end offset', 8, selection.getEndOffset());
  71. }
  72. function testBlendedClickToEdit() {
  73. testClickToEdit(true);
  74. }
  75. function testClickToEditWithAnchor(opt_isBlended) {
  76. setUpField(opt_isBlended);
  77. goog.dom.getElement('testAnchor').focus();
  78. goog.testing.events.fireClickSequence(goog.dom.getElement('testAnchor'));
  79. CLOCK.tick(1);
  80. assertTrue('Field should be editable', FIELD.isLoaded());
  81. var dom = FIELD.getEditableDomHelper();
  82. var selection = goog.dom.Range.createFromWindow(dom.getWindow());
  83. // TODO(brndn): the location of the cursor is not yet specified by the W3C
  84. // Editing APIs (https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html).
  85. // See b/15678403. IE and some webkit (all Safari, and up to Chrome 57)
  86. // return the end of the previous text node, while other browsers return
  87. // the start of the next node.
  88. var body = FIELD.getElement();
  89. var text = body.firstChild;
  90. var link = dom.getElementsByTagNameAndClass(goog.dom.TagName.A, null, body)[0]
  91. .firstChild;
  92. if (selection.getStartNode() == text) {
  93. assertEquals('Wrong start node', text, selection.getStartNode());
  94. assertEquals('Wrong start offset', 17, selection.getStartOffset());
  95. assertEquals('Wrong end node', text, selection.getEndNode());
  96. assertEquals('Wrong end offset', 17, selection.getEndOffset());
  97. } else {
  98. assertEquals('Wrong start node', link, selection.getStartNode());
  99. assertEquals('Wrong start offset', 0, selection.getStartOffset());
  100. assertEquals('Wrong end node', link, selection.getEndNode());
  101. assertEquals('Wrong end offset', 0, selection.getEndOffset());
  102. }
  103. }
  104. function testBlendedClickToEditWithAnchor() {
  105. testClickToEditWithAnchor(true);
  106. }