public void ReentrancyPostAndWaitTest()
        {
            int actionCallCount      = 0;
            int innerActionCallCount = 0;

            using (var context = UnitTestSynchronizationContext.Create())
            {
                Func <Task> asyncInnerAction = async() =>
                {
                    await Task.Delay(1);

                    Interlocked.Increment(ref innerActionCallCount);
                };

                Func <Task> asyncAction = async() =>
                {
                    await Task.Delay(1);

                    asyncInnerAction().Wait(context);
                    await Task.Delay(1);

                    asyncInnerAction().Wait(context);
                    Interlocked.Increment(ref actionCallCount);
                };

                var task = asyncAction();
                Assert.AreEqual(0, actionCallCount);
                Assert.AreEqual(0, innerActionCallCount);

                task.Wait(context);
                Assert.AreEqual(1, actionCallCount);
                Assert.AreEqual(2, innerActionCallCount);
            }
        }
        public void PostAndWaitTest()
        {
            int actionCallCount = 0;
            var context         = UnitTestSynchronizationContext.Create();

            Func <Task> asyncAction = async() =>
            {
                await Task.Delay(1);

                Interlocked.Increment(ref actionCallCount);
            };

            var task = asyncAction();

            Assert.AreEqual(0, actionCallCount);

            task.Wait(context);
            Assert.AreEqual(1, actionCallCount);

            task = asyncAction();
            context.Wait(TimeSpan.FromMilliseconds(50));
            Assert.AreEqual(2, actionCallCount);

            context.Dispose();
            Assert.AreEqual(2, actionCallCount);
        }
Beispiel #3
0
        public void WaitForTest()
        {
            using (var context = UnitTestSynchronizationContext.Create())
            {
                bool   flag        = false;
                Action asyncAction = async() =>
                {
                    await Task.Delay(10);

                    flag = true;
                };

                asyncAction();
                Assert.IsFalse(flag);

                context.WaitFor(() => flag, TimeSpan.FromSeconds(1));
                Assert.IsTrue(flag);

                AssertHelper.ExpectedException <TimeoutException>(() => context.WaitFor(() => !flag, TimeSpan.FromMilliseconds(10)));

                AssertHelper.ExpectedException <ArgumentNullException>(() => UnitTestSynchronizationContextExtensions.WaitFor(context, null, TimeSpan.FromMilliseconds(10)));
            }

            AssertHelper.ExpectedException <ArgumentNullException>(() => UnitTestSynchronizationContextExtensions.WaitFor(null, () => true, TimeSpan.FromMilliseconds(10)));
        }
        public void Should_Return_BindingNotification_If_Stream_Operator_Applied_To_Not_Supported_Type()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var data   = new Class2("foo");
                var target = new ExpressionObserver(data, "Foo^", true);
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                sync.ExecutePostedCallbacks();

                Assert.Equal(
                    new[]
                {
                    new BindingNotification(
                        new MarkupBindingChainException("Stream operator applied to unsupported type", "Foo^", "Foo^"),
                        BindingErrorType.Error)
                },
                    result);

                sub.Dispose();

                GC.KeepAlive(data);
            }
        }
        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 = ExpressionObserver.Create(data, o => o.Foo.StreamBinding());
                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);
            }
        }
Beispiel #6
0
        public void Should_Get_Property_Value_From_Observable_With_DataValidation_Enabled()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var data1  = new Class1();
                var data2  = new Class2("foo");
                var target = ExpressionObserver.Create(data1, o => o.Next.StreamBinding().Foo, true);
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                data1.Next.OnNext(data2);
                sync.ExecutePostedCallbacks();

                // Forces WeakEvent compact
                Dispatcher.UIThread.RunJobs();
                Assert.Equal(new[] { new BindingNotification("foo") }, result);

                sub.Dispose();

                // Forces WeakEvent compact
                Dispatcher.UIThread.RunJobs();

                Assert.Equal(0, data1.PropertyChangedSubscriptionCount);

                GC.KeepAlive(data1);
                GC.KeepAlive(data2);
            }
        }
 public void PostAndWaitExceptionTest()
 {
     using (var context = UnitTestSynchronizationContext.Create())
     {
         AssertHelper.ExpectedException <ArgumentNullException>(() => context.Post(null, null));
         AssertHelper.ExpectedException <ArgumentNullException>(() => context.Wait(null));
     }
 }
Beispiel #8
0
        protected override void OnInitialize()
        {
            base.OnInitialize();

            Context = UnitTestSynchronizationContext.Create();

            var configuration = new ContainerConfiguration()
                                .WithAssembly(typeof(ShellViewModel).GetTypeInfo().Assembly)
                                .WithAssembly(typeof(ApplicationsTest).GetTypeInfo().Assembly);

            Container = configuration.CreateContainer();
        }
Beispiel #9
0
        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 object[] { "foo" }, result.ToArray());
            }
        }
        public void SendTest()
        {
            int actionCallCount = 0;
            var context         = UnitTestSynchronizationContext.Create();

            context.Send(state => actionCallCount++, null);
            Assert.AreEqual(1, actionCallCount);

            AssertHelper.ExpectedException <ArgumentNullException>(() => context.Send(null, null));

            context.Dispose();
            context.Send(state => actionCallCount++, null);
            Assert.AreEqual(1, actionCallCount);
        }
        public void Should_Get_Completed_Task_Value()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var data   = new { Foo = Task.FromResult("foo") };
                var target = ExpressionObserver.Create(data, o => o.Foo.StreamBinding());
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));

                Assert.Equal(new[] { "foo" }, result);

                GC.KeepAlive(data);
            }
        }
Beispiel #12
0
        public void Should_Get_Simple_Observable_Value()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var source = new BehaviorSubject <string>("foo");
                var data   = new { Foo = source };
                var target = new ExpressionObserver(data, "Foo");
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                source.OnNext("bar");
                sync.ExecutePostedCallbacks();

                Assert.Equal(new[] { PerspexProperty.UnsetValue, "foo", "bar" }, result);
            }
        }
Beispiel #13
0
        public void Should_Not_Get_Observable_Value_Without_Modifier_Char()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var source = new BehaviorSubject <string>("foo");
                var data   = new { Foo = source };
                var target = new ExpressionObserver(data, "Foo");
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                source.OnNext("bar");
                sync.ExecutePostedCallbacks();

                Assert.Equal(new[] { source }, result);
            }
        }
        public void Should_Get_Simple_Task_Value()
        {
            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.Equal(new[] { "foo" }, result);
            }
        }
Beispiel #15
0
        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 object[] { PerspexProperty.UnsetValue, "foo" }, result.ToArray());
            }
        }
Beispiel #16
0
        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.Equal(1, result.Count);
                Assert.IsType <Task <string> >(result[0]);
            }
        }
        protected override void OnInitialize()
        {
            base.OnInitialize();

            context = UnitTestSynchronizationContext.Create();

            catalog = new AggregateCatalog();
            OnCatalogInitialize(catalog);

            container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
            CompositionBatch batch = new CompositionBatch();
            batch.AddExportedValue(container);
            container.Compose(batch);

            ShellService shellService = Container.GetExportedValue<ShellService>();
            shellService.Settings = new AppSettings();
        }
        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 = ExpressionObserver.Create(data, o => o.Next.StreamBinding().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);
            }
        }
Beispiel #19
0
        public void Should_Get_Simple_Observable_Value()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var source = new BehaviorSubject <string>("foo");
                var data   = new { Foo = source };
                var target = ExpressionObserver.Create(data, o => o.Foo.StreamBinding());
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                source.OnNext("bar");
                sync.ExecutePostedCallbacks();

                Assert.Equal(new[] { "foo", "bar" }, result);

                GC.KeepAlive(data);
            }
        }
        public void Should_Get_Simple_Observable_Value_With_DataValidation_Enabled()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var source = new BehaviorSubject <string>("foo");
                var data   = new { Foo = source };
                var target = new ExpressionObserver(data, "Foo", true);
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                source.OnNext("bar");
                sync.ExecutePostedCallbacks();

                // What does it mean to have data validation on an observable? 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", "bar" }, result);
            }
        }
Beispiel #21
0
        public void Should_Work_With_Value_Type()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var source = new BehaviorSubject <int>(1);
                var data   = new { Foo = source };
                var target = ExpressionObserver.Create(data, o => o.Foo.StreamBinding());
                var result = new List <int>();

                var sub = target.Subscribe(x => result.Add((int)x));
                source.OnNext(42);
                sync.ExecutePostedCallbacks();

                Assert.Equal(new[] { 1, 42 }, result);

                GC.KeepAlive(data);
            }
        }
        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);
            }
        }
Beispiel #23
0
        public void Should_Get_Property_Value_From_Observable()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var data   = new Class1();
                var target = new ExpressionObserver(data, "Next.Foo");
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                data.Next.OnNext(new Class2("foo"));
                sync.ExecutePostedCallbacks();

                Assert.Equal(new[] { PerspexProperty.UnsetValue, "foo" }, result);

                sub.Dispose();
                Assert.Equal(0, data.SubscriptionCount);
            }
        }
        public void Should_Get_Property_Value_From_Observable_With_DataValidation_Enabled()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var data   = new Class1();
                var target = new ExpressionObserver(data, "Next.Foo", true);
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                data.Next.OnNext(new Class2("foo"));
                sync.ExecutePostedCallbacks();

                Assert.Equal(new[] { new BindingNotification("foo") }, result);

                sub.Dispose();
                Assert.Equal(0, data.PropertyChangedSubscriptionCount);
            }
        }
        public void Should_Not_Get_Task_Result_Without_StreamBinding()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var tcs    = new TaskCompletionSource <string>();
                var data   = new { Foo = tcs.Task };
                var target = ExpressionObserver.Create(data, o => o.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);
            }
        }
        public void CreateAndDisposingTest()
        {
            Assert.IsNull(SynchronizationContext.Current);
            using (var context = UnitTestSynchronizationContext.Create())
            {
                Assert.AreEqual(context, SynchronizationContext.Current);
                Assert.AreEqual(context, UnitTestSynchronizationContext.Current);

                using (var nestedContext = (UnitTestSynchronizationContext)context.CreateCopy())
                {
                    SynchronizationContext.SetSynchronizationContext(nestedContext);
                    Assert.AreEqual(nestedContext, SynchronizationContext.Current);
                    Assert.AreEqual(nestedContext, UnitTestSynchronizationContext.Current);
                }

                Assert.AreEqual(context, SynchronizationContext.Current);
            }
            Assert.IsNull(SynchronizationContext.Current);
        }
        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);
            }
        }
Beispiel #28
0
        public void Should_Get_Property_Value_From_Observable()
        {
            using (var sync = UnitTestSynchronizationContext.Begin())
            {
                var data   = new Class1();
                var target = ExpressionObserver.Create(data, o => o.Next.StreamBinding().Foo);
                var result = new List <object>();

                var sub = target.Subscribe(x => result.Add(x));
                data.Next.OnNext(new Class2("foo"));
                sync.ExecutePostedCallbacks();

                Assert.Equal(new[] { "foo" }, result);

                sub.Dispose();
                Assert.Equal(0, data.PropertyChangedSubscriptionCount);

                GC.KeepAlive(data);
            }
        }
        protected override void OnInitialize()
        {
            base.OnInitialize();

            context = UnitTestSynchronizationContext.Create();

            catalog = new AggregateCatalog();
            OnCatalogInitialize(catalog);

            container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
            CompositionBatch batch = new CompositionBatch();

            batch.AddExportedValue(container);
            container.Compose(batch);

            ShellService shellService = Container.GetExportedValue <ShellService>();

            shellService.Settings = new AppSettings();

            ServiceLocator.RegisterInstance <IChangeTrackerService>(new ChangeTrackerService());
        }
Beispiel #30
0
        public void WaitTest()
        {
            using (var context = UnitTestSynchronizationContext.Create())
            {
                int actionCallCount            = 0;
                Func <Task <int> > asyncAction = async() =>
                {
                    await Task.Delay(1);

                    Interlocked.Increment(ref actionCallCount);
                    return(actionCallCount);
                };

                var task = asyncAction();
                Assert.AreEqual(0, actionCallCount);

                task.Wait(context);
                Assert.AreEqual(1, actionCallCount);
            }

            AssertHelper.ExpectedException <ArgumentNullException>(() => UnitTestSynchronizationContextExtensions.Wait(Task.FromResult((object)null), null));
        }
Beispiel #31
0
        public void InvokeMaxEveryDelayTimeTestWithSynchronizationContext()
        {
            using var context = UnitTestSynchronizationContext.Create();
            int actionCallCount = 0;
            var throttledAction = new ThrottledAction(() => Interlocked.Add(ref actionCallCount, 1), ThrottledActionMode.InvokeMaxEveryDelayTime, TimeSpan.FromMilliseconds(100));

            Assert.IsFalse(throttledAction.IsRunning);

            throttledAction.InvokeAccumulated();
            Assert.IsTrue(throttledAction.IsRunning);
            throttledAction.InvokeAccumulated();
            throttledAction.InvokeAccumulated();

            // As long the unit test synchronization context is not executed the actionCallCount must not be increased.
            Task.Delay(200).Wait();
            Assert.AreEqual(0, actionCallCount);

            // Execute the unit test synchronization context.
            context.WaitFor(() => actionCallCount > 0, TimeSpan.FromMilliseconds(200));
            Assert.AreEqual(1, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);
        }