moduleinfo_test.js 3.7 KB

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