Integrazione 0.1.1: Febe per Windows si collega, VS Code, prove su Windows, storico e vista remota #4
No files matched your search
@@ -113,7 +113,7 @@ public sealed class WebView2CdpSession : ICdpSession, ICdpConnectionEvents, IAsy
|
||||
_log.Write(BrowserLogLevel.Debug, "CDP: WebView2 collegato.");
|
||||
if (reconnect)
|
||||
{
|
||||
Reconnected?.Invoke(this, EventArgs.Empty);
|
||||
RaiseReconnected();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,6 +275,31 @@ public sealed class WebView2CdpSession : ICdpSession, ICdpConnectionEvents, IAsy
|
||||
return JsonElement.ParseValue(ref reader);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Reconnected"/>, on the thread of <see cref="Attach"/> (WebView2's UI thread), one handler at a time: an
|
||||
/// error of a handler is logged and never reaches <see cref="Attach"/>, so it cannot undo the start of the new
|
||||
/// WebView that called it. Handlers only queue their work (re-enabling a domain is a command, never sent from here).
|
||||
/// </summary>
|
||||
private void RaiseReconnected()
|
||||
{
|
||||
if (Reconnected is not { } handlers)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var handler in handlers.GetInvocationList().Cast<EventHandler>())
|
||||
{
|
||||
try
|
||||
{
|
||||
handler(this, EventArgs.Empty);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
_log.Write(BrowserLogLevel.Warning, "CDP: errore in chi ascolta il nuovo collegamento con WebView2.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IDisposable SubscribeNative(IWebView2DevTools devTools, string eventName) =>
|
||||
devTools.Subscribe(eventName, json => _events.Writer.TryWrite(new QueuedEvent(eventName, json ?? string.Empty, null)));
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Febe.Browser.Diagnostics;
|
||||
using Febe.Browser.WebView2;
|
||||
|
||||
namespace Febe.Tests.Windows;
|
||||
|
||||
/// <summary>
|
||||
/// The attach of a new WebView to the session (after a crash or «Scollega»), without WebView2: an error of whoever
|
||||
/// listens to <see cref="WebView2CdpSession.Reconnected"/> must not undo a start that worked.
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public sealed class WebView2CdpSessionAttachTests
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task AFailingReconnectionHandlerDoesNotUndoTheAttach()
|
||||
{
|
||||
var log = new ListLog();
|
||||
await using var session = new WebView2CdpSession(log);
|
||||
var later = 0;
|
||||
session.Reconnected += (_, _) => throw new InvalidOperationException("guasto di chi ascolta");
|
||||
session.Reconnected += (_, _) => later++;
|
||||
|
||||
session.Attach(new SilentDevTools());
|
||||
session.Attach(new SilentDevTools());
|
||||
|
||||
Assert.IsTrue(session.IsAttached, "the new WebView stays attached");
|
||||
Assert.AreEqual(1, later, "the other handlers still hear of it");
|
||||
Assert.IsTrue(log.Lines.Any(l => l.StartsWith("Warning ", StringComparison.Ordinal) && l.Contains("InvalidOperationException", StringComparison.Ordinal)), string.Join('\n', log.Lines));
|
||||
Assert.AreEqual("{}", (await session.SendAsync("Page.enable", ct: CancellationToken.None)).GetRawText());
|
||||
}
|
||||
|
||||
private sealed class SilentDevTools : IWebView2DevTools
|
||||
{
|
||||
public Task<string> CallMethodAsync(string methodName, string parametersAsJson) => Task.FromResult("{}");
|
||||
|
||||
public IDisposable Subscribe(string eventName, Action<string> onEvent) => new Nothing();
|
||||
|
||||
private sealed class Nothing : IDisposable
|
||||
{
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ListLog : IBrowserLog
|
||||
{
|
||||
private readonly ConcurrentQueue<string> _lines = new();
|
||||
|
||||
public IReadOnlyList<string> Lines => [.. _lines];
|
||||
|
||||
public void Write(BrowserLogLevel level, string message, Exception? exception = null) =>
|
||||
_lines.Enqueue($"{level} {message} {exception?.GetType().Name}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user