Browse Source
Remove IRuntimePlatform.OperatingSystem API and replace with trimmable OperatingSystem APIpull/9803/head
committed by
GitHub
17 changed files with 105 additions and 177 deletions
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Runtime.InteropServices; |
|||
|
|||
namespace Avalonia.Compatibility |
|||
{ |
|||
internal sealed class OperatingSystemEx |
|||
{ |
|||
#if NET6_0_OR_GREATER
|
|||
public static bool IsWindows() => OperatingSystem.IsWindows(); |
|||
public static bool IsMacOS() => OperatingSystem.IsMacOS(); |
|||
public static bool IsLinux() => OperatingSystem.IsLinux(); |
|||
public static bool IsAndroid() => OperatingSystem.IsAndroid(); |
|||
public static bool IsIOS() => OperatingSystem.IsIOS(); |
|||
public static bool IsBrowser() => OperatingSystem.IsBrowser(); |
|||
public static bool IsOSPlatform(string platform) => OperatingSystem.IsOSPlatform(platform); |
|||
#else
|
|||
public static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
|||
public static bool IsMacOS() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX); |
|||
public static bool IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux); |
|||
public static bool IsAndroid() => IsOSPlatform("ANDROID"); |
|||
public static bool IsIOS() => IsOSPlatform("IOS"); |
|||
public static bool IsBrowser() => IsOSPlatform("BROWSER"); |
|||
public static bool IsOSPlatform(string platform) => RuntimeInformation.IsOSPlatform(OSPlatform.Create(platform)); |
|||
#endif
|
|||
} |
|||
} |
|||
@ -1,75 +0,0 @@ |
|||
using Avalonia.Controls; |
|||
using Avalonia.Platform; |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions; |
|||
|
|||
public class OnPlatformExtensionTests : XamlTestBase |
|||
{ |
|||
[Fact] |
|||
public void Should_Resolve_Default_Value() |
|||
{ |
|||
using (AvaloniaLocator.EnterScope()) |
|||
{ |
|||
AvaloniaLocator.CurrentMutable.Bind<IRuntimePlatform>() |
|||
.ToConstant(new TestRuntimePlatform(OperatingSystemType.Unknown)); |
|||
|
|||
var xaml = @"
|
|||
<UserControl xmlns='https://github.com/avaloniaui'
|
|||
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
|
|||
<TextBlock Text='{OnPlatform Default=""Hello World""}'/> |
|||
</UserControl>";
|
|||
|
|||
var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); |
|||
var textBlock = (TextBlock)userControl.Content!; |
|||
|
|||
Assert.Equal("Hello World", textBlock.Text); |
|||
} |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(OperatingSystemType.WinNT, "Im Windows")] |
|||
[InlineData(OperatingSystemType.OSX, "Im macOS")] |
|||
[InlineData(OperatingSystemType.Linux, "Im Linux")] |
|||
[InlineData(OperatingSystemType.Android, "Im Android")] |
|||
[InlineData(OperatingSystemType.iOS, "Im iOS")] |
|||
[InlineData(OperatingSystemType.Browser, "Im Browser")] |
|||
[InlineData(OperatingSystemType.Unknown, "Default value")] |
|||
public void Should_Resolve_Expected_Value_Per_Platform(OperatingSystemType currentPlatform, string expectedResult) |
|||
{ |
|||
using (AvaloniaLocator.EnterScope()) |
|||
{ |
|||
AvaloniaLocator.CurrentMutable.Bind<IRuntimePlatform>() |
|||
.ToConstant(new TestRuntimePlatform(currentPlatform)); |
|||
|
|||
var xaml = @"
|
|||
<UserControl xmlns='https://github.com/avaloniaui'
|
|||
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
|
|||
<TextBlock Text='{OnPlatform ""Default value"", |
|||
Windows=""Im Windows"", macOS=""Im macOS"", |
|||
Linux=""Im Linux"", Android=""Im Android"", |
|||
iOS=""Im iOS"", Browser=""Im Browser""}'/> |
|||
</UserControl>";
|
|||
|
|||
var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); |
|||
var textBlock = (TextBlock)userControl.Content!; |
|||
|
|||
Assert.Equal(expectedResult, textBlock.Text); |
|||
} |
|||
} |
|||
|
|||
private class TestRuntimePlatform : StandardRuntimePlatform |
|||
{ |
|||
private readonly OperatingSystemType _operatingSystemType; |
|||
|
|||
public TestRuntimePlatform(OperatingSystemType operatingSystemType) |
|||
{ |
|||
_operatingSystemType = operatingSystemType; |
|||
} |
|||
|
|||
public override RuntimePlatformInfo GetRuntimeInfo() |
|||
{ |
|||
return new RuntimePlatformInfo() { OperatingSystem = _operatingSystemType }; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue