testing.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2013 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. /**
  15. * @fileoverview Utilities to create arbitrary values of goog.html types for
  16. * testing purposes. These utility methods perform no validation, and the
  17. * resulting instances may violate type contracts.
  18. *
  19. * These methods are useful when types are constructed in a manner where using
  20. * the production API is too inconvenient. Please do use the production API
  21. * whenever possible; there is value in having tests reflect common usage and it
  22. * avoids, by design, non-contract complying instances from being created.
  23. */
  24. goog.provide('goog.html.testing');
  25. goog.setTestOnly();
  26. goog.require('goog.html.SafeHtml');
  27. goog.require('goog.html.SafeScript');
  28. goog.require('goog.html.SafeStyle');
  29. goog.require('goog.html.SafeStyleSheet');
  30. goog.require('goog.html.SafeUrl');
  31. goog.require('goog.html.TrustedResourceUrl');
  32. goog.require('goog.testing.mockmatchers.ArgumentMatcher');
  33. /**
  34. * Creates a SafeHtml wrapping the given value. No validation is performed.
  35. *
  36. * This function is for use in tests only and must never be used in production
  37. * code.
  38. *
  39. * @param {string} html The string to wrap into a SafeHtml.
  40. * @param {?goog.i18n.bidi.Dir=} opt_dir The optional directionality of the
  41. * SafeHtml to be constructed. A null or undefined value signifies an
  42. * unknown directionality.
  43. * @return {!goog.html.SafeHtml}
  44. */
  45. goog.html.testing.newSafeHtmlForTest = function(html, opt_dir) {
  46. return goog.html.SafeHtml.createSafeHtmlSecurityPrivateDoNotAccessOrElse(
  47. html, (opt_dir == undefined ? null : opt_dir));
  48. };
  49. /**
  50. * Creates a SafeScript wrapping the given value. No validation is performed.
  51. *
  52. * This function is for use in tests only and must never be used in production
  53. * code.
  54. *
  55. * @param {string} script The string to wrap into a SafeScript.
  56. * @return {!goog.html.SafeScript}
  57. */
  58. goog.html.testing.newSafeScriptForTest = function(script) {
  59. return goog.html.SafeScript.createSafeScriptSecurityPrivateDoNotAccessOrElse(
  60. script);
  61. };
  62. /**
  63. * Creates a SafeStyle wrapping the given value. No validation is performed.
  64. *
  65. * This function is for use in tests only and must never be used in production
  66. * code.
  67. *
  68. * @param {string} style String to wrap into a SafeStyle.
  69. * @return {!goog.html.SafeStyle}
  70. */
  71. goog.html.testing.newSafeStyleForTest = function(style) {
  72. return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
  73. style);
  74. };
  75. /**
  76. * Creates a SafeStyleSheet wrapping the given value. No validation is
  77. * performed.
  78. *
  79. * This function is for use in tests only and must never be used in production
  80. * code.
  81. *
  82. * @param {string} styleSheet String to wrap into a SafeStyleSheet.
  83. * @return {!goog.html.SafeStyleSheet}
  84. */
  85. goog.html.testing.newSafeStyleSheetForTest = function(styleSheet) {
  86. return goog.html.SafeStyleSheet
  87. .createSafeStyleSheetSecurityPrivateDoNotAccessOrElse(styleSheet);
  88. };
  89. /**
  90. * Creates a SafeUrl wrapping the given value. No validation is performed.
  91. *
  92. * This function is for use in tests only and must never be used in production
  93. * code.
  94. *
  95. * @param {string} url String to wrap into a SafeUrl.
  96. * @return {!goog.html.SafeUrl}
  97. */
  98. goog.html.testing.newSafeUrlForTest = function(url) {
  99. return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(url);
  100. };
  101. /**
  102. * Creates a TrustedResourceUrl wrapping the given value. No validation is
  103. * performed.
  104. *
  105. * This function is for use in tests only and must never be used in production
  106. * code.
  107. *
  108. * @param {string} url String to wrap into a TrustedResourceUrl.
  109. * @return {!goog.html.TrustedResourceUrl}
  110. */
  111. goog.html.testing.newTrustedResourceUrlForTest = function(url) {
  112. return goog.html.TrustedResourceUrl
  113. .createTrustedResourceUrlSecurityPrivateDoNotAccessOrElse(url);
  114. };
  115. /**
  116. * Creates an argument matcher for SafeHtml.
  117. * @param {string|!goog.html.SafeHtml} expected
  118. * @return {!goog.testing.mockmatchers.ArgumentMatcher}
  119. */
  120. goog.html.testing.matchSafeHtml = function(expected) {
  121. if (expected instanceof goog.html.SafeHtml) {
  122. expected = goog.html.SafeHtml.unwrap(expected);
  123. }
  124. return new goog.testing.mockmatchers.ArgumentMatcher(function(actual) {
  125. return goog.html.SafeHtml.unwrap(actual) == expected;
  126. });
  127. };
  128. /**
  129. * Creates an argument matcher for SafeScript.
  130. * @param {string|!goog.html.SafeScript} expected
  131. * @return {!goog.testing.mockmatchers.ArgumentMatcher}
  132. */
  133. goog.html.testing.matchSafeScript = function(expected) {
  134. if (expected instanceof goog.html.SafeScript) {
  135. expected = goog.html.SafeScript.unwrap(expected);
  136. }
  137. return new goog.testing.mockmatchers.ArgumentMatcher(function(actual) {
  138. return goog.html.SafeScript.unwrap(actual) == expected;
  139. });
  140. };
  141. /**
  142. * Creates an argument matcher for SafeStyle.
  143. * @param {string|!goog.html.SafeStyle} expected
  144. * @return {!goog.testing.mockmatchers.ArgumentMatcher}
  145. */
  146. goog.html.testing.matchSafeStyle = function(expected) {
  147. if (expected instanceof goog.html.SafeStyle) {
  148. expected = goog.html.SafeStyle.unwrap(expected);
  149. }
  150. return new goog.testing.mockmatchers.ArgumentMatcher(function(actual) {
  151. return goog.html.SafeStyle.unwrap(actual) == expected;
  152. });
  153. };
  154. /**
  155. * Creates an argument matcher for SafeStyleSheet.
  156. * @param {string|!goog.html.SafeStyleSheet} expected
  157. * @return {!goog.testing.mockmatchers.ArgumentMatcher}
  158. */
  159. goog.html.testing.matchSafeStyleSheet = function(expected) {
  160. if (expected instanceof goog.html.SafeStyleSheet) {
  161. expected = goog.html.SafeStyleSheet.unwrap(expected);
  162. }
  163. return new goog.testing.mockmatchers.ArgumentMatcher(function(actual) {
  164. return goog.html.SafeStyleSheet.unwrap(actual) == expected;
  165. });
  166. };
  167. /**
  168. * Creates an argument matcher for SafeUrl.
  169. * @param {string|!goog.html.SafeUrl} expected
  170. * @return {!goog.testing.mockmatchers.ArgumentMatcher}
  171. */
  172. goog.html.testing.matchSafeUrl = function(expected) {
  173. if (expected instanceof goog.html.SafeUrl) {
  174. expected = goog.html.SafeUrl.unwrap(expected);
  175. }
  176. return new goog.testing.mockmatchers.ArgumentMatcher(function(actual) {
  177. return goog.html.SafeUrl.unwrap(actual) == expected;
  178. });
  179. };
  180. /**
  181. * Creates an argument matcher for TrustedResourceUrl.
  182. * @param {string|!goog.html.TrustedResourceUrl} expected
  183. * @return {!goog.testing.mockmatchers.ArgumentMatcher}
  184. */
  185. goog.html.testing.matchTrustedResourceUrl = function(expected) {
  186. if (expected instanceof goog.html.TrustedResourceUrl) {
  187. expected = goog.html.TrustedResourceUrl.unwrap(expected);
  188. }
  189. return new goog.testing.mockmatchers.ArgumentMatcher(function(actual) {
  190. return goog.html.TrustedResourceUrl.unwrap(actual) == expected;
  191. });
  192. };