jsloader_test.js 6.2 KB

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