Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homepage tests refactor #15078

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
54 changes: 46 additions & 8 deletions src/DynamoCoreWpf/Views/HomePage/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public partial class HomePage : UserControl, IDisposable
private static readonly string fontUrl = $"http://{virtualFolderName}/ArtifaktElement-Regular.woff";
private static readonly string virtualFolderPath = Path.Combine(Path.GetTempPath(), virtualFolderName);

private string fontFilePath;

private StartPageViewModel startPage;

/// <summary>
Expand All @@ -61,6 +59,9 @@ public partial class HomePage : UserControl, IDisposable
/// </summary>
internal static Action<string> TestHook { get; set; }

internal AsyncMethodState initState = AsyncMethodState.NotStarted;
private DirectoryInfo userDataFolder;

public HomePage()
{
InitializeComponent();
Expand Down Expand Up @@ -143,14 +144,17 @@ private async void UserControl_Loaded(object sender, System.Windows.RoutedEventA
PathHelper.CreateFolderIfNotExist(userDataDir.ToString());
var webBrowserUserDataFolder = userDataDir.Exists ? userDataDir : null;

userDataFolder = CreateUniqueUserDataFolder(webBrowserUserDataFolder.FullName);

dynWebView.CreationProperties = new CoreWebView2CreationProperties
{
UserDataFolder = webBrowserUserDataFolder.FullName
UserDataFolder = userDataFolder.FullName
};

//ContentRendered ensures that the webview2 component is visible.
try
{
initState = AsyncMethodState.Started;
await dynWebView.Initialize();

// Set WebView2 settings
Expand Down Expand Up @@ -194,6 +198,8 @@ private async void UserControl_Loaded(object sender, System.Windows.RoutedEventA
RequestShowSampleFilesInFolder,
RequestShowBackupFilesInFolder,
RequestShowTemplate));

initState = AsyncMethodState.Done;
}
catch (ObjectDisposedException ex)
{
Expand Down Expand Up @@ -471,17 +477,49 @@ internal void ApplicationLoaded()

#endregion

#region Helper Functions
/// <summary>
/// Create unique subfolder to allocate webView2 resources
/// </summary>
/// <param name="baseDir"></param>
/// <returns></returns>
private DirectoryInfo CreateUniqueUserDataFolder(string baseDir)
{
Directory.CreateDirectory(baseDir);

var uniqueSubfolderName = Guid.NewGuid().ToString();
var uniquePath = Path.Combine(baseDir, uniqueSubfolderName);
Directory.CreateDirectory(uniquePath);

return new DirectoryInfo(uniquePath);
}
#endregion

#region Dispose
public void Dispose()
{
DataContextChanged -= OnDataContextChanged;
if(startPage != null) startPage.DynamoViewModel.PropertyChanged -= DynamoViewModel_PropertyChanged;

this.dynWebView.CoreWebView2.NewWindowRequested -= CoreWebView2_NewWindowRequested;

if (File.Exists(fontFilePath))
if (startPage != null) startPage.DynamoViewModel.PropertyChanged -= DynamoViewModel_PropertyChanged;
if (this.dynWebView != null)
{
File.Delete(fontFilePath);
if (userDataFolder != null && Directory.Exists(userDataFolder.FullName))
{
// Try shutting down the browser with Dispose, and wait for process to exit
try
{
var webViewProcessId = Convert.ToInt32(this.dynWebView.CoreWebView2.BrowserProcessId);
var webViewProcess = Process.GetProcessById(webViewProcessId);

this.dynWebView.Dispose();
webViewProcess.WaitForExit(3000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might be able to use this event dynWebView.CoreWebView2.Environment.BrowserProcessExited

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, great, I can definitely do that. Thank you for the suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably have to clean it up too..


Directory.Delete(userDataFolder.FullName, true);
}
catch {
}
}

}
}
#endregion
Expand Down
Loading
Loading