chain_test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.chainTest');
  4. goog.setTestOnly('goog.result.chainTest');
  5. goog.require('goog.Timer');
  6. goog.require('goog.result');
  7. goog.require('goog.testing.MockClock');
  8. goog.require('goog.testing.jsunit');
  9. goog.require('goog.testing.recordFunction');
  10. var givenResult, dependentResult, counter, actionCallback;
  11. var mockClock;
  12. function setUpPage() {
  13. mockClock = new goog.testing.MockClock();
  14. mockClock.install();
  15. }
  16. function setUp() {
  17. mockClock.reset();
  18. givenResult = new goog.result.SimpleResult();
  19. dependentResult = new goog.result.SimpleResult();
  20. counter = new goog.testing.recordFunction();
  21. actionCallback = goog.testing.recordFunction(function(result) {
  22. return dependentResult;
  23. });
  24. }
  25. function tearDown() {
  26. givenResult = dependentResult = counter = null;
  27. }
  28. function tearDownPage() {
  29. mockClock.uninstall();
  30. }
  31. // SYNCHRONOUS TESTS:
  32. function testChainWhenBothResultsSuccess() {
  33. var finalResult = goog.result.chain(givenResult, actionCallback);
  34. goog.result.wait(finalResult, counter);
  35. givenResult.setValue(1);
  36. dependentResult.setValue(2);
  37. assertSuccess(actionCallback, givenResult, 1);
  38. assertSuccess(counter, finalResult, 2);
  39. }
  40. function testChainWhenFirstResultError() {
  41. var finalResult = goog.result.chain(givenResult, actionCallback);
  42. goog.result.wait(finalResult, counter);
  43. givenResult.setError(4);
  44. assertNoCall(actionCallback);
  45. assertError(counter, finalResult, 4);
  46. }
  47. function testChainWhenSecondResultError() {
  48. var finalResult = goog.result.chain(givenResult, actionCallback);
  49. goog.result.wait(finalResult, counter);
  50. givenResult.setValue(1);
  51. dependentResult.setError(5);
  52. assertSuccess(actionCallback, givenResult, 1);
  53. assertError(counter, finalResult, 5);
  54. }
  55. function testChainCancelFirstResult() {
  56. var finalResult = goog.result.chain(givenResult, actionCallback);
  57. goog.result.wait(finalResult, counter);
  58. goog.result.cancelParentResults(finalResult);
  59. assertNoCall(actionCallback);
  60. assertTrue(givenResult.isCanceled());
  61. assertTrue(finalResult.isCanceled());
  62. }
  63. function testChainCancelSecondResult() {
  64. var finalResult = goog.result.chain(givenResult, actionCallback);
  65. goog.result.wait(finalResult, counter);
  66. givenResult.setValue(1);
  67. goog.result.cancelParentResults(finalResult);
  68. assertSuccess(actionCallback, givenResult, 1);
  69. assertTrue(dependentResult.isCanceled());
  70. assertTrue(finalResult.isCanceled());
  71. }
  72. function testDoubleChainCancel() {
  73. var intermediateResult = goog.result.chain(givenResult, actionCallback);
  74. var finalResult = goog.result.chain(intermediateResult, actionCallback);
  75. assertTrue(goog.result.cancelParentResults(finalResult));
  76. assertTrue(finalResult.isCanceled());
  77. assertTrue(intermediateResult.isCanceled());
  78. assertFalse(givenResult.isCanceled());
  79. assertFalse(goog.result.cancelParentResults(finalResult));
  80. }
  81. function testCustomScope() {
  82. var scope = {};
  83. var finalResult = goog.result.chain(givenResult, actionCallback, scope);
  84. goog.result.wait(finalResult, counter);
  85. givenResult.setValue(1);
  86. dependentResult.setValue(2);
  87. assertEquals(scope, actionCallback.popLastCall().getThis());
  88. }
  89. // ASYNCHRONOUS TESTS:
  90. function testChainAsyncWhenBothResultsSuccess() {
  91. var finalResult = goog.result.chain(givenResult, actionCallback);
  92. goog.result.wait(finalResult, counter);
  93. goog.Timer.callOnce(function() { givenResult.setValue(1); });
  94. mockClock.tick();
  95. assertSuccess(actionCallback, givenResult, 1);
  96. goog.Timer.callOnce(function() { dependentResult.setValue(2); });
  97. mockClock.tick();
  98. assertSuccess(counter, finalResult, 2);
  99. }
  100. function testChainAsyncWhenFirstResultError() {
  101. var finalResult = goog.result.chain(givenResult, actionCallback);
  102. goog.result.wait(finalResult, counter);
  103. goog.Timer.callOnce(function() { givenResult.setError(6); });
  104. mockClock.tick();
  105. assertNoCall(actionCallback);
  106. assertError(counter, finalResult, 6);
  107. }
  108. function testChainAsyncWhenSecondResultError() {
  109. var finalResult = goog.result.chain(givenResult, actionCallback);
  110. goog.result.wait(finalResult, counter);
  111. goog.Timer.callOnce(function() { givenResult.setValue(1); });
  112. mockClock.tick();
  113. assertSuccess(actionCallback, givenResult, 1);
  114. goog.Timer.callOnce(function() { dependentResult.setError(7); });
  115. mockClock.tick();
  116. assertError(counter, finalResult, 7);
  117. }
  118. function testChainAsyncCancelFirstResult() {
  119. var finalResult = goog.result.chain(givenResult, actionCallback);
  120. goog.result.wait(finalResult, counter);
  121. goog.Timer.callOnce(function() {
  122. goog.result.cancelParentResults(finalResult);
  123. });
  124. mockClock.tick();
  125. assertNoCall(actionCallback);
  126. assertTrue(givenResult.isCanceled());
  127. assertTrue(finalResult.isCanceled());
  128. }
  129. function testChainAsyncCancelSecondResult() {
  130. var finalResult = goog.result.chain(givenResult, actionCallback);
  131. goog.result.wait(finalResult, counter);
  132. goog.Timer.callOnce(function() { givenResult.setValue(1); });
  133. mockClock.tick();
  134. assertSuccess(actionCallback, givenResult, 1);
  135. goog.Timer.callOnce(function() {
  136. goog.result.cancelParentResults(finalResult);
  137. });
  138. mockClock.tick();
  139. assertTrue(dependentResult.isCanceled());
  140. assertTrue(finalResult.isCanceled());
  141. }
  142. // HELPER FUNCTIONS:
  143. // Assert that the recordFunction was called once with an argument of
  144. // 'result' (the second argument) which has a state of SUCCESS and
  145. // a value of 'value' (the third argument).
  146. function assertSuccess(recordFunction, result, value) {
  147. assertEquals(1, recordFunction.getCallCount());
  148. var res = recordFunction.popLastCall().getArgument(0);
  149. assertEquals(result, res);
  150. assertEquals(goog.result.Result.State.SUCCESS, res.getState());
  151. assertEquals(value, res.getValue());
  152. }
  153. // Assert that the recordFunction was called once with an argument of
  154. // 'result' (the second argument) which has a state of ERROR.
  155. function assertError(recordFunction, result, value) {
  156. assertEquals(1, recordFunction.getCallCount());
  157. var res = recordFunction.popLastCall().getArgument(0);
  158. assertEquals(result, res);
  159. assertEquals(goog.result.Result.State.ERROR, res.getState());
  160. assertEquals(value, res.getError());
  161. }
  162. // Assert that the recordFunction wasn't called
  163. function assertNoCall(recordFunction) {
  164. assertEquals(0, recordFunction.getCallCount());
  165. }