wait_test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. goog.provide('goog.result.waitTest');
  4. goog.setTestOnly('goog.result.waitTest');
  5. goog.require('goog.Timer');
  6. goog.require('goog.result.SimpleResult');
  7. goog.require('goog.result');
  8. goog.require('goog.testing.MockClock');
  9. goog.require('goog.testing.jsunit');
  10. goog.require('goog.testing.recordFunction');
  11. var result, waitCallback, waitOnSuccessCallback, waitOnErrorCallback;
  12. var mockClock, propertyReplacer;
  13. function setUpPage() {
  14. mockClock = new goog.testing.MockClock();
  15. mockClock.install();
  16. }
  17. function setUp() {
  18. mockClock.reset();
  19. result = new goog.result.SimpleResult();
  20. propertyReplacer = new goog.testing.PropertyReplacer();
  21. waitCallback = new goog.testing.recordFunction();
  22. waitOnSuccessCallback = new goog.testing.recordFunction();
  23. waitOnErrorCallback = new goog.testing.recordFunction();
  24. }
  25. function tearDown() {
  26. result = waitCallback = waitOnSuccessCallback = waitOnErrorCallback = null;
  27. propertyReplacer.reset();
  28. }
  29. function tearDownPage() {
  30. mockClock.uninstall();
  31. }
  32. function testSynchronousSuccess() {
  33. assertEquals(goog.result.Result.State.PENDING, result.getState());
  34. assertUndefined(result.getValue());
  35. goog.result.wait(result, waitCallback);
  36. goog.result.waitOnSuccess(result, waitOnSuccessCallback);
  37. goog.result.waitOnError(result, waitOnErrorCallback);
  38. result.setValue(1);
  39. assertEquals(goog.result.Result.State.SUCCESS, result.getState());
  40. assertEquals(1, result.getValue());
  41. assertWaitCall(waitCallback, result);
  42. assertCall(waitOnSuccessCallback, 1, result);
  43. assertNoCall(waitOnErrorCallback);
  44. }
  45. function testAsynchronousSuccess() {
  46. goog.result.wait(result, waitCallback);
  47. goog.result.waitOnSuccess(result, waitOnSuccessCallback);
  48. goog.result.waitOnError(result, waitOnErrorCallback);
  49. goog.Timer.callOnce(function() {
  50. result.setValue(1);
  51. });
  52. assertUndefined(result.getValue());
  53. assertEquals(goog.result.Result.State.PENDING, result.getState());
  54. assertNoCall(waitCallback);
  55. assertNoCall(waitOnSuccessCallback);
  56. assertNoCall(waitOnErrorCallback);
  57. mockClock.tick();
  58. assertEquals(goog.result.Result.State.SUCCESS, result.getState());
  59. assertEquals(1, result.getValue());
  60. assertWaitCall(waitCallback, result);
  61. assertCall(waitOnSuccessCallback, 1, result);
  62. assertNoCall(waitOnErrorCallback);
  63. }
  64. function testSynchronousError() {
  65. assertEquals(goog.result.Result.State.PENDING, result.getState());
  66. assertUndefined(result.getValue());
  67. goog.result.wait(result, waitCallback);
  68. goog.result.waitOnSuccess(result, waitOnSuccessCallback);
  69. goog.result.waitOnError(result, waitOnErrorCallback);
  70. result.setError();
  71. assertEquals(goog.result.Result.State.ERROR, result.getState());
  72. assertUndefined(result.getValue());
  73. assertWaitCall(waitCallback, result);
  74. assertNoCall(waitOnSuccessCallback);
  75. assertCall(waitOnErrorCallback, undefined, result);
  76. }
  77. function testAsynchronousError() {
  78. goog.result.wait(result, waitCallback);
  79. goog.result.waitOnSuccess(result, waitOnSuccessCallback);
  80. goog.result.waitOnError(result, waitOnErrorCallback);
  81. goog.Timer.callOnce(function() {
  82. result.setError();
  83. });
  84. assertEquals(goog.result.Result.State.PENDING, result.getState());
  85. assertUndefined(result.getValue());
  86. assertNoCall(waitCallback);
  87. assertNoCall(waitOnSuccessCallback);
  88. assertNoCall(waitOnErrorCallback);
  89. mockClock.tick();
  90. assertEquals(goog.result.Result.State.ERROR, result.getState());
  91. assertUndefined(result.getValue());
  92. assertWaitCall(waitCallback, result);
  93. assertNoCall(waitOnSuccessCallback);
  94. assertCall(waitOnErrorCallback, undefined, result);
  95. }
  96. function testCustomScope() {
  97. var scope = {};
  98. goog.result.wait(result, waitCallback, scope);
  99. result.setValue(1);
  100. assertEquals(scope, waitCallback.popLastCall().getThis());
  101. }
  102. function testDefaultScope() {
  103. goog.result.wait(result, waitCallback);
  104. result.setValue(1);
  105. assertEquals(goog.global, waitCallback.popLastCall().getThis());
  106. }
  107. function testOnSuccessScope() {
  108. var scope = {};
  109. goog.result.waitOnSuccess(result, waitOnSuccessCallback, scope);
  110. result.setValue(1);
  111. assertCall(waitOnSuccessCallback, 1, result, scope);
  112. }
  113. function testOnErrorScope() {
  114. var scope = {};
  115. goog.result.waitOnError(result, waitOnErrorCallback, scope);
  116. result.setError();
  117. assertCall(waitOnErrorCallback, undefined, result, scope);
  118. }
  119. /**
  120. * Assert that a callback function stubbed out with goog.recordFunction was
  121. * called with the expected arguments by goog.result.waitOnSuccess/Error.
  122. * @param {Function} recordedFunction The callback function.
  123. * @param {?} value The value stored in the result.
  124. * @param {!goog.result.Result} result The result that was resolved to SUCCESS
  125. * or ERROR.
  126. * @param {Object=} opt_scope Optional scope that the test function should be
  127. * called in. By default, it is goog.global.
  128. */
  129. function assertCall(recordedFunction, value, result, opt_scope) {
  130. var scope = opt_scope || goog.global;
  131. assertEquals(1, recordedFunction.getCallCount());
  132. var call = recordedFunction.popLastCall();
  133. assertEquals(2, call.getArguments().length);
  134. assertEquals(value, call.getArgument(0));
  135. assertEquals(result, call.getArgument(1));
  136. assertEquals(scope, call.getThis());
  137. }
  138. /**
  139. * Assert that a callback function stubbed out with goog.recordFunction was
  140. * called with the expected arguments by goog.result.wait.
  141. * @param {Function} recordedFunction The callback function.
  142. * @param {!goog.result.Result} result The result that was resolved to SUCCESS
  143. * or ERROR.
  144. * @param {Object=} opt_scope Optional scope that the test function should be
  145. * called in. By default, it is goog.global.
  146. */
  147. function assertWaitCall(recordedFunction, result, opt_scope) {
  148. var scope = opt_scope || goog.global;
  149. assertEquals(1, recordedFunction.getCallCount());
  150. var call = recordedFunction.popLastCall();
  151. assertEquals(1, call.getArguments().length);
  152. assertEquals(result, call.getArgument(0));
  153. assertEquals(scope, call.getThis());
  154. }
  155. function assertNoCall(recordedFunction) {
  156. assertEquals(0, recordedFunction.getCallCount());
  157. }