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
4 changes: 2 additions & 2 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,15 +1386,15 @@ private void DynamoView_Loaded(object sender, EventArgs e)
}

// Load the new HomePage
if (IsNewAppHomeEnabled) LoadHomePage();
if (DynamoModel.IsTestMode || IsNewAppHomeEnabled) LoadHomePage();

loaded = true;
}

// Add the HomePage to the DynamoView once its loaded
private void LoadHomePage()
{
if (homePage == null && (startPage != null))
if (homePage == null && (startPage != null || DynamoModel.IsTestMode))
{
homePage = new UI.Views.HomePage();
homePage.DataContext = startPage;
Expand Down
50 changes: 47 additions & 3 deletions src/DynamoCoreWpf/Views/HomePage/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public partial class HomePage : UserControl, IDisposable
/// </summary>
internal static Action<string> TestHook { get; set; }

internal AsyncMethodState initState = AsyncMethodState.NotStarted;

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

var userDataFolder = CreateUniqueUserDataFolder(webBrowserUserDataFolder.FullName);
Copy link
Contributor

Choose a reason for hiding this comment

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

this has not been a problem with other vebview2 tests...
Are you certain this solves your test issues ?
Also maybe we should just create and cleanup these unique folders only during testing ? either use the testing flag...or make these APIs accessible to the test project and call them directly from there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For the time being, testing things locally, I didn't run on any sporadic test failures as before.

I was also thinking that maybe this is only necessary under test environment, so if the build completes correctly, I can make the necessary changes.


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 +199,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,14 +478,51 @@ 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(startPage != null) startPage.DynamoViewModel.PropertyChanged -= DynamoViewModel_PropertyChanged;
if (this.dynWebView != null)
{
var userDataFolder = this.dynWebView.CoreWebView2.Environment.UserDataFolder;
if (userDataFolder != null && Directory.Exists(userDataFolder))
{
// 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, true);
}
catch {
}
}

}
if (File.Exists(fontFilePath))
{
File.Delete(fontFilePath);
Expand Down
Loading
Loading