transform_test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.transformTest');
  4. goog.setTestOnly('goog.result.transformTest');
  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, resultCallback, multiplyResult, mockClock;
  12. function setUpPage() {
  13. mockClock = new goog.testing.MockClock();
  14. mockClock.install();
  15. }
  16. function setUp() {
  17. mockClock.reset();
  18. result = new goog.result.SimpleResult();
  19. resultCallback = new goog.testing.recordFunction();
  20. multiplyResult = goog.testing.recordFunction(function(value) {
  21. return value * 2;
  22. });
  23. }
  24. function tearDown() {
  25. result = multiplyResult = null;
  26. }
  27. function tearDownPage() {
  28. mockClock.uninstall();
  29. goog.dispose(mockClock);
  30. }
  31. function testTransformWhenResultSuccess() {
  32. var transformedResult = goog.result.transform(result, multiplyResult);
  33. goog.result.wait(transformedResult, resultCallback);
  34. assertEquals(goog.result.Result.State.PENDING, result.getState());
  35. result.setValue(1);
  36. assertTransformerCall(multiplyResult, 1);
  37. assertSuccessCall(resultCallback, transformedResult, 2);
  38. }
  39. function testTransformWhenResultSuccessAsync() {
  40. var transformedResult = goog.result.transform(result, multiplyResult);
  41. goog.result.wait(transformedResult, resultCallback);
  42. goog.Timer.callOnce(function() {
  43. result.setValue(1);
  44. });
  45. assertEquals(goog.result.Result.State.PENDING, result.getState());
  46. mockClock.tick();
  47. assertTransformerCall(multiplyResult, 1);
  48. assertSuccessCall(resultCallback, transformedResult, 2);
  49. }
  50. function testTransformWhenResultError() {
  51. var transformedResult = goog.result.transform(result, multiplyResult);
  52. goog.result.wait(transformedResult, resultCallback);
  53. assertEquals(goog.result.Result.State.PENDING, result.getState());
  54. result.setError(4);
  55. assertNoCall(multiplyResult);
  56. assertErrorCall(resultCallback, transformedResult, 4);
  57. }
  58. function testTransformWhenResultErrorAsync() {
  59. var transformedResult = goog.result.transform(result, multiplyResult);
  60. goog.result.wait(transformedResult, resultCallback);
  61. goog.Timer.callOnce(function() {
  62. result.setError(5);
  63. });
  64. assertEquals(goog.result.Result.State.PENDING, result.getState());
  65. mockClock.tick();
  66. assertNoCall(multiplyResult);
  67. assertErrorCall(resultCallback, transformedResult, 5);
  68. }
  69. function testCancelParentResults() {
  70. var transformedResult = goog.result.transform(result, multiplyResult);
  71. goog.result.wait(transformedResult, resultCallback);
  72. goog.result.cancelParentResults(transformedResult);
  73. assertTrue(result.isCanceled());
  74. result.setValue(1);
  75. assertNoCall(multiplyResult);
  76. }
  77. function testDoubleTransformCancel() {
  78. var step1Result = goog.result.transform(result, multiplyResult);
  79. var step2Result = goog.result.transform(step1Result, multiplyResult);
  80. goog.result.cancelParentResults(step2Result);
  81. assertFalse(result.isCanceled());
  82. assertTrue(step1Result.isCanceled());
  83. assertTrue(step2Result.isCanceled());
  84. }
  85. function assertSuccessCall(recordFunction, result, value) {
  86. assertEquals(1, recordFunction.getCallCount());
  87. var res = recordFunction.popLastCall().getArgument(0);
  88. assertEquals(result, res);
  89. assertEquals(goog.result.Result.State.SUCCESS, res.getState());
  90. assertEquals(value, res.getValue());
  91. }
  92. function assertErrorCall(recordFunction, result, value) {
  93. assertEquals(1, recordFunction.getCallCount());
  94. var res = recordFunction.popLastCall().getArgument(0);
  95. assertEquals(result, res);
  96. assertEquals(goog.result.Result.State.ERROR, res.getState());
  97. assertEquals(value, res.getError());
  98. }
  99. function assertNoCall(recordFunction) {
  100. assertEquals(0, recordFunction.getCallCount());
  101. }
  102. function assertTransformerCall(recordFunction, value) {
  103. assertEquals(1, recordFunction.getCallCount());
  104. var argValue = recordFunction.popLastCall().getArgument(0);
  105. assertEquals(value, argValue);
  106. }