Skip to content

Commit 03b6181

Browse files
authored
fix tests (#15159)
* fix tests * update * Update WorkspaceSaving.cs * update * Update RecordedTests.cs * Update RecordedTests.cs * udpate * update * Update TestObjects.cs * update * update
1 parent c46b4e2 commit 03b6181

File tree

13 files changed

+119
-142
lines changed

13 files changed

+119
-142
lines changed

src/DynamoCore/Models/DynamoModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ protected DynamoModel(IStartConfiguration config)
723723
//run process startup/reading on another thread so we don't block dynamo startup.
724724
//if we end up needing to control aspects of dynamo model or view startup that we can't make
725725
//event based/async then just run this on main thread - ie get rid of the Task.Run()
726-
var mainThreadSyncContext = new SynchronizationContext();
726+
var mainThreadSyncContext = SynchronizationContext.Current ?? new SynchronizationContext();
727727
Task.Run(() =>
728728
{
729729
try

src/DynamoCore/Scheduler/AsyncTaskExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading;
@@ -27,7 +27,7 @@ public static IDisposable Then(this AsyncTask task, AsyncTaskCompletedHandler ac
2727
/// <returns>An IDisposable representing the event subscription</returns>
2828
public static IDisposable ThenPost(this AsyncTask task, AsyncTaskCompletedHandler action, SynchronizationContext context = null)
2929
{
30-
if (context == null) context = new SynchronizationContext(); // uses the default
30+
if (context == null) context = SynchronizationContext.Current ?? new SynchronizationContext(); // uses the current or a default
3131
return task.Then((t) => context.Post((_) => action(task), null));
3232
}
3333

@@ -38,7 +38,7 @@ public static IDisposable ThenPost(this AsyncTask task, AsyncTaskCompletedHandle
3838
/// <returns>An IDisposable representing the event subscription</returns>
3939
public static IDisposable ThenSend(this AsyncTask task, AsyncTaskCompletedHandler action, SynchronizationContext context = null)
4040
{
41-
if (context == null) context = new SynchronizationContext(); // uses the default
41+
if (context == null) context = SynchronizationContext.Current ?? new SynchronizationContext(); // uses the current or a default
4242
return task.Then((t) => context.Send((_) => action(task), null));
4343
}
4444

src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,9 @@ private void UnsubscribeDispatcherEvents()
11151115
}
11161116
#endregion
11171117

1118-
private void InitializeAutomationSettings(string commandFilePath)
1118+
// InitializeAutomationSettings initializes the Dynamo automationSettings object.
1119+
// This method is meant to be accessed only within this class and within tests.
1120+
internal void InitializeAutomationSettings(string commandFilePath)
11191121
{
11201122
if (String.IsNullOrEmpty(commandFilePath) || !File.Exists(commandFilePath))
11211123
commandFilePath = null;

src/DynamoUtilities/DynamoFeatureFlagsManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal void CacheAllFlags()
6969
try
7070
{
7171
AllFlagsCache = JsonConvert.DeserializeObject<Dictionary<string, object>>(dataFromCLI);
72-
//invoke the flags retrieved event on the sync context which should be the main ui thread.
72+
// Invoke the flags retrieved event on the sync context which should be the main ui thread (if in Dynamo with UI) or the default SyncContext (if in headless mode).
7373
syncContext?.Send((_) =>
7474
{
7575
FlagsRetrieved?.Invoke();

test/DynamoCoreTests/UnitsOfMeasureTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal class UnitsOfMeasureTests : UnitTestBase
1111
[SetUp]
1212
public override void Setup()
1313
{
14+
base.Setup();
1415
Display.PrecisionFormat = "f4";
1516
}
1617

@@ -758,12 +759,6 @@ public void CanMapOverUnits()
758759
}
759760
internal class ForgeUnitsTests : UnitTestBase
760761
{
761-
[SetUp]
762-
public override void Setup()
763-
{
764-
765-
}
766-
767762
const string milimeters = "autodesk.unit.unit:millimeters";
768763
const string meters = "autodesk.unit.unit:meters";
769764

test/DynamoCoreWpfTests/DynamoTestUIBase.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Reflection;
77
using System.Threading;
8+
using System.Threading.Tasks;
89
using System.Windows;
910
using System.Windows.Threading;
1011
using Dynamo.Configuration;
@@ -15,7 +16,6 @@
1516
using Dynamo.Nodes;
1617
using Dynamo.Scheduler;
1718
using Dynamo.ViewModels;
18-
using Dynamo.Wpf.Utilities;
1919
using DynamoCoreWpfTests.Utility;
2020
using DynamoShapeManager;
2121
using DynamoUtilities;
@@ -32,6 +32,11 @@ public class DynamoTestUIBase
3232
protected DynamoView View { get; set; }
3333
protected DynamoModel Model { get; set; }
3434

35+
// Use this flag to skip trying to execute all the dispatched operations during the test lifetime.
36+
// This flag should only be used very sparingly
37+
protected bool SkipDispatcherFlush = false;
38+
protected int DispatcherOpsCounter = 0;
39+
3540
protected string ExecutingDirectory
3641
{
3742
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
@@ -84,6 +89,11 @@ public virtual void Start()
8489
View.Show();
8590

8691
SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext());
92+
93+
if (!SkipDispatcherFlush)
94+
{
95+
Dispatcher.CurrentDispatcher.Hooks.OperationPosted += Hooks_OperationPosted;
96+
}
8797
}
8898

8999
protected static void RaiseLoadedEvent(FrameworkElement element)
@@ -96,6 +106,14 @@ protected static void RaiseLoadedEvent(FrameworkElement element)
96106
eventMethod.Invoke(element, new object[] { args });
97107
}
98108

109+
private void Hooks_OperationPosted(object sender, DispatcherHookEventArgs e)
110+
{
111+
e.Operation.Task.ContinueWith((t) => {
112+
Interlocked.Decrement(ref DispatcherOpsCounter);
113+
}, TaskScheduler.Default);
114+
Interlocked.Increment(ref DispatcherOpsCounter);
115+
}
116+
99117
/// <summary>
100118
/// Derived test classes can override this method to provide different configurations.
101119
/// </summary>
@@ -114,6 +132,13 @@ protected virtual DynamoModel.IStartConfiguration CreateStartConfiguration(IPath
114132
[TearDown]
115133
public void Exit()
116134
{
135+
if (!SkipDispatcherFlush)
136+
{
137+
Dispatcher.CurrentDispatcher.Hooks.OperationPosted -= Hooks_OperationPosted;
138+
139+
DispatcherUtil.DoEventsLoop(() => DispatcherOpsCounter == 0);
140+
}
141+
117142
//Ensure that we leave the workspace marked as
118143
//not having changes.
119144
ViewModel.HomeSpace.HasUnsavedChanges = false;

test/DynamoCoreWpfTests/DynamoViewModelUnitTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class DynamoViewModelUnitTest : DSEvaluationUnitTestBase
3030
{
3131
protected DynamoViewModel ViewModel;
3232
protected Preloader preloader;
33+
protected string CommandFilePath;
3334

3435
protected override DynamoModel GetModel()
3536
{
@@ -131,6 +132,7 @@ protected void StartDynamo()
131132
new DynamoViewModel.StartConfiguration()
132133
{
133134
DynamoModel = model,
135+
CommandFilePath = CommandFilePath,
134136
Watch3DViewModel = new DefaultWatch3DViewModel(null, watch3DViewParams)
135137
});
136138

test/DynamoCoreWpfTests/PreviewBubbleTests.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,9 @@ public void PreviewBubble_ListMargin()
102102
nodeView.PreviewControl.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent));
103103

104104
// Fire transition on dynamo main ui thread.
105-
View.Dispatcher.Invoke(() =>
106-
{
107-
nodeView.PreviewControl.BindToDataSource();
108-
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed);
109-
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Expanded);
110-
});
105+
nodeView.PreviewControl.BindToDataSource();
106+
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed);
107+
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Expanded);
111108

112109
DispatcherUtil.DoEvents();
113110
var watchTree = nodeView.PreviewControl.ChildOfType<WatchTree>();
@@ -124,12 +121,9 @@ public void PreviewBubble_NumberMargin()
124121
nodeView.PreviewControl.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent));
125122

126123
// Fire transition on dynamo main ui thread.
127-
View.Dispatcher.Invoke(() =>
128-
{
129-
nodeView.PreviewControl.BindToDataSource();
130-
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed);
131-
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Expanded);
132-
});
124+
nodeView.PreviewControl.BindToDataSource();
125+
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed);
126+
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Expanded);
133127

134128
DispatcherUtil.DoEvents();
135129
var watchTree = nodeView.PreviewControl.ChildOfType<WatchTree>();
@@ -145,11 +139,9 @@ public void PreviewBubble_CloseWhenFocusInCodeBlock()
145139
var nodeView = NodeViewWithGuid("1382aaf9-9432-4cf0-86ae-c586d311767e");
146140
nodeView.PreviewControl.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent));
147141

148-
View.Dispatcher.Invoke(() =>
149-
{
150-
nodeView.PreviewControl.BindToDataSource();
151-
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed);
152-
});
142+
nodeView.PreviewControl.BindToDataSource();
143+
nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed);
144+
153145
DispatcherUtil.DoEvents();
154146
nodeView.ChildOfType<ICSharpCode.AvalonEdit.Editing.TextArea>().Focus();
155147

0 commit comments

Comments
 (0)