diff --git a/WinUIGalleryUnitTests/UnitTestApp.xaml.cs b/WinUIGalleryUnitTests/UnitTestApp.xaml.cs index bc53d5909..ffe58902a 100644 --- a/WinUIGalleryUnitTests/UnitTestApp.xaml.cs +++ b/WinUIGalleryUnitTests/UnitTestApp.xaml.cs @@ -17,32 +17,31 @@ using Windows.Foundation; using Windows.Foundation.Collections; -namespace WinUIGalleryUnitTests +namespace WinUIGalleryUnitTests; + +public partial class UnitTestApp : Application { - public partial class UnitTestApp : Application + public UnitTestApp() { - public UnitTestApp() - { - this.InitializeComponent(); - } + this.InitializeComponent(); + } - protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) - { - Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI(); + protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) + { + Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI(); - s_window = new UnitTestAppWindow(); - s_window.Activate(); + s_window = new UnitTestAppWindow(); + s_window.Activate(); - UITestMethodAttribute.DispatcherQueue = s_window.DispatcherQueue; + UITestMethodAttribute.DispatcherQueue = s_window.DispatcherQueue; - Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(Environment.CommandLine); - } + Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(Environment.CommandLine); + } - private static UnitTestAppWindow s_window; + private static UnitTestAppWindow s_window; - public static UnitTestAppWindow UnitTestAppWindow - { - get { return s_window; } - } + public static UnitTestAppWindow UnitTestAppWindow + { + get { return s_window; } } } diff --git a/WinUIGalleryUnitTests/UnitTestAppWindow.xaml.cs b/WinUIGalleryUnitTests/UnitTestAppWindow.xaml.cs index 25549fb6c..3ae36837c 100644 --- a/WinUIGalleryUnitTests/UnitTestAppWindow.xaml.cs +++ b/WinUIGalleryUnitTests/UnitTestAppWindow.xaml.cs @@ -13,31 +13,30 @@ using Windows.Foundation; using Windows.Foundation.Collections; -namespace WinUIGalleryUnitTests +namespace WinUIGalleryUnitTests; + +public sealed partial class UnitTestAppWindow : Window { - public sealed partial class UnitTestAppWindow : Window + public UnitTestAppWindow() { - public UnitTestAppWindow() - { - this.InitializeComponent(); - } + this.InitializeComponent(); + } - public Grid RootGrid + public Grid RootGrid + { + get { - get - { - return rootGrid; - } + return rootGrid; } + } - public void AddToVisualTree(UIElement element) - { - this.RootGrid.Children.Add(element); - } + public void AddToVisualTree(UIElement element) + { + this.RootGrid.Children.Add(element); + } - public void CleanupVisualTree() - { - this.RootGrid.Children.Clear(); - } + public void CleanupVisualTree() + { + this.RootGrid.Children.Clear(); } } diff --git a/WinUIGalleryUnitTests/UnitTests.cs b/WinUIGalleryUnitTests/UnitTests.cs index 8c0aad7db..00da02004 100644 --- a/WinUIGalleryUnitTests/UnitTests.cs +++ b/WinUIGalleryUnitTests/UnitTests.cs @@ -18,177 +18,176 @@ using Windows.Foundation; using Windows.Foundation.Diagnostics; -namespace WinUIGalleryUnitTests +namespace WinUIGalleryUnitTests; + +[TestClass] +public class UnitTests { - [TestClass] - public class UnitTests + [TestMethod] + public async Task TestControlInfoDataSource() { - [TestMethod] - public async Task TestControlInfoDataSource() + var groups = await ControlInfoDataSource.Instance.GetGroupsAsync(); + var groupsList = groups.ToList(); + + var expectedGroups = new List { - var groups = await ControlInfoDataSource.Instance.GetGroupsAsync(); - var groupsList = groups.ToList(); + "Design guidance", + "Accessibility", + "Menus & toolbars", + "Collections", + "Date & time", + "Basic input", + "Status & info", + "Dialogs & flyouts", + "Scrolling", + "Layout", + "Navigation", + "Media", + "Styles", + "Text", + "Motion", + "Windowing", + "System" + }; + + Assert.AreEqual(expectedGroups.Count, groupsList.Count); + + int groupCount = expectedGroups.Count; + for(int i = 0; i < groupCount; i++) + { + var actualTitle = groupsList[i].Title; + Assert.AreEqual(expectedGroups[i], actualTitle); + } + } - var expectedGroups = new List + // Use the UITestMethod attribute for tests that need to run on the UI thread. + [UITestMethod] + public void TestWrapGrid() + { + WinUIGallery.WrapPanel wrapPanel = new WinUIGallery.WrapPanel(); + wrapPanel.Width = 250; + wrapPanel.Height = 250; + for (int i = 0; i < 4; i++) + { + Button button = new Button { - "Design guidance", - "Accessibility", - "Menus & toolbars", - "Collections", - "Date & time", - "Basic input", - "Status & info", - "Dialogs & flyouts", - "Scrolling", - "Layout", - "Navigation", - "Media", - "Styles", - "Text", - "Motion", - "Windowing", - "System" + Width = 120, + Height = 80, + Content = $"Button {i}" }; + wrapPanel.Children.Add(button); + } - Assert.AreEqual(expectedGroups.Count, groupsList.Count); + UnitTestApp.UnitTestAppWindow.AddToVisualTree(wrapPanel); + wrapPanel.UpdateLayout(); - int groupCount = expectedGroups.Count; - for(int i = 0; i < groupCount; i++) - { - var actualTitle = groupsList[i].Title; - Assert.AreEqual(expectedGroups[i], actualTitle); - } + List expectedLayouts = new List + { + new Rect(0, 0, 120, 80), + new Rect(120, 0, 120, 80), + new Rect(0, 80, 120, 80), + new Rect(120, 80, 120, 80) + }; + for (int i = 0; i < 4; i++) + { + var actualLayout = LayoutInformation.GetLayoutSlot(wrapPanel.Children[i] as FrameworkElement); + Assert.AreEqual(expectedLayouts[i], actualLayout); } + } - // Use the UITestMethod attribute for tests that need to run on the UI thread. - [UITestMethod] - public void TestWrapGrid() + // This test demonstrates executing test code both on and off the UI thread. + // We use the ExecuteOnUIThread method to run code on the UI thread. + [TestMethod] + public void MultiThreadTest() + { + Border border = null; + AutoResetEvent borderSizeChanged = new AutoResetEvent(false); + + ExecuteOnUIThread(() => { - WinUIGallery.WrapPanel wrapPanel = new WinUIGallery.WrapPanel(); - wrapPanel.Width = 250; - wrapPanel.Height = 250; - for (int i = 0; i < 4; i++) + Grid grid = new Grid { - Button button = new Button - { - Width = 120, - Height = 80, - Content = $"Button {i}" - }; - wrapPanel.Children.Add(button); - } - - UnitTestApp.UnitTestAppWindow.AddToVisualTree(wrapPanel); - wrapPanel.UpdateLayout(); + Width = 200, + }; - List expectedLayouts = new List + border = new Border { - new Rect(0, 0, 120, 80), - new Rect(120, 0, 120, 80), - new Rect(0, 80, 120, 80), - new Rect(120, 80, 120, 80) + Background = new SolidColorBrush(Colors.Green), + HorizontalAlignment = HorizontalAlignment.Stretch, + Child = new Rectangle + { + Fill = new SolidColorBrush(Colors.Red), + Width = 100, + } }; - for (int i = 0; i < 4; i++) - { - var actualLayout = LayoutInformation.GetLayoutSlot(wrapPanel.Children[i] as FrameworkElement); - Assert.AreEqual(expectedLayouts[i], actualLayout); - } - } - // This test demonstrates executing test code both on and off the UI thread. - // We use the ExecuteOnUIThread method to run code on the UI thread. - [TestMethod] - public void MultiThreadTest() - { - Border border = null; - AutoResetEvent borderSizeChanged = new AutoResetEvent(false); - - ExecuteOnUIThread(() => + border.SizeChanged += (s, e) => { - Grid grid = new Grid - { - Width = 200, - }; + borderSizeChanged.Set(); + }; - border = new Border - { - Background = new SolidColorBrush(Colors.Green), - HorizontalAlignment = HorizontalAlignment.Stretch, - Child = new Rectangle - { - Fill = new SolidColorBrush(Colors.Red), - Width = 100, - } - }; - - border.SizeChanged += (s, e) => - { - borderSizeChanged.Set(); - }; + grid.Children.Add(border); + + UnitTestApp.UnitTestAppWindow.AddToVisualTree(grid); + }); + Assert.IsTrue(borderSizeChanged.WaitOne()); - grid.Children.Add(border); - - UnitTestApp.UnitTestAppWindow.AddToVisualTree(grid); - }); - Assert.IsTrue(borderSizeChanged.WaitOne()); + ExecuteOnUIThread(() => + { + Assert.AreEqual(200, border.ActualWidth); - ExecuteOnUIThread(() => - { - Assert.AreEqual(200, border.ActualWidth); + border.HorizontalAlignment = HorizontalAlignment.Left; + }); - border.HorizontalAlignment = HorizontalAlignment.Left; - }); + Assert.IsTrue(borderSizeChanged.WaitOne()); - Assert.IsTrue(borderSizeChanged.WaitOne()); + ExecuteOnUIThread(() => + { + Assert.AreEqual(100, border.ActualWidth); + }); + } - ExecuteOnUIThread(() => - { - Assert.AreEqual(100, border.ActualWidth); - }); + private void ExecuteOnUIThread(Action action) + { + AutoResetEvent done = new AutoResetEvent(false); + DispatcherQueue dispatcherQueue = UnitTestApp.UnitTestAppWindow.DispatcherQueue; + if (dispatcherQueue.HasThreadAccess) + { + action(); } - - private void ExecuteOnUIThread(Action action) + else { - AutoResetEvent done = new AutoResetEvent(false); - DispatcherQueue dispatcherQueue = UnitTestApp.UnitTestAppWindow.DispatcherQueue; - if (dispatcherQueue.HasThreadAccess) - { - action(); - } - else + Exception exception = null; + var success = dispatcherQueue.TryEnqueue(() => { - Exception exception = null; - var success = dispatcherQueue.TryEnqueue(() => - { - try - { - action(); - } - catch (Exception ex) - { - exception = ex; - } - finally - { - done.Set(); - } - }); - Assert.IsTrue(success); - Assert.IsTrue(done.WaitOne()); - if(exception != null) + try { - Assert.Fail(exception.ToString()); + action(); } + catch (Exception ex) + { + exception = ex; + } + finally + { + done.Set(); + } + }); + Assert.IsTrue(success); + Assert.IsTrue(done.WaitOne()); + if(exception != null) + { + Assert.Fail(exception.ToString()); } } + } - [TestCleanup] - public void Cleanup() + [TestCleanup] + public void Cleanup() + { + ExecuteOnUIThread(() => { - ExecuteOnUIThread(() => - { - UnitTestApp.UnitTestAppWindow.CleanupVisualTree(); - }); - } + UnitTestApp.UnitTestAppWindow.CleanupVisualTree(); + }); } }