Skip to content

Commit 6ef9abb

Browse files
committed
Add UiDispatcher.Invoke to the test engine.
Update UiContext to provide the GetEntryAssembly method to replace the Assembly.GetEntryAssembly() because the latter can return null and thus is not reliable.
1 parent 53fc9dd commit 6ef9abb

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

Rubberduck.API/API/VBA/ParserState.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Globalization;
55
using System.Linq;
6+
using System.Reflection;
67
using System.Runtime.InteropServices;
78
using Rubberduck.Common;
89
using Rubberduck.Parsing.PreProcessing;
@@ -57,7 +58,7 @@ public sealed class ParserState : IParserState, IDisposable
5758

5859
public ParserState()
5960
{
60-
UiContextProvider.Initialize();
61+
UiContextProvider.Initialize(Assembly.GetExecutingAssembly());
6162
_dispatcher = new UiDispatcher(UiContextProvider.Instance());
6263
}
6364

Rubberduck.Core/App.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
using Rubberduck.Parsing.Inspections.Resources;
1414
using Rubberduck.Parsing.UIContext;
1515
using Rubberduck.UI.Command;
16-
using Rubberduck.VBEditor.ComManagement;
1716
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
18-
using Rubberduck.VBEditor.Utility;
1917
using Rubberduck.VersionCheck;
2018
using Application = System.Windows.Forms.Application;
2119

@@ -50,8 +48,6 @@ public App(IVBE vbe,
5048
_checkVersionCommand = checkVersionCommand;
5149

5250
_configService.SettingsChanged += _configService_SettingsChanged;
53-
54-
UiContextProvider.Initialize();
5551
}
5652

5753
private void _configService_SettingsChanged(object sender, ConfigurationChangedEventArgs e)

Rubberduck.Core/UnitTesting/TestEngine.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NLog;
77
using Rubberduck.Parsing.Annotations;
88
using Rubberduck.Parsing.Symbols;
9+
using Rubberduck.Parsing.UIContext;
910
using Rubberduck.Parsing.VBA;
1011
using Rubberduck.UI;
1112
using Rubberduck.UI.UnitTesting;
@@ -21,17 +22,36 @@ public class TestEngine : ITestEngine
2122
private readonly RubberduckParserState _state;
2223
private readonly IFakesFactory _fakesFactory;
2324
private readonly IVBETypeLibsAPI _typeLibApi;
25+
private readonly IUiDispatcher _uiDispatcher;
2426

2527
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2628

27-
public TestEngine(TestExplorerModel model, IVBE vbe, RubberduckParserState state, IFakesFactory fakesFactory, IVBETypeLibsAPI typeLibApi)
29+
private bool _testRequested;
30+
private IEnumerable<TestMethod> _tests;
31+
32+
public TestEngine(TestExplorerModel model, IVBE vbe, RubberduckParserState state, IFakesFactory fakesFactory, IVBETypeLibsAPI typeLibApi, IUiDispatcher uiDispatcher)
2833
{
2934
Debug.WriteLine("TestEngine created.");
3035
Model = model;
3136
_vbe = vbe;
3237
_state = state;
3338
_fakesFactory = fakesFactory;
3439
_typeLibApi = typeLibApi;
40+
_uiDispatcher = uiDispatcher;
41+
42+
_state.StateChanged += StateChangedHandler;
43+
}
44+
45+
private void StateChangedHandler(object sender, ParserStateEventArgs e)
46+
{
47+
if (_testRequested && (e.State == ParserState.Ready))
48+
{
49+
_testRequested = false;
50+
_uiDispatcher.InvokeAsync(() =>
51+
{
52+
RunInternal(_tests);
53+
});
54+
}
3555
}
3656

3757
public TestExplorerModel Model { get; }
@@ -51,11 +71,18 @@ public void Refresh()
5171

5272
public void Run()
5373
{
74+
_testRequested = true;
75+
_tests = Model.LastRun;
76+
// We will run the tests once parsing has completed
5477
Refresh();
55-
Run(Model.LastRun);
5678
}
5779

5880
public void Run(IEnumerable<TestMethod> tests)
81+
{
82+
_uiDispatcher.InvokeAsync(() => RunInternal(tests));
83+
}
84+
85+
private void RunInternal(IEnumerable<TestMethod> tests)
5986
{
6087
var testMethods = tests as IList<TestMethod> ?? tests.ToList();
6188
if (!testMethods.Any())

Rubberduck.Core/UnitTesting/TestMethod.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Rubberduck.UI;
99
using Rubberduck.UI.Controls;
1010
using Rubberduck.VBEditor;
11-
using Rubberduck.VBEditor.Application;
1211
using Rubberduck.VBEditor.ComManagement.TypeLibsAPI;
1312
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
1413

Rubberduck.Main/Extension.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Rubberduck.VBEditor.ComManagement;
2020
using Rubberduck.VBEditor.Events;
2121
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
22+
using Rubberduck.VBEditor.Utility;
2223
using Windows = Rubberduck.VBEditor.SafeComWrappers.VBA.Windows;
2324

2425
namespace Rubberduck
@@ -219,7 +220,9 @@ private void Startup()
219220
currentDomain.AssemblyResolve += LoadFromSameFolder;
220221

221222
_container = new WindsorContainer().Install(new RubberduckIoCInstaller(_ide, _addin, _initialSettings));
222-
223+
224+
UiContextProvider.Initialize(Assembly.GetExecutingAssembly());
225+
223226
_app = _container.Resolve<App>();
224227
_app.Startup();
225228

Rubberduck.VBEEditor/Utility/UiContext.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using System.Threading;
34
using System.Threading.Tasks;
45

@@ -9,6 +10,7 @@ public interface IUiContextProvider
910
bool IsExecutingInUiContext();
1011
SynchronizationContext UiContext { get; }
1112
TaskScheduler UiTaskScheduler { get; }
13+
Assembly GetEntryAssembly { get; }
1214
}
1315

1416
public class UiContextProvider : IUiContextProvider
@@ -17,10 +19,11 @@ public class UiContextProvider : IUiContextProvider
1719
private static TaskScheduler TaskScheduler { get; set; }
1820
private static readonly UiContextProvider UiContextInstance = new UiContextProvider();
1921
private static readonly object Lock = new object();
22+
private static Assembly _entryAssembly;
2023

2124
private UiContextProvider() { }
2225

23-
public static void Initialize()
26+
public static void Initialize(Assembly entryAssembly)
2427
{
2528
lock (Lock)
2629
{
@@ -31,13 +34,15 @@ public static void Initialize()
3134

3235
Context = SynchronizationContext.Current;
3336
TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
37+
_entryAssembly = entryAssembly;
3438
}
3539
}
3640

3741
public static UiContextProvider Instance() => UiContextInstance;
3842

3943
public SynchronizationContext UiContext => Context;
4044
public TaskScheduler UiTaskScheduler => TaskScheduler;
45+
public Assembly GetEntryAssembly => _entryAssembly;
4146

4247
public bool IsExecutingInUiContext()
4348
{

0 commit comments

Comments
 (0)