NumericUpDownTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reactive.Subjects;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Data;
  7. using Avalonia.Interactivity;
  8. using Avalonia.Threading;
  9. using Avalonia.UnitTests;
  10. using Xunit;
  11. namespace Avalonia.Controls.UnitTests
  12. {
  13. public class NumericUpDownTests : ScopedTestBase
  14. {
  15. private static TestServices Services => TestServices.StyledWindow;
  16. [Fact]
  17. public void Text_Validation()
  18. {
  19. RunTest((control, textbox) =>
  20. {
  21. var exception = new InvalidCastException("failed validation");
  22. var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  23. control.Bind(NumericUpDown.TextProperty, textObservable);
  24. Dispatcher.UIThread.RunJobs();
  25. Assert.True(DataValidationErrors.GetHasErrors(control));
  26. Assert.True(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }));
  27. });
  28. }
  29. [Fact]
  30. public void Value_Validation()
  31. {
  32. RunTest((control, textbox) =>
  33. {
  34. var exception = new InvalidCastException("failed validation");
  35. var valueObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  36. control.Bind(NumericUpDown.ValueProperty, valueObservable);
  37. Dispatcher.UIThread.RunJobs();
  38. Assert.True(DataValidationErrors.GetHasErrors(control));
  39. Assert.True(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }));
  40. });
  41. }
  42. [Theory]
  43. [MemberData(nameof(Increment_Decrement_TestData))]
  44. public void Increment_Decrement_Tests(decimal min, decimal max, decimal? value, SpinDirection direction,
  45. decimal? expected)
  46. {
  47. var control = CreateControl();
  48. if (min > decimal.MinValue) control.Minimum = min;
  49. if (max < decimal.MaxValue) control.Maximum = max;
  50. control.Value = value;
  51. var spinner = GetSpinner(control);
  52. spinner.RaiseEvent(new SpinEventArgs(Spinner.SpinEvent, direction));
  53. Assert.Equal(control.Value, expected);
  54. }
  55. [Fact]
  56. public void FormatString_Is_Applied_Immediately()
  57. {
  58. RunTest((control, textbox) =>
  59. {
  60. const decimal value = 10.11m;
  61. // Establish and verify initial conditions.
  62. control.FormatString = "F0";
  63. control.Value = value;
  64. Assert.Equal(value.ToString("F0"), control.Text);
  65. // Check that FormatString is applied.
  66. control.FormatString = "F2";
  67. Assert.Equal(value.ToString("F2"), control.Text);
  68. });
  69. }
  70. public static IEnumerable<object[]> Increment_Decrement_TestData()
  71. {
  72. // if min and max are not defined and value was null, 0 should be ne new value after spin
  73. yield return new object[] { decimal.MinValue, decimal.MaxValue, null, SpinDirection.Decrease, 0m };
  74. yield return new object[] { decimal.MinValue, decimal.MaxValue, null, SpinDirection.Increase, 0m };
  75. // if no value was defined, but Min or Max are defined, use these as the new value
  76. yield return new object[] { -400m, -200m, null, SpinDirection.Decrease, -200m };
  77. yield return new object[] { 200m, 400m, null, SpinDirection.Increase, 200m };
  78. // Value should be clamped to Min / Max after spinning
  79. yield return new object[] { 200m, 400m, 5m, SpinDirection.Increase, 200m };
  80. yield return new object[] { 200m, 400m, 200m, SpinDirection.Decrease, 200m };
  81. }
  82. private void RunTest(Action<NumericUpDown, TextBox> test)
  83. {
  84. using (UnitTestApplication.Start(Services))
  85. {
  86. var control = CreateControl();
  87. TextBox textBox = GetTextBox(control);
  88. var window = new Window { Content = control };
  89. window.ApplyStyling();
  90. window.ApplyTemplate();
  91. window.Presenter.ApplyTemplate();
  92. Dispatcher.UIThread.RunJobs();
  93. test.Invoke(control, textBox);
  94. }
  95. }
  96. private NumericUpDown CreateControl()
  97. {
  98. var control = new NumericUpDown
  99. {
  100. Template = CreateTemplate()
  101. };
  102. control.ApplyTemplate();
  103. return control;
  104. }
  105. private static TextBox GetTextBox(NumericUpDown control)
  106. {
  107. return control.GetTemplateChildren()
  108. .OfType<ButtonSpinner>()
  109. .Select(b => b.Content)
  110. .OfType<TextBox>()
  111. .First();
  112. }
  113. private static ButtonSpinner GetSpinner(NumericUpDown control)
  114. {
  115. return control.GetTemplateChildren()
  116. .OfType<ButtonSpinner>()
  117. .First();
  118. }
  119. private static IControlTemplate CreateTemplate()
  120. {
  121. return new FuncControlTemplate<NumericUpDown>((control, scope) =>
  122. {
  123. var textBox =
  124. new TextBox
  125. {
  126. Name = "PART_TextBox"
  127. }.RegisterInNameScope(scope);
  128. return new ButtonSpinner
  129. {
  130. Name = "PART_Spinner",
  131. Content = textBox,
  132. }.RegisterInNameScope(scope);
  133. });
  134. }
  135. [Fact]
  136. public void TabIndex_Should_Be_Synchronized_With_Inner_TextBox()
  137. {
  138. RunTest((control, textbox) =>
  139. {
  140. // Set TabIndex on NumericUpDown
  141. control.TabIndex = 5;
  142. // The inner TextBox should inherit the same TabIndex
  143. Assert.Equal(5, textbox.TabIndex);
  144. // Change TabIndex and verify it gets synchronized
  145. control.TabIndex = 10;
  146. Assert.Equal(10, textbox.TabIndex);
  147. });
  148. }
  149. }
  150. }