LoggingTests.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Avalonia.Controls;
  2. using Avalonia.Controls.Shapes;
  3. using Avalonia.Markup.Xaml;
  4. using Avalonia.UnitTests;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests.Logging
  7. {
  8. public class LoggingTests
  9. {
  10. [Fact]
  11. public void Control_Should_Not_Log_Binding_Errors_When_Detached_From_Visual_Tree()
  12. {
  13. using (UnitTestApplication.Start(TestServices.StyledWindow))
  14. {
  15. var xaml = @"
  16. <Window xmlns='https://github.com/avaloniaui'
  17. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  18. xmlns:local='clr-namespace:Avalonia.Base.UnitTests.Logging;assembly=Avalonia.UnitTests'>
  19. <Panel Name='panel'>
  20. <Rectangle Name='rect' Fill='{Binding $parent[Window].Background}'/>
  21. </Panel>
  22. </Window>";
  23. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  24. var calledTimes = 0;
  25. using var logSink = TestLogSink.Start((l, a, s, m, d) =>
  26. {
  27. if (l >= Avalonia.Logging.LogEventLevel.Warning)
  28. {
  29. calledTimes++;
  30. }
  31. });
  32. var panel = window.FindControl<Panel>("panel");
  33. var rect = window.FindControl<Rectangle>("rect");
  34. window.ApplyTemplate();
  35. ((Control)window.Presenter).ApplyTemplate();
  36. panel.Children.Remove(rect);
  37. Assert.Equal(0, calledTimes);
  38. }
  39. }
  40. [Fact]
  41. public void Control_Should_Log_Binding_Errors_When_No_Ancestor_With_Such_Name()
  42. {
  43. using (UnitTestApplication.Start(TestServices.StyledWindow))
  44. {
  45. var xaml = @"
  46. <Window xmlns='https://github.com/avaloniaui'
  47. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  48. xmlns:local='clr-namespace:Avalonia.Base.UnitTests.Logging;assembly=Avalonia.UnitTests'>
  49. <Panel>
  50. <Rectangle Fill='{Binding $parent[Grid].Background}'/>
  51. </Panel>
  52. </Window>";
  53. var calledTimes = 0;
  54. using var logSink = TestLogSink.Start((l, a, s, m, d) =>
  55. {
  56. if (l >= Avalonia.Logging.LogEventLevel.Warning && s is Rectangle)
  57. {
  58. calledTimes++;
  59. }
  60. });
  61. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  62. window.ApplyTemplate();
  63. ((Control)window.Presenter).ApplyTemplate();
  64. Assert.Equal(1, calledTimes);
  65. }
  66. }
  67. }
  68. }