Skip to content

Commit

Permalink
Test crash fixes (#14789) (#14896)
Browse files Browse the repository at this point in the history
* update

* Update NodeModelAssemblyLoader.cs

* update

* update

* update

* update

* Update AnalyticsTests.cs

* Update SerializationTests.cs

* Update Setup.cs

* update

* update

* Update SplashScreen.xaml.cs

* Update PublishPackageViewModelTests.cs

* Update PublishPackageViewModelTests.cs

* update

* Update AssemblyInfo.cs

* Update TestUtilities.cs

* update

* upate

* update

* update

* update

* Update AnalyticsTests.cs

* Update WebView2Utilities.cs

* update

* update

* Update Setup.cs

* update

* update

* Update PeriodicEvaluationTests.cs

---------

Co-authored-by: pinzart90 <[email protected]>
Co-authored-by: pinzart <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2024
1 parent 0571f06 commit 1ceb403
Show file tree
Hide file tree
Showing 22 changed files with 173 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<UserControl x:Class="Dynamo.DocumentationBrowser.DocumentationBrowserView"
<UserControl x:Class="Dynamo.DocumentationBrowser.DocumentationBrowserView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="clr-namespace:Dynamo.UI;assembly=DynamoCoreWpf"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:wv2="clr-namespace:Dynamo.Wpf.Utilities;assembly=DynamoCoreWpf"
mc:Ignorable="d"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Expand All @@ -22,7 +22,7 @@
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<wv2:WebView2 Name="documentationBrowser"
<wv2:DynamoWebView2 Name="documentationBrowser"
Grid.Row="0"
HorizontalAlignment="Stretch"
Visibility="{Binding Path=ShowBrowser,Converter={StaticResource BooleanToVisibilityConverter}}"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public partial class DocumentationBrowserView : UserControl, IDisposable
public DocumentationBrowserView(DocumentationBrowserViewModel viewModel)
{
InitializeComponent();

this.DataContext = viewModel;
this.viewModel = viewModel;

Expand Down
45 changes: 42 additions & 3 deletions src/DynamoCoreWpf/Utilities/WebView2Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
using System.Windows;
using Dynamo.Wpf.Properties;
using Dynamo.Wpf.Utilities;
using DynamoUtilities;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;

namespace Dynamo.Utilities
namespace Dynamo.Wpf.Utilities
{
/// <summary>
/// Custom Webview2 class designed to have a safer cleanup logic and give better debugging capabilities
/// </summary>
public class DynamoWebView2 : WebView2
{
#region API/Data used for debugging/testing
private string tag;
#endregion

public DynamoWebView2() : base()
{
tag = TestUtilities.WebView2Tag;
}

protected override void Dispose(bool disposing)
{
if (System.Environment.CurrentManagedThreadId != Dispatcher.Thread.ManagedThreadId)
{
System.Console.WriteLine($"WebView2 instance with stamp {tag} is being disposed of on non-UI thread");
}
// We should dispose of webview2 only in the UI thread.
// Dispose can be called from the Finalizer (which can run on a non UI thread)
if (Dispatcher != null)
{
Dispatcher.Invoke(() =>
{
base.Dispose(disposing);
});
}
else
{
System.Console.WriteLine($"WebView2 instance with stamp {tag} is being disposed of but has no valid Dispatcher");
// Should we still try to dispose ? (might crash if not on UI thread)
base.Dispose(disposing);
}
}
}

/// <summary>
/// This class will contain several utility functions that will be used for the WebView2 component
/// </summary>
Expand All @@ -25,7 +64,7 @@ public static bool ValidateWebView2RuntimeInstalled()
catch (WebView2RuntimeNotFoundException)
{
var messageStr = Resources.ResourceManager.GetString("WebView2RequiredMessage");
if(messageStr.IndexOf("\\n") >= 0)
if (messageStr.IndexOf("\\n") >= 0)
messageStr = messageStr.Replace("\\n", "\n");

MessageBoxService.Show(messageStr,
Expand Down
8 changes: 4 additions & 4 deletions src/DynamoCoreWpf/Views/GuidedTour/PopupWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using System.Windows.Input;
using Dynamo.Utilities;
using Dynamo.Wpf.UI.GuidedTour;
using Dynamo.Wpf.Utilities;
using Dynamo.Wpf.ViewModels.GuidedTour;
using Microsoft.Web.WebView2.Wpf;

namespace Dynamo.Wpf.Views.GuidedTour
{
Expand All @@ -27,7 +27,7 @@ public partial class PopupWindow : Popup
//Field that indicates wheter popups are left-aligned or right-aligned
private const string menuDropAligment = "_menuDropAlignment";

internal WebView2 webBrowserComponent;
internal DynamoWebView2 webBrowserComponent;
//Assembly path to the Font file
private const string mainFontStylePath = "Dynamo.Wpf.Views.GuidedTour.HtmlPages.Resources.ArtifaktElement-Regular.woff";
//Assembly path to the Resources folder
Expand Down Expand Up @@ -93,7 +93,6 @@ private void PopupWindow_Closed(object sender, EventArgs e)
webBrowserComponent.Visibility = Visibility.Collapsed;
}


if (isClosingTour)
{
Opened -= PopupWindow_Opened;
Expand All @@ -114,7 +113,8 @@ private void PopupWindow_Opened(object sender, EventArgs e)

private async void InitWebView2Component()
{
webBrowserComponent = new WebView2();
webBrowserComponent = new DynamoWebView2();

webBrowserComponent.Margin = new System.Windows.Thickness(popupBordersOffSet, 0, 0, 0);
webBrowserComponent.Width = popupViewModel.Width;
//The height is subtracted by a const that sums the height of the header and footer of the popup
Expand Down
7 changes: 5 additions & 2 deletions src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using Dynamo.Wpf.Utilities;
using DynamoUtilities;
using Greg.AuthProviders;
using Microsoft.Web.WebView2.Core;
Expand Down Expand Up @@ -88,7 +89,7 @@ public DynamoView DynamoView
/// <summary>
/// The WebView2 Browser instance used to display splash screen
/// </summary>
internal WebView2 webView;
internal DynamoWebView2 webView;

/// <summary>
/// This delegate is used in StaticSplashScreenReady events
Expand Down Expand Up @@ -142,8 +143,9 @@ public SplashScreen(bool enableSignInButton = true)
loadingTimer = new Stopwatch();
loadingTimer.Start();

webView = new WebView2();
webView = new DynamoWebView2();
ShadowGrid.Children.Add(webView);

// Bind event handlers
webView.NavigationCompleted += WebView_NavigationCompleted;
DynamoModel.RequestUpdateLoadBarStatus += DynamoModel_RequestUpdateLoadBarStatus;
Expand Down Expand Up @@ -549,6 +551,7 @@ protected override void OnClosed(EventArgs e)
{
authManager.LoginStateChanged -= OnLoginStateChanged;
}

webView.Dispose();
webView = null;

Expand Down
2 changes: 1 addition & 1 deletion src/DynamoSandbox/DynamoCoreSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using Dynamo.DynamoSandbox.Properties;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using Dynamo.Wpf.UI;
using Dynamo.Wpf.Utilities;
using Dynamo.Wpf.ViewModels.Watch3D;

Expand Down
2 changes: 2 additions & 0 deletions src/DynamoUtilities/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@
[assembly: InternalsVisibleTo("DynamoCLI")]
[assembly: InternalsVisibleTo("NodeDocumentationMarkdownGenerator")]
[assembly: InternalsVisibleTo("DynamoUtilitiesTests")]
[assembly: InternalsVisibleTo("LibraryViewExtensionWebView2")]
[assembly: InternalsVisibleTo("Notifications")]
[assembly: InternalsVisibleTo("SystemTestServices")]
15 changes: 15 additions & 0 deletions src/DynamoUtilities/TestUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DynamoUtilities
{
// Bag of utilities designed for use during tests
internal static class TestUtilities
{
// Simple string that we can store in DynamoWebView2 instances so that we can track them down more easily
internal static string WebView2Tag;
}
}
9 changes: 5 additions & 4 deletions src/LibraryViewExtensionWebView2/LibraryViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
using Dynamo.LibraryViewExtensionWebView2.Handlers;
using Dynamo.LibraryViewExtensionWebView2.ViewModels;
using Dynamo.LibraryViewExtensionWebView2.Views;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Search;
using Dynamo.Search.SearchElements;
using Dynamo.ViewModels;
using Dynamo.Wpf.Interfaces;
using Dynamo.Wpf.UI.GuidedTour;
using Dynamo.Wpf.Utilities;
using Dynamo.Wpf.ViewModels;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
Expand Down Expand Up @@ -75,7 +75,7 @@ public class LibraryViewController : IDisposable
private FloatingLibraryTooltipPopup libraryViewTooltip;
// private ResourceHandlerFactory resourceFactory;
private IDisposable observer;
internal WebView2 browser;
internal DynamoWebView2 browser;
ScriptingObject twoWayScriptingObject;
private const string CreateNodeInstrumentationString = "Search-NodeAdded";
// TODO remove this when we can control the library state from Dynamo more precisely.
Expand Down Expand Up @@ -125,12 +125,13 @@ internal LibraryViewController(Window dynamoView, ICommandExecutive commandExecu
/// Also load the library.html and js files.
LibraryViewModel model = new LibraryViewModel();
LibraryView view = new LibraryView(model);

//Adding the LibraryView to the sidebar ensures that the webview2 component is visible.
var sidebarGrid = dynamoWindow.FindName("sidebarGrid") as Grid;
sidebarGrid.Children.Add(view);

browser = view.mainGrid.Children.OfType<WebView2>().FirstOrDefault();
browser = view.mainGrid.Children.OfType<DynamoWebView2>().FirstOrDefault();

browser.Loaded += Browser_Loaded;
browser.SizeChanged += Browser_SizeChanged;

Expand Down
6 changes: 3 additions & 3 deletions src/LibraryViewExtensionWebView2/Views/LibraryView.xaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<UserControl x:Class="Dynamo.LibraryViewExtensionWebView2.Views.LibraryView"
<UserControl x:Class="Dynamo.LibraryViewExtensionWebView2.Views.LibraryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:wv2="clr-namespace:Dynamo.Wpf.Utilities;assembly=DynamoCoreWpf"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Dynamo.LibraryViewExtensionWebView2.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="mainGrid">
<wv2:WebView2 Visibility="Hidden"/>
<wv2:DynamoWebView2 Visibility="Hidden"/>
</Grid>
</UserControl>
2 changes: 1 addition & 1 deletion src/LibraryViewExtensionWebView2/Views/LibraryView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


using System.Windows.Controls;
using Dynamo.LibraryViewExtensionWebView2.ViewModels;

Expand Down
6 changes: 3 additions & 3 deletions src/Notifications/View/NotificationUI.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Popup x:Class="Dynamo.Notifications.View.NotificationUI"
<Popup x:Class="Dynamo.Notifications.View.NotificationUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -7,7 +7,7 @@
xmlns:controls="clr-namespace:Dynamo.Controls;assembly=DynamoCoreWpf"
xmlns:p="clr-namespace:Dynamo.Wpf.Properties;assembly=DynamoCoreWpf"
xmlns:fa="http://schemas.fontawesome.io/icons/"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:wv2="clr-namespace:Dynamo.Wpf.Utilities;assembly=DynamoCoreWpf"
mc:Ignorable="d"
AllowsTransparency="True"
StaysOpen="False"
Expand Down Expand Up @@ -73,7 +73,7 @@

<Grid x:Name="mainPopupGrid" Background="White"
Width="{Binding PopupRectangleWidth}">
<wv2:WebView2 Name="webView" ></wv2:WebView2>
<wv2:DynamoWebView2 Name="webView" ></wv2:DynamoWebView2>
</Grid>
</Canvas>
</Popup>
Loading

0 comments on commit 1ceb403

Please sign in to comment.