Skip to content

Commit 028ec83

Browse files
authored
Merge pull request #5576 from Particular/fix-retention-slider-units-label
SCMU retention not showing correct value from config
2 parents 978ae51 + 2b9d6d2 commit 028ec83

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace ServiceControl.Config.Tests;
2+
3+
using System.Threading;
4+
using NUnit.Framework;
5+
using ServiceControl.Config.Xaml.Controls;
6+
7+
[TestFixture]
8+
[Apartment(ApartmentState.STA)]
9+
public class FormSliderTests
10+
{
11+
[Test]
12+
public void Summary_is_in_days_when_value_equals_minimum()
13+
{
14+
// Mirrors the order in which the retention slider bindings are applied in XAML
15+
// (initializer members assign in source order): Minimum coerces Value (0 -> 1) and
16+
// renders the summary before Units and Value are set. With a stored retention equal
17+
// to the minimum, the Value assignment causes no further change event, so the summary
18+
// rendered during coercion is the one the user sees.
19+
var slider = new FormSlider
20+
{
21+
Minimum = 1,
22+
Units = TimeSpanUnits.Days,
23+
Value = 1
24+
};
25+
26+
Assert.That(slider.Summary, Is.EqualTo("1 Day"));
27+
}
28+
29+
[Test]
30+
public void Summary_refreshes_when_units_change()
31+
{
32+
var slider = new FormSlider { Minimum = 2 };
33+
34+
Assert.That(slider.Summary, Is.EqualTo("2 Days"));
35+
36+
slider.Units = TimeSpanUnits.Hours;
37+
38+
Assert.That(slider.Summary, Is.EqualTo("2 Hours"));
39+
}
40+
}

src/ServiceControl.Config/Xaml/Controls/FormSlider.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ protected override void OnValueChanged(double oldValue, double newValue)
7070
{
7171
base.OnValueChanged(oldValue, newValue);
7272

73+
RefreshSummary();
74+
}
75+
76+
void RefreshSummary()
77+
{
7378
var period = Units == TimeSpanUnits.Days
7479
? TimeSpan.FromDays(Math.Truncate(Value))
7580
: TimeSpan.FromHours(Math.Truncate(Value));
@@ -107,8 +112,12 @@ void UpdateSummary(TimeSpan period)
107112
public static readonly DependencyProperty HeaderProperty =
108113
DependencyProperty.Register("Header", typeof(string), typeof(FormSlider), new PropertyMetadata(string.Empty));
109114

115+
// The summary label is computed in OnValueChanged, which can fire before the Units binding is
116+
// applied (e.g. when the Minimum binding coerces Value). Refresh the summary when Units changes
117+
// so the label never sticks with a unit it was not meant to use.
110118
public static readonly DependencyProperty UnitsProperty =
111-
DependencyProperty.Register("Units", typeof(TimeSpanUnits), typeof(FormSlider));
119+
DependencyProperty.Register("Units", typeof(TimeSpanUnits), typeof(FormSlider),
120+
new PropertyMetadata(TimeSpanUnits.Days, (d, _) => ((FormSlider)d).RefreshSummary()));
112121
}
113122

114123
public enum TimeSpanUnits

0 commit comments

Comments
 (0)