Skip to content

Commit

Permalink
Fix ConnectionPointCookie usages (#12333)
Browse files Browse the repository at this point in the history
* Fix ConnectionPointCookie usages

Broke this when we converted to CsWin32. Missed this as it silently fails, just won't get any events when it is broken.

Adds regression tests to validate the cookie is created and we actually get events returned in WebBrowser.

* Add usings to test
  • Loading branch information
JeremyKuhne authored Oct 16, 2024
1 parent c7a89dc commit 85d4b67
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal void StartEvents()
}

object? nativeObject = _host.GetOcx();
_connectionPoint = new ConnectionPointCookie(nativeObject, this, typeof(IPropertyNotifySink), throwException: false);
_connectionPoint = new ConnectionPointCookie(nativeObject, this, typeof(IPropertyNotifySink.Interface), throwException: false);
}

internal void StopEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,18 @@ internal void StartEvents()
{
try
{
_connectionPoint = new AxHost.ConnectionPointCookie(nativeObject, this, typeof(IPropertyNotifySink));
_connectionPoint = new AxHost.ConnectionPointCookie(nativeObject, this, typeof(IPropertyNotifySink.Interface));
}
#if DEBUG
catch (Exception)
{
throw;
}
#else
catch (Exception ex) when (!ex.IsCriticalException())
{
}
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3085,6 +3085,18 @@ public unsafe void AxHost_Ocx_Dispose_Success()
(ocx.Value->Release() - 1).Should().Be(0);
}

[WinFormsFact]
public unsafe void AxHost_Ocx_ConnectionPoint_Success()
{
using SubAxHost control = new(WebBrowserClsidString);
control.CreateControl();

object site = control.TestAccessor().Dynamic._oleSite;
AxHost.ConnectionPointCookie cookie = site.TestAccessor().Dynamic._connectionPoint;
cookie.Should().NotBeNull();
cookie.Connected.Should().BeTrue();
}

private class SubComponentEditor : ComponentEditor
{
public override bool EditComponent(ITypeDescriptorContext context, object component)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4751,4 +4751,32 @@ private class SubWebBrowser : WebBrowser

public new void WndProc(ref Message m) => base.WndProc(ref m);
}

[WinFormsFact]
public void WebBrowser_NavigateToFileFolder()
{
using Form form = new();
using WebBrowser browser = new()
{
Dock = DockStyle.Fill
};

string navigated = null;
browser.Navigated += (sender, e) =>
{
navigated = browser.Url.ToString();
form.Close();
};

form.Controls.Add(browser);

form.Load += (sender, e) =>
{
browser.Navigate(@"file://C:/");
};

form.Show();

navigated.Should().Be(@"file:///C:/");
}
}

0 comments on commit 85d4b67

Please sign in to comment.