From 984cc8dcd6f2100c093c3818322d9316f8640cef Mon Sep 17 00:00:00 2001 From: pinzart90 Date: Wed, 10 Jan 2024 09:56:50 -0500 Subject: [PATCH] update --- .../DocumentationBrowserView.xaml | 6 +-- .../DocumentationBrowserView.xaml.cs | 10 ---- .../Utilities/WebView2Utilities.cs | 40 +++++++++++++-- .../Views/GuidedTour/PopupWindow.xaml.cs | 13 ++--- .../Views/SplashScreen/SplashScreen.xaml.cs | 15 ++---- src/DynamoSandbox/DynamoCoreSetup.cs | 2 +- src/DynamoUtilities/TestUtilities.cs | 50 +------------------ .../LibraryViewController.cs | 15 ++---- .../Views/LibraryView.xaml | 6 +-- .../NotificationCenterController.cs | 10 ---- src/Notifications/View/NotificationUI.xaml | 6 +-- test/DynamoCoreTests/UnitTestBase.cs | 5 +- test/DynamoCoreWpfTests/DynamoTestUIBase.cs | 10 ++-- test/DynamoCoreWpfTests/SplashScreenTests.cs | 4 +- .../SystemTestServices/SystemTestBase.cs | 4 +- 15 files changed, 68 insertions(+), 128 deletions(-) diff --git a/src/DocumentationBrowserViewExtension/DocumentationBrowserView.xaml b/src/DocumentationBrowserViewExtension/DocumentationBrowserView.xaml index a5690cca527..c98cb5294e0 100644 --- a/src/DocumentationBrowserViewExtension/DocumentationBrowserView.xaml +++ b/src/DocumentationBrowserViewExtension/DocumentationBrowserView.xaml @@ -1,10 +1,10 @@ - - diff --git a/src/DocumentationBrowserViewExtension/DocumentationBrowserView.xaml.cs b/src/DocumentationBrowserViewExtension/DocumentationBrowserView.xaml.cs index 4916f439e81..40c4bbb3fa2 100644 --- a/src/DocumentationBrowserViewExtension/DocumentationBrowserView.xaml.cs +++ b/src/DocumentationBrowserViewExtension/DocumentationBrowserView.xaml.cs @@ -43,11 +43,6 @@ public DocumentationBrowserView(DocumentationBrowserViewModel viewModel) { InitializeComponent(); - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.IncrementWebView2(nameof(DocumentationBrowserView)); - } - this.DataContext = viewModel; this.viewModel = viewModel; @@ -121,11 +116,6 @@ protected virtual void Dispose(bool disposing) this.viewModel.LinkChanged -= NavigateToPage; if (this.documentationBrowser != null) { - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.DecrementWebView2(nameof(DocumentationBrowserView)); - } - this.documentationBrowser.NavigationStarting -= ShouldAllowNavigation; this.documentationBrowser.DpiChanged -= DocumentationBrowser_DpiChanged; if (this.documentationBrowser.CoreWebView2 != null) diff --git a/src/DynamoCoreWpf/Utilities/WebView2Utilities.cs b/src/DynamoCoreWpf/Utilities/WebView2Utilities.cs index b82589c481d..0e2e2fe339a 100644 --- a/src/DynamoCoreWpf/Utilities/WebView2Utilities.cs +++ b/src/DynamoCoreWpf/Utilities/WebView2Utilities.cs @@ -1,10 +1,44 @@ 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 { + public class DynamoWebView2 : WebView2 + { + #region API/Data used for debugging/testing + private string stamp; + #endregion + + public DynamoWebView2() : base() + { + stamp = TestUtilities.WebView2Stamp; + } + + protected override void Dispose(bool disposing) + { + if (System.Environment.CurrentManagedThreadId != Dispatcher.Thread.ManagedThreadId) + { + System.Console.WriteLine($"WebView2 instance with stamp {stamp} is being disposed of on non-UI thread"); + } + if (Dispatcher != null) + { + Dispatcher.Invoke(() => + { + base.Dispose(disposing); + }); + } + else + { + System.Console.WriteLine($"WebView2 instance with stamp {stamp} is being disposed of but has no valid Dispatcher"); + // Should we still try to dispose ? + base.Dispose(disposing); + } + } + } + /// /// This class will contain several utility functions that will be used for the WebView2 component /// @@ -25,7 +59,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, diff --git a/src/DynamoCoreWpf/Views/GuidedTour/PopupWindow.xaml.cs b/src/DynamoCoreWpf/Views/GuidedTour/PopupWindow.xaml.cs index f966652022e..6030b2c1118 100644 --- a/src/DynamoCoreWpf/Views/GuidedTour/PopupWindow.xaml.cs +++ b/src/DynamoCoreWpf/Views/GuidedTour/PopupWindow.xaml.cs @@ -5,12 +5,10 @@ using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; -using Dynamo.Models; using Dynamo.Utilities; using Dynamo.Wpf.UI.GuidedTour; +using Dynamo.Wpf.Utilities; using Dynamo.Wpf.ViewModels.GuidedTour; -using DynamoUtilities; -using Microsoft.Web.WebView2.Wpf; namespace Dynamo.Wpf.Views.GuidedTour { @@ -29,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 @@ -115,12 +113,7 @@ private void PopupWindow_Opened(object sender, EventArgs e) private async void InitWebView2Component() { - webBrowserComponent = new WebView2(); - - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.IncrementWebView2(nameof(PopupWindow)); - } + webBrowserComponent = new DynamoWebView2(); webBrowserComponent.Margin = new System.Windows.Thickness(popupBordersOffSet, 0, 0, 0); webBrowserComponent.Width = popupViewModel.Width; diff --git a/src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs b/src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs index 440138057e6..0fdc43afb74 100644 --- a/src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs +++ b/src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs @@ -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; @@ -88,7 +89,7 @@ public DynamoView DynamoView /// /// The WebView2 Browser instance used to display splash screen /// - internal WebView2 webView; + internal DynamoWebView2 webView; /// /// This delegate is used in StaticSplashScreenReady events @@ -142,14 +143,9 @@ public SplashScreen(bool enableSignInButton = true) loadingTimer = new Stopwatch(); loadingTimer.Start(); - webView = new WebView2(); + webView = new DynamoWebView2(); ShadowGrid.Children.Add(webView); - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.IncrementWebView2(nameof(SplashScreen)); - } - // Bind event handlers webView.NavigationCompleted += WebView_NavigationCompleted; DynamoModel.RequestUpdateLoadBarStatus += DynamoModel_RequestUpdateLoadBarStatus; @@ -558,11 +554,6 @@ protected override void OnClosed(EventArgs e) authManager.LoginStateChanged -= OnLoginStateChanged; } - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.DecrementWebView2(nameof(SplashScreen)); - } - webView.Dispose(); webView = null; diff --git a/src/DynamoSandbox/DynamoCoreSetup.cs b/src/DynamoSandbox/DynamoCoreSetup.cs index 71ff2ca5d8b..f00694831f4 100644 --- a/src/DynamoSandbox/DynamoCoreSetup.cs +++ b/src/DynamoSandbox/DynamoCoreSetup.cs @@ -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; diff --git a/src/DynamoUtilities/TestUtilities.cs b/src/DynamoUtilities/TestUtilities.cs index da972bbd9e8..8d4a3d7e13b 100644 --- a/src/DynamoUtilities/TestUtilities.cs +++ b/src/DynamoUtilities/TestUtilities.cs @@ -1,59 +1,13 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; -using System.Reflection; using System.Text; using System.Threading.Tasks; namespace DynamoUtilities { - internal class TestUtilities + internal static class TestUtilities { - internal static bool RunningFromNUnit = false; - static TestUtilities() - { - foreach (Assembly assem in AppDomain.CurrentDomain.GetAssemblies()) - { - // Can't do something like this as it will load the nUnit assembly - // if (assem == typeof(NUnit.Framework.Assert)) - - if (assem.FullName.ToLowerInvariant().StartsWith("nunit.framework")) - { - RunningFromNUnit = true; - break; - } - } - } - - private static ConcurrentDictionary WebView2Counter = new ConcurrentDictionary(); - internal static void IncrementWebView2(string containingType) - { - if (!WebView2Counter.TryGetValue(containingType, out _)) - { - WebView2Counter.TryAdd(containingType, 0); - } - WebView2Counter[containingType]++; - } - - internal static void DecrementWebView2(string containingType) - { - WebView2Counter[containingType]--; - } - - internal static void AssertCounters() - { - var exceptions = new List(); - foreach (var counter in WebView2Counter) - { - if (counter.Value != 0) - { - exceptions.Add(new Exception($"Unexpected number of webview2 allocations/deallocations: {counter.Value} webview2 instances for containing class {counter.Key}")); - } - } - WebView2Counter.Clear(); - if ( exceptions.Count > 0 ) { throw new AggregateException(exceptions.ToArray()); } - } + internal static string WebView2Stamp; } } diff --git a/src/LibraryViewExtensionWebView2/LibraryViewController.cs b/src/LibraryViewExtensionWebView2/LibraryViewController.cs index 716dbf451c9..3a0cd630845 100644 --- a/src/LibraryViewExtensionWebView2/LibraryViewController.cs +++ b/src/LibraryViewExtensionWebView2/LibraryViewController.cs @@ -20,6 +20,7 @@ using Dynamo.ViewModels; using Dynamo.Wpf.Interfaces; using Dynamo.Wpf.UI.GuidedTour; +using Dynamo.Wpf.Utilities; using Dynamo.Wpf.ViewModels; using DynamoUtilities; using ICSharpCode.AvalonEdit.Document; @@ -76,7 +77,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. @@ -131,12 +132,7 @@ internal LibraryViewController(Window dynamoView, ICommandExecutive commandExecu var sidebarGrid = dynamoWindow.FindName("sidebarGrid") as Grid; sidebarGrid.Children.Add(view); - browser = view.mainGrid.Children.OfType().FirstOrDefault(); - - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.IncrementWebView2(nameof(LibraryView)); - } + browser = view.mainGrid.Children.OfType().FirstOrDefault(); browser.Loaded += Browser_Loaded; browser.SizeChanged += Browser_SizeChanged; @@ -723,11 +719,6 @@ protected void Dispose(bool disposing) } if (this.browser != null) { - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.DecrementWebView2(nameof(LibraryView)); - } - browser.CoreWebView2.RemoveHostObjectFromScript("bridgeTwoWay"); browser.SizeChanged -= Browser_SizeChanged; browser.Loaded -= Browser_Loaded; diff --git a/src/LibraryViewExtensionWebView2/Views/LibraryView.xaml b/src/LibraryViewExtensionWebView2/Views/LibraryView.xaml index 276249a8b55..fcb98691ec7 100644 --- a/src/LibraryViewExtensionWebView2/Views/LibraryView.xaml +++ b/src/LibraryViewExtensionWebView2/Views/LibraryView.xaml @@ -1,13 +1,13 @@ - - + diff --git a/src/Notifications/NotificationCenterController.cs b/src/Notifications/NotificationCenterController.cs index 84c04d298aa..44714d32d9b 100755 --- a/src/Notifications/NotificationCenterController.cs +++ b/src/Notifications/NotificationCenterController.cs @@ -98,11 +98,6 @@ internal NotificationCenterController(DynamoView view, DynamoLogger dynLogger) }; logger = dynLogger; - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.IncrementWebView2(nameof(Notifications)); - } - // If user turns on the feature, they will need to restart Dynamo to see the count // This ensures no network traffic when Notification center feature is turned off if (dynamoViewModel.PreferenceSettings.EnableNotificationCenter && !dynamoViewModel.Model.NoNetworkMode ) @@ -336,11 +331,6 @@ protected virtual void Dispose(bool disposing) if (notificationUIPopup.webView != null) { - if (TestUtilities.RunningFromNUnit) - { - TestUtilities.DecrementWebView2(nameof(Notifications)); - } - notificationUIPopup.webView.Visibility = Visibility.Hidden; notificationUIPopup.webView.Loaded -= InitializeBrowserAsync; notificationUIPopup.webView.NavigationCompleted -= WebView_NavigationCompleted; diff --git a/src/Notifications/View/NotificationUI.xaml b/src/Notifications/View/NotificationUI.xaml index 9557761cc21..f3110fca342 100644 --- a/src/Notifications/View/NotificationUI.xaml +++ b/src/Notifications/View/NotificationUI.xaml @@ -1,4 +1,4 @@ - - + diff --git a/test/DynamoCoreTests/UnitTestBase.cs b/test/DynamoCoreTests/UnitTestBase.cs index f5aba882b7a..60ad5f7769a 100644 --- a/test/DynamoCoreTests/UnitTestBase.cs +++ b/test/DynamoCoreTests/UnitTestBase.cs @@ -69,7 +69,7 @@ public virtual void Setup() { System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Start test: {TestContext.CurrentContext.Test.Name}"); - TestUtilities.AssertCounters(); + TestUtilities.WebView2Stamp = TestContext.CurrentContext.Test.Name; SetupDirectories(); @@ -104,8 +104,7 @@ public virtual void Cleanup() { AppDomain.CurrentDomain.AssemblyResolve -= assemblyHelper.ResolveAssembly; } - - TestUtilities.AssertCounters(); + TestUtilities.WebView2Stamp = string.Empty; System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Finished test: {TestContext.CurrentContext.Test.Name}"); } diff --git a/test/DynamoCoreWpfTests/DynamoTestUIBase.cs b/test/DynamoCoreWpfTests/DynamoTestUIBase.cs index b253f7a9807..e6d3be7cafb 100644 --- a/test/DynamoCoreWpfTests/DynamoTestUIBase.cs +++ b/test/DynamoCoreWpfTests/DynamoTestUIBase.cs @@ -15,6 +15,7 @@ using Dynamo.Nodes; using Dynamo.Scheduler; using Dynamo.ViewModels; +using Dynamo.Wpf.Utilities; using DynamoCoreWpfTests.Utility; using DynamoShapeManager; using DynamoUtilities; @@ -42,8 +43,8 @@ protected string ExecutingDirectory public virtual void Start() { System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Start test: {TestContext.CurrentContext.Test.Name}"); - TestUtilities.AssertCounters(); - + TestUtilities.WebView2Stamp = TestContext.CurrentContext.Test.Name; + var assemblyPath = Assembly.GetExecutingAssembly().Location; preloader = new Preloader(Path.GetDirectoryName(assemblyPath)); preloader.Preload(); @@ -143,10 +144,7 @@ public void Exit() Console.WriteLine(ex.StackTrace); } - GC.Collect(); - GC.WaitForPendingFinalizers(); - - TestUtilities.AssertCounters(); + TestUtilities.WebView2Stamp = string.Empty; System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Finished test: {TestContext.CurrentContext.Test.Name}"); } diff --git a/test/DynamoCoreWpfTests/SplashScreenTests.cs b/test/DynamoCoreWpfTests/SplashScreenTests.cs index b11a7e7e723..cee97cc82fd 100644 --- a/test/DynamoCoreWpfTests/SplashScreenTests.cs +++ b/test/DynamoCoreWpfTests/SplashScreenTests.cs @@ -11,13 +11,13 @@ internal class SplashScreenTests [SetUp] public void SetUp() { - TestUtilities.AssertCounters(); + TestUtilities.WebView2Stamp = TestContext.CurrentContext.Test.Name; } [TearDown] public void CleanUp() { - TestUtilities.AssertCounters(); + TestUtilities.WebView2Stamp = string.Empty; } [Test] diff --git a/test/Libraries/SystemTestServices/SystemTestBase.cs b/test/Libraries/SystemTestServices/SystemTestBase.cs index 3b365106b78..f6d94450707 100644 --- a/test/Libraries/SystemTestServices/SystemTestBase.cs +++ b/test/Libraries/SystemTestServices/SystemTestBase.cs @@ -87,7 +87,7 @@ protected string ExecutingDirectory public virtual void Setup() { System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Start test: {TestContext.CurrentContext.Test.Name}"); - TestUtilities.AssertCounters(); + TestUtilities.WebView2Stamp = TestContext.CurrentContext.Test.Name; var testConfig = GetTestSessionConfiguration(); @@ -167,7 +167,7 @@ public virtual void TearDown() Console.WriteLine(ex.StackTrace); } - TestUtilities.AssertCounters(); + TestUtilities.WebView2Stamp = string.Empty; System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Finished test: {TestContext.CurrentContext.Test.Name} "); }