Skip to content

Commit

Permalink
Close WebView2s during test cleanup
Browse files Browse the repository at this point in the history
Co-authored-by: Kristen Schau <krschau@users.noreply.github.com>
  • Loading branch information
krschau and krschau authored Sep 30, 2021
1 parent 9672592 commit 438e5eb
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions dev/WebView2/TestUI/WebView2BasicPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ private void TestNameComboBox_SelectionChanged(object sender, SelectionChangedEv
// if there are more webviews than just default one, remove them
if (WebView2Collection.Children.Count > 1)
{
RemoveAllButDefaultWebViewControl();
RemoveWebViewControls(true /* keep default webview */);
}

var MyWebView2 = FindName("MyWebView2") as WebView2;
Expand Down Expand Up @@ -886,24 +886,10 @@ void RemoveWebViewEventHandlers(WebView2 webview)
webview.CoreWebView2Initialized -= OnCoreWebView2Initialized;
}

void RemoveAllButDefaultWebViewControl()
void RemoveWebViewControls(bool keepDefault = false)
{
for (int i = WebView2Collection.Children.Count - 1; i > 0; i--)
{
StackPanel webviewStackPanel = WebView2Collection.Children[i] as StackPanel;
Debug.Assert(webviewStackPanel != null);
string stackPanelName = webviewStackPanel.Name;
string webviewName = stackPanelName.Replace("StackPanel", ""); // as per convention, stackpanel name is foowebviewStackPanel
var webview = FindName(webviewName) as WebView2;
RemoveWebViewEventHandlers(webview);

WebView2Collection.Children.RemoveAt(i);
}
}

void RemoveAllWebViewControls()
{
for (int i = WebView2Collection.Children.Count - 1; i >= 0; i--)
int lastToRemove = keepDefault ? 1 : 0;
for (int i = WebView2Collection.Children.Count - 1; i >= lastToRemove; i--)
{
StackPanel webviewStackPanel = WebView2Collection.Children[i] as StackPanel;
Debug.Assert(webviewStackPanel != null);
Expand All @@ -913,6 +899,7 @@ void RemoveAllWebViewControls()
if (webview != null)
{
RemoveWebViewEventHandlers(webview);
webview.Close();
}

WebView2Collection.Children.RemoveAt(i);
Expand All @@ -925,10 +912,10 @@ void RemoveAllWebViewControls()
// This is because the UIA tree from the browser HWND does not get disconnected synchronously on WebView2 Element destruction,
// and its presence after leaving the test page prevents the test runner from activating (also via UIA) the next test.
//
// TODO_WebView2: Work with Anaheim to provide a "Disconnect" method on CoreWebView2 that syncrhonously removes web UIA tree.
// TODO_WebView2: Work with Anaheim to provide a "Disconnect" method on CoreWebView2 that synchronously removes web UIA tree.
private async Task CleanupWebViewElements()
{
RemoveAllWebViewControls();
RemoveWebViewControls();
GC.Collect();
await Task.Delay(3000);
_areWebviewElementsCleanedUp = true;
Expand Down

0 comments on commit 438e5eb

Please sign in to comment.