asserts_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2017 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.dom.assertsTest');
  15. goog.setTestOnly('goog.dom.assertsTest');
  16. goog.require('goog.dom.asserts');
  17. goog.require('goog.testing.StrictMock');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.userAgent');
  20. function testAssertIsLocation() {
  21. assertNotThrows(function() {
  22. goog.dom.asserts.assertIsLocation(window.location);
  23. });
  24. // Ad-hoc mock objects are allowed.
  25. var o = {foo: 'bar'};
  26. assertNotThrows(function() {
  27. goog.dom.asserts.assertIsLocation(o);
  28. });
  29. // So are fancy mocks.
  30. var mock = new goog.testing.StrictMock(window.location);
  31. assertNotThrows(function() {
  32. goog.dom.asserts.assertIsLocation(mock);
  33. });
  34. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  35. var linkElement = document.createElement('LINK');
  36. var ex = assertThrows(function() {
  37. goog.dom.asserts.assertIsLocation(linkElement);
  38. });
  39. assertContains('Argument is not a Location', ex.message);
  40. }
  41. }
  42. function testAssertIsHtmlAnchorElement() {
  43. var anchorElement = document.createElement('A');
  44. assertNotThrows(function() {
  45. goog.dom.asserts.assertIsHTMLAnchorElement(anchorElement);
  46. });
  47. // Ad-hoc mock objects are allowed.
  48. var o = {foo: 'bar'};
  49. assertNotThrows(function() {
  50. goog.dom.asserts.assertIsHTMLAnchorElement(o);
  51. });
  52. // So are fancy mocks.
  53. var mock = new goog.testing.StrictMock(anchorElement);
  54. assertNotThrows(function() {
  55. goog.dom.asserts.assertIsHTMLAnchorElement(mock);
  56. });
  57. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  58. var otherElement = document.createElement('LINK');
  59. var ex = assertThrows(function() {
  60. goog.dom.asserts.assertIsHTMLAnchorElement(otherElement);
  61. });
  62. assertContains('Argument is not a HTMLAnchorElement', ex.message);
  63. }
  64. }
  65. function testAssertIsHtmlLinkElement() {
  66. var linkElement = document.createElement('LINK');
  67. assertNotThrows(function() {
  68. goog.dom.asserts.assertIsHTMLLinkElement(linkElement);
  69. });
  70. // Ad-hoc mock objects are allowed.
  71. var o = {foo: 'bar'};
  72. assertNotThrows(function() {
  73. goog.dom.asserts.assertIsHTMLLinkElement(o);
  74. });
  75. // So are fancy mocks.
  76. var mock = new goog.testing.StrictMock(linkElement);
  77. assertNotThrows(function() {
  78. goog.dom.asserts.assertIsHTMLLinkElement(mock);
  79. });
  80. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  81. var otherElement = document.createElement('A');
  82. var ex = assertThrows(function() {
  83. goog.dom.asserts.assertIsHTMLLinkElement(otherElement);
  84. });
  85. assertContains('Argument is not a HTMLLinkElement', ex.message);
  86. }
  87. }
  88. function testAssertIsHtmlImageElement() {
  89. var imgElement = document.createElement('IMG');
  90. assertNotThrows(function() {
  91. goog.dom.asserts.assertIsHTMLImageElement(imgElement);
  92. });
  93. // Ad-hoc mock objects are allowed.
  94. var o = {foo: 'bar'};
  95. assertNotThrows(function() {
  96. goog.dom.asserts.assertIsHTMLImageElement(o);
  97. });
  98. // So are fancy mocks.
  99. var mock = new goog.testing.StrictMock(imgElement);
  100. assertNotThrows(function() {
  101. goog.dom.asserts.assertIsHTMLImageElement(mock);
  102. });
  103. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  104. var otherElement = document.createElement('SCRIPT');
  105. var ex = assertThrows(function() {
  106. goog.dom.asserts.assertIsHTMLImageElement(otherElement);
  107. });
  108. assertContains('Argument is not a HTMLImageElement', ex.message);
  109. }
  110. }
  111. function testAssertIsHtmlEmbedElement() {
  112. var el = document.createElement('EMBED');
  113. assertNotThrows(function() {
  114. goog.dom.asserts.assertIsHTMLEmbedElement(el);
  115. });
  116. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  117. var otherElement = document.createElement('SCRIPT');
  118. var ex = assertThrows(function() {
  119. goog.dom.asserts.assertIsHTMLEmbedElement(otherElement);
  120. });
  121. assertContains('Argument is not a HTMLEmbedElement', ex.message);
  122. }
  123. }
  124. function testAssertIsHtmlFrameElement() {
  125. var el = document.createElement('FRAME');
  126. assertNotThrows(function() {
  127. goog.dom.asserts.assertIsHTMLFrameElement(el);
  128. });
  129. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  130. var otherElement = document.createElement('SCRIPT');
  131. var ex = assertThrows(function() {
  132. goog.dom.asserts.assertIsHTMLFrameElement(otherElement);
  133. });
  134. assertContains('Argument is not a HTMLFrameElement', ex.message);
  135. }
  136. }
  137. function testAssertIsHtmlIFrameElement() {
  138. var el = document.createElement('IFRAME');
  139. assertNotThrows(function() {
  140. goog.dom.asserts.assertIsHTMLIFrameElement(el);
  141. });
  142. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  143. var otherElement = document.createElement('SCRIPT');
  144. var ex = assertThrows(function() {
  145. goog.dom.asserts.assertIsHTMLIFrameElement(otherElement);
  146. });
  147. assertContains('Argument is not a HTMLIFrameElement', ex.message);
  148. }
  149. }
  150. function testAssertIsHtmlObjectElement() {
  151. var el = document.createElement('OBJECT');
  152. assertNotThrows(function() {
  153. goog.dom.asserts.assertIsHTMLObjectElement(el);
  154. });
  155. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  156. var otherElement = document.createElement('SCRIPT');
  157. var ex = assertThrows(function() {
  158. goog.dom.asserts.assertIsHTMLObjectElement(otherElement);
  159. });
  160. assertContains('Argument is not a HTMLObjectElement', ex.message);
  161. }
  162. }
  163. function testAssertIsHtmlScriptElement() {
  164. var el = document.createElement('SCRIPT');
  165. assertNotThrows(function() {
  166. goog.dom.asserts.assertIsHTMLScriptElement(el);
  167. });
  168. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  169. var otherElement = document.createElement('IMG');
  170. var ex = assertThrows(function() {
  171. goog.dom.asserts.assertIsHTMLScriptElement(otherElement);
  172. });
  173. assertContains('Argument is not a HTMLScriptElement', ex.message);
  174. }
  175. }
  176. function testInOtherWindow() {
  177. var iframe = document.createElement('IFRAME');
  178. document.body.appendChild(iframe);
  179. var el = iframe.contentWindow.document.createElement('SCRIPT');
  180. assertNotThrows(function() {
  181. goog.dom.asserts.assertIsHTMLScriptElement(el);
  182. });
  183. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) {
  184. var ex = assertThrows(function() {
  185. goog.dom.asserts.assertIsHTMLImageElement(el);
  186. });
  187. assertContains('Argument is not a HTMLImageElement', ex.message);
  188. }
  189. document.body.removeChild(iframe);
  190. }