Debian Codegraph @

closure-library/closure/goog/module/moduleinfo_test.js

raw
1 // Copyright 2008 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.module.ModuleInfoTest');
16 goog.setTestOnly('goog.module.ModuleInfoTest');
17
18 goog.require('goog.module.BaseModule');
19 goog.require('goog.module.ModuleInfo');
20 goog.require('goog.testing.MockClock');
21 goog.require('goog.testing.jsunit');
22
23
24
25 var mockClock;
26
27
28 function setUp() {
29 mockClock = new goog.testing.MockClock(true);
30 }
31
32
33 function tearDown() {
34 mockClock.uninstall();
35 }
36
37
38 /**
39 * Test initial state of module info.
40 */
41 function testNotLoadedAtStart() {
42 var m = new goog.module.ModuleInfo();
43 assertFalse('Shouldn\'t be loaded', m.isLoaded());
44 }
45
46 var TestModule = function() {
47 goog.module.BaseModule.call(this);
48 };
49 goog.inherits(TestModule, goog.module.BaseModule);
50
51
52 /**
53 * Test loaded module info.
54 */
55 function testOnLoad() {
56 var m = new goog.module.ModuleInfo();
57
58 m.setModuleConstructor(TestModule);
59 m.onLoad(goog.nullFunction);
60 assertTrue(m.isLoaded());
61
62 var module = m.getModule();
63 assertNotNull(module);
64 assertTrue(module instanceof TestModule);
65
66 m.dispose();
67 assertTrue(m.isDisposed());
68 assertTrue(
69 'Disposing of ModuleInfo should dispose of its module',
70 module.isDisposed());
71 }
72
73
74 /**
75 * Test callbacks on module load.
76 */
77 function testCallbacks() {
78 var m = new goog.module.ModuleInfo();
79 m.setModuleConstructor(TestModule);
80 var index = 0;
81 var a = -1, b = -1, c = -1, d = -1;
82 var ca = m.registerCallback(function() { a = index++; });
83 var cb = m.registerCallback(function() { b = index++; });
84 var cc = m.registerCallback(function() { c = index++; });
85 var cd = m.registerEarlyCallback(function() { d = index++; });
86 cb.abort();
87 m.onLoad(goog.nullFunction);
88
89 assertTrue('callback A should have fired', a >= 0);
90 assertFalse('callback B should have been aborted', b >= 0);
91 assertTrue('callback C should have fired', c >= 0);
92 assertTrue('early callback d should have fired', d >= 0);
93
94 assertEquals('ordering of callbacks was wrong', 0, d);
95 assertEquals('ordering of callbacks was wrong', 1, a);
96 assertEquals('ordering of callbacks was wrong', 2, c);
97 }
98
99
100 function testErrorsInCallbacks() {
101 var m = new goog.module.ModuleInfo();
102 m.setModuleConstructor(TestModule);
103 m.registerCallback(function() { throw new Error('boom1'); });
104 m.registerCallback(function() { throw new Error('boom2'); });
105 var hadError = m.onLoad(goog.nullFunction);
106 assertTrue(hadError);
107
108 var e = assertThrows(function() { mockClock.tick(); });
109
110 assertEquals('boom1', e.message);
111 }
112
113
114 /**
115 * Tests the error callbacks.
116 */
117 function testErrbacks() {
118 var m = new goog.module.ModuleInfo();
119 m.setModuleConstructor(TestModule);
120 var index = 0;
121 var a = -1, b = -1, c = -1, d = -1;
122 var ca = m.registerErrback(function() { a = index++; });
123 var cb = m.registerErrback(function() { b = index++; });
124 var cc = m.registerErrback(function() { c = index++; });
125 m.onError('foo');
126
127 assertTrue('callback A should have fired', a >= 0);
128 assertTrue('callback B should have fired', b >= 0);
129 assertTrue('callback C should have fired', c >= 0);
130
131 assertEquals('ordering of callbacks was wrong', 0, a);
132 assertEquals('ordering of callbacks was wrong', 1, b);
133 assertEquals('ordering of callbacks was wrong', 2, c);
134 }