Skip to content

Commit 5a523c9

Browse files
committed
Added unit test
1 parent fd8aee5 commit 5a523c9

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

Microsoft.Toolkit.Uwp.UI/Triggers/ControlSizeTrigger.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public bool CanTrigger
3434
/// <summary>
3535
/// Gets or sets the max width at which to trigger.
3636
/// This value is exclusive, meaning this trigger
37-
/// could be active if the value is < MaxWidth.
37+
/// could be active if the value is less than MaxWidth.
3838
/// </summary>
3939
public double MaxWidth
4040
{
@@ -74,7 +74,7 @@ public double MinWidth
7474
/// <summary>
7575
/// Gets or sets the max height at which to trigger.
7676
/// This value is exclusive, meaning this trigger
77-
/// could be active if the value is < MaxHeight.
77+
/// could be active if the value is less than MaxHeight.
7878
/// </summary>
7979
public double MaxHeight
8080
{
@@ -133,6 +133,11 @@ public FrameworkElement TargetElement
133133
typeof(ControlSizeTrigger),
134134
new PropertyMetadata(null, OnTargetElementPropertyChanged));
135135

136+
/// <summary>
137+
/// Gets a value indicating whether the trigger is active.
138+
/// </summary>
139+
public bool IsActive { get; private set; }
140+
136141
private static void OnTargetElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
137142
{
138143
((ControlSizeTrigger)d).UpdateTargetElement((FrameworkElement)e.OldValue, (FrameworkElement)e.NewValue);
@@ -168,11 +173,13 @@ private void UpdateTrigger()
168173
return;
169174
}
170175

171-
SetActive(
172-
MinWidth <= TargetElement.ActualWidth &&
173-
TargetElement.ActualWidth < MaxWidth &&
174-
MinHeight <= TargetElement.ActualHeight &&
175-
TargetElement.ActualHeight < MaxHeight);
176+
bool activate = MinWidth <= TargetElement.ActualWidth &&
177+
TargetElement.ActualWidth < MaxWidth &&
178+
MinHeight <= TargetElement.ActualHeight &&
179+
TargetElement.ActualHeight < MaxHeight;
180+
181+
IsActive = activate;
182+
SetActive(activate);
176183
}
177184
}
178185
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.Toolkit.Uwp;
2+
using Microsoft.Toolkit.Uwp.UI.Triggers;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using System.Threading.Tasks;
5+
using Windows.UI.Xaml.Controls;
6+
7+
namespace UnitTests.UWP.UI.Triggers
8+
{
9+
[TestClass]
10+
[TestCategory("Test_ControlSizeTrigger")]
11+
public class Test_ControlSizeTrigger : VisualUITestBase
12+
{
13+
[DataTestMethod]
14+
[DataRow(450, 450, true)]
15+
[DataRow(400, 400, true)]
16+
[DataRow(500, 500, false)]
17+
[DataRow(399, 400, false)]
18+
[DataRow(400, 399, false)]
19+
public async Task ControlSizeTriggerTest(double width, double height, bool expectedResult)
20+
{
21+
await App.DispatcherQueue.EnqueueAsync(() =>
22+
{
23+
Grid grid = CreateGrid(width, height);
24+
var trigger = new ControlSizeTrigger();
25+
26+
trigger.TargetElement = grid;
27+
trigger.MaxHeight = 500;
28+
trigger.MinHeight = 400;
29+
trigger.MaxWidth = 500;
30+
trigger.MinWidth = 400;
31+
32+
Assert.AreEqual(expectedResult, trigger.IsActive);
33+
});
34+
}
35+
36+
private Grid CreateGrid(double width, double height)
37+
{
38+
var grid = new Grid()
39+
{
40+
Height = height,
41+
Width = width
42+
};
43+
grid.Measure(new Windows.Foundation.Size(1000, 1000));
44+
grid.Arrange(new Windows.Foundation.Rect(0, 0, 1000, 1000));
45+
grid.UpdateLayout();
46+
47+
return grid;
48+
}
49+
}
50+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@
250250
<Compile Include="UI\Extensions\Test_VisualExtensions.cs" />
251251
<Compile Include="UI\Person.cs" />
252252
<Compile Include="UI\Test_AdvancedCollectionView.cs" />
253+
<Compile Include="UI\Triggers\Test_ControlSizeTrigger.cs" />
253254
<Compile Include="VisualUITestBase.cs" />
254255
</ItemGroup>
255256
<ItemGroup>

0 commit comments

Comments
 (0)