Skip to content

Commit 787665b

Browse files
committed
Core - WebBrowserExtensions throw explicit ObjectDisposedException if IsDisposed
- Make it clear that the ChromiumWebBrowser instance has been disposed. Previously when the browser was disposed BrowserCore would be null. An explicit ObjectDisposedException is now thrown.
1 parent cae407f commit 787665b

File tree

4 files changed

+184
-1
lines changed

4 files changed

+184
-1
lines changed

CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ public async Task CanLoadGoogle()
6464
}
6565
}
6666

67+
[Fact]
68+
public async Task ShouldRespectDisposed()
69+
{
70+
ChromiumWebBrowser browser;
71+
72+
using (browser = new ChromiumWebBrowser(CefExample.DefaultUrl))
73+
{
74+
var response = await browser.WaitForInitialLoadAsync();
75+
76+
Assert.True(response.Success);
77+
78+
var mainFrame = browser.GetMainFrame();
79+
Assert.True(mainFrame.IsValid);
80+
Assert.Equal(CefExample.DefaultUrl, mainFrame.Url);
81+
Assert.Equal(200, response.HttpStatusCode);
82+
83+
output.WriteLine("Url {0}", mainFrame.Url);
84+
}
85+
86+
Assert.True(browser.IsDisposed);
87+
88+
Assert.Throws<ObjectDisposedException>(() =>
89+
{
90+
browser.Copy();
91+
});
92+
}
93+
6794
[Fact]
6895
public async Task CanLoadInvalidDomain()
6996
{

CefSharp.Test/WinForms/WinFormsBrowserBasicFacts.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
//
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

5+
using System;
56
using System.Threading.Tasks;
7+
using CefSharp.Example;
68
using CefSharp.WinForms;
79
using Xunit;
810
using Xunit.Abstractions;
@@ -126,5 +128,32 @@ public async Task CanSetRequestContext()
126128
output.WriteLine("Url {0}", mainFrame.Url);
127129
}
128130
}
131+
132+
[WinFormsFact]
133+
public async Task ShouldRespectDisposed()
134+
{
135+
ChromiumWebBrowser browser;
136+
137+
using (browser = new ChromiumWebBrowser(CefExample.DefaultUrl))
138+
{
139+
browser.Size = new System.Drawing.Size(1024, 768);
140+
browser.CreateControl();
141+
142+
await browser.WaitForInitialLoadAsync();
143+
144+
var mainFrame = browser.GetMainFrame();
145+
Assert.True(mainFrame.IsValid);
146+
Assert.Equal(CefExample.DefaultUrl, mainFrame.Url);
147+
148+
output.WriteLine("Url {0}", mainFrame.Url);
149+
}
150+
151+
Assert.True(browser.IsDisposed);
152+
153+
Assert.Throws<ObjectDisposedException>(() =>
154+
{
155+
browser.Copy();
156+
});
157+
}
129158
}
130159
}

CefSharp.Test/Wpf/WpfBrowserBasicFacts.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

5+
using System;
56
using System.Threading.Tasks;
67
using System.Windows;
8+
using CefSharp.Example;
79
using CefSharp.Wpf;
810
using Xunit;
911
using Xunit.Abstractions;
@@ -111,5 +113,23 @@ public async Task CanSetRequestContextViaBuilder()
111113
output.WriteLine("Url {0}", mainFrame.Url);
112114
}
113115
}
116+
117+
[WpfFact]
118+
public async Task ShouldRespectDisposed()
119+
{
120+
ChromiumWebBrowser browser;
121+
122+
using (browser = new ChromiumWebBrowser(null, CefExample.DefaultUrl, new Size(1024, 786)))
123+
{
124+
await browser.WaitForInitialLoadAsync();
125+
}
126+
127+
Assert.True(browser.IsDisposed);
128+
129+
var ex = Assert.Throws<ObjectDisposedException>(() =>
130+
{
131+
browser.Copy();
132+
});
133+
}
114134
}
115135
}

0 commit comments

Comments
 (0)