browserfeature_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.BrowserFeatureTest');
  15. goog.setTestOnly('goog.editor.BrowserFeatureTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.Range');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.editor.BrowserFeature');
  20. goog.require('goog.testing.ExpectedFailures');
  21. goog.require('goog.testing.jsunit');
  22. var expectedFailures;
  23. function setUpPage() {
  24. expectedFailures = new goog.testing.ExpectedFailures();
  25. }
  26. function tearDown() {
  27. var root = goog.dom.getElement('root');
  28. goog.dom.removeChildren(root);
  29. expectedFailures.handleTearDown();
  30. }
  31. function testEmptyNodeNormalization() {
  32. var root = goog.dom.getElement('root');
  33. goog.dom.appendChild(root, goog.dom.createTextNode('text'));
  34. var textNode = root.firstChild;
  35. textNode.splitText(0);
  36. root.normalize();
  37. assertEquals(
  38. 'NORMALIZE_CORRUPTS_EMPTY_TEXT_NODES incorrect for ' +
  39. navigator.userAgent,
  40. goog.editor.BrowserFeature.NORMALIZE_CORRUPTS_EMPTY_TEXT_NODES,
  41. textNode.parentNode == null);
  42. }
  43. function testLeavesPWhenRemovingLists() {
  44. if (!goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
  45. return;
  46. }
  47. var root = goog.dom.getElement('root');
  48. goog.dom.removeChildren(root);
  49. root.innerHTML = '<div>foo</div>';
  50. goog.dom.Range.createFromNodeContents(root.firstChild.firstChild).select();
  51. document.execCommand('insertorderedlist', false, true);
  52. document.execCommand('insertorderedlist', false, true);
  53. assertEquals(
  54. 'LEAVES_P_WHEN_REMOVING_LISTS incorrect for ' + navigator.userAgent,
  55. goog.editor.BrowserFeature.LEAVES_P_WHEN_REMOVING_LISTS,
  56. !!goog.dom.getElementsByTagName(goog.dom.TagName.P, root).length);
  57. }
  58. function testActiveElement() {
  59. var root = goog.dom.getElement('root');
  60. var div = goog.dom.createElement(goog.dom.TagName.DIV);
  61. root.appendChild(div);
  62. div.tabIndex = 0;
  63. div.focus();
  64. expectedFailures.expectFailureFor(
  65. !goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT);
  66. try {
  67. assertEquals(
  68. 'document.activeElement should be the created div', div,
  69. document.activeElement);
  70. } catch (e) {
  71. expectedFailures.handleException(e);
  72. }
  73. }
  74. function testNormalizeCorruption() {
  75. var root = goog.dom.getElement('testNormalizeCorruption');
  76. var textNode = root.firstChild;
  77. textNode.splitText(0);
  78. var secondTextNode = textNode.nextSibling;
  79. root.normalize();
  80. expectedFailures.expectFailureFor(
  81. goog.editor.BrowserFeature.NORMALIZE_CORRUPTS_EMPTY_TEXT_NODES);
  82. try {
  83. assertEquals(
  84. 'text node should not be corrupted', textNode, root.firstChild);
  85. } catch (e) {
  86. expectedFailures.handleException(e);
  87. expectedFailures.expectFailureFor(
  88. goog.editor.BrowserFeature.NORMALIZE_CORRUPTS_ALL_TEXT_NODES);
  89. try {
  90. assertEquals(
  91. 'first text node should be corrupted and replaced by sibling',
  92. secondTextNode, root.firstChild);
  93. } catch (e) {
  94. expectedFailures.handleException(e);
  95. }
  96. }
  97. }