UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

143 lines (110 loc) 3.89 kB
<!DOCTYPE html> <html> <!-- Copyright 2011 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by the Apache License, Version 2.0. See the COPYING file for details. --> <!-- --> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Closure Unit Tests - goog.net.jsloader</title> <script src="../base.js"></script> <script> goog.require('goog.async.Deferred'); goog.require('goog.dom'); goog.require('goog.net.jsloader'); goog.require('goog.testing.AsyncTestCase'); goog.require('goog.testing.jsunit'); </script> </head> <body> <script> // Initialize the AsyncTestCase. var testCase = goog.testing.AsyncTestCase.createAndInstall(document.title); testCase.stepTimeout = 5 * 1000; // 5 seconds testCase.setUp = function() { goog.provide = goog.nullFunction; } testCase.tearDown = function() { // Remove all the fake scripts. var scripts = goog.array.clone( document.getElementsByTagName('SCRIPT')); for (var i = 0; i < scripts.length; i++) { if (scripts[i].src.indexOf('testdata') != -1) { goog.dom.removeNode(scripts[i]); } } } // Sunny day scenario for load function. function testLoad() { testCase.waitForAsync('testLoad'); window.test1 = null; var testUrl = 'testdata/jsloader_test1.js'; var result = goog.net.jsloader.load(testUrl); result.addCallback(function() { testCase.continueTesting(); var script = result.defaultScope_.script_; assertNotNull('script created', script); assertEquals('encoding is utf-8', 'UTF-8', script.charset); // Check that the URI matches ours. assertTrue('server URI', script.src.indexOf(testUrl) >= 0); // Check that the script was really loaded. assertEquals('verification object', 'Test #1 loaded', window.test1); }); } // Sunny day scenario for loadAndVerify function. function testLoadAndVerify() { testCase.waitForAsync('testLoadAndVerify'); var testUrl = 'testdata/jsloader_test2.js'; var result = goog.net.jsloader.loadAndVerify(testUrl, 'test2'); result.addCallback(function(verifyObj) { testCase.continueTesting(); // Check that the verification object has passed ok. assertEquals('verification object', 'Test #2 loaded', verifyObj); }); } // What happens when the verification object is not set by the loaded script? function testLoadAndVerifyError() { testCase.waitForAsync('testLoadAndVerifyError'); var testUrl = 'testdata/jsloader_test2.js'; var result = goog.net.jsloader.loadAndVerify(testUrl, 'fake'); result.addErrback(function(error) { testCase.continueTesting(); // Check that the error code is right. assertEquals('verification error', goog.net.jsloader.ErrorCode.VERIFY_ERROR, error.code); }); } // Tests that callers can cancel the deferred without error. function testLoadAndVerifyCancelled() { var testUrl = 'testdata/jsloader_test2.js'; var result = goog.net.jsloader.loadAndVerify(testUrl, 'test2'); result.cancel(); } // Test the loadMany function. function testLoadMany() { testCase.waitForAsync('testLoadMany'); // Load test #3 and then #1. window.test1 = null; var testUrls1 = ['testdata/jsloader_test3.js', 'testdata/jsloader_test1.js']; goog.net.jsloader.loadMany(testUrls1); window.test3Callback = function(msg) { testCase.continueTesting(); // Check that the 1st test was not loaded yet. assertEquals('verification object', null, window.test1); // Load test #4, which is supposed to wait for #1 to load. testCase.waitForAsync('testLoadMany'); var testUrls2 = ['testdata/jsloader_test4.js']; goog.net.jsloader.loadMany(testUrls2); }; window.test4Callback = function(msg) { testCase.continueTesting(); // Check that the 1st test was already loaded. assertEquals('verification object', 'Test #1 loaded', window.test1); }; } </script> </body> </html>