Skip to content

Commit

Permalink
Added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaulino committed Aug 15, 2021
1 parent fd8aee5 commit 5a523c9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
21 changes: 14 additions & 7 deletions Microsoft.Toolkit.Uwp.UI/Triggers/ControlSizeTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public bool CanTrigger
/// <summary>
/// Gets or sets the max width at which to trigger.
/// This value is exclusive, meaning this trigger
/// could be active if the value is < MaxWidth.
/// could be active if the value is less than MaxWidth.
/// </summary>
public double MaxWidth
{
Expand Down Expand Up @@ -74,7 +74,7 @@ public double MinWidth
/// <summary>
/// Gets or sets the max height at which to trigger.
/// This value is exclusive, meaning this trigger
/// could be active if the value is < MaxHeight.
/// could be active if the value is less than MaxHeight.
/// </summary>
public double MaxHeight
{
Expand Down Expand Up @@ -133,6 +133,11 @@ public FrameworkElement TargetElement
typeof(ControlSizeTrigger),
new PropertyMetadata(null, OnTargetElementPropertyChanged));

/// <summary>
/// Gets a value indicating whether the trigger is active.
/// </summary>
public bool IsActive { get; private set; }

private static void OnTargetElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ControlSizeTrigger)d).UpdateTargetElement((FrameworkElement)e.OldValue, (FrameworkElement)e.NewValue);
Expand Down Expand Up @@ -168,11 +173,13 @@ private void UpdateTrigger()
return;
}

SetActive(
MinWidth <= TargetElement.ActualWidth &&
TargetElement.ActualWidth < MaxWidth &&
MinHeight <= TargetElement.ActualHeight &&
TargetElement.ActualHeight < MaxHeight);
bool activate = MinWidth <= TargetElement.ActualWidth &&
TargetElement.ActualWidth < MaxWidth &&
MinHeight <= TargetElement.ActualHeight &&
TargetElement.ActualHeight < MaxHeight;

IsActive = activate;
SetActive(activate);
}
}
}
50 changes: 50 additions & 0 deletions UnitTests/UnitTests.UWP/UI/Triggers/Test_ControlSizeTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.Toolkit.Uwp;
using Microsoft.Toolkit.Uwp.UI.Triggers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

namespace UnitTests.UWP.UI.Triggers
{
[TestClass]
[TestCategory("Test_ControlSizeTrigger")]
public class Test_ControlSizeTrigger : VisualUITestBase
{
[DataTestMethod]
[DataRow(450, 450, true)]
[DataRow(400, 400, true)]
[DataRow(500, 500, false)]
[DataRow(399, 400, false)]
[DataRow(400, 399, false)]
public async Task ControlSizeTriggerTest(double width, double height, bool expectedResult)
{
await App.DispatcherQueue.EnqueueAsync(() =>
{
Grid grid = CreateGrid(width, height);
var trigger = new ControlSizeTrigger();

trigger.TargetElement = grid;
trigger.MaxHeight = 500;
trigger.MinHeight = 400;
trigger.MaxWidth = 500;
trigger.MinWidth = 400;

Assert.AreEqual(expectedResult, trigger.IsActive);
});
}

private Grid CreateGrid(double width, double height)
{
var grid = new Grid()
{
Height = height,
Width = width
};
grid.Measure(new Windows.Foundation.Size(1000, 1000));
grid.Arrange(new Windows.Foundation.Rect(0, 0, 1000, 1000));
grid.UpdateLayout();

return grid;
}
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.UWP/UnitTests.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
<Compile Include="UI\Extensions\Test_VisualExtensions.cs" />
<Compile Include="UI\Person.cs" />
<Compile Include="UI\Test_AdvancedCollectionView.cs" />
<Compile Include="UI\Triggers\Test_ControlSizeTrigger.cs" />
<Compile Include="VisualUITestBase.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 5a523c9

Please sign in to comment.