ExpressionObserverTests_Task.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Linq;
  4. using System.Threading.Tasks;
  5. using Avalonia.Data;
  6. using Avalonia.Data.Core;
  7. using Avalonia.Markup.Parsers;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Base.UnitTests.Data.Core
  11. {
  12. public class ExpressionObserverTests_Task
  13. {
  14. [Fact]
  15. public void Should_Not_Get_Task_Result_Without_StreamBinding()
  16. {
  17. using (var sync = UnitTestSynchronizationContext.Begin())
  18. {
  19. var tcs = new TaskCompletionSource<string>();
  20. var data = new { Foo = tcs.Task };
  21. var target = ExpressionObserver.Create(data, o => o.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.Single(result);
  27. Assert.IsType<Task<string>>(result[0]);
  28. GC.KeepAlive(data);
  29. }
  30. }
  31. [Fact]
  32. public void Should_Get_Completed_Task_Value()
  33. {
  34. using (var sync = UnitTestSynchronizationContext.Begin())
  35. {
  36. var data = new { Foo = Task.FromResult("foo") };
  37. var target = ExpressionObserver.Create(data, o => o.Foo.StreamBinding());
  38. var result = new List<object>();
  39. var sub = target.Subscribe(x => result.Add(x));
  40. Assert.Equal(new[] { "foo" }, result);
  41. GC.KeepAlive(data);
  42. }
  43. }
  44. [Fact]
  45. public void Should_Get_Property_Value_From_Task()
  46. {
  47. using (var sync = UnitTestSynchronizationContext.Begin())
  48. {
  49. var tcs = new TaskCompletionSource<Class2>();
  50. var data = new Class1(tcs.Task);
  51. var target = ExpressionObserver.Create(data, o => o.Next.StreamBinding().Foo);
  52. var result = new List<object>();
  53. var sub = target.Subscribe(x => result.Add(x));
  54. tcs.SetResult(new Class2("foo"));
  55. sync.ExecutePostedCallbacks();
  56. Assert.Equal(new[] { "foo" }, result);
  57. GC.KeepAlive(data);
  58. }
  59. }
  60. [Fact]
  61. public void Should_Return_BindingNotification_Error_On_Task_Exception()
  62. {
  63. using (var sync = UnitTestSynchronizationContext.Begin())
  64. {
  65. var tcs = new TaskCompletionSource<string>();
  66. var data = new { Foo = tcs.Task };
  67. var target = ExpressionObserver.Create(data, o => o.Foo.StreamBinding());
  68. var result = new List<object>();
  69. var sub = target.Subscribe(x => result.Add(x));
  70. tcs.SetException(new NotSupportedException());
  71. sync.ExecutePostedCallbacks();
  72. Assert.Equal(
  73. new[]
  74. {
  75. new BindingNotification(
  76. new AggregateException(new NotSupportedException()),
  77. BindingErrorType.Error)
  78. },
  79. result);
  80. GC.KeepAlive(data);
  81. }
  82. }
  83. [Fact]
  84. public void Should_Return_BindingNotification_Error_For_Faulted_Task()
  85. {
  86. using (var sync = UnitTestSynchronizationContext.Begin())
  87. {
  88. var data = new { Foo = TaskFromException(new NotSupportedException()) };
  89. var target = ExpressionObserver.Create(data, o => o.Foo.StreamBinding());
  90. var result = new List<object>();
  91. var sub = target.Subscribe(x => result.Add(x));
  92. Assert.Equal(
  93. new[]
  94. {
  95. new BindingNotification(
  96. new AggregateException(new NotSupportedException()),
  97. BindingErrorType.Error)
  98. },
  99. result);
  100. GC.KeepAlive(data);
  101. }
  102. }
  103. [Fact]
  104. public void Should_Get_Simple_Task_Value_With_Data_DataValidation_Enabled()
  105. {
  106. using (var sync = UnitTestSynchronizationContext.Begin())
  107. {
  108. var tcs = new TaskCompletionSource<string>();
  109. var data = new { Foo = tcs.Task };
  110. var target = ExpressionObserver.Create(data, o => o.Foo.StreamBinding(), true);
  111. var result = new List<object>();
  112. var sub = target.Subscribe(x => result.Add(x));
  113. tcs.SetResult("foo");
  114. sync.ExecutePostedCallbacks();
  115. // What does it mean to have data validation on a Task? Without a use-case it's
  116. // hard to know what to do here so for the moment the value is returned.
  117. Assert.Equal(new [] { "foo" }, result);
  118. GC.KeepAlive(data);
  119. }
  120. }
  121. private static Task TaskFromException(Exception e)
  122. {
  123. var tcs = new TaskCompletionSource<object>();
  124. tcs.SetException(e);
  125. return tcs.Task;
  126. }
  127. private class Class1 : NotifyingBase
  128. {
  129. public Class1(Task<Class2> next)
  130. {
  131. Next = next;
  132. }
  133. public Task<Class2> Next { get; }
  134. }
  135. private class Class2 : NotifyingBase
  136. {
  137. public Class2(string foo)
  138. {
  139. Foo = foo;
  140. }
  141. public string Foo { get; }
  142. }
  143. }
  144. }