errorhandler_async_test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.debug.ErrorHandlerAsyncTest');
  15. goog.setTestOnly('goog.debug.ErrorHandlerAsyncTest');
  16. goog.require('goog.Promise');
  17. goog.require('goog.debug.ErrorHandler');
  18. goog.require('goog.testing.TestCase');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.userAgent');
  21. /** @type {!goog.promise.Resolver} */
  22. var resolver;
  23. var testCase = new goog.testing.TestCase(document.title);
  24. testCase.setUpPage = function() {
  25. resolver = goog.Promise.withResolver();
  26. this.oldTimeout = window.setTimeout;
  27. this.oldInterval = window.setInterval;
  28. this.oldRequestAnimationFrame = window.requestAnimationFrame;
  29. // Whether requestAnimationFrame is available for testing.
  30. this.testingReqAnimFrame = !!window.requestAnimationFrame;
  31. this.handler = new goog.debug.ErrorHandler(goog.bind(this.onException, this));
  32. this.handler.protectWindowSetTimeout();
  33. this.handler.protectWindowSetInterval();
  34. this.handler.protectWindowRequestAnimationFrame();
  35. this.exceptions = [];
  36. this.errors = 0;
  37. // Override the error event handler, since we are purposely throwing
  38. // exceptions from global functions, and expect them
  39. this.oldWindowOnError = window.onerror;
  40. window.onerror = goog.bind(this.onError, this);
  41. window.setTimeout(goog.bind(this.timeOut, this), 10);
  42. this.intervalId = window.setInterval(goog.bind(this.interval, this), 20);
  43. if (this.testingReqAnimFrame) {
  44. window.requestAnimationFrame(goog.bind(this.animFrame, this));
  45. }
  46. this.promiseTimeout = 10000; // 10s.
  47. };
  48. testCase.tearDownPage = function() {
  49. window.setTimeout = this.oldTimeout;
  50. window.setInterval = this.oldInterval;
  51. window.requestAnimationFrame = this.oldRequestAnimationFrame;
  52. };
  53. testCase.onException = function(e) {
  54. this.exceptions.push(e);
  55. if (this.timeoutHit && this.intervalHit &&
  56. (!this.testingReqAnimFrame || this.animFrameHit)) {
  57. resolver.resolve();
  58. }
  59. };
  60. testCase.onError = function(msg, url, line) {
  61. this.errors++;
  62. return true;
  63. };
  64. testCase.timeOut = function() {
  65. this.timeoutHit = true;
  66. throw arguments.callee;
  67. };
  68. testCase.interval = function() {
  69. this.intervalHit = true;
  70. window.clearTimeout(this.intervalId);
  71. throw arguments.callee;
  72. };
  73. testCase.animFrame = function() {
  74. this.animFrameHit = true;
  75. throw arguments.callee;
  76. };
  77. testCase.addNewTest('testResults', function() {
  78. return resolver.promise.then(function() {
  79. var timeoutHit, intervalHit, animFrameHit;
  80. for (var i = 0; i < this.exceptions.length; ++i) {
  81. switch (this.exceptions[i]) {
  82. case this.timeOut:
  83. timeoutHit = true;
  84. break;
  85. case this.interval:
  86. intervalHit = true;
  87. break;
  88. case this.animFrame:
  89. animFrameHit = true;
  90. break;
  91. }
  92. }
  93. assertTrue('timeout exception not received', timeoutHit);
  94. assertTrue('timeout not called', this.timeoutHit);
  95. assertTrue('interval exception not received', intervalHit);
  96. assertTrue('interval not called', this.intervalHit);
  97. if (this.testingReqAnimFrame) {
  98. assertTrue('anim frame exception not received', animFrameHit);
  99. assertTrue('animFrame not called', this.animFrameHit);
  100. }
  101. if (!goog.userAgent.WEBKIT) {
  102. var expectedRethrownCount = this.testingReqAnimFrame ? 3 : 2;
  103. assertEquals(
  104. expectedRethrownCount + ' exceptions should have been rethrown',
  105. expectedRethrownCount, this.errors);
  106. }
  107. }, null, this);
  108. });
  109. // Standalone Closure Test Runner.
  110. G_testRunner.initialize(testCase);