From ff8e2bdfb2a623fac234c306952f914e4d6c9d5f Mon Sep 17 00:00:00 2001 From: McNets Date: Thu, 5 Dec 2024 13:37:41 +0100 Subject: [PATCH] feat: Added runtime tests to check platform detection. --- .../Given_RuntimePlatformDetected.cs | 83 +++++++++++++++++++ .../Xaml/UnoRuntimePlatformExtensions.cs | 19 +++-- 2 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 src/Uno.UI.RuntimeTests/Tests/Uno_UI_Toolkit/Given_RuntimePlatformDetected.cs diff --git a/src/Uno.UI.RuntimeTests/Tests/Uno_UI_Toolkit/Given_RuntimePlatformDetected.cs b/src/Uno.UI.RuntimeTests/Tests/Uno_UI_Toolkit/Given_RuntimePlatformDetected.cs new file mode 100644 index 000000000000..8914fdcf1377 --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/Uno_UI_Toolkit/Given_RuntimePlatformDetected.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Private.Infrastructure; +using Uno.Disposables; +using Uno.UI.RuntimeTests.Extensions; +using Uno.UI.RuntimeTests.Helpers; +using Uno.UI.Toolkit; +using Uno.UI.Helpers; +using Windows.UI; +using Windows.UI.ViewManagement; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Shapes; +using static Private.Infrastructure.TestServices; +using FluentAssertions; + +namespace Uno.UI.RuntimeTests.Tests.Uno_UI_Toolkit +{ + [TestClass] + [RunsOnUIThread] + public class Given_RuntimePlatformDetected + { +#if __SKIA__ + [TestMethod] + public void When_IsSkia() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsSkia().Should().BeTrue(); + } +#endif + +#if __IOS__ + [TestMethod] + public void When_IsIOS() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsIOS().Should().BeTrue(); + } +#endif + +#if __ANDROID__ + [TestMethod] + public void When_IsAndroid() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsAndroid().Should().BeTrue(); + } +#endif + +#if __MACCATALYST__ + [TestMethod] + public void When_IsMacCatalyst() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsMacCatalyst().Should().BeTrue(); + } +#endif + +#if __MACOS__ + [TestMethod] + public void When_IsMacOS() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsMacOS().Should().BeTrue(); + } +#endif + +#if __WASM__ + [TestMethod] + public void When_IsWebAssembly() + { + Uno.UI.Helpers.PlatformRuntimeHelper.Current.IsWebAssembly().Should().BeTrue(); + } +#endif + +#if !HAS_UNO + [TestMethod] + public void When_IsWindows() + { + Uno.UI.Toolkit.PlatformRuntimeHelper.Current.Should().Be(Uno.UI.Toolkit.UnoRuntimePlatform.Windows); + } +#endif + } +} diff --git a/src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs b/src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs index 8e6c183b52fb..76ebb14e666f 100644 --- a/src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs +++ b/src/Uno.UI/Helpers/Xaml/UnoRuntimePlatformExtensions.cs @@ -1,13 +1,22 @@ namespace Uno.UI.Helpers; -internal static class UnoRuntimePlatformExtensions +public static class UnoRuntimePlatformExtensions { - public static bool IsSkia(UnoRuntimePlatform platform) - { - return platform == UnoRuntimePlatform.SkiaFrameBuffer + public static bool IsSkia(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.SkiaFrameBuffer || platform == UnoRuntimePlatform.SkiaGtk || platform == UnoRuntimePlatform.SkiaMacOS || platform == UnoRuntimePlatform.SkiaWpfIslands || platform == UnoRuntimePlatform.SkiaX11; - } + + public static bool IsIOS(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.iOS; + + public static bool IsMacCatalyst(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.MacCatalyst; + + public static bool IsMacOS(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.MacOSX; + + public static bool IsWebAssembly(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.WebAssembly; + + public static bool IsWindows(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.Windows; + + public static bool IsAndroid(this UnoRuntimePlatform platform) => platform == UnoRuntimePlatform.Android; }