ExpressionObserverTests_Task.cs 5.5 KB

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