Beispiel #1
0
        public void Bind_Logs_BindingError()
        {
            var target = new Class1();
            var source = new Subject <object>();
            var called = false;
            var expectedMessageTemplate = "Error binding to {Target}.{Property}: {Message}";

            LogCallback checkLogMessage = (level, area, src, mt, pv) =>
            {
                if (level == LogEventLevel.Error &&
                    area == LogArea.Binding &&
                    mt == expectedMessageTemplate)
                {
                    called = true;
                }
            };

            using (TestLogSink.Start(checkLogMessage))
            {
                target.Bind(Class1.QuxProperty, source);
                source.OnNext(6.7);
                source.OnNext(new BindingError(new InvalidOperationException("Foo")));

                Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
                Assert.True(called);
            }
        }
Beispiel #2
0
        public void Binding_To_Direct_Property_Logs_BindingError()
        {
            var target = new Class1();
            var source = new Subject <object>();
            var called = false;

            LogCallback checkLogMessage = (level, area, src, mt, pv) =>
            {
                if (level == LogEventLevel.Warning &&
                    area == LogArea.Binding &&
                    mt == "Error in binding to {Target}.{Property}: {Message}" &&
                    pv.Length == 3 &&
                    pv[0] is Class1 &&
                    object.ReferenceEquals(pv[1], Class1.FooProperty) &&
                    (string)pv[2] == "Binding Error Message")
                {
                    called = true;
                }
            };

            using (TestLogSink.Start(checkLogMessage))
            {
                target.Bind(Class1.FooProperty, source);
                source.OnNext("baz");
                source.OnNext(new BindingNotification(new InvalidOperationException("Binding Error Message"), BindingErrorType.Error));
            }

            Assert.True(called);
        }
        public void Invalid_FallbackValue_Logs_Error()
        {
            var called = false;

            LogCallback checkLogMessage = (level, area, src, mt, pv) =>
            {
                if (level == LogEventLevel.Warning &&
                    area == LogArea.Binding &&
                    mt == "Error in binding to {Target}.{Property}: {Message}" &&
                    pv.Length == 3 &&
                    pv[0] is ProgressBar &&
                    object.ReferenceEquals(pv[1], ProgressBar.ValueProperty) &&
                    (string)pv[2] == "Could not convert FallbackValue 'bar' to 'System.Double'")
                {
                    called = true;
                }
            };

            using (UnitTestApplication.Start(TestServices.StyledWindow))
                using (TestLogSink.Start(checkLogMessage))
                {
                    var xaml        = @"
<Window xmlns='https://github.com/avaloniaui'>
    <ProgressBar Maximum='10' Value='{Binding Value, FallbackValue=bar}'/>
</Window>";
                    var window      = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
                    var progressBar = (ProgressBar)window.Content;

                    window.DataContext = new { Value = "foo" };
                    window.ApplyTemplate();

                    Assert.Equal(0, progressBar.Value);
                    Assert.True(called);
                }
        }
Beispiel #4
0
        public void Control_Should_Log_Binding_Errors_When_No_Ancestor_With_Such_Name()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml        = @"
<Window xmlns='https://github.com/avaloniaui'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        xmlns:local='clr-namespace:Avalonia.Base.UnitTests.Logging;assembly=Avalonia.UnitTests'>
    <Panel>
    <Rectangle Fill='{Binding $parent[Grid].Background}'/>
  </Panel>
</Window>";
                var calledTimes = 0;
                using var logSink = TestLogSink.Start((l, a, s, m, d) =>
                {
                    if (l >= Avalonia.Logging.LogEventLevel.Warning && s is Rectangle)
                    {
                        calledTimes++;
                    }
                });
                var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
                window.ApplyTemplate();
                window.Presenter.ApplyTemplate();
                Assert.Equal(1, calledTimes);
            }
        }
Beispiel #5
0
        public void Control_Should_Not_Log_Binding_Errors_When_Detached_From_Visual_Tree()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        xmlns:local='clr-namespace:Avalonia.Base.UnitTests.Logging;assembly=Avalonia.UnitTests'>
    <Panel Name='panel'>
    <Rectangle Name='rect' Fill='{Binding $parent[Window].Background}'/>
  </Panel>
</Window>";

                var window      = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
                var calledTimes = 0;
                using var logSink = TestLogSink.Start((l, a, s, m, d) =>
                {
                    if (l >= Avalonia.Logging.LogEventLevel.Warning)
                    {
                        calledTimes++;
                    }
                });
                var panel = window.FindControl <Panel>("panel");
                var rect  = window.FindControl <Rectangle>("rect");
                window.ApplyTemplate();
                window.Presenter.ApplyTemplate();
                panel.Children.Remove(rect);
                Assert.Equal(0, calledTimes);
            }
        }
        public void Should_Not_Log_Binding_Error_When_Not_Attached_To_Logical_Tree()
        {
            var target = new Decorator {
                DataContext = "foo"
            };
            var called = false;

            LogCallback checkLogMessage = (level, area, src, mt, pv) =>
            {
                if (level >= Logging.LogEventLevel.Warning)
                {
                    called = true;
                }
            };

            using (TestLogSink.Start(checkLogMessage))
            {
                target.Bind(Decorator.TagProperty, new Binding("Foo"));
            }

            Assert.False(called);
        }