ExpressionObserverTests_Task.cs 5.3 KB

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