Skip to content

Commit

Permalink
fix: Add and apply NumberBox CommonStates
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Aug 30, 2021
1 parent 349d437 commit 500c262
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.UI.Xaml.Controls;
using MUXControlsTestApp.Utilities;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Automation;
using MUXControlsTestApp;

namespace Uno.UI.RuntimeTests.MUX.Microsoft_UI_Xaml_Controls
{
[TestClass]
public class NumberBoxTests : MUXApiTestBase
{
//[TestMethod]
//public void VerifyTextAlignmentPropogates()
//{
// var numberBox = SetupNumberBox();
// TextBox textBox = null;

// RunOnUIThread.Execute(() =>
// {
// Content.UpdateLayout();

// textBox = TestUtilities.FindDescendents<TextBox>(numberBox).Where(e => e.Name == "InputBox").Single();
// Assert.AreEqual(TextAlignment.Left, textBox.TextAlignment, "The default TextAlignment should be left.");

// numberBox.TextAlignment = TextAlignment.Right;
// Content.UpdateLayout();

// Assert.AreEqual(TextAlignment.Right, textBox.TextAlignment, "The TextAlignment should have been updated to Right.");
// });
//}

//[TestMethod]
//public void VerifyInputScopePropogates()
//{
// var numberBox = SetupNumberBox();

// RunOnUIThread.Execute(() =>
// {
// Content.UpdateLayout();
// var inputTextBox = TestUtilities.FindDescendents<TextBox>(numberBox).Where(e => e.Name == "InputBox").Single();

// Assert.AreEqual(1, inputTextBox.InputScope.Names.Count);
// Assert.AreEqual(InputScopeNameValue.Number, inputTextBox.InputScope.Names[0].NameValue, "The default InputScope should be 'Number'.");

// var scopeName = new InputScopeName();
// scopeName.NameValue = InputScopeNameValue.CurrencyAmountAndSymbol;
// var scope = new InputScope();
// scope.Names.Add(scopeName);

// numberBox.InputScope = scope;
// Content.UpdateLayout();

// Assert.AreEqual(1, inputTextBox.InputScope.Names.Count);
// Assert.AreEqual(InputScopeNameValue.CurrencyAmountAndSymbol, inputTextBox.InputScope.Names[0].NameValue, "The InputScope should be 'CurrencyAmountAndSymbol'.");
// });

// return;
//}

[TestMethod]
public void VerifyIsEnabledChangeUpdatesVisualState()
{
var numberBox = SetupNumberBox();

VisualStateGroup commonStatesGroup = null;
RunOnUIThread.Execute(() =>
{
// Check 1: Set IsEnabled to true.
numberBox.IsEnabled = true;
Content.UpdateLayout();

var numberBoxLayoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(numberBox, 0);
commonStatesGroup = VisualStateManager.GetVisualStateGroups(numberBoxLayoutRoot).First(vsg => vsg.Name.Equals("CommonStates"));

Assert.AreEqual("Normal", commonStatesGroup.CurrentState.Name);

// Check 2: Set IsEnabled to false.
numberBox.IsEnabled = false;
});
IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
Assert.AreEqual("Disabled", commonStatesGroup.CurrentState.Name);

// Check 3: Set IsEnabled back to true.
numberBox.IsEnabled = true;
});
IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
Assert.AreEqual("Normal", commonStatesGroup.CurrentState.Name);
});
}

[TestMethod]
public void VerifyUIANameBehavior()
{
NumberBox numberBox = null;
TextBox textBox = null;

RunOnUIThread.Execute(() =>
{
numberBox = new NumberBox();
Content = numberBox;
Content.UpdateLayout();

textBox = TestPage.FindVisualChildrenByType<TextBox>(numberBox)[0];
Assert.IsNotNull(textBox);
numberBox.Header = "Some header";
});

IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
VerifyUIAName("Some header");
numberBox.Header = new Button();
AutomationProperties.SetName(numberBox, "Some UIA name");
});

IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
VerifyUIAName("Some UIA name");
numberBox.Header = new Button();
});

IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
VerifyUIAName("Some UIA name");
});

void VerifyUIAName(string value)
{
Assert.AreEqual(value, FrameworkElementAutomationPeer.CreatePeerForElement(textBox).GetName());
}
}

private NumberBox SetupNumberBox()
{
NumberBox numberBox = null;
RunOnUIThread.Execute(() =>
{
numberBox = new NumberBox();
Content = numberBox;
});

return numberBox;
}

}
}
15 changes: 15 additions & 0 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/NumberBox/NumberBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,17 @@ private void InitializeTemplate()
registrations.Add(() => popupSpinUp.Click -= OnSpinUpClick);
}

IsEnabledChanged += OnIsEnabledChanged;
registrations.Add(() => IsEnabledChanged -= OnIsEnabledChanged);

// .NET rounds to 12 significant digits when displaying doubles, so we will do the same.
m_displayRounder.SignificantDigits = 12;

UpdateSpinButtonPlacement();
UpdateSpinButtonEnabled();

UpdateVisualStateForIsEnabledChange();

if (ReadLocalValue(ValueProperty) == DependencyProperty.UnsetValue
&& ReadLocalValue(TextProperty) != DependencyProperty.UnsetValue)
{
Expand Down Expand Up @@ -303,6 +308,16 @@ private void OnValidationModePropertyChanged(DependencyPropertyChangedEventArgs
UpdateSpinButtonEnabled();
}

private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs args)
{
UpdateVisualStateForIsEnabledChange();
}

private void UpdateVisualStateForIsEnabledChange()
{
VisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled", false);
}

private void OnNumberBoxGotFocus(object sender, RoutedEventArgs args)
{
// When the control receives focus, select the text
Expand Down
12 changes: 10 additions & 2 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/NumberBox/NumberBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@
<Setter.Value>
<ControlTemplate TargetType="local:NumberBox">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SpinButtonStates">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Target="HeaderContentPresenter.Foreground" Value="{ThemeResource TextControlHeaderForegroundDisabled}"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SpinButtonStates">
<VisualState x:Name="SpinButtonsCollapsed" />

<VisualState x:Name="SpinButtonsVisible">
Expand Down

0 comments on commit 500c262

Please sign in to comment.