Skip to content

Commit 190649c

Browse files
committed
added samples for the NumericUpDown control and tried to set up a first test (WIP)
1 parent ee04080 commit 190649c

File tree

6 files changed

+154
-0
lines changed

6 files changed

+154
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<UserControl x:Class="MaterialDesignThemes.UITests.Samples.UpDownControls.BoundNumericUpDown"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:local="clr-namespace:MaterialDesignThemes.UITests.Samples.UpDownControls"
6+
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
d:DataContext="{d:DesignInstance local:BoundNumericUpDownViewModel,
9+
IsDesignTimeCreatable=False}"
10+
d:DesignHeight="450"
11+
d:DesignWidth="800"
12+
mc:Ignorable="d">
13+
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
14+
<materialDesign:NumericUpDown Value="{Binding Value}" />
15+
<Button x:Name="btnToFocus" Content="Button to focus" />
16+
</StackPanel>
17+
</UserControl>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
using System.Windows.Documents;
10+
using System.Windows.Input;
11+
using System.Windows.Media;
12+
using System.Windows.Media.Imaging;
13+
using System.Windows.Navigation;
14+
using System.Windows.Shapes;
15+
using Xunit.Sdk;
16+
17+
namespace MaterialDesignThemes.UITests.Samples.UpDownControls;
18+
19+
/// <summary>
20+
/// Interaction logic for BoundNumericUpDown.xaml
21+
/// </summary>
22+
public partial class BoundNumericUpDown : UserControl
23+
{
24+
public BoundNumericUpDown()
25+
{
26+
this.ViewModel = new BoundNumericUpDownViewModel();
27+
this.DataContext = this.ViewModel;
28+
InitializeComponent();
29+
}
30+
31+
//I expose the ViewModel here so I can access it in the tests
32+
public BoundNumericUpDownViewModel ViewModel { get; }
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using CommunityToolkit.Mvvm.ComponentModel;
7+
8+
namespace MaterialDesignThemes.UITests.Samples.UpDownControls;
9+
10+
//If needed this can be refactored to be generic so all variations of the UpDownControl can use this as a VM
11+
public partial class BoundNumericUpDownViewModel : ObservableObject
12+
{
13+
[ObservableProperty]
14+
private int _value;
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Window x:Class="MaterialDesignThemes.UITests.Samples.UpDownControls.BoundNumericUpDownWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:local="clr-namespace:MaterialDesignThemes.UITests.Samples.UpDownControls"
6+
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
Title="BoundNumericUpDownWindow"
9+
Width="800"
10+
Height="450"
11+
Background="{DynamicResource MaterialDesign.Brush.Background}"
12+
FontFamily="{materialDesign:MaterialDesignFont}"
13+
Loaded="BoundNumericUpDownWindow_OnLoaded"
14+
TextElement.FontSize="13"
15+
TextElement.FontWeight="Regular"
16+
TextElement.Foreground="{DynamicResource MaterialDesign.Brush.Foreground}"
17+
TextOptions.TextFormattingMode="Ideal"
18+
TextOptions.TextRenderingMode="Auto"
19+
WindowStartupLocation="CenterScreen"
20+
mc:Ignorable="d">
21+
<local:BoundNumericUpDown />
22+
</Window>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
using System.Windows.Documents;
10+
using System.Windows.Input;
11+
using System.Windows.Media;
12+
using System.Windows.Media.Imaging;
13+
using System.Windows.Shapes;
14+
15+
namespace MaterialDesignThemes.UITests.Samples.UpDownControls;
16+
17+
/// <summary>
18+
/// Interaction logic for BoundNumericUpDownWindow.xaml
19+
/// </summary>
20+
public partial class BoundNumericUpDownWindow : Window
21+
{
22+
public BoundNumericUpDownWindow()
23+
{
24+
InitializeComponent();
25+
}
26+
27+
private void BoundNumericUpDownWindow_OnLoaded(object sender, RoutedEventArgs e)
28+
{
29+
Activate();
30+
Topmost = true;
31+
Topmost = false;
32+
Focus();
33+
}
34+
}

tests/MaterialDesignThemes.UITests/WPF/UpDownControls/NumericUpDownTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel;
2+
using MaterialDesignThemes.UITests.Samples.UpDownControls;
23

34
namespace MaterialDesignThemes.UITests.WPF.UpDownControls;
45

@@ -156,4 +157,36 @@ public async Task InternalTextBoxIsFocused_WhenGettingKeyboardFocus()
156157

157158
recorder.Success();
158159
}
160+
161+
[Fact]
162+
public async Task NumericUpDown_ValueSetGreaterThanMaximum_CoercesToMaximum()
163+
{
164+
await using var recorder = new TestRecorder(App);
165+
166+
//Arrange
167+
await App.InitializeWithMaterialDesign();
168+
IWindow window = await App.CreateWindow<BoundNumericUpDownWindow>();
169+
var userControl = await window.GetElement<BoundNumericUpDown>();
170+
var numericUpDown = await userControl.GetElement<NumericUpDown>();
171+
var buttonToFocus = await userControl.GetElement<Button>("btnToFocus");
172+
173+
//Set the current value to the maximum
174+
await numericUpDown.SetMaximum(10);
175+
await numericUpDown.SetValue(10);
176+
177+
//Act
178+
//Input a number greater than the maximum
179+
await numericUpDown.MoveKeyboardFocus();
180+
//await numericUpDown.SendInput(new KeyboardInput("99"));
181+
await numericUpDown.SendKeyboardInput($"99");
182+
await buttonToFocus.MoveKeyboardFocus();
183+
184+
//Assert
185+
//The value and bound property in the VM should be set to the maximum
186+
Assert.Equal(10, await numericUpDown.GetValue());
187+
var viewModel = await userControl.GetProperty<BoundNumericUpDownViewModel>(nameof(BoundNumericUpDown.ViewModel));
188+
Assert.Equal(10, viewModel.Value);
189+
190+
recorder.Success();
191+
}
159192
}

0 commit comments

Comments
 (0)