Skip to content

Commit 4eb0703

Browse files
authored
Publish frontend message when Auto Run changes, added integration test. (#2754)
* Publish frontend message when Auto Run changes, added integration test. * Remove testing for persistence since it doesn't work well in a parallel execution in CI * Implement review feedback
1 parent f102679 commit 4eb0703

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

MobiFlight/BrowserMessages/MessageExchange.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ public class MessageExchange : IMessageExchange
1414
private static MessageExchange _instance;
1515
private IMessagePublisher _messagePublisher;
1616

17+
/// <summary>
18+
/// Setting a contextProvider is only required for integration tests
19+
/// Provide a () => null provider so that the synchronization context is not used during unit tests,
20+
/// Outside of unit tests, a working synchronization context will automatically be available
21+
/// </summary>
22+
private Func<System.Threading.SynchronizationContext> _syncContextProvider;
23+
1724
public static MessageExchange Instance
1825
{
1926
get
@@ -91,8 +98,10 @@ private void PublishReceivedMessage(string jsonMessage)
9198
try
9299
{
93100
var deserializedPayload = JsonConvert.DeserializeObject(eventToPublish.payload.ToString(), eventType);
94-
var synchronizationContext = System.Threading.SynchronizationContext.Current;
95-
101+
var synchronizationContext = _syncContextProvider != null
102+
? _syncContextProvider.Invoke()
103+
: System.Threading.SynchronizationContext.Current;
104+
96105
foreach (var subscriber in subscribers)
97106
{
98107
Action invokeSubscriber = () =>
@@ -154,5 +163,9 @@ public void Unsubscribe<TEvent>(Action<TEvent> callback)
154163
}
155164
}
156165
}
166+
public void SetSynchronizationContextProvider(Func<System.Threading.SynchronizationContext> provider)
167+
{
168+
_syncContextProvider = provider;
169+
}
157170
}
158171
}

MobiFlightUnitTests/UI/MainFormTests.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using MobiFlight.BrowserMessages.Incoming;
55
using MobiFlight.BrowserMessages.Outgoing;
66
using MobiFlight.Controllers;
7+
using Newtonsoft.Json;
78
using System;
89
using System.Collections.Generic;
910
using System.Collections.Specialized;
@@ -17,6 +18,7 @@ namespace MobiFlight.UI.Tests
1718
[TestClass()]
1819
public class MainFormTests
1920
{
21+
private bool originalAutoRun;
2022
private StringCollection originalRecentFiles;
2123
private string _tempDirectory;
2224

@@ -89,6 +91,9 @@ public void SetUp()
8991
// Save original RecentFiles
9092
originalRecentFiles = Properties.Settings.Default.RecentFiles;
9193

94+
// Save original AutoRun setting
95+
originalAutoRun = Properties.Settings.Default.AutoRun;
96+
9297
// Initialize with clean state
9398
Properties.Settings.Default.RecentFiles = new StringCollection();
9499

@@ -104,6 +109,7 @@ public void Cleanup()
104109
{
105110
// Restore original RecentFiles
106111
Properties.Settings.Default.RecentFiles = originalRecentFiles;
112+
Properties.Settings.Default.AutoRun = originalAutoRun;
107113
Properties.Settings.Default.Save();
108114

109115
_mainForm.RestorePublisher();
@@ -495,21 +501,55 @@ public void ConfigItemModified_BindingsUnchanged_DoesNotPublishUpdate()
495501
}
496502
#endregion
497503

504+
#region Browser Message Handling Tests
505+
[TestMethod]
506+
public void ToggleAutoRun_PublishesSettingsUpdate()
507+
{
508+
// Arrange
509+
var initialAutoRun = Properties.Settings.Default.AutoRun;
510+
511+
// Act (Simulate Frontend action after clicking on autorun toggle button)
512+
var jsonMessage = JsonConvert.SerializeObject(new BrowserMessages.Message<CommandProjectToolbar>(new CommandProjectToolbar()
513+
{
514+
Action = CommandProjectToolbarAction.toggleAutoRun
515+
}));
516+
517+
_mainForm.Publisher.SimulateIncomingMessage(jsonMessage);
518+
519+
// Assert
520+
var settingsMessage = _mainForm.Publisher.PublishedMessages.FirstOrDefault(m => m.GetType() == typeof(Settings)) as Settings;
521+
Assert.IsNotNull(settingsMessage, "Settings message should be published");
522+
Assert.AreEqual(!initialAutoRun, settingsMessage.AutoRun,
523+
"AutoRun should be toggled in published settings");
524+
}
525+
#endregion
526+
498527
public class TestMessagePublisher : IMessagePublisher
499528
{
500529
public List<object> PublishedMessages { get; } = new List<object>();
530+
private Action<object> _onMessageReceived;
501531

502532
public void Publish<TEvent>(TEvent eventToPublish)
503533
{
504534
PublishedMessages.Add(eventToPublish);
505535
}
506536

507-
public void OnMessageReceived(Action<string> callback) { }
537+
public void OnMessageReceived(Action<string> callback)
538+
{
539+
_onMessageReceived = (message) => callback((string)message);
540+
}
508541

509542
public void Reset()
510543
{
511544
PublishedMessages.Clear();
512545
}
546+
547+
public void SimulateIncomingMessage(string jsonMessage)
548+
{
549+
MessageExchange.Instance.SetSynchronizationContextProvider(() => null);
550+
_onMessageReceived?.Invoke(jsonMessage);
551+
MessageExchange.Instance.SetSynchronizationContextProvider(null);
552+
}
513553
}
514554
}
515555
}

UI/MainForm.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,6 @@ private async void MainForm_Shown(object sender, EventArgs e)
521521
xPlaneDirectToolStripMenuItem.Image = Properties.Resources.warning;
522522
toolStripConnectedDevicesIcon.Image = Properties.Resources.warning;
523523

524-
// we only load the autorun value stored in settings
525-
// and do not use possibly passed in autoRun from cmdline
526-
// because latter shall only have an temporary influence
527-
// on the program
528-
setAutoRunValue(Properties.Settings.Default.AutoRun);
529-
530524
updateNotifyContextMenu(false);
531525

532526
// Reset the Title of the Main Window so that it displays the Version too.
@@ -2461,6 +2455,9 @@ public void RenameProject(string newName)
24612455
private void setAutoRunValue(bool value)
24622456
{
24632457
Properties.Settings.Default.AutoRun = value;
2458+
2459+
// Triggers update to frontend
2460+
Properties.Settings.Default.Save();
24642461
}
24652462

24662463
public void settingsToolStripMenuItem_Click(object sender, EventArgs e)

0 commit comments

Comments
 (0)