Debian Codegraph @

closure-library/closure/goog/net/jsloader_test.js

raw
1 // Copyright 2011 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 goog.provide('goog.net.jsloaderTest');
16 goog.setTestOnly('goog.net.jsloaderTest');
17
18 goog.require('goog.array');
19 goog.require('goog.dom');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.html.TrustedResourceUrl');
22 goog.require('goog.net.jsloader');
23 goog.require('goog.net.jsloader.ErrorCode');
24 goog.require('goog.string.Const');
25 goog.require('goog.testing.jsunit');
26
27
28 function setUp() {
29 goog.provide = goog.nullFunction;
30 }
31
32
33 function tearDown() {
34 // Remove all the fake scripts.
35 var scripts =
36 goog.array.clone(goog.dom.getElementsByTagName(goog.dom.TagName.SCRIPT));
37 for (var i = 0; i < scripts.length; i++) {
38 if (scripts[i].src.indexOf('testdata') != -1) {
39 goog.dom.removeNode(scripts[i]);
40 }
41 }
42 }
43
44
45 // Sunny day scenario for load function.
46 function testSafeLoad() {
47 window.test1 = null;
48 var testUrl = goog.html.TrustedResourceUrl.fromConstant(
49 goog.string.Const.from('testdata/jsloader_test1.js'));
50 var testUrlValue = goog.html.TrustedResourceUrl.unwrap(testUrl);
51 var result = goog.net.jsloader.safeLoad(testUrl);
52
53 return result.then(function() {
54 var script = result.defaultScope_.script_;
55
56 assertNotNull('script created', script);
57 assertEquals('encoding is utf-8', 'UTF-8', script.charset);
58
59 // Check that the URI matches ours.
60 assertTrue('server URI', script.src.indexOf(testUrlValue) >= 0);
61
62 // Check that the script was really loaded.
63 assertEquals('verification object', 'Test #1 loaded', window.test1);
64 });
65 }
66
67
68 // Sunny day scenario for safeLoadAndVerify function.
69 function testSafeLoadAndVerify() {
70 var testUrl = goog.html.TrustedResourceUrl.fromConstant(
71 goog.string.Const.from('testdata/jsloader_test2.js'));
72 var result = goog.net.jsloader.safeLoadAndVerify(testUrl, 'test2');
73
74 return result.then(function(verifyObj) {
75 // Check that the verification object has passed ok.
76 assertEquals('verification object', 'Test #2 loaded', verifyObj);
77 });
78 }
79
80
81 // What happens when the verification object is not set by the loaded script?
82 function testSafeLoadAndVerifyError() {
83 var testUrl = goog.html.TrustedResourceUrl.fromConstant(
84 goog.string.Const.from('testdata/jsloader_test2.js'));
85 var result = goog.net.jsloader.safeLoadAndVerify(testUrl, 'fake');
86
87 return result.then(fail, function(error) {
88 // Check that the error code is right.
89 assertEquals(
90 'verification error', goog.net.jsloader.ErrorCode.VERIFY_ERROR,
91 error.code);
92 });
93 }
94
95
96 // Tests that callers can cancel the deferred without error.
97 function testSafeLoadAndVerifyCanceled() {
98 var testUrl = goog.html.TrustedResourceUrl.fromConstant(
99 goog.string.Const.from('testdata/jsloader_test2.js'));
100 var result = goog.net.jsloader.safeLoadAndVerify(testUrl, 'test2');
101 result.cancel();
102 }
103
104
105 // Test the safeLoadMany function.
106 function testSafeLoadMany() {
107 window.test1 = null;
108 window.test4 = null;
109
110 // Load test #3 and then #1.
111 var testUrls1 = [
112 goog.html.TrustedResourceUrl.fromConstant(
113 goog.string.Const.from('testdata/jsloader_test3.js')),
114 goog.html.TrustedResourceUrl.fromConstant(
115 goog.string.Const.from('testdata/jsloader_test1.js'))
116 ];
117 var result = goog.net.jsloader.safeLoadMany(testUrls1);
118
119 window.test3Callback = function(msg) {
120 // Check that the 1st test was not loaded yet.
121 assertEquals('verification object', null, window.test1);
122
123 // Load test #4, which is supposed to wait for #1 to load.
124 var testUrls2 = [goog.html.TrustedResourceUrl.fromConstant(
125 goog.string.Const.from('testdata/jsloader_test4.js'))];
126 goog.net.jsloader.safeLoadMany(testUrls2);
127 };
128
129 window.test4Callback = function(msg) {
130 // Check that the 1st test was already loaded.
131 assertEquals('verification object', 'Test #1 loaded', window.test1);
132
133 // on last script loaded, set variable
134 window.test4 = msg;
135 };
136
137 return result.then(function() {
138 // verify that the last loaded script callback has executed
139 assertEquals('verification object', 'Test #4 loaded', window.test4);
140 });
141 }
142
143
144 // Test the load function with additional options.
145 function testLoadWithOptions() {
146 var testUrl = goog.html.TrustedResourceUrl.fromConstant(
147 goog.string.Const.from('testdata/jsloader_test1.js'));
148 var testUrlValue = goog.html.TrustedResourceUrl.unwrap(testUrl);
149 var options = {
150 attributes: {'data-attr1': 'enabled', 'data-attr2': 'disabled'},
151 timeout: undefined, // Use default
152 cleanupWhenDone: undefined, // Use default
153 document: undefined // Use default
154 };
155 var result = goog.net.jsloader.safeLoad(testUrl, options);
156
157 return result.then(function() {
158 var script = result.defaultScope_.script_;
159
160 // Check that the URI matches ours.
161 assertTrue('server URI', script.src.indexOf(testUrlValue) >= 0);
162
163 // Check that the attributes specified are set on the script tag.
164 assertEquals(
165 'attribute option not applied for attr1', 'enabled',
166 script.getAttribute('data-attr1'));
167 assertEquals(
168 'attribute option not applied for attr2', 'disabled',
169 script.getAttribute('data-attr2'));
170 });
171 }