Debian Codegraph @

closure-library/closure/goog/html/legacyconversions_test.js

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