ControlBindingTests.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.Error &&
  35. area == LogArea.Binding &&
  36. mt == "Error 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] == "Object reference not set to an instance of an object. | " +
  41. "Could not convert FallbackValue 'bar' to 'System.Double'")
  42. {
  43. called = true;
  44. }
  45. };
  46. using (UnitTestApplication.Start(TestServices.StyledWindow))
  47. using (TestLogSink.Start(checkLogMessage))
  48. {
  49. var xaml = @"
  50. <Window xmlns='https://github.com/avaloniaui'>
  51. <ProgressBar Maximum='10' Value='{Binding Value, FallbackValue=bar}'/>
  52. </Window>";
  53. var loader = new AvaloniaXamlLoader();
  54. var window = (Window)loader.Load(xaml);
  55. var progressBar = (ProgressBar)window.Content;
  56. window.DataContext = new { Value = "foo" };
  57. window.ApplyTemplate();
  58. Assert.Equal(0, progressBar.Value);
  59. Assert.True(called);
  60. }
  61. }
  62. }
  63. }