using System.Runtime.CompilerServices;
using Microsoft.Web.WebView2.Core;
namespace Febe.Tests.Windows.Support;
/// Whether these tests can run here: Windows with the WebView2 runtime (the tests are inconclusive otherwise).
internal static class WebView2Runtime
{
/// Null when WebView2 can run; otherwise why not, in Italian (the text of the inconclusive result).
public static string? UnavailableReason()
{
if (!OperatingSystem.IsWindows())
{
return "Prova solo per Windows: qui non c'è WebView2.";
}
try
{
return InstalledVersion() is null ? "Runtime WebView2 non installato su questo Windows." : null;
}
catch (Exception ex) when (ex is WebView2RuntimeNotFoundException or DllNotFoundException or BadImageFormatException)
{
return $"Runtime WebView2 non disponibile ({ex.GetType().Name}).";
}
}
/// The version of the installed WebView2 runtime, e.g. «141.0.3537.71».
[MethodImpl(MethodImplOptions.NoInlining)]
public static string? InstalledVersion() => CoreWebView2Environment.GetAvailableBrowserVersionString();
/// Marks the test inconclusive where WebView2 cannot run.
public static void Require()
{
if (UnavailableReason() is { } reason)
{
Assert.Inconclusive(reason);
}
}
}