ControlBindingTests.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Controls;
  4. using Avalonia.Logging;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  8. {
  9. public class ControlBindingTests
  10. {
  11. [Fact]
  12. public void Binding_ProgressBar_Value_To_Invalid_Value_Uses_FallbackValue()
  13. {
  14. using (UnitTestApplication.Start(TestServices.StyledWindow))
  15. {
  16. var xaml = @"
  17. <Window xmlns='https://github.com/avaloniaui'>
  18. <ProgressBar Maximum='10' Value='{Binding Value, FallbackValue=3}'/>
  19. </Window>";
  20. var loader = new AvaloniaXamlLoader();
  21. var window = (Window)loader.Load(xaml);
  22. var progressBar = (ProgressBar)window.Content;
  23. window.DataContext = new { Value = "foo" };
  24. window.ApplyTemplate();
  25. Assert.Equal(3, progressBar.Value);
  26. }
  27. }
  28. [Fact]
  29. public void Invalid_FallbackValue_Logs_Error()
  30. {
  31. var called = false;
  32. LogCallback checkLogMessage = (level, area, src, mt, pv) =>
  33. {
  34. if (level == LogEventLevel.Warning &&
  35. area == LogArea.Binding &&
  36. mt == "Error in binding to {Target}.{Property}: {Message}" &&
  37. pv.Length == 3 &&
  38. pv[0] is ProgressBar &&
  39. object.ReferenceEquals(pv[1], ProgressBar.ValueProperty) &&
  40. (string)pv[2] == "Could not convert FallbackValue 'bar' to 'System.Double'")
  41. {
  42. called = true;
  43. }
  44. };
  45. using (UnitTestApplication.Start(TestServices.StyledWindow))
  46. using (TestLogSink.Start(checkLogMessage))
  47. {
  48. var xaml = @"
  49. <Window xmlns='https://github.com/avaloniaui'>
  50. <ProgressBar Maximum='10' Value='{Binding Value, FallbackValue=bar}'/>
  51. </Window>";
  52. var loader = new AvaloniaXamlLoader();
  53. var window = (Window)loader.Load(xaml);
  54. var progressBar = (ProgressBar)window.Content;
  55. window.DataContext = new { Value = "foo" };
  56. window.ApplyTemplate();
  57. Assert.Equal(0, progressBar.Value);
  58. Assert.True(called);
  59. }
  60. }
  61. }
  62. }