|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +namespace Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | + |
| 6 | +internal static class ConditionalTestHelper |
| 7 | +{ |
| 8 | + private static readonly Lazy<RuntimeTestPlatforms> _currentPlatform = new Lazy<RuntimeTestPlatforms>(GetCurrentPlatform); |
| 9 | + |
| 10 | + public static RuntimeTestPlatforms CurrentPlatform => _currentPlatform.Value; |
| 11 | + |
| 12 | + private static RuntimeTestPlatforms GetCurrentPlatform() |
| 13 | + { |
| 14 | + var values = Enum.GetValues<RuntimeTestPlatforms>(); |
| 15 | + var currentPlatform = default(RuntimeTestPlatforms); |
| 16 | + var counter = 0; |
| 17 | + foreach (var value in values.Where(HasSingleFlag)) |
| 18 | + { |
| 19 | + if (IsCurrentTarget(value)) |
| 20 | + { |
| 21 | + currentPlatform |= value; |
| 22 | + counter++; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + if (counter == 0) |
| 27 | + { |
| 28 | + throw new InvalidOperationException("Unrecognized runtime platform."); |
| 29 | + } |
| 30 | + |
| 31 | + if (counter > 1) |
| 32 | + { |
| 33 | + throw new InvalidOperationException($"Multiple runtime platforms detected ({currentPlatform:g})"); |
| 34 | + } |
| 35 | + |
| 36 | + return currentPlatform; |
| 37 | + } |
| 38 | + |
| 39 | + private static bool HasSingleFlag(RuntimeTestPlatforms value) |
| 40 | + { |
| 41 | + var numericValue = Convert.ToInt64(value); |
| 42 | + |
| 43 | + // Check if exactly one bit is set (i.e., power of two) |
| 44 | + return numericValue != 0 && (numericValue & (numericValue - 1)) == 0; |
| 45 | + } |
| 46 | + |
| 47 | + private static bool IsCurrentTarget(RuntimeTestPlatforms singlePlatform) |
| 48 | + { |
| 49 | + return singlePlatform switch |
| 50 | + { |
| 51 | + RuntimeTestPlatforms.NativeWinUI => IsWinUI(), |
| 52 | + RuntimeTestPlatforms.NativeWasm => IsNativeWasm(), |
| 53 | + RuntimeTestPlatforms.NativeAndroid => IsNativeAndroid(), |
| 54 | + RuntimeTestPlatforms.NativeIOS => IsNativeIOS(), |
| 55 | + RuntimeTestPlatforms.NativeMacCatalyst => IsNativeMacCatalyst(), |
| 56 | + RuntimeTestPlatforms.NativeTvOS => IsNativetvOS(), |
| 57 | + RuntimeTestPlatforms.SkiaGtk => IsSkia() && IsSkiaGtk(), |
| 58 | + RuntimeTestPlatforms.SkiaWpf => IsSkia() && IsSkiaWpf(), |
| 59 | + RuntimeTestPlatforms.SkiaWin32 => IsSkia() && IsSkiaWin32(), |
| 60 | + RuntimeTestPlatforms.SkiaX11 => IsSkia() && IsSkiaX11(), |
| 61 | + RuntimeTestPlatforms.SkiaMacOS => IsSkia() && IsSkiaMacOS(), |
| 62 | + RuntimeTestPlatforms.SkiaIslands => IsSkia() && IsSkiaIslands(), |
| 63 | + RuntimeTestPlatforms.SkiaWasm => IsSkia() && OperatingSystem.IsBrowser(), |
| 64 | + RuntimeTestPlatforms.SkiaAndroid => IsSkia() && OperatingSystem.IsAndroid(), |
| 65 | + RuntimeTestPlatforms.SkiaIOS => IsSkia() && OperatingSystem.IsIOS(), |
| 66 | + RuntimeTestPlatforms.SkiaTvOS => IsSkia() && OperatingSystem.IsTvOS(), |
| 67 | + RuntimeTestPlatforms.SkiaMacCatalyst => IsSkia() && OperatingSystem.IsMacCatalyst(), |
| 68 | + _ => throw new ArgumentException(nameof(singlePlatform)), |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + private static bool IsSkiaHostAssembly(string name) |
| 73 | +#if __SKIA__ |
| 74 | + => Microsoft.UI.Xaml.Application.Current.Host?.GetType().Assembly.GetName().Name == name; |
| 75 | +#else |
| 76 | + => false; |
| 77 | +#endif |
| 78 | + |
| 79 | + private static bool IsSkia() => |
| 80 | +#if __SKIA__ |
| 81 | + true; |
| 82 | +#else |
| 83 | + false; |
| 84 | +#endif |
| 85 | + |
| 86 | + private static bool IsWinUI() => |
| 87 | +#if WINAPPSDK |
| 88 | + true; |
| 89 | +#else |
| 90 | + false; |
| 91 | +#endif |
| 92 | + |
| 93 | + private static bool IsSkiaGtk() |
| 94 | + => IsSkiaHostAssembly("Uno.UI.Runtime.Skia.Gtk"); |
| 95 | + |
| 96 | + private static bool IsSkiaWpf() |
| 97 | + => IsSkiaHostAssembly("Uno.UI.Runtime.Skia.Wpf"); |
| 98 | + |
| 99 | + private static bool IsSkiaWin32() |
| 100 | + => IsSkiaHostAssembly("Uno.UI.Runtime.Skia.Win32"); |
| 101 | + |
| 102 | + private static bool IsSkiaX11() |
| 103 | + => IsSkiaHostAssembly("Uno.UI.Runtime.Skia.X11"); |
| 104 | + |
| 105 | + private static bool IsSkiaMacOS() |
| 106 | + => IsSkiaHostAssembly("Uno.UI.Runtime.Skia.MacOS"); |
| 107 | + |
| 108 | + private static bool IsSkiaBrowser() |
| 109 | + => IsSkiaHostAssembly("Uno.UI.Runtime.Skia.WebAssembly.Browser"); |
| 110 | + |
| 111 | + private static bool IsSkiaIslands() |
| 112 | +#if __SKIA__ |
| 113 | + => Microsoft.UI.Xaml.Application.Current.Host is null; |
| 114 | +#else |
| 115 | + => false; |
| 116 | +#endif |
| 117 | + |
| 118 | + private static bool IsNativeWasm() |
| 119 | + { |
| 120 | +#if __WASM__ |
| 121 | + return true; |
| 122 | +#else |
| 123 | + return false; |
| 124 | +#endif |
| 125 | + } |
| 126 | + |
| 127 | + private static bool IsNativeAndroid() |
| 128 | + { |
| 129 | +#if __ANDROID__ |
| 130 | + return true; |
| 131 | +#else |
| 132 | + return false; |
| 133 | +#endif |
| 134 | + } |
| 135 | + |
| 136 | + private static bool IsNativeIOS() |
| 137 | + { |
| 138 | +#if __IOS__ |
| 139 | + return true; |
| 140 | +#else |
| 141 | + return false; |
| 142 | +#endif |
| 143 | + } |
| 144 | + |
| 145 | + private static bool IsNativetvOS() |
| 146 | + { |
| 147 | +#if __TVOS__ |
| 148 | + return true; |
| 149 | +#else |
| 150 | + return false; |
| 151 | +#endif |
| 152 | + } |
| 153 | + |
| 154 | + private static bool IsNativeMacCatalyst() |
| 155 | + { |
| 156 | +#if __MACCATALYST__ |
| 157 | + return true; |
| 158 | +#else |
| 159 | + return false; |
| 160 | +#endif |
| 161 | + } |
| 162 | +} |
0 commit comments