Skip to content

Commit 7113a60

Browse files
Merge pull request #4321 from XAML-Knight/dev/AspectRatioConverter_II
Aspect Ratio: Add implicit converter for integer
2 parents 3381011 + 811f4e0 commit 7113a60

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/AspectRatio.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Globalization;
67

78
namespace Microsoft.Toolkit.Uwp.UI.Controls
@@ -63,6 +64,13 @@ public AspectRatio(double ratio)
6364
/// <param name="ratio"><see cref="double"/> value representing the <see cref="AspectRatio"/>.</param>
6465
public static implicit operator AspectRatio(double ratio) => new AspectRatio(ratio);
6566

67+
/// <summary>
68+
/// Implicit conversion operator to convert a <see cref="int"/> to an <see cref="AspectRatio"/> value.
69+
/// Creates a simple aspect ratio of N:1, where N is int
70+
/// </summary>
71+
/// <param name="width"><see cref="int"/> value representing the <see cref="AspectRatio"/>.</param>
72+
public static implicit operator AspectRatio(int width) => new AspectRatio(width, 1.0);
73+
6674
/// <summary>
6775
/// Converter to take a string aspect ration like "16:9" and convert it to an <see cref="AspectRatio"/> struct.
6876
/// Used automatically by XAML.

UITests/UITests.App/UITests.App.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@
227227
<Project>{e9faabfb-d726-42c1-83c1-cb46a29fea81}</Project>
228228
<Name>Microsoft.Toolkit.Uwp.UI.Controls.Core</Name>
229229
</ProjectReference>
230+
<ProjectReference Include="..\..\Microsoft.Toolkit.Uwp.UI.Controls.Primitives\Microsoft.Toolkit.Uwp.UI.Controls.Primitives.csproj">
231+
<Project>{84ab7dc5-95c9-4cf8-a370-d077e9e9ef1a}</Project>
232+
<Name>Microsoft.Toolkit.Uwp.UI.Controls.Primitives</Name>
233+
</ProjectReference>
230234
<ProjectReference Include="..\..\Microsoft.Toolkit.Uwp.UI.Media\Microsoft.Toolkit.Uwp.UI.Media.csproj">
231235
<Project>{75f9ee44-3efa-47bc-aedd-351b9834a0af}</Project>
232236
<Name>Microsoft.Toolkit.Uwp.UI.Media</Name>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Windows.Apps.Test.Automation;
6+
using Microsoft.Windows.Apps.Test.Foundation;
7+
using Microsoft.Windows.Apps.Test.Foundation.Controls;
8+
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Common;
9+
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Infra;
10+
11+
#if USING_TAEF
12+
using WEX.Logging.Interop;
13+
using WEX.TestExecution;
14+
using WEX.TestExecution.Markup;
15+
#else
16+
using Microsoft.VisualStudio.TestTools.UnitTesting;
17+
#endif
18+
19+
namespace UITests.Tests
20+
{
21+
[TestClass]
22+
public class ConstrainedBoxTest : UITestBase
23+
{
24+
[ClassInitialize]
25+
[TestProperty("RunAs", "User")]
26+
[TestProperty("Classification", "ScenarioTestSuite")]
27+
[TestProperty("Platform", "Any")]
28+
public static void ClassInitialize(TestContext testContext)
29+
{
30+
TestEnvironment.Initialize(testContext, WinUICsUWPSampleApp);
31+
}
32+
33+
[TestMethod]
34+
[TestPage("ConstrainedBoxTestPage")]
35+
public void Test_AspectRatioBoundToInteger_Placeholder()
36+
{
37+
// The test is if the AspectRatio can be bound to integer
38+
// This test method acts as a placeholder, to spawn the XAML test page
39+
// and test the binding to an integer
40+
}
41+
}
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Page x:Class="UITests.App.Pages.ConstrainedBoxTestPage"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
8+
mc:Ignorable="d">
9+
<!-- Merely binding the AspectRatio to an integer, here, is enough for a valid test
10+
of implicit operator for int -->
11+
<controls:ConstrainedBox x:Name="ConstrainedBox"
12+
Height="100"
13+
AspectRatio="{x:Bind IntegerWidth}">
14+
<Border HorizontalAlignment="Stretch"
15+
VerticalAlignment="Stretch"
16+
Background="Red" />
17+
</controls:ConstrainedBox>
18+
</Page>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using Microsoft.Toolkit.Uwp.UI.Controls;
7+
using Windows.UI.Xaml.Controls;
8+
9+
namespace UITests.App.Pages
10+
{
11+
public sealed partial class ConstrainedBoxTestPage : Page
12+
{
13+
public int IntegerWidth { get; set; } = 2;
14+
15+
public ConstrainedBoxTestPage()
16+
{
17+
this.InitializeComponent();
18+
}
19+
}
20+
}

UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.AspectRatio.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,46 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
9191
});
9292
}
9393

94+
[TestCategory("ConstrainedBox")]
95+
[TestMethod]
96+
public async Task Test_ConstrainedBox_Normal_IntegerWidth()
97+
{
98+
await App.DispatcherQueue.EnqueueAsync(async () =>
99+
{
100+
var treeRoot = XamlReader.Load(@"<Page
101+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
102+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
103+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
104+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2"" Height=""100"">
105+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
106+
</controls:ConstrainedBox>
107+
</Page>") as FrameworkElement;
108+
109+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
110+
111+
// Initialize Visual Tree
112+
await SetTestContentAsync(treeRoot);
113+
114+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
115+
116+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
117+
118+
// Check Size
119+
Assert.AreEqual(2.0, panel.AspectRatio, 0.01, "ApectRatio does not meet expected value of 2.0");
120+
121+
// Force Layout calculations
122+
panel.UpdateLayout();
123+
124+
var child = panel.Content as Border;
125+
126+
Assert.IsNotNull(child, "Could not find inner Border");
127+
128+
// Check Size
129+
Assert.AreEqual(200, child.ActualWidth, 0.01, "Actual width does not meet expected value of 200");
130+
Assert.AreEqual(100, child.ActualHeight, 0.01, "Actual height does not meet expected value of 100");
131+
});
132+
}
133+
94134
[TestCategory("ConstrainedBox")]
95135
[TestMethod]
96136
public void Test_ConstrainedBox_AspectRatioParsing_WidthAndHeight()

0 commit comments

Comments
 (0)