aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/net/jsloader_test.html
blob: 0436abe677d9fc8a3b2916068d439e189a0b450e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!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);
  });
}


// 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>