legacyconversions_test.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 Unit tests for goog.html.legacyconversions.
  16. */
  17. goog.provide('goog.html.legacyconversionsTest');
  18. goog.require('goog.html.SafeHtml');
  19. goog.require('goog.html.SafeStyle');
  20. goog.require('goog.html.SafeStyleSheet');
  21. goog.require('goog.html.SafeUrl');
  22. goog.require('goog.html.TrustedResourceUrl');
  23. goog.require('goog.html.legacyconversions');
  24. goog.require('goog.testing.jsunit');
  25. goog.setTestOnly('goog.html.legacyconversionsTest');
  26. function testSafeHtmlFromString() {
  27. var html = '<div>irrelevant</div>';
  28. var safeHtml = goog.html.legacyconversions.safeHtmlFromString(html);
  29. assertEquals(html, goog.html.SafeHtml.unwrap(safeHtml));
  30. assertFunctionReports(goog.html.legacyconversions.safeHtmlFromString);
  31. }
  32. function testSafeStyleFromString() {
  33. var style = 'color: red; width: 1em;';
  34. var safeStyle = goog.html.legacyconversions.safeStyleFromString(style);
  35. assertEquals(style, goog.html.SafeStyle.unwrap(safeStyle));
  36. assertFunctionReports(goog.html.legacyconversions.safeStyleFromString);
  37. }
  38. function testSafeStyleSheetFromString() {
  39. var styleSheet = 'P.special { color: red; background: url(http://test); }';
  40. var safeStyleSheet =
  41. goog.html.legacyconversions.safeStyleSheetFromString(styleSheet);
  42. assertEquals(styleSheet, goog.html.SafeStyleSheet.unwrap(safeStyleSheet));
  43. assertFunctionReports(goog.html.legacyconversions.safeStyleSheetFromString);
  44. }
  45. function testSafeUrlFromString() {
  46. var url = 'https://www.google.com';
  47. var safeUrl = goog.html.legacyconversions.safeUrlFromString(url);
  48. assertEquals(url, goog.html.SafeUrl.unwrap(safeUrl));
  49. assertFunctionReports(goog.html.legacyconversions.safeUrlFromString);
  50. }
  51. function testTrustedResourceUrlFromString() {
  52. var url = 'https://www.google.com/script.js';
  53. var trustedResourceUrl =
  54. goog.html.legacyconversions.trustedResourceUrlFromString(url);
  55. assertEquals(url, goog.html.TrustedResourceUrl.unwrap(trustedResourceUrl));
  56. assertFunctionReports(
  57. goog.html.legacyconversions.trustedResourceUrlFromString);
  58. }
  59. /**
  60. * Asserts that conversionFunction calls the report callback.
  61. * @param {function(string) : *} conversionFunction
  62. */
  63. function assertFunctionReports(conversionFunction) {
  64. var reported = false;
  65. try {
  66. goog.html.legacyconversions.setReportCallback(function() {
  67. reported = true;
  68. });
  69. conversionFunction('irrelevant');
  70. assertTrue('Expected legacy conversion to be reported.', reported);
  71. } finally {
  72. goog.html.legacyconversions.setReportCallback(goog.nullFunction);
  73. }
  74. }