123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System;
- using System.Collections.Generic;
- using System.Reactive.Linq;
- using System.Threading.Tasks;
- using Avalonia.Data;
- using Avalonia.Data.Core;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Base.UnitTests.Data.Core
- {
- public class ExpressionObserverTests_Task
- {
- [Fact]
- public void Should_Not_Get_Task_Result_Without_Modifier_Char()
- {
- using (var sync = UnitTestSynchronizationContext.Begin())
- {
- var tcs = new TaskCompletionSource<string>();
- var data = new { Foo = tcs.Task };
- var target = new ExpressionObserver(data, "Foo");
- var result = new List<object>();
- var sub = target.Subscribe(x => result.Add(x));
- tcs.SetResult("foo");
- sync.ExecutePostedCallbacks();
- Assert.Single(result);
- Assert.IsType<Task<string>>(result[0]);
- GC.KeepAlive(data);
- }
- }
- [Fact]
- public void Should_Get_Completed_Task_Value()
- {
- using (var sync = UnitTestSynchronizationContext.Begin())
- {
- var data = new { Foo = Task.FromResult("foo") };
- var target = new ExpressionObserver(data, "Foo^");
- var result = new List<object>();
- var sub = target.Subscribe(x => result.Add(x));
- Assert.Equal(new[] { "foo" }, result);
- GC.KeepAlive(data);
- }
- }
- [Fact]
- public void Should_Get_Property_Value_From_Task()
- {
- using (var sync = UnitTestSynchronizationContext.Begin())
- {
- var tcs = new TaskCompletionSource<Class2>();
- var data = new Class1(tcs.Task);
- var target = new ExpressionObserver(data, "Next^.Foo");
- var result = new List<object>();
- var sub = target.Subscribe(x => result.Add(x));
- tcs.SetResult(new Class2("foo"));
- sync.ExecutePostedCallbacks();
- Assert.Equal(new[] { "foo" }, result);
- GC.KeepAlive(data);
- }
- }
- [Fact]
- public void Should_Return_BindingNotification_Error_On_Task_Exception()
- {
- using (var sync = UnitTestSynchronizationContext.Begin())
- {
- var tcs = new TaskCompletionSource<string>();
- var data = new { Foo = tcs.Task };
- var target = new ExpressionObserver(data, "Foo^");
- var result = new List<object>();
- var sub = target.Subscribe(x => result.Add(x));
- tcs.SetException(new NotSupportedException());
- sync.ExecutePostedCallbacks();
- Assert.Equal(
- new[]
- {
- new BindingNotification(
- new AggregateException(new NotSupportedException()),
- BindingErrorType.Error)
- },
- result);
- GC.KeepAlive(data);
- }
- }
- [Fact]
- public void Should_Return_BindingNotification_Error_For_Faulted_Task()
- {
- using (var sync = UnitTestSynchronizationContext.Begin())
- {
- var data = new { Foo = TaskFromException(new NotSupportedException()) };
- var target = new ExpressionObserver(data, "Foo^");
- var result = new List<object>();
- var sub = target.Subscribe(x => result.Add(x));
- Assert.Equal(
- new[]
- {
- new BindingNotification(
- new AggregateException(new NotSupportedException()),
- BindingErrorType.Error)
- },
- result);
- GC.KeepAlive(data);
- }
- }
- [Fact]
- public void Should_Get_Simple_Task_Value_With_Data_DataValidation_Enabled()
- {
- using (var sync = UnitTestSynchronizationContext.Begin())
- {
- var tcs = new TaskCompletionSource<string>();
- var data = new { Foo = tcs.Task };
- var target = new ExpressionObserver(data, "Foo^", true);
- var result = new List<object>();
- var sub = target.Subscribe(x => result.Add(x));
- tcs.SetResult("foo");
- sync.ExecutePostedCallbacks();
- // What does it mean to have data validation on a Task? Without a use-case it's
- // hard to know what to do here so for the moment the value is returned.
- Assert.Equal(new [] { "foo" }, result);
- GC.KeepAlive(data);
- }
- }
- private Task TaskFromException(Exception e)
- {
- var tcs = new TaskCompletionSource<object>();
- tcs.SetException(e);
- return tcs.Task;
- }
- private class Class1 : NotifyingBase
- {
- public Class1(Task<Class2> next)
- {
- Next = next;
- }
- public Task<Class2> Next { get; }
- }
- private class Class2 : NotifyingBase
- {
- public Class2(string foo)
- {
- Foo = foo;
- }
- public string Foo { get; }
- }
- }
- }
|