deferredadaptor_test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.DeferredAdaptorTest');
  4. goog.setTestOnly('goog.result.DeferredAdaptorTest');
  5. goog.require('goog.result');
  6. goog.require('goog.result.DeferredAdaptor');
  7. goog.require('goog.result.SimpleResult');
  8. goog.require('goog.testing.jsunit');
  9. goog.require('goog.testing.recordFunction');
  10. var result, deferred, record;
  11. function setUp() {
  12. result = new goog.result.SimpleResult();
  13. deferred = new goog.result.DeferredAdaptor(result);
  14. record = new goog.testing.recordFunction();
  15. }
  16. function tearDown() {
  17. result = deferred = record = null;
  18. }
  19. function testResultSuccessfulResolution() {
  20. deferred.addCallback(record);
  21. result.setValue(1);
  22. assertEquals(1, record.getCallCount());
  23. var call = record.popLastCall();
  24. assertEquals(1, call.getArgument(0));
  25. }
  26. function testResultErrorResolution() {
  27. deferred.addErrback(record);
  28. result.setError(2);
  29. assertEquals(1, record.getCallCount());
  30. var call = record.popLastCall();
  31. assertEquals(2, call.getArgument(0));
  32. }
  33. function testResultCancelResolution() {
  34. deferred.addCallback(record);
  35. var cancelCallback = new goog.testing.recordFunction();
  36. deferred.addErrback(cancelCallback);
  37. result.cancel();
  38. assertEquals(0, record.getCallCount());
  39. assertEquals(1, cancelCallback.getCallCount());
  40. var call = cancelCallback.popLastCall();
  41. assertTrue(call.getArgument(0) instanceof
  42. goog.async.Deferred.CanceledError);
  43. }
  44. function testAddCallbackOnResolvedResult() {
  45. result.setValue(1);
  46. assertEquals(1, result.getValue());
  47. deferred.addCallback(record);
  48. // callback should be called immediately when result is already resolved.
  49. assertEquals(1, record.getCallCount());
  50. assertEquals(1, record.popLastCall().getArgument(0));
  51. }
  52. function testAddErrbackOnErroredResult() {
  53. result.setError(1);
  54. assertEquals(1, result.getError());
  55. // errback should be called immediately when result already errored.
  56. deferred.addErrback(record);
  57. assertEquals(1, record.getCallCount());
  58. assertEquals(1, record.popLastCall().getArgument(0));
  59. }