BindingExpressionTests.Task.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Threading.Tasks;
  3. using Avalonia.Data;
  4. using Avalonia.UnitTests;
  5. using Xunit;
  6. #nullable enable
  7. namespace Avalonia.Base.UnitTests.Data.Core;
  8. public partial class BindingExpressionTests
  9. {
  10. [Fact]
  11. public void Should_Not_Get_Task_Result_Without_StreamBinding()
  12. {
  13. using var sync = UnitTestSynchronizationContext.Begin();
  14. var tcs = new TaskCompletionSource<string>();
  15. var data = new { Foo = tcs.Task };
  16. var target = CreateTargetWithSource(data, o => o.Foo);
  17. Assert.Null(target.String);
  18. tcs.SetResult("foo");
  19. sync.ExecutePostedCallbacks();
  20. Assert.Null(target.String);
  21. GC.KeepAlive(data);
  22. }
  23. [Fact]
  24. public void Should_Get_Completed_Task_Value()
  25. {
  26. using var sync = UnitTestSynchronizationContext.Begin();
  27. var data = new { Foo = Task.FromResult("foo") };
  28. var target = CreateTargetWithSource(data, o => o.Foo.StreamBinding());
  29. Assert.Equal("foo", target.String);
  30. GC.KeepAlive(data);
  31. }
  32. [Fact]
  33. public void Should_Get_Property_Value_From_Task()
  34. {
  35. using var sync = UnitTestSynchronizationContext.Begin();
  36. var tcs = new TaskCompletionSource<ViewModel>();
  37. var data = new ViewModel { NextTask = tcs.Task };
  38. var target = CreateTargetWithSource(data, o => o.NextTask.StreamBinding().StringValue);
  39. tcs.SetResult(new ViewModel { StringValue = "foo" });
  40. sync.ExecutePostedCallbacks();
  41. Assert.Equal("foo", target.String);
  42. GC.KeepAlive(data);
  43. }
  44. [Fact]
  45. public void Should_Update_Data_Validation_On_Task_Exception()
  46. {
  47. using var sync = UnitTestSynchronizationContext.Begin();
  48. var tcs = new TaskCompletionSource<string>();
  49. var data = new { Foo = tcs.Task };
  50. var target = CreateTargetWithSource(
  51. data,
  52. o => o.Foo.StreamBinding(),
  53. enableDataValidation: true);
  54. tcs.SetException(new NotSupportedException());
  55. sync.ExecutePostedCallbacks();
  56. AssertBindingError(
  57. target,
  58. TargetClass.StringProperty,
  59. new BindingChainException("Specified method is not supported.", "Foo^", "^"),
  60. BindingErrorType.Error);
  61. GC.KeepAlive(data);
  62. }
  63. [Fact]
  64. public void Should_Update_Data_Validation_On_Faulted_Task()
  65. {
  66. using var sync = UnitTestSynchronizationContext.Begin();
  67. var data = new { Foo = TaskFromException(new NotSupportedException()) };
  68. var target = CreateTargetWithSource(
  69. data,
  70. o => o.Foo.StreamBinding(),
  71. enableDataValidation: true);
  72. AssertBindingError(
  73. target,
  74. TargetClass.StringProperty,
  75. new BindingChainException("Specified method is not supported.", "Foo^", "^"),
  76. BindingErrorType.Error);
  77. GC.KeepAlive(data);
  78. }
  79. [Fact]
  80. public void Should_Get_Simple_Task_Value_With_Data_DataValidation_Enabled()
  81. {
  82. using var sync = UnitTestSynchronizationContext.Begin();
  83. var tcs = new TaskCompletionSource<string>();
  84. var data = new { Foo = tcs.Task };
  85. var target = CreateTargetWithSource(
  86. data,
  87. o => o.Foo.StreamBinding(),
  88. enableDataValidation: true);
  89. tcs.SetResult("foo");
  90. sync.ExecutePostedCallbacks();
  91. // What does it mean to have data validation on a Task? Without a use-case it's
  92. // hard to know what to do here so for the moment the value is returned.
  93. Assert.Equal("foo", target.String);
  94. GC.KeepAlive(data);
  95. }
  96. private static Task<string> TaskFromException(Exception e)
  97. {
  98. var tcs = new TaskCompletionSource<string>();
  99. tcs.SetException(e);
  100. return tcs.Task;
  101. }
  102. }