testhelper_test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.testing.editor.TestHelperTest');
  15. goog.setTestOnly('goog.testing.editor.TestHelperTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.editor.node');
  19. goog.require('goog.testing.TestCase');
  20. goog.require('goog.testing.editor.TestHelper');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.userAgent');
  23. var root;
  24. var helper;
  25. function setUp() {
  26. // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
  27. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
  28. root = goog.dom.getElement('root');
  29. goog.dom.removeChildren(root);
  30. helper = new goog.testing.editor.TestHelper(root);
  31. }
  32. function tearDown() {
  33. helper.dispose();
  34. }
  35. function testSetRoot() {
  36. helper.setRoot(goog.dom.getElement('root2'));
  37. helper.assertHtmlMatches('Root 2');
  38. }
  39. function testSetupEditableElement() {
  40. helper.setUpEditableElement();
  41. assertTrue(goog.editor.node.isEditableContainer(root));
  42. }
  43. function testTearDownEditableElement() {
  44. helper.setUpEditableElement();
  45. assertTrue(goog.editor.node.isEditableContainer(root));
  46. helper.tearDownEditableElement();
  47. assertFalse(goog.editor.node.isEditableContainer(root));
  48. }
  49. function testFindNode() {
  50. // Test the easiest case.
  51. root.innerHTML = 'a<br>b';
  52. assertEquals(helper.findTextNode('a'), root.firstChild);
  53. assertEquals(helper.findTextNode('b'), root.lastChild);
  54. assertNull(helper.findTextNode('c'));
  55. }
  56. function testFindNodeDuplicate() {
  57. // Test duplicate.
  58. root.innerHTML = 'c<br>c';
  59. assertEquals(
  60. 'Should return first duplicate', helper.findTextNode('c'),
  61. root.firstChild);
  62. }
  63. function findNodeWithHierarchy() {
  64. // Test a more complicated hierarchy.
  65. root.innerHTML = '<div>a<p>b<span>c</span>d</p>e</div>';
  66. assertEquals(
  67. String(goog.dom.TagName.DIV),
  68. helper.findTextNode('a').parentNode.tagName);
  69. assertEquals(
  70. String(goog.dom.TagName.P), helper.findTextNode('b').parentNode.tagName);
  71. assertEquals(
  72. String(goog.dom.TagName.SPAN),
  73. helper.findTextNode('c').parentNode.tagName);
  74. assertEquals(
  75. String(goog.dom.TagName.P), helper.findTextNode('d').parentNode.tagName);
  76. assertEquals(
  77. String(goog.dom.TagName.DIV),
  78. helper.findTextNode('e').parentNode.tagName);
  79. }
  80. function setUpAssertHtmlMatches() {
  81. var tag1, tag2;
  82. if (goog.userAgent.EDGE_OR_IE) {
  83. tag1 = goog.dom.TagName.DIV;
  84. } else if (goog.userAgent.WEBKIT) {
  85. tag1 = goog.dom.TagName.P;
  86. tag2 = goog.dom.TagName.BR;
  87. } else if (goog.userAgent.GECKO) {
  88. tag1 = goog.dom.TagName.SPAN;
  89. tag2 = goog.dom.TagName.BR;
  90. }
  91. var parent = goog.dom.createDom(goog.dom.TagName.DIV);
  92. root.appendChild(parent);
  93. parent.style.fontSize = '2em';
  94. parent.style.display = 'none';
  95. if (goog.userAgent.EDGE_OR_IE || goog.userAgent.GECKO) {
  96. parent.appendChild(goog.dom.createTextNode('NonWebKitText'));
  97. }
  98. if (tag1) {
  99. var e1 = goog.dom.createDom(tag1);
  100. parent.appendChild(e1);
  101. parent = e1;
  102. }
  103. if (tag2) {
  104. parent.appendChild(goog.dom.createDom(tag2));
  105. }
  106. parent.appendChild(goog.dom.createTextNode('Text'));
  107. if (goog.userAgent.WEBKIT) {
  108. root.firstChild.appendChild(goog.dom.createTextNode('WebKitText'));
  109. }
  110. }
  111. function testAssertHtmlMatches() {
  112. setUpAssertHtmlMatches();
  113. helper.assertHtmlMatches(
  114. '<div style="display: none; font-size: 2em">' +
  115. '[[IE EDGE GECKO]]NonWebKitText<div class="IE EDGE"><p class="WEBKIT">' +
  116. '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
  117. '</div>[[WEBKIT]]WebKitText');
  118. }
  119. function testAssertHtmlMismatchText() {
  120. setUpAssertHtmlMatches();
  121. var e = assertThrows('Should fail due to mismatched text', function() {
  122. helper.assertHtmlMatches(
  123. '<div style="display: none; font-size: 2em">' +
  124. '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
  125. '<span class="GECKO"><br class="GECKO WEBKIT">Bad</span></p></div>' +
  126. '</div>[[WEBKIT]]Extra');
  127. });
  128. assertContains('Text should match', e.message);
  129. }
  130. function testAssertHtmlMismatchTag() {
  131. setUpAssertHtmlMatches();
  132. var e = assertThrows('Should fail due to mismatched tag', function() {
  133. helper.assertHtmlMatches(
  134. '<span style="display: none; font-size: 2em">[[IE EDGE GECKO]]' +
  135. 'NonWebKitText<div class="IE EDGE"><p class="WEBKIT">' +
  136. '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
  137. '</span>[[WEBKIT]]Extra');
  138. });
  139. assertContains('Tag names should match', e.message);
  140. }
  141. function testAssertHtmlMismatchStyle() {
  142. setUpAssertHtmlMatches();
  143. var e = assertThrows('Should fail due to mismatched style', function() {
  144. helper.assertHtmlMatches(
  145. '<div style="display: none; font-size: 3em">[[IE EDGE GECKO]]' +
  146. 'NonWebKitText<div class="IE EDGE"><p class="WEBKIT">' +
  147. '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
  148. '</div>[[WEBKIT]]Extra');
  149. });
  150. assertContains('Should have same styles', e.message);
  151. }
  152. function testAssertHtmlMismatchOptionalText() {
  153. setUpAssertHtmlMatches();
  154. var e = assertThrows('Should fail due to mismatched style', function() {
  155. helper.assertHtmlMatches(
  156. '<div style="display: none; font-size: 2em">' +
  157. '[[IE EDGE GECKO]]Bad<div class="IE EDGE"><p class="WEBKIT">' +
  158. '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
  159. '</div>[[WEBKIT]]Bad');
  160. });
  161. assertContains('Text should match', e.message);
  162. }