dom_test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2009 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.domTest');
  15. goog.setTestOnly('goog.testing.editor.domTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.functions');
  19. goog.require('goog.testing.TestCase');
  20. goog.require('goog.testing.editor.dom');
  21. goog.require('goog.testing.jsunit');
  22. var root;
  23. var parentNode, childNode1, childNode2, childNode3;
  24. var first, middle, last;
  25. function setUpPage() {
  26. // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
  27. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
  28. root = goog.dom.getElement('root');
  29. }
  30. function tearDown() {
  31. goog.dom.removeChildren(root);
  32. }
  33. function setUpNonEmptyTests() {
  34. childNode1 = goog.dom.createElement(goog.dom.TagName.DIV);
  35. childNode2 = goog.dom.createElement(goog.dom.TagName.DIV);
  36. childNode3 = goog.dom.createElement(goog.dom.TagName.DIV);
  37. parentNode = goog.dom.createDom(
  38. goog.dom.TagName.DIV, null, childNode1, childNode2, childNode3);
  39. goog.dom.appendChild(root, parentNode);
  40. childNode1.appendChild(goog.dom.createTextNode('One'));
  41. childNode1.appendChild(goog.dom.createTextNode(''));
  42. childNode2.appendChild(goog.dom.createElement(goog.dom.TagName.BR));
  43. childNode2.appendChild(goog.dom.createTextNode('TwoA'));
  44. childNode2.appendChild(goog.dom.createTextNode('TwoB'));
  45. childNode2.appendChild(goog.dom.createElement(goog.dom.TagName.BR));
  46. childNode3.appendChild(goog.dom.createTextNode(''));
  47. childNode3.appendChild(goog.dom.createTextNode('Three'));
  48. }
  49. function testGetNextNonEmptyTextNode() {
  50. setUpNonEmptyTests();
  51. var nodeOne = goog.testing.editor.dom.getNextNonEmptyTextNode(parentNode);
  52. assertEquals(
  53. 'Should have found the next non-empty text node', 'One',
  54. nodeOne.nodeValue);
  55. var nodeTwoA = goog.testing.editor.dom.getNextNonEmptyTextNode(nodeOne);
  56. assertEquals(
  57. 'Should have found the next non-empty text node', 'TwoA',
  58. nodeTwoA.nodeValue);
  59. var nodeTwoB = goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoA);
  60. assertEquals(
  61. 'Should have found the next non-empty text node', 'TwoB',
  62. nodeTwoB.nodeValue);
  63. var nodeThree = goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoB);
  64. assertEquals(
  65. 'Should have found the next non-empty text node', 'Three',
  66. nodeThree.nodeValue);
  67. var nodeNull =
  68. goog.testing.editor.dom.getNextNonEmptyTextNode(nodeThree, parentNode);
  69. assertNull('Should not have found any non-empty text node', nodeNull);
  70. var nodeStop =
  71. goog.testing.editor.dom.getNextNonEmptyTextNode(nodeOne, childNode1);
  72. assertNull('Should have stopped before finding a node', nodeStop);
  73. var nodeBeforeStop =
  74. goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoA, childNode2);
  75. assertEquals(
  76. 'Should have found the next non-empty text node', 'TwoB',
  77. nodeBeforeStop.nodeValue);
  78. }
  79. function testGetPreviousNonEmptyTextNode() {
  80. setUpNonEmptyTests();
  81. var nodeThree =
  82. goog.testing.editor.dom.getPreviousNonEmptyTextNode(parentNode);
  83. assertEquals(
  84. 'Should have found the previous non-empty text node', 'Three',
  85. nodeThree.nodeValue);
  86. var nodeTwoB = goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeThree);
  87. assertEquals(
  88. 'Should have found the previous non-empty text node', 'TwoB',
  89. nodeTwoB.nodeValue);
  90. var nodeTwoA = goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoB);
  91. assertEquals(
  92. 'Should have found the previous non-empty text node', 'TwoA',
  93. nodeTwoA.nodeValue);
  94. var nodeOne = goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoA);
  95. assertEquals(
  96. 'Should have found the previous non-empty text node', 'One',
  97. nodeOne.nodeValue);
  98. var nodeNull =
  99. goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeOne, parentNode);
  100. assertNull('Should not have found any non-empty text node', nodeNull);
  101. var nodeStop = goog.testing.editor.dom.getPreviousNonEmptyTextNode(
  102. nodeThree, childNode3);
  103. assertNull('Should have stopped before finding a node', nodeStop);
  104. var nodeBeforeStop =
  105. goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoB, childNode2);
  106. assertEquals(
  107. 'Should have found the previous non-empty text node', 'TwoA',
  108. nodeBeforeStop.nodeValue);
  109. }
  110. function setUpAssertRangeBetweenText() {
  111. // Create the following structure: <[01]><[]><[23]>
  112. // Where <> delimits spans, [] delimits text nodes, 01 and 23 are text.
  113. // We will test all 10 positions in between 0 and 2. All should pass.
  114. first = goog.dom.createDom(goog.dom.TagName.SPAN, null, '01');
  115. middle = goog.dom.createElement(goog.dom.TagName.SPAN);
  116. var emptyTextNode = goog.dom.createTextNode('');
  117. goog.dom.appendChild(middle, emptyTextNode);
  118. last = goog.dom.createDom(goog.dom.TagName.SPAN, null, '23');
  119. goog.dom.appendChild(root, first);
  120. goog.dom.appendChild(root, middle);
  121. goog.dom.appendChild(root, last);
  122. }
  123. function createFakeRange(startNode, startOffset, opt_endNode, opt_endOffset) {
  124. opt_endNode = opt_endNode || startNode;
  125. opt_endOffset = opt_endOffset || startOffset;
  126. return {
  127. getStartNode: goog.functions.constant(startNode),
  128. getStartOffset: goog.functions.constant(startOffset),
  129. getEndNode: goog.functions.constant(opt_endNode),
  130. getEndOffset: goog.functions.constant(opt_endOffset)
  131. };
  132. }
  133. function testAssertRangeBetweenText0() {
  134. setUpAssertRangeBetweenText();
  135. goog.testing.editor.dom.assertRangeBetweenText(
  136. '0', '1', createFakeRange(first.firstChild, 1));
  137. }
  138. function testAssertRangeBetweenText1() {
  139. setUpAssertRangeBetweenText();
  140. goog.testing.editor.dom.assertRangeBetweenText(
  141. '1', '2', createFakeRange(first.firstChild, 2));
  142. }
  143. function testAssertRangeBetweenText2() {
  144. setUpAssertRangeBetweenText();
  145. goog.testing.editor.dom.assertRangeBetweenText(
  146. '1', '2', createFakeRange(first, 1));
  147. }
  148. function testAssertRangeBetweenText3() {
  149. setUpAssertRangeBetweenText();
  150. goog.testing.editor.dom.assertRangeBetweenText(
  151. '1', '2', createFakeRange(root, 1));
  152. }
  153. function testAssertRangeBetweenText4() {
  154. setUpAssertRangeBetweenText();
  155. goog.testing.editor.dom.assertRangeBetweenText(
  156. '1', '2', createFakeRange(middle, 0));
  157. }
  158. function testAssertRangeBetweenText5() {
  159. setUpAssertRangeBetweenText();
  160. goog.testing.editor.dom.assertRangeBetweenText(
  161. '1', '2', createFakeRange(middle.firstChild, 0));
  162. }
  163. function testAssertRangeBetweenText6() {
  164. setUpAssertRangeBetweenText();
  165. goog.testing.editor.dom.assertRangeBetweenText(
  166. '1', '2', createFakeRange(middle, 1));
  167. }
  168. function testAssertRangeBetweenText7() {
  169. setUpAssertRangeBetweenText();
  170. goog.testing.editor.dom.assertRangeBetweenText(
  171. '1', '2', createFakeRange(root, 2));
  172. }
  173. function testAssertRangeBetweenText8() {
  174. setUpAssertRangeBetweenText();
  175. goog.testing.editor.dom.assertRangeBetweenText(
  176. '1', '2', createFakeRange(last, 0));
  177. }
  178. function testAssertRangeBetweenText9() {
  179. setUpAssertRangeBetweenText();
  180. goog.testing.editor.dom.assertRangeBetweenText(
  181. '1', '2', createFakeRange(last.firstChild, 0));
  182. }
  183. function testAssertRangeBetweenTextBefore() {
  184. setUpAssertRangeBetweenText();
  185. // Test that it works when the cursor is at the beginning of all text.
  186. goog.testing.editor.dom.assertRangeBetweenText(
  187. '', '0', createFakeRange(first.firstChild, 0),
  188. root); // Restrict to root div so it won't find /n's and script.
  189. }
  190. function testAssertRangeBetweenTextAfter() {
  191. setUpAssertRangeBetweenText();
  192. // Test that it works when the cursor is at the end of all text.
  193. goog.testing.editor.dom.assertRangeBetweenText(
  194. '3', '', createFakeRange(last.firstChild, 2),
  195. root); // Restrict to root div so it won't find /n's and script.
  196. }
  197. function testAssertRangeBetweenTextFail1() {
  198. setUpAssertRangeBetweenText();
  199. var e = assertThrows('assertRangeBetweenText should have failed', function() {
  200. goog.testing.editor.dom.assertRangeBetweenText(
  201. '1', '3', createFakeRange(first.firstChild, 2));
  202. });
  203. assertContains(
  204. 'Assert reason incorrect', 'Expected <3> after range but found <23>',
  205. e.message);
  206. }
  207. function testAssertRangeBetweenTextFail2() {
  208. setUpAssertRangeBetweenText();
  209. var e = assertThrows('assertRangeBetweenText should have failed', function() {
  210. goog.testing.editor.dom.assertRangeBetweenText(
  211. '1', '2', createFakeRange(first.firstChild, 2, last.firstChild, 1));
  212. });
  213. assertContains(
  214. 'Assert reason incorrect', 'Expected <2> after range but found <3>',
  215. e.message);
  216. }
  217. function testAssertRangeBetweenTextBeforeFail() {
  218. setUpAssertRangeBetweenText();
  219. // Test that it gives the right message when the cursor is at the beginning
  220. // of all text but you're expecting something before it.
  221. var e = assertThrows('assertRangeBetweenText should have failed', function() {
  222. goog.testing.editor.dom.assertRangeBetweenText(
  223. '-1', '0', createFakeRange(first.firstChild, 0),
  224. root); // Restrict to root div so it won't find /n's and script.
  225. });
  226. assertContains(
  227. 'Assert reason incorrect', 'Expected <-1> before range but found nothing',
  228. e.message);
  229. }
  230. function testAssertRangeBetweenTextAfterFail() {
  231. setUpAssertRangeBetweenText();
  232. // Test that it gives the right message when the cursor is at the end
  233. // of all text but you're expecting something after it.
  234. var e = assertThrows('assertRangeBetweenText should have failed', function() {
  235. goog.testing.editor.dom.assertRangeBetweenText(
  236. '3', '4', createFakeRange(last.firstChild, 2),
  237. root); // Restrict to root div so it won't find /n's and script.
  238. });
  239. assertContains(
  240. 'Assert reason incorrect', 'Expected <4> after range but found nothing',
  241. e.message);
  242. }