ExpressionObserverTests_Task.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reactive.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Perspex.Markup.Data;
  9. using Xunit;
  10. namespace Perspex.Markup.UnitTests.Binding
  11. {
  12. public class ExpressionObserverTests_Task
  13. {
  14. [Fact]
  15. public void Should_Get_Simple_Task_Value()
  16. {
  17. using (var sync = UnitTestSynchronizationContext.Begin())
  18. {
  19. var tcs = new TaskCompletionSource<string>();
  20. var data = new { Foo = tcs.Task };
  21. var target = new ExpressionObserver(data, "Foo");
  22. var result = new List<object>();
  23. var sub = target.Subscribe(x => result.Add(x));
  24. tcs.SetResult("foo");
  25. sync.ExecutePostedCallbacks();
  26. Assert.Equal(new object[] { PerspexProperty.UnsetValue, "foo" }, result.ToArray());
  27. }
  28. }
  29. [Fact]
  30. public void Should_Get_Completed_Task_Value()
  31. {
  32. using (var sync = UnitTestSynchronizationContext.Begin())
  33. {
  34. var data = new { Foo = Task.FromResult("foo") };
  35. var target = new ExpressionObserver(data, "Foo");
  36. var result = new List<object>();
  37. var sub = target.Subscribe(x => result.Add(x));
  38. Assert.Equal(new object[] { "foo" }, result.ToArray());
  39. }
  40. }
  41. [Fact]
  42. public void Should_Get_Property_Value_From_Task()
  43. {
  44. using (var sync = UnitTestSynchronizationContext.Begin())
  45. {
  46. var tcs = new TaskCompletionSource<Class2>();
  47. var data = new Class1(tcs.Task);
  48. var target = new ExpressionObserver(data, "Next.Foo");
  49. var result = new List<object>();
  50. var sub = target.Subscribe(x => result.Add(x));
  51. tcs.SetResult(new Class2("foo"));
  52. sync.ExecutePostedCallbacks();
  53. Assert.Equal(new object[] { PerspexProperty.UnsetValue, "foo" }, result.ToArray());
  54. }
  55. }
  56. private class Class1 : NotifyingBase
  57. {
  58. public Class1(Task<Class2> next)
  59. {
  60. Next = next;
  61. }
  62. public Task<Class2> Next { get; }
  63. }
  64. private class Class2 : NotifyingBase
  65. {
  66. public Class2(string foo)
  67. {
  68. Foo = foo;
  69. }
  70. public string Foo { get; }
  71. }
  72. }
  73. }